浏览代码

exercise 4.5: comparison of analytic and numerical velocity and acceleration functions derived from an analytic position function

George C. Privon 1 年之前
父节点
当前提交
3cd19fc268
共有 1 个文件被更改,包括 19 次插入4 次删除
  1. 19 4
      04-motion/exercises/ex4.5.hs

+ 19 - 4
04-motion/exercises/ex4.5.hs

@@ -3,6 +3,7 @@
 -- 2024-07-20
 
 type R = Double
+type Derivative = (R -> R) -> R -> R
 
 type Time = R
 type Position = R
@@ -12,6 +13,11 @@ type Acceleration = R
 type PositionFunction = Time -> Position
 type VelocityFunction = Time -> Velocity
 type AccelerationFunction = Time -> Acceleration
+--
+-- 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
+
 
 pos1 :: PositionFunction
 pos1 t = if t < 0
@@ -19,14 +25,23 @@ pos1 t = if t < 0
          else 5 * t**2
 
 vel1Analytic :: VelocityFunction
-vel1Analytic t = undefined
+vel1Analytic t = if t < 0
+                 then 0
+                 else 10 * t
 
 acc1Analytic :: AccelerationFunction
-acc1Analytic t = undefined
+acc1Analytic t = if t < 0
+                 then 0
+                 else 10
 
 vel1Numerical :: VelocityFunction
-vel1Numerical t = undefined
+vel1Numerical t = derivative 0.01 pos1  t
 
 acc1Numerical :: AccelerationFunction
-acc1Numerical t = undefined
+acc1Numerical t = derivative 0.01 (derivative 0.01 pos1 ) t
 
+-- Solution
+-- The numerical and analytic velocity functions seem to agree closely across
+-- the range, except when t~<0.001.
+-- The numerical acceleration function differs the most around t=0 but is
+-- in agreement across the rest of the range.