Skip to content
Merged
Prev Previous commit
Next Next commit
Move timestep over to solveEuler
  • Loading branch information
Thurii committed Jul 7, 2018
commit 0dd8148ed9148debdc6ee2857df598933ab6cfd1
13 changes: 6 additions & 7 deletions chapters/differential_equations/euler/code/haskell/euler.hs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
solveEuler :: Num a => (a -> a) -> a -> [a]
solveEuler = iterate
solveEuler :: Num a => (a -> a) -> a -> a -> [a]
solveEuler f ts = iterate (\x -> x + f x * ts)

checkResult :: (Ord a, Num a, Num t, Enum t) => a -> (t -> a) -> [a] -> Bool
checkResult thresh check =
and . zipWith (\i k -> abs (check i - k) < thresh) [0..]

kinematics :: Double -> Double -> Double
kinematics timestep x = x - 3 * x * timestep
kinematics :: Double -> Double
kinematics x = -3 * x

main :: IO ()
main =
let timestep = 0.01
n = 100
threshold = 0.01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding a value for accFun:

accFun x = x - 3 * x * timestep
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sould the acceleration function be:

accFun x = -3 * x
kinematics' = kinematics timestep
checkResult' = checkResult threshold $ exp . kinematics'
checkResult' = checkResult threshold $ exp . (\x -> x - 3 * x * timestep)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it should be (\x -> - 3 * x * timestep)

in putStrLn $
if checkResult' (take n $ solveEuler kinematics' 1)
if checkResult' (take n $ solveEuler kinematics timestep 1)
then "All values within threshold"
else "Value(s) not in threshold"