Advertisement
calcpage

lrte_graphical_sage.py

Dec 13th, 2019
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #MrG 2015.0507 LRTE + Graphics
  2. f="1/x"
  3. a=1
  4. b=2
  5. n=10
  6.  
  7. def lSum(f,a,b,n):
  8.     p=Graphics()
  9.     h=(b-a)/n
  10.     for i in range(n):
  11.         x=a+i*h
  12.         y=f(a+i*h)
  13.         p+=polygon([(x,0),(x,y),(x+h,y),(x+h,0)],fill=False)
  14.     p+=plot(f,a,b,color='red',fill=True)
  15.     p.show(aspect_ratio=1)
  16.     return sum([f(a+i*h)*h for i in range(n)])
  17.  
  18. def rSum(f,a,b,n):
  19.     p=Graphics()
  20.     h=(b-a)/n
  21.     for i in range(n):
  22.         x=a+i*h
  23.         y=f(a+i*h+h)
  24.         p+=polygon([(x,0),(x,y),(x+h,y),(x+h,0)],fill=False)
  25.     p+=plot(f,a,b,color='red',fill=True)
  26.     p.show(aspect_ratio=1)
  27.     return sum([f(a+i*h)*h for i in range(n)])-f(a)*h+f(b)*h
  28.  
  29. def tSum(f,a,b,n):
  30.     p=Graphics()
  31.     h=(b-a)/n
  32.     for i in range(n):
  33.         x=a+i*h
  34.         y1=f(a+i*h)
  35.         y2=f(a+i*h+h)
  36.         p+=polygon([(x,0),(x,y1),(x+h,y2),(x+h,0)],fill=False)
  37.     p+=plot(f,a,b,color='red',fill=True)
  38.     p.show(aspect_ratio=1)
  39.     l=sum([f(a+i*h)*h for i in range(n)]).n(digits=6)
  40.     r=sum([f(a+i*h)*h for i in range(n)])-f(a)*h+f(b)*h
  41.     return [(r+l)/2,(r-l)/2]
  42.  
  43. print "l=",lSum(lambda x:eval(f),a,b,n).n(digits=6)
  44. print "r=",rSum(lambda x:eval(f),a,b,n).n(digits=6)
  45. print "[t,e]=",tSum(lambda x:eval(f),a,b,n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement