| 12345678910111213141516171819202122232425262728 |
- 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 x = 1/2 * x**2
- -- take the derivative with dt=10
- dOne :: R -> R
- dOne x = derivative 10 myfunc x
- -- take the derivative with dt=1
- dTwo :: R -> R
- dTwo x = derivative 1 myfunc x
- -- take the derivative with dt=0.1
- dThree :: R -> R
- dThree x = derivative 0.1 myfunc x
- -- Solution discussion
- -- dThree does not give an exact solution because the floating point value
- -- `0.1` does not have an exact finite binary representation and so is
- -- truncated. That truncation manifests as an inexact answer for the evaluation
- -- of the derivative.
|