280
8 Solving Ordinary Differential Equations
Exercise 8.16: The Runge-Kutta Third Order Method
A general ODE problem u (t) = f (u, t), u(0) = U 0 , may be solved numerically by
the third order Runge-Kutta method. The computational scheme reads
u
n+1
= u
n
+
Δt
6
(k 1 + 4k 2 + k 3 ) ,
n= 0, 1, . . . , N t − 1,
k 1 = f (u
n , t n ),
k 2 = f (u
n
+
Δt
2
, t n +
Δt
2
),
k 3 = f (u
n
− Δtk 1 + 2Δtk 2 , t n + Δt) ,
with u 0 = U 0 .
a) Implement the scheme in a function RK3 that takes appropriate parameters, so
that it is easy to import and use whenever needed.
b) Write a function compare_FE_RK3 that compares graphically the solutions produced by the Forward Euler and RK3 methods, when they solve the population
growth model u = 0.1u, with u(0) = 100. Let the total time span T = 20, and
use a time step dt = 2. In the plot produced, include also the exact solution, so
that the numerical solutions can be assessed.
c) Suggest a reasonable asymptotic error model before you write a proper test
function test_convergence_rates that may be used to compute and check the
convergence rates of the implemented RK3 method. However, the test function
should take appropriate input parameters, so that it can be used also for other
ODE solvers, in particular the ode_FE implemented previously (if you already
have written this test function when doing Exercise 8.15, you may prefer to
import the function).
Include your test function in a program, together with the two functions you
defined previously (RK3 and compare_FE_RK3). Write the code with a test block,
so that it gets easy to either import functions from the module, or to run it as a
program.
Finally, run the program (so that compare_FE_RK3 gets called, as well as
test_convergence_rates for both FE and RK3) and confirm that it works as
expected. In particular, does the plot look good, and do you get the convergence
rates you expected for Forward Euler and RK3?
Filename: runge_kutta_3.py.
Exercise 8.17: The Two-Step Adams-Bashforth Method
Differing from the single-step methods presented in this chapter, we have the multistep methods, for example the Adams-Bashforth methods. With the single-step
methods, u n+1 is computed by use of the solution from the previous time step,
i.e. u n . In multi-step methods, the computed solutions from several previous time
steps, e.g., u n , u n−1 and u n−2 are used to estimate u n+1 . How many time steps
that are involved in the computing of u n+1 , and how the previous solutions are
8 Solving Ordinary Differential Equations
Exercise 8.16: The Runge-Kutta Third Order Method
A general ODE problem u (t) = f (u, t), u(0) = U 0 , may be solved numerically by
the third order Runge-Kutta method. The computational scheme reads
u
n+1
= u
n
+
Δt
6
(k 1 + 4k 2 + k 3 ) ,
n= 0, 1, . . . , N t − 1,
k 1 = f (u
n , t n ),
k 2 = f (u
n
+
Δt
2
, t n +
Δt
2
),
k 3 = f (u
n
− Δtk 1 + 2Δtk 2 , t n + Δt) ,
with u 0 = U 0 .
a) Implement the scheme in a function RK3 that takes appropriate parameters, so
that it is easy to import and use whenever needed.
b) Write a function compare_FE_RK3 that compares graphically the solutions produced by the Forward Euler and RK3 methods, when they solve the population
growth model u = 0.1u, with u(0) = 100. Let the total time span T = 20, and
use a time step dt = 2. In the plot produced, include also the exact solution, so
that the numerical solutions can be assessed.
c) Suggest a reasonable asymptotic error model before you write a proper test
function test_convergence_rates that may be used to compute and check the
convergence rates of the implemented RK3 method. However, the test function
should take appropriate input parameters, so that it can be used also for other
ODE solvers, in particular the ode_FE implemented previously (if you already
have written this test function when doing Exercise 8.15, you may prefer to
import the function).
Include your test function in a program, together with the two functions you
defined previously (RK3 and compare_FE_RK3). Write the code with a test block,
so that it gets easy to either import functions from the module, or to run it as a
program.
Finally, run the program (so that compare_FE_RK3 gets called, as well as
test_convergence_rates for both FE and RK3) and confirm that it works as
expected. In particular, does the plot look good, and do you get the convergence
rates you expected for Forward Euler and RK3?
Filename: runge_kutta_3.py.
Exercise 8.17: The Two-Step Adams-Bashforth Method
Differing from the single-step methods presented in this chapter, we have the multistep methods, for example the Adams-Bashforth methods. With the single-step
methods, u n+1 is computed by use of the solution from the previous time step,
i.e. u n . In multi-step methods, the computed solutions from several previous time
steps, e.g., u n , u n−1 and u n−2 are used to estimate u n+1 . How many time steps
that are involved in the computing of u n+1 , and how the previous solutions are
