SHARE
TWEET

4H2012 rsum2graph.py

calcpage Apr 19th, 2013 32 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. from matplotlib import *
  3. from pylab import *
  4.  
  5. a=-pi/2
  6. b=pi/2
  7. n=1000
  8. def f(x):
  9.     return sin(x)
  10.  
  11. h=1.0*(b-a)/n
  12. x=[a+(i+1)*h for i in range(n)]
  13. y=[f(a+(i+1)*h) for i in range(n)]
  14. areas=[f(a+(i+1)*h)*h for i in range(n)]
  15. area=sum(areas)
  16.  
  17. print "x=",x
  18. print "y=",y
  19. print "areas=",areas
  20. print "area=",area
  21.  
  22. plot(x,y,color='red')
  23.  
  24. for i in range(n):
  25.     matplotlib.pyplot.gca().add_patch(matplotlib.patches.Rectangle((a+i*h,0),h,f(a+(i+1)*h)))
  26.  
  27. show()
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top