274
8 Solving Ordinary Differential Equations
runs a number of steps in ode_FE and compares the computed solution with the
listed formula for u n .
Filename: test_ode_FE.py.
Exercise 8.4: Implement and Evaluate Heun’s Method
a) A second-order Runge-Kutta method, also known has Heun’s method, is derived
in Sect. 8.4.5. Make a function ode_Heun(f, U_0, dt, T) (as a counterpart
to ode_FE(f, U_0, dt, T) in ode_FE.py) for solving a scalar ODE problem
u = f (u, t), u(0) = U 0 , t ∈ (0, T ], with this method using a time step size
Δt.
b) Solve the simple ODE problem u = u, u(0) = 1, by the ode_Heun and the
ode_FE function. Make a plot that compares Heun’s method and the Forward
Euler method with the exact solution u(t) = e t for t ∈ [0, 6]. Use a time step
Δt = 0.5.
c) For the case in b), find through experimentation the largest value of Δt where
the exact solution and the numerical solution by Heun’s method cannot be
distinguished visually. It is of interest to see how far off the curve the Forward
Euler method is when Heun’s method can be regarded as “exact” (for visual
purposes).
Filename: ode_Heun.py.
Exercise 8.5: Find an Appropriate Time Step; Logistic Model
Compute the numerical solution of the logistic equation for a set of repeatedly
halved time steps: Δt k = 2 −k Δt, k = 0, 1, . . .. Plot the solutions corresponding
to the last two time steps Δt k and Δt k−1 in the same plot. Continue doing this until
you cannot visually distinguish the two curves in the plot. Then one has found a
sufficiently small time step.
Hint Extend the logistic.py file. Introduce a loop over k, write out Δt k , and ask
the user if the loop is to be continued.
Filename: logistic_dt.py.
Exercise 8.6: Find an Appropriate Time Step; SIR Model
Repeat Exercise 8.5 for the SIR model.
Hint Import the ode_FE function from the ode_system_FE module and make a
modified demo_SIR function that has a loop over repeatedly halved time steps. Plot
S, I , and R versus time for the two last time step sizes in the same plot.
Filename: SIR_dt.py.
Exercise 8.7: Model an Adaptive Vaccination Campaign
In the SIRV model with time-dependent vaccination from Sect. 8.3.9, we want to
test the effect of an adaptive vaccination campaign where vaccination is offered
8 Solving Ordinary Differential Equations
runs a number of steps in ode_FE and compares the computed solution with the
listed formula for u n .
Filename: test_ode_FE.py.
Exercise 8.4: Implement and Evaluate Heun’s Method
a) A second-order Runge-Kutta method, also known has Heun’s method, is derived
in Sect. 8.4.5. Make a function ode_Heun(f, U_0, dt, T) (as a counterpart
to ode_FE(f, U_0, dt, T) in ode_FE.py) for solving a scalar ODE problem
u = f (u, t), u(0) = U 0 , t ∈ (0, T ], with this method using a time step size
Δt.
b) Solve the simple ODE problem u = u, u(0) = 1, by the ode_Heun and the
ode_FE function. Make a plot that compares Heun’s method and the Forward
Euler method with the exact solution u(t) = e t for t ∈ [0, 6]. Use a time step
Δt = 0.5.
c) For the case in b), find through experimentation the largest value of Δt where
the exact solution and the numerical solution by Heun’s method cannot be
distinguished visually. It is of interest to see how far off the curve the Forward
Euler method is when Heun’s method can be regarded as “exact” (for visual
purposes).
Filename: ode_Heun.py.
Exercise 8.5: Find an Appropriate Time Step; Logistic Model
Compute the numerical solution of the logistic equation for a set of repeatedly
halved time steps: Δt k = 2 −k Δt, k = 0, 1, . . .. Plot the solutions corresponding
to the last two time steps Δt k and Δt k−1 in the same plot. Continue doing this until
you cannot visually distinguish the two curves in the plot. Then one has found a
sufficiently small time step.
Hint Extend the logistic.py file. Introduce a loop over k, write out Δt k , and ask
the user if the loop is to be continued.
Filename: logistic_dt.py.
Exercise 8.6: Find an Appropriate Time Step; SIR Model
Repeat Exercise 8.5 for the SIR model.
Hint Import the ode_FE function from the ode_system_FE module and make a
modified demo_SIR function that has a loop over repeatedly halved time steps. Plot
S, I , and R versus time for the two last time step sizes in the same plot.
Filename: SIR_dt.py.
Exercise 8.7: Model an Adaptive Vaccination Campaign
In the SIRV model with time-dependent vaccination from Sect. 8.3.9, we want to
test the effect of an adaptive vaccination campaign where vaccination is offered
