| 12345678910111213141516171819202122232425262728293031323334 |
- -- Exercise 4.4
- -- George C. Privon
- -- 2023-12-22
- type R = Double
- type Derivative = (R -> R) -> R -> R
- -- a function to take the numerical derivative, approximated for some dt
- derivative :: R -> Derivative
- derivative dt x t = (x (t + dt/2) - x (t - dt/2)) / dt
- -- the function we want to take the derivative of
- myfunc :: R -> R
- myfunc t = cos t
- -- exact derivative of our function of interest
- dmyfunc :: R -> R
- dmyfunc t = - sin t
- -- take the derivative
- dOne :: R -> R -> R
- dOne dt x = derivative dt myfunc x
- -- compare the exact derivative with the numerical derivative using dt=1
- abserr :: R -> R -> R
- abserr dt x = abs (dmyfunc x - dOne dt x)
- -- compare the differences between dt=0.1 and dt=1 for a value of x
- dteffect :: R -> R
- dteffect x = abs(abserr 0.1 x - abserr 1 x)
- -- Solution
- -- The numerical derivative is insensitive to a (dt) around multiples of pi,
- -- where the slope is fairly constant. In contrast, derivative is more sensitive
- -- around pi/2, where the derivative changes more quickly.
|