SHARE
TWEET

4H2012 area.py

calcpage Apr 21st, 2013 95 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. o=input("o=? (0:left,0.5:mid,1:right)")
  6. a=input("a=?")
  7. b=input("b=?")
  8. n=input("n=?")
  9. f=raw_input("f(x)=?")
  10.  
  11. def findArea(o,a,b,n,f):
  12.     h=1.0*(b-a)/n
  13.     x=[a+(i+o)*h for i in range(n)]
  14.     y=[f(a+(i+o)*h) for i in range(n)]
  15.     areas=[f(a+(i+o)*h)*h for i in range(n)]
  16.     area=sum(areas)
  17.     return area
  18.  
  19. def drawArea(o,a,b,n,f):
  20.     h=1.0*(b-a)/n
  21.     x=[a+(i+o)*h for i in range(n)]
  22.     y=[f(a+(i+o)*h) for i in range(n)]
  23.     plot(x,y,color='red')
  24.     for i in range(n):
  25.         matplotlib.pyplot.gca().add_patch(matplotlib.patches.Rectangle((a+i*h,0),h,f(a+(i+o)*h)))
  26.     show()
  27.  
  28. print "area=",findArea(o,a,b,n,lambda x:eval(f))
  29. drawArea(o,a,b,n,lambda x:eval(f))
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