ex4.5.hs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. -- Exercise 4.5
  2. -- George C. Privon
  3. -- 2024-07-20
  4. type R = Double
  5. type Derivative = (R -> R) -> R -> R
  6. type Time = R
  7. type Position = R
  8. type Velocity = R
  9. type Acceleration = R
  10. type PositionFunction = Time -> Position
  11. type VelocityFunction = Time -> Velocity
  12. type AccelerationFunction = Time -> Acceleration
  13. --
  14. -- a function to take the numerical derivative, approximated for some dt
  15. derivative :: R -> Derivative
  16. derivative dt x t = (x (t + dt/2) - x (t - dt/2)) / dt
  17. pos1 :: PositionFunction
  18. pos1 t = if t < 0
  19. then 0
  20. else 5 * t**2
  21. vel1Analytic :: VelocityFunction
  22. vel1Analytic t = if t < 0
  23. then 0
  24. else 10 * t
  25. acc1Analytic :: AccelerationFunction
  26. acc1Analytic t = if t < 0
  27. then 0
  28. else 10
  29. vel1Numerical :: VelocityFunction
  30. vel1Numerical t = derivative 0.01 pos1 t
  31. acc1Numerical :: AccelerationFunction
  32. acc1Numerical t = derivative 0.01 (derivative 0.01 pos1 ) t
  33. -- Solution
  34. -- The numerical and analytic velocity functions seem to agree closely across
  35. -- the range, except when t~<0.001.
  36. -- The numerical acceleration function differs the most around t=0 but is
  37. -- in agreement across the rest of the range.