ex4.3.hs 778 B

1234567891011121314151617181920212223242526272829
  1. -- Exercise 4.3
  2. -- George C. Privon
  3. -- 2023-12-22
  4. type R = Double
  5. type Derivative = (R -> R) -> R -> R
  6. -- a function to take the numerical derivative, approximated for some dt
  7. derivative :: R -> Derivative
  8. derivative dt x t = (x (t + dt/2) - x (t - dt/2)) / dt
  9. -- the function we want to take the derivative of
  10. myfunc :: R -> R
  11. myfunc x = x**4
  12. -- exact derivative of our function of interest
  13. dmyfunc :: R -> R
  14. dmyfunc x = 4 * x**3
  15. -- take the derivative with dt=1
  16. dOne :: R -> R
  17. dOne x = derivative 0.01 myfunc x
  18. -- compare the exact derivative with the numerical derivative using dt=1
  19. relabserr :: R -> R
  20. relabserr x = abs (dmyfunc x - dOne x) / dmyfunc x
  21. -- Solution
  22. -- The derivative of f(x) = x**4, evaluated at x=0.01 with dt=0.01 produces a
  23. -- relative error of 25%