Skip to content

Commit

Permalink
Forward Euler method, version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaras authored and berquist committed Dec 29, 2021
1 parent 51936d7 commit 3060849
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions contents/forward_euler_method/code/coconut/euler.coco
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np

def forward_euler(time_step, n):
y = 1
for _ in range(n):
yield y
y *= (1 - 3 * time_step)


def check(result, threshold, time_step):
x = np.linspace(0, 1, 1 / time_step)
solution = np.exp(-3 * x)
return np.abs(solution - result) <= threshold).all()


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

result = np.array(list(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")

0 comments on commit 3060849

Please sign in to comment.