diff --git a/contents/forward_euler_method/code/coconut/euler.coco b/contents/forward_euler_method/code/coconut/euler.coco new file mode 100644 index 000000000..89f82213a --- /dev/null +++ b/contents/forward_euler_method/code/coconut/euler.coco @@ -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")