Advertisement
calcpage

lrte_gui_sage.py

Dec 13th, 2019
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. #1) MRG 2015.0511 LRTE with GUI input!
  2. @interact
  3. def lrte(f=x^2,a=1,b=5,n=slider(1,1000,1,4),method=selector(['Left','Right','Trap']),fillIt=('fill',False)):
  4.     p=Graphics()
  5.     h=(b-a)/n
  6.     if method=='Left':
  7.         l=sum([f(a+i*h)*h for i in range(n)])
  8.         for i in range(n):
  9.             x=a+i*h
  10.             y=f(a+i*h)
  11.             p+=polygon([(x,0),(x,y),(x+h,y),(x+h,0)],fill=fillIt)
  12.         p+=plot(f,a,b,color='red',fill=True)
  13.         p.show()
  14.         print "l="
  15.         print l.n(digits=6)
  16.     if method=='Right':
  17.         l=sum([f(a+i*h)*h for i in range(n)])
  18.         r=l-f(a)*h+f(b)*h
  19.         for i in range(n):
  20.             x=a+i*h
  21.             y=f(a+(i+1)*h)
  22.             p+=polygon([(x,0),(x,y),(x+h,y),(x+h,0)],fill=fillIt)
  23.         p+=plot(f,a,b,color='red',fill=True)
  24.         p.show()
  25.         print "[l,r]="
  26.         print [l.n(digits=6),r.n(digits=6)]
  27.     if method=='Trap':
  28.         l=sum([f(a+i*h)*h for i in range(n)])
  29.         r=sum([f(a+i*h)*h for i in range(n)])-f(a)*h+f(b)*h
  30.         t=(r+l)/2
  31.         e=(r-l)/2
  32.         for i in range(n):
  33.             x=a+i*h
  34.             y1=f(a+i*h)
  35.             y2=f(a+(i+1)*h)
  36.             p+=polygon([(x,0),(x,y1),(x+h,y2),(x+h,0)],fill=fillIt)
  37.         p+=plot(f,a,b,color='red',fill=True)
  38.         p.show()
  39.         print "[l,r,t,e]="
  40.         print [l.n(digits=6),r.n(digits=6),t.n(digits=6),e.n(digits=6)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement