SHARE
TWEET

2016AB2d.py

calcpage Jun 23rd, 2016 (edited) 657 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. #MrG 2016.0621 2016AB2
  3. #2d) d=5.301
  4. import math
  5. def f(t):
  6.     return 1+2*math.sin(t**2/2)
  7.  
  8. a=float(0)
  9. b=2.7074679792
  10. for x in range(4):
  11.     h=(b-a)/10**x
  12.     l=sum([f(a+i*h)*h for i in range(10**x)])
  13.     r=l-f(a)*h+f(b)*h
  14.     d1=(l+r)/2
  15.     print d1
  16.    
  17. a=2.7074679792
  18. b=float(3)
  19. for x in range(4):
  20.     h=(b-a)/10**x
  21.     l=sum([f(a+i*h)*h for i in range(10**x)])
  22.     r=l-f(a)*h+f(b)*h
  23.     d2=(l+r)/2
  24.     print d2
  25.    
  26. print "ans=", d1-d2
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Top