Skip to content

Commit 3060849

Browse files
Amarasberquist
authored andcommitted
Forward Euler method, version 1
1 parent 51936d7 commit 3060849

File tree

1 file changed

+23
-0
lines changed
  • contents/forward_euler_method/code/coconut

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numpy as np
2+
3+
def forward_euler(time_step, n):
4+
y = 1
5+
for _ in range(n):
6+
yield y
7+
y *= (1 - 3 * time_step)
8+
9+
10+
def check(result, threshold, time_step):
11+
x = np.linspace(0, 1, 1 / time_step)
12+
solution = np.exp(-3 * x)
13+
return np.abs(solution - result) <= threshold).all()
14+
15+
16+
if __name__ == '__main__':
17+
time_step = 0.01
18+
n = 100
19+
threshold = 0.01
20+
21+
result = np.array(list(forward_euler(time_step, n)))
22+
approx = check(result, threshold, time_step)
23+
print("All values within threshold") if approx else print("Value(s) not in threshold")

0 commit comments

Comments
 (0)