I think this post will wrap up the series on Vectors and Simple Mechanics and we’ll look at Simple Harmonic Motion (SHM) and compare the numerical solutions to SHM using the naive step function from the previous post – aka the ‘Euler‘ step and the more accurate ‘Euler-Cromer‘ method. Here’s the Euler Step from last time.
1 2 3 4 5 |
step :: Accnf -> Float -> State -> State step f dt st@(t, r, v) = (t', r', v') where t' = t + dt r' = r ^+^ v ^* dt v' = v ^+^ f st ^* dt |
a very simple change to the above yields the ‘Euler-Cromer‘ step where the ‘new velocity‘ rather than the ‘old‘ is used to determine the ‘new‘ position.
1 2 3 4 5 |
ecStep :: Accnf -> Float -> State -> State ecStep f dt st@(t, r, v) = (t', r', v') where t' = t + dt r' = r ^+^ v' ^* dt v' = v ^+^ f st ^* dt |
These two functions have the same signature, Accnf -> Float -> State -> State which allows us to generalise a solution based Read More