From af2d691e7057392ca0f0164c1624431754492c96 Mon Sep 17 00:00:00 2001 From: Sklan Date: Mon, 2 Jul 2018 20:40:41 +0530 Subject: [PATCH 1/3] Added forward_euler in python --- .../euler/code/python/euler.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 chapters/differential_equations/euler/code/python/euler.py diff --git a/chapters/differential_equations/euler/code/python/euler.py b/chapters/differential_equations/euler/code/python/euler.py new file mode 100644 index 000000000..813c6df21 --- /dev/null +++ b/chapters/differential_equations/euler/code/python/euler.py @@ -0,0 +1,27 @@ +import math + + +def forward_euler(time_step, n): + result = [0] * n + result[0] = 1 + for i in range(1, n): result[i] = result[i - 1] - 3.0 * result[i - 1] * time_step + return result + + +def check(result, threshold, time_step): + approx = True + for i in range(len(result)): + solution = math.exp(-3. * i * time_step) + if abs(result[i] - solution) > threshold: + print(result[i], solution) + approx = False + return approx + + +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") From 3bda2b41336c672c172a73de99f0101f7fb5a741 Mon Sep 17 00:00:00 2001 From: Sklan Date: Mon, 2 Jul 2018 23:09:45 +0530 Subject: [PATCH 2/3] Making requested changes --- .../euler/code/python/euler.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/chapters/differential_equations/euler/code/python/euler.py b/chapters/differential_equations/euler/code/python/euler.py index 813c6df21..993be5b95 100644 --- a/chapters/differential_equations/euler/code/python/euler.py +++ b/chapters/differential_equations/euler/code/python/euler.py @@ -4,24 +4,28 @@ def forward_euler(time_step, n): result = [0] * n result[0] = 1 - for i in range(1, n): result[i] = result[i - 1] - 3.0 * result[i - 1] * time_step + for i in range(1, n): + result[i] = result[i - 1] - 3 * result[i - 1] * time_step return result def check(result, threshold, time_step): approx = True for i in range(len(result)): - solution = math.exp(-3. * i * time_step) + solution = math.exp(-3 * i * time_step) if abs(result[i] - solution) > threshold: print(result[i], solution) approx = False - return approx + return approx -time_step = 0.01 -n = 100 -threshold = 0.01 +def 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") + 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") + +main() From a575fe1960d509aa0a836fad1b6ddc69f7edcb00 Mon Sep 17 00:00:00 2001 From: Sklan Date: Mon, 2 Jul 2018 23:14:17 +0530 Subject: [PATCH 3/3] Update euler.md --- chapters/differential_equations/euler/euler.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chapters/differential_equations/euler/euler.md b/chapters/differential_equations/euler/euler.md index 2ce9a63de..40e195c9c 100644 --- a/chapters/differential_equations/euler/euler.md +++ b/chapters/differential_equations/euler/euler.md @@ -110,6 +110,10 @@ So, this time, let's remove ourselves from any physics and instead solve the fol Full code for the visualization follows: [import, lang:"elm"](code/elm/euler.elm) + +{% sample lang="py" %} +### Python +[import, lang:"python"](code/python/euler.py) {% endmethod %}