ex4.4.hs 1014 B

12345678910111213141516171819202122232425262728293031323334
  1. -- Exercise 4.4
  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 t = cos t
  12. -- exact derivative of our function of interest
  13. dmyfunc :: R -> R
  14. dmyfunc t = - sin t
  15. -- take the derivative
  16. dOne :: R -> R -> R
  17. dOne dt x = derivative dt myfunc x
  18. -- compare the exact derivative with the numerical derivative using dt=1
  19. abserr :: R -> R -> R
  20. abserr dt x = abs (dmyfunc x - dOne dt x)
  21. -- compare the differences between dt=0.1 and dt=1 for a value of x
  22. dteffect :: R -> R
  23. dteffect x = abs(abserr 0.1 x - abserr 1 x)
  24. -- Solution
  25. -- The numerical derivative is insensitive to a (dt) around multiples of pi,
  26. -- where the slope is fairly constant. In contrast, derivative is more sensitive
  27. -- around pi/2, where the derivative changes more quickly.