t535.py 419 B

123456789101112131415161718
  1. from math import *
  2. def differentiate(f, method, h=1.0E-5):
  3. if method == 'Forward1':
  4. def Forward1(x):
  5. return (f(x+h) -f(x)) / h
  6. return Forward1
  7. elif method == 'Backward1':
  8. def Backward1(x):
  9. return (f(x) -f(x-h)) / h
  10. return Backward1
  11. mycos = differentiate(sin, 'Forward1')
  12. mysin = differentiate(mycos, 'Backward1', 1.0E-6)
  13. x = pi
  14. print "%.10f %.10f %.5f %.10f" % (mycos(x), cos(x), mysin(x), -sin(x))