Skip to content

Commit

Permalink
Forward Euler method, version 2.2: as functional as I could make it (…
Browse files Browse the repository at this point in the history
…for now)
  • Loading branch information
Amaras authored and berquist committed Dec 29, 2021
1 parent bf7923a commit 62d0ceb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 9 additions & 8 deletions contents/forward_euler_method/code/coconut/euler.coco
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import math

def forward_euler(time_step, n):
y = 1
for _ in range(n):
yield y
y *= (1 - 3 * time_step)
factors = [1] + [1 - 3 * time_step] * (n-1)
# We want all the cumulative values, thus the use of scan
return scan((*), factors)



def check(result, threshold, time_step):
approx = True
for i, y in enumerate(result):
solution = math.exp(-3 * i * time_step)
if not math.isclose(y, solution, abs_tol=threshold):
print(y, solution)
# A scan object has a len if the underlying iterable has a len
solution = range(len(result)) |> map$(i -> math.exp(-3*i*time_step))
for y, sol in zip(result, solution):
if not math.isclose(y, sol, abs_tol=threshold):
print(y, sol)
approx = False
return approx

Expand Down
2 changes: 2 additions & 0 deletions contents/forward_euler_method/forward_euler_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Full code for the visualization follows:
[import, lang:"nim"](code/nim/forwardeuler.nim)
{% sample lang="lisp" %}
[import, lang="lisp"](code/clisp/euler.lisp)
{%sample lang="coco" %}
[import, lang:"coconut"](code/coconut/euler.coco)
{% endmethod %}

<script>
Expand Down

0 comments on commit 62d0ceb

Please sign in to comment.