Skip to content

Commit 015489e

Browse files
authored
Merge pull request #737 from Amaras/forward_euler_coconut
2 parents 51936d7 + 62d0ceb commit 015489e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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")

contents/forward_euler_method/forward_euler_method.md

+2
Original file line numberDiff line numberDiff 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>

0 commit comments

Comments
 (0)