Kaynağa Gözat

exercise 4.4: the sensitivity of the numerical derivative of cos as a function of the dt value

George C. Privon 1 yıl önce
ebeveyn
işleme
48dee65f2e
1 değiştirilmiş dosya ile 34 ekleme ve 0 silme
  1. 34 0
      04-motion/exercises/ex4.4.hs

+ 34 - 0
04-motion/exercises/ex4.4.hs

@@ -0,0 +1,34 @@
+-- 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.