File tree 1 file changed +9
-5
lines changed
contents/forward_euler_method/code/coconut
1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change 1
- import numpy as np
1
+ import math
2
2
3
3
def forward_euler(time_step, n):
4
4
y = 1
@@ -8,16 +8,20 @@ def forward_euler(time_step, n):
8
8
9
9
10
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()
11
+ approx = True
12
+ for i, y in enumerate(result):
13
+ solution = math.exp(-3 * i * time_step)
14
+ if not math.isclose(y, solution, abs_tol=threshold):
15
+ print(y, solution)
16
+ approx = False
17
+ return approx
14
18
15
19
16
20
if __name__ == '__main__':
17
21
time_step = 0.01
18
22
n = 100
19
23
threshold = 0.01
20
24
21
- result = np.array( list(forward_euler(time_step, n) ))
25
+ result = list(forward_euler(time_step, n))
22
26
approx = check(result, threshold, time_step)
23
27
print("All values within threshold") if approx else print("Value(s) not in threshold")
You can’t perform that action at this time.
0 commit comments