Skip to content

Commit 73f5060

Browse files
Thuriijiegillet
authored andcommitted
Added Forward Euler to Haskell (#215)
* Added Forward Euler in Haskell
1 parent b1d367c commit 73f5060

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
solveEuler :: Num a => (a -> a) -> a -> a -> [a]
2+
solveEuler f ts = iterate (\x -> x + f x * ts)
3+
4+
checkResult :: (Ord a, Num a, Num t, Enum t) => a -> (t -> a) -> [a] -> Bool
5+
checkResult thresh check =
6+
and . zipWith (\i k -> abs (check i - k) < thresh) [0..]
7+
8+
kinematics :: Double -> Double
9+
kinematics x = -3 * x
10+
11+
main :: IO ()
12+
main =
13+
let timestep = 0.01
14+
n = 100
15+
threshold = 0.01
16+
checkResult' = checkResult threshold $ exp . (\x -> -3 * x * timestep)
17+
in putStrLn $
18+
if checkResult' (take n $ solveEuler kinematics timestep 1)
19+
then "All values within threshold"
20+
else "Value(s) not in threshold"

chapters/differential_equations/euler/euler.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ Full code for the visualization follows:
121121
{% sample lang="py" %}
122122
### Python
123123
[import, lang:"python"](code/python/euler.py)
124+
{% sample lang="hs" %}
125+
### Haskell
126+
[import, lang:"haskell"](code/haskell/euler.hs)
124127
{% endmethod %}
125128

126129
<script>
@@ -145,4 +148,3 @@ $$
145148
\newcommand{\bfomega}{\boldsymbol{\omega}}
146149
\newcommand{\bftau}{\boldsymbol{\tau}}
147150
$$
148-

0 commit comments

Comments
 (0)