Advertisement
calcpage

4H2012 rsum2graph.py

Apr 19th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement