Advertisement
calcpage

4H2012 lsum2graph.py

Apr 19th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. #!/usr/bin/python
  2. from matplotlib import *
  3. from pylab import *
  4.  
  5. a=1
  6. b=5
  7. n=8
  8. h=1.0*(b-a)/n
  9. def f(x):
  10.     return x**2
  11.  
  12. x=[a+i*h for i in range(n)]
  13. y=[f(a+i*h) for i in range(n)]
  14. areas=[f(a+i*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*h)))
  26.  
  27. show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement