Skip to content

Commit

Permalink
Merge pull request algorithm-archivists#737 from Amaras/forward_euler…
Browse files Browse the repository at this point in the history
…_coconut
  • Loading branch information
berquist authored Dec 29, 2021
2 parents 51936d7 + 62d0ceb commit 015489e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions contents/forward_euler_method/code/coconut/euler.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import math

def forward_euler(time_step, n):
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
# 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


if __name__ == '__main__':
time_step = 0.01
n = 100
threshold = 0.01

result = forward_euler(time_step, n)
approx = check(result, threshold, time_step)
print("All values within threshold") if approx else print("Value(s) not in threshold")
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 015489e

Please sign in to comment.