Skip to content

Forward Euler method in Coconut #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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