File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
contents/forward_euler_method Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ import math
2+
3+ def forward_euler(time_step, n):
4+ factors = [1] + [1 - 3 * time_step] * (n-1)
5+ # We want all the cumulative values, thus the use of scan
6+ return scan((*), factors)
7+
8+
9+
10+ def check(result, threshold, time_step):
11+ approx = True
12+ # A scan object has a len if the underlying iterable has a len
13+ solution = range(len(result)) |> map$(i -> math.exp(-3*i*time_step))
14+ for y, sol in zip(result, solution):
15+ if not math.isclose(y, sol, abs_tol=threshold):
16+ print(y, sol)
17+ approx = False
18+ return approx
19+
20+
21+ if __name__ == '__main__':
22+ time_step = 0.01
23+ n = 100
24+ threshold = 0.01
25+
26+ result = forward_euler(time_step, n)
27+ approx = check(result, threshold, time_step)
28+ print("All values within threshold") if approx else print("Value(s) not in threshold")
Original file line number Diff line number Diff line change @@ -146,6 +146,8 @@ Full code for the visualization follows:
146146[ import, lang:"nim"] ( code/nim/forwardeuler.nim )
147147{% sample lang="lisp" %}
148148[ import, lang="lisp"] ( code/clisp/euler.lisp )
149+ {%sample lang="coco" %}
150+ [ import, lang:"coconut"] ( code/coconut/euler.coco )
149151{% endmethod %}
150152
151153<script >
You can’t perform that action at this time.
0 commit comments