Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions chapters/differential_equations/euler/code/haskell/euler.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
kinematics x = -3 * x

main :: IO ()
main =
let timestep = 0.01
n = 100
threshold = 0.01
checkResult' = checkResult threshold $ exp . (\x -> -3 * x * timestep)
in putStrLn $
if checkResult' (take n $ solveEuler kinematics timestep 1)
then "All values within threshold"
else "Value(s) not in threshold"
4 changes: 3 additions & 1 deletion chapters/differential_equations/euler/euler.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ Full code for the visualization follows:
{% sample lang="py" %}
### Python
[import, lang:"python"](code/python/euler.py)
{% sample lang="hs" %}
### Haskell
[import, lang:"haskell"](code/haskell/euler.hs)
{% endmethod %}

<script>
Expand All @@ -145,4 +148,3 @@ $$
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$