SHARE
TWEET

4H2012 lsum2graph.py

calcpage Apr 19th, 2013 25 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=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()
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