ex4.1.hs 824 B

12345678910111213141516171819202122232425262728
  1. type R = Double
  2. type Derivative = (R -> R) -> R -> R
  3. -- a function to take the numerical derivative, approximated for some dt
  4. derivative :: R -> Derivative
  5. derivative dt x t = (x (t + dt/2) - x (t - dt/2)) / dt
  6. -- the function we want to take the derivative of
  7. myfunc :: R -> R
  8. myfunc x = 1/2 * x**2
  9. -- take the derivative with dt=10
  10. dOne :: R -> R
  11. dOne x = derivative 10 myfunc x
  12. -- take the derivative with dt=1
  13. dTwo :: R -> R
  14. dTwo x = derivative 1 myfunc x
  15. -- take the derivative with dt=0.1
  16. dThree :: R -> R
  17. dThree x = derivative 0.1 myfunc x
  18. -- Solution discussion
  19. -- dThree does not give an exact solution because the floating point value
  20. -- `0.1` does not have an exact finite binary representation and so is
  21. -- truncated. That truncation manifests as an inexact answer for the evaluation
  22. -- of the derivative.