Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit 31bbe00

Browse files
committed
upload solutions + typo fix
1 parent 24ec966 commit 31bbe00

36 files changed

+892
-2
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import golden_module as gm
22

3-
print(gm.PHI)
3+
print("The golden ratio is given by (1+sqrt(5))/2 = " + str(gm.PHI))

day_7/Chemical Reactions/exercises/Ex3/runge_kutta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def integrate(f, x0, tspan, h, step):
5757
ts.append(t)
5858
return trajectory, ts
5959

60-
def adaptive_explicit_RK_stepper(x,t,f,h,a,b,c,b_control):
60+
def adaptive_explicit_RK_stepper(f,x,t,h,a,b,c,b_control):
6161
"""
6262
Implementation of generic explicit Runge-Kutta update for explicit ODEs
6363
-3.03 MB
Binary file not shown.

day_7/Chemical Reactions/solutions/Ex0/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PHI = 1.618 # golden ratio
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import golden_module as gm
2+
3+
print(gm.PHI)

day_7/Chemical Reactions/solutions/Ex1/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a = [[1/5],
2+
[3/40, 9/40],
3+
[44/45, -56/15, 32/9],
4+
[19372/6561, -25360/2187, 64448/6561, -212/729],
5+
[9017/3168, -355/33, 46732/5247, 49/176, -5103/18656],
6+
[35/384, 0, 500/1113, 125/192, -2187/6784, 11/84]]
7+
b = [35/384, 0, 500/1113, 125/192, -2187/6784, 11/84, 0]
8+
c = [0, 1/5, 3/10, 4/5, 8/9, 1, 1]
9+
b_control = [5179/57600, 0, 7571/16695, 393/640, -92097/339200, 187/2100, 1/40]
8.47 KB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# numpy for fast arrays and some linear algebra.
2+
import numpy as np
3+
# import matplotlib for plotting
4+
import matplotlib.pyplot as plt
5+
# import Dormand-Prince Butcher tableau
6+
import dormand_prince as dp
7+
# import our Runge-Kutta integrators
8+
import runge_kutta as rk
9+
10+
def dormand_prince_integrator(f, x, t, h):
11+
return rk.explicit_RK_stepper(f, x, t, h, dp.a, dp.b, dp.c)
12+
#return ... # please complete this function
13+
# so it returns the prediction for the
14+
# Dormand-Prince method
15+
# To that end, use rk.explicit_rk_stepper!
16+
17+
# Feel free play around with the following quantities
18+
# and see how the solution changes!
19+
20+
# time horizon
21+
tspan = (0.0,2.0)
22+
# time step
23+
h = 0.2
24+
# initial condition
25+
x_0 = 3.0
26+
27+
########################################
28+
### hereafter no more code modification necessary
29+
########################################
30+
31+
# model right-hand-side
32+
def f(x,t):
33+
return -2*x
34+
35+
# simulate model
36+
trajectory, time_points = rk.integrate(f, # ODE right-hand-side
37+
x_0, # initial condition
38+
tspan, # time horizon
39+
h, # time step
40+
dormand_prince_integrator) # integrator
41+
42+
# analytical solution
43+
time_points_analytical = np.linspace(tspan[0],tspan[1], 1000)
44+
trajectory_analytical = x_0*np.exp(-2*time_points_analytical)
45+
46+
# plot trace
47+
fig, ax = plt.subplots()
48+
ax.set_xlabel("time")
49+
ax.set_ylabel("x(t)")
50+
ax.plot(time_points, trajectory, linewidth=2, color="red", marker="o")
51+
ax.plot(time_points_analytical, trajectory_analytical, linewidth=2, color="black", linestyle="dashed")
52+
fig.savefig("example_ode.pdf")

0 commit comments

Comments
 (0)