Ode 1 and 2
Ode 1 and 2
Numerical Methods
www.smstc.ac.uk
Contents
0–1
SMST C: Numerical Methods (i)
www.smstc.ac.uk
Contents
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–2
1.1.1 The Standard ODE Initial Value Problem (IVP) . . . . . . . . . . . . . . . . . 1–2
1.1.2 Notation and general ideas about approximating ODEs . . . . . . . . . . . . . 1–3
1.1.3 Newton’s method for systems of algebraic equations . . . . . . . . . . . . . . . 1–3
1.2 Euler’s Method and variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–4
1.2.1 Derivations of Euler’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–5
1.2.2 Extensions of Euler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–5
1.2.3 Errors and Convergence of the Euler Scheme . . . . . . . . . . . . . . . . . . . 1–7
1.3 Runge-Kutta (RK) methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–8
1.3.1 Explicit RK methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–8
1.3.2 Implicit RK methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–9
1.4 Linear Multistep Methods — LMMs . . . . . . . . . . . . . . . . . . . . . . . 1–10
1.4.1 General form of LMMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–10
1.4.2 Examples of LMM Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–11
1.5 Desired and essential properties of schemes . . . . . . . . . . . . . . . . . . 1–11
1.5.1 Zero-stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–12
1.5.2 Absolute stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–12
1.5.3 Local Truncation Error (LTE) . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–13
1.5.4 Consistency of scheme and initial data . . . . . . . . . . . . . . . . . . . . . . . 1–15
1.5.5 Consistency of initial data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–15
1.5.6 Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1–15
a D.B.Duncan@hw.ac.uk
1–1
SMST C: Numerical Methods 1–2
1.1 Introduction
These two lectures are about the accurate and efficient approximation of the solution of systems of
ordinary differential equations - hereafter abbreviated to “ODEs”. The aim is to introduce key ideas and
the language used to describe the attributes of numerical methods, as opposed to doing rigorous analysis.
There are numerous references describing methods for solving ODEs and related problems. The book [1]
gives an approachable introduction, in particular for stiff problems and DAEs, while [5] is also very
accessible, with interesting examples and final sections on geometric integration. The book [6] gives a
general introduction to numerical ODEs and PDEs, as do the more general books on numerical analysis
such as [2, 3]. For a modern view of numerical methods from a dynamical systems point of view the book
by [7] is a good source.
There is usually no need to write your own solver for ODEs. There are lots of good ODE software
packages available that many people have devoted years of their working life to. You should first see
what software is already available, and only consider writing a solver yourself if no software is suitable.
However it is useful to have a good idea of the theory and ideas that underlie solving ODEs and hence
understand the limitations and advantages of packages.
du
= f (t, u), given u(0) = u0 . (1.1)
dt
Usually we want the solution over a fixed, finite time interval 0 ≤ t ≤ T . The ODE above is linear if it
can be written in the form
du
= A(t)u + b(t)
dt
where A ∈ RM ×M and b ∈ RM are functions of t only. Otherwise it is nonlinear.
Different notations for derivatives, e.g.
du d2 u
ut ≡ u′ ≡ u̇ ≡ u(1) ≡ , utt ≡ u′′ ≡ ü ≡ u(2) ≡ ,
dt dt2
are used in different settings.
Note that we can write many higher order ODEs in this form — see the example below. Also, systems of
complex valued ODEs can be written as a standard real system of twice the dimension by equating real
and imaginary parts of each complex ODE. The following example illustrates the conversion of a real,
second order ODE into a two component system of first order ODEs.
Example 1.1 (General forced Van der Pol equation) Consider the general forced Van der Pol equa-
tion
x′′ + ϕ(x)x′ + x = p(t), x(0) = a, x′ (0) = b.
Here x(t) is the current in an electronic circuit, x′ its rate of change with time t, p(t) is a known forcing
function and ϕ(x) is a function determined by the properties of the circuit. Now define u1 (t) = x(t) and
u2 (t) = x′ (t), then this second order ODE is equivalent to the first order system
u′1 = u2 , u1 (0) = a
u′2 = p(t) − ϕ(u1 )u2 − u2 , u2 (0) = b.
T
Setting u = (u1 , u2 ) and
u2
f (t, u) =
p(t) − ϕ(u1 )u2 − u1
we get our standard ODE IVP (1.1).
SMST C: Numerical Methods 1–3
When trying to solve any mathematical problem it is worth reviewing if there is any information to tell
you that the solution actually exists, if it is unique, if it has any special properties that might be useful
or troublesome etc.. We are not going to go into the theory of existence and uniqueness for ODEs, but
you can find that in standard texts. However, note that even for continuous f it is not true that (1.1)
has a unique solution – see the next example.
Example 1.2 Consider the simple ODE IVP u′ = u1/2 , u(0) = 0. This has two solutions u(t) ≡ 0 and
u(t) = t2 /4, and hence the solution is not unique.
un ≈ u(tn ) n = 0, 1, 2, . . . , N
and we will examine methods to find these approximations. Interpolation may be used to fill in the gaps
in the approximate solution between these time levels, although often we simply choose the time levels
to give output at the times we are interested in.
To make life simple for now we take constant steps of size ∆t between time levels so that
In the next lecture we consider how to change the step size as the calculation progresses from t = 0 to
t = T with the goal of making the overall process as efficient as possible.
The usual aim is to obtain an approximate solution un that is close enough to the exact solution. For
a given numerical method the only control we have is to choose the timestep size ∆t. For any sensible
method we want the difference between exact and approximate solutions (we will come on to how that is
measured) to → 0 as the step size ∆t → 0. Usually we want the approximate solution over a fixed time
interval t ∈ [0, T ], and so
For ν = 0, 1, . . . do
−1
∂g [ν]
x[ν+1] = x[ν] − (x ) g(x[ν] ) (1.2)
∂x
and stop when ∥x[ν+1] − x[ν] ∥ is small enough.
Then x[ν+1] approximates the solution (root) of g(x) = 0.
SMST C: Numerical Methods 1–4
Exact
1 ∆t=1
∆ t = 0.5
∆ t = 0.25
Solution 0.8
0.6
0.4
0.2
0
0 1 2 3 4 5 6 7 8 9 10
time t
Figure 1.1: Explicit Euler method approximation of u′ = u(1 − u) with initial condition u(0) = 0.01 and
various stepsizes ∆t. The symbols show the computed values and linear interpolation (“join the dots”)
is used to fill in the detail between.
Here ∂g/∂x is the M × M Jacobian matrix with elements ∂gi /∂xj . Note that if g is linear in x, then
Newton’s method gives the exact solution in one iteration.
Of course one should usually not compute the matrix inverse, and the iteration (1.2) is often done in two
steps: solve the linear system of equations
∂g [ν]
(x ) δ [ν+1] = −g(x[ν] )
∂x
for δ [ν+1] ; and set
x[ν+1] = x[ν] + δ [ν+1] .
Example 1.3 Consider the approximate solution of the logistic equation IVP u′ = u(1 − u) with u(0) =
0.2 by the explicit Euler method, using steps of size ∆t = 0.1.
We have
u1 = u0 + ∆t u0 (1 − u0 ) = 0.2 + 0.1 × 0.2 × 0.8 = 0.216
u2 = u1 + ∆t u1 (1 − u1 ) = 0.216 + 0.1 × 0.216 × 0.784 = 0.2329344
...
u10 = u9 + ∆t u9 (1 − u9 ) = ··· = 0.399696 . . .
SMST C: Numerical Methods 1–5
Hence u(1) ≈ u10 = 0.399696 . . .. (The exact solution is u(1) = 0.40460967519 . . ..)
Suppose the solution u(t) has 2 continuous derivatives on [0, T ] and then apply Taylor’s Theorem so that
(tn+1 − tn )2 ′′ n
u(tn+1 ) = u(tn ) + tn+1 − tn u′ (tn ) +
u (ξ )
2
for some ξ n in the interval (tn , tn+1 ). Since ∆t = tn+1 − tn and u′ (t) satisfies the differential equation
(1.1) we get
∆t2 ′′ n
u(tn+1 ) = u(tn ) + ∆t f (tn , u(tn )) + u (ξ ). (1.4)
2
Euler’s method is obtained by neglecting the remainder term and replacing u(tn ) and u(tn+1 ) by their
approximations un and un+1 .
An alternative derivation of the explicit Euler method is found by integrating the ODE between tn and
tn+1 to get
Z tn+1 Z tn+1 Z tn+1
′ n+1 n
u (s)ds = f (s, u(s))ds ⇔ u(t ) = u(t ) + f (s, u(s)) ds (1.5)
tn tn tn
This gives
Z tn+1
n+1 n
u(t ) = u(t ) + f (s, u(s)) ds ≈ u(tn ) + (tn+1 − tn )f (tn , u(tn ))
tn
and Euler’s method follows as for the last stage of the Taylor approach.
The approach of approximating the integral is illustrated further below and is particularly useful in
constructing schemes for stochastic differential equations later in this stream.
With θ = 0 we recover the explicit Euler method and with θ = 1 we get the Implicit Euler method
which is widely used in approximating the ODE systems that arise in the numerical solution of parabolic
PDEs. Another commonly used scheme in PDEs and elsewhere has θ = 1/2, giving
∆t
un+1 = un + f (tn , un ) + f (tn+1 , un+1 ) ,
(1.9)
2
and is called the Trapezoidal rule.
The Trapezoidal rule and implicit Euler method are examples of implicit schemes. In order to get un+1
we need to solve a system of algebraic equations
∆t n+1 n+1 ∆t n n
Trapezoidal Rule: un+1 − f (t ,u ) − un − f (t , u ) = 0
2 | 2{z }
all known
n+1 n+1
for the unknown u . Both have the form g(u ) = 0 ready to fit directly into Newton’s method (1.2).
The initial guess un+1,[0] may be obtained in various ways, e.g.
using explicit Euler as a predictor of the value in the second case. These algebraic equations are linear
when f is linear in u and nonlinear in the general case that f is nonlinear in u. Implicit schemes clearly
need much more work for each step than explicit schemes, but we will see that they often allow larger
time steps to be used and for a wide class of ODEs are more efficient.
A final Euler-related method is obtained by approximating an approximation! Instead of solving the
Trapezoidal rule algebraic equations exactly for un+1 , use Euler to approximate un+1 on the right hand
side of (1.9) to get
∆t n n
un+1 = un + n n n n
f (t , u ) + f (t + ∆t, u + ∆tf (t , u )) . (1.10)
2 | {z }
≈un+1
This is called the Improved Euler scheme and is a Runge-Kutta method. It is usually implemented
and written in the equivalent Runge-Kutta format (1.14) that we discuss later. Notice that it involves
a term of the form f (t + ∆t, u + ∆t f (t, u)) – i.e. composition of f with itself rather than just a linear
combination of f ’s.
There are various natural ways to generalise the explicit Euler method beyond what we have seen above.
One approach is to use information from more than one previous time level to get the approximation un+1
at tn+1 . This results in Linear Multistep Methods (LMMs) which we examine in Section 1.4. The
other main approach is like the Improved Euler scheme above, essentially to to use function evaluations
between time levels tn and tn+1 as described in Section 1.3 on Runge–Kutta Methods.
SMST C: Numerical Methods 1–7
0
Max Error for t [0,10]
Error at t=10
-1 Slope 1
log10(Absolute Error) -2
-3
-4
-5
-6
-7
-3.5 -3 -2.5 -2 -1.5 -1 -0.5 0
log10(Step size t)
Figure 1.2: Error in the explicit Euler method approximation of u′ = u(1 − u) with initial condition
u(0) = 0.01 and various stepsizes ∆t. Both the maximum error over the time interval and the error at
t = 10 appear to be proportional to ∆t as ∆t → 0.
en = u(tn ) − un , n = 0, 1, 2, . . . , N.
In other words the difference between exact and approximate solutions. Ideally we want to show that
as ∆t is reduced the global error is also reduced. This certainly appears to be happening in the Euler
example in Figure 1.1, where the approximation gets better as ∆t is reduced. Figure 1.2 evaluates this
more carefully, and we see that the global error in the Euler approximation on the fixed time interval
appears to behave like
Global Error in explicit Euler = O(∆t)
as ∆t → 0 in this example. To verify this, work out the slope of the lines as ∆t → 0 (i.e. moving to the
left on the plot).
A useful step to proving convergence is to look at the local truncation error (LTE) described in Section
1.5.3. For explicit Euler this is
u(tn+1 ) − u(tn )
LTE(tn ) = − f (tn , u(tn )) = O(∆t)
∆t
where u(t) is an exact solution of the ODE u′ = f (t, u), and the end result is found by Taylor expansion
about t = tn . The LTE gives the order of the method. For explicit Euler it is order 1. If other conditions
SMST C: Numerical Methods 1–8
are satisfied (and they are for Euler applied to this problem), the order of the LTE gives the convergence
rate observed in the experiment.
Watch out! Different definitions of the LTE are used in various reference books. Sometimes this involves
an extra power of ∆t.
The key thing is that every stage of this process involves only an explicit evaluation of a formula involving
previously computed information — un gives k1 gives k2 gives · · · gives ks , and finally all are combined to
give un+1 . These schemes are often written in condensed form in terms of their coefficients in a Butcher
tableau
0 0 0 ··· 0
c2 a21 0 · · · 0
c A . .. .. .. .
T
:= .. . . . ..
b c a a ··· 0
s s1 s2
b1 b2 ··· bs
SMST C: Numerical Methods 1–9
Ps
where ci = j=1 aij (so c1 = 0) and the diagonal and upper portion of coefficient matrix A is zero. The
zero entries in A have not been left as gaps here, despite the common convention in textbooks.
Example 1.4 The Butcher tableau for the Classical RK4 scheme (1.11) is
0 0 0 0 0
1/2 1/2 0 0 0
1/2 0 1/2 0 0 .
1 0 0 1 0
1/6 1/3 1/3 1/6
The general two-stage second order explicit Runge-Kutta is given by the Butcher table
0 0 0
1/(2α) 1/(2α) 0 , (1.13)
1−α α
where α is a parameter. With α = 1/2 we recover the Improved Euler method first seen as (1.10)
k1 = ∆t f (tn , un )
k2 ∆t f tn + ∆t, un + k1 ,
=
1 1
un+1 un + k + k2 .
= (1.14)
2
Example 1.5 The Implicit Euler scheme (1.8) is an implicit, 1-stage RK method:
k1 = ∆t f tn + ∆t, un + k1
un+1 = un + k1
There are lots of different Runge-Kutta schemes available apart from those we have already met above.
SMST C: Numerical Methods 1–10
Example 1.6 (Derivation of a multistep method) Integrate the differential equation u′ = f (t, u)
from tn to tn+2 to get
Z tn+2 Z tn+2 Z tn+2
u′ (s) ds = f (s, u(s)) ds ⇔ u(tn+2 ) − u(tn ) = f (s, u(s)) ds.
tn tn tn
written in the form for the Newton iteration (1.2). If βk = 0, then the scheme is explicit since un+k can
be evaluated directly without the need to solve an algebraic system of equations. However, if βk ̸= 0,
then we do have to solve an algebraic system of equations at each time step for un+k and the scheme is
implicit.
Note that to get started, the k-step LMM needs the first k time levels of the approximate solution,
u0 , u1 , . . . , uk−1 , to be specified. The ODE initial conditions only give u0 , so something extra has to
be done. Standard approaches include using a one-step method to get u1 , . . . , uk−1 or using a one-step
method to get u1 , then a two-step method for u2 ,. . . , then a (k − 1)-step method to get uk−1 and then
continue with the k-step method.
SMST C: Numerical Methods 1–11
where given k, the βj are chosen to make the schemes as accurate as possible (Section 1.5.3.) Bashforth
has βk = 0 and Moulton has βk ̸= 0. A typical Adams-Bashforth method (AB4) is
∆t
un+4 = un+3 + 55f n+3 − 59f n+2 + 37f n+1 − 9f n
24
and it is explicit since the approximate solution can be found at each step without solving any algebraic
equations. In contrast, Adams-Moulton 4 is
∆t
un+3 = un+2 + 9f n+3 + 19f n+2 − 5f n+1 − 9f n
24
and is implicit since we do need to solve an algebraic equation to get un+3 .
Another common class of methods are the Backward Difference Formulae (BDF) with general form
k
X 1
∇i∆t un+k = ∆t f n+k (1.19)
i=1
i
where the backward difference operator ∇∆t and its iterates are defined by
∇∆t ur = ur − ur−1 ,
∇2∆t ur = ∇∆t (∇∆t ur )
= ∇∆t ur − ∇∆t ur−1
= (un − un−1 ) − (un−1 − un−2 )
and so on. BDFs are implicit methods. A typical example, written in standard LMM format (1.18)
normalised so that α2 = 1, is BDF2
4 1 2∆t n+2
un+2 − un+1 + un = f . (1.20)
3 3 3
We discuss Backward Difference Formulae some more in Section 2.2, since they have very useful stability
properties.
Schemes with special features are used for special types of problems. For example, the Leap-Frog method
(also known as the explicit midpoint rule) is a two step method
It has particularly useful properties for Hamiltonian systems, like N -body gravitational problems (such
as the motion of the solar system or star clusters) and molecular dynamics. It is one of the most used
numerical methods, although better known in applications areas under names like the Verlet method.
For completeness, let us note that some, but not all, of the early examples we met can be cast as one-step
LMMs. e.g. Euler and Backward Euler are simple examples of one-step LMMs as well as of RK methods.
Definition 1.1 For the general LMM (1.18), we define the first and second characteristic polyno-
mials
k
X Xk
ρ(z) = αj z j and σ(z) = βj z j .
j=0 j=0
SMST C: Numerical Methods 1–12
1.5.1 Zero-stability
A starting point for establishing if a numerical method for approximating ODEs is any good or not is to
see if it can solve u′ = 0. The solution of this ODE is clearly
u(t) = u(0) ∀ t ≥ 0.
This sounds too trivial a test problem, but it can actually give vital information about the scheme. For
example the Explicit Euler method (1.3) applied to u′ = 0 gives
un+1 = un n = 0, 1, . . . .
So does the Implicit Euler method (1.8). In fact this is the case for all RK methods.
The property related to solving u′ = 0 that is required for k-step LMMs is actually less demanding than
getting the right answer. It is called Zero Stability.
Theorem 1.1 (Zero Stability and the Root Condition) A linear multi-step method is zero-stable
if and only if all the roots of the characteristic polynomial ρ(z) (Definition 1.1) satisfy |z| ≤ 1, and any
root with |z| = 1 is simple.
The characteristic polynomial ρ(z) is obtained by applying the general LMM (1.18) to u′ = 0 to get
k
X
αj un+j = 0.
j=0
u′ = λu λ ∈ C−
u(t) = exp(λt)u(0)
Definition 1.2 The range of values of ∆tλ in the complex plane for which the scheme is absolutely
stable is called the Region of Absolute Stability while the section of the negative real axis starting
from hλ = 0 and intersecting the RAS is the Interval of Absolute Stability.
for n = 0, 1, . . .. So
un = (1 + ∆tλ)n u0
SMST C: Numerical Methods 1–13
and un → 0 in general, if and only if |1 + ∆tλ| < 1. So the RAS is the interior of the disk centre −1
radius 1 in the complex ∆tλ plane. Restricting λ to be real gives the IAS to be ∆tλ ∈ (0, −2) where
λ ∈ R. So, when λ < 0 we have to choose
2
0 < ∆t <
−λ
for absolute stability.
k1 = ∆tλun
2
∆tλ(un + k 1 ) = ∆tλ + (∆tλ)2 un
k =
1 1 1
un+1 n
k + k = 1 + ∆tλ + (∆tλ) un = R(∆tλ)un
2 2
= u +
2 2
for n = 0, 1, . . ., so that
un = R(∆tλ)n u0
and un → 0 in general, if and only if |R(∆tλ)| < 1. So the RAS is the interior of a complicated region
in the complex ∆tλ plane. Restricting λ to be real again gives the IAS to be ∆tλ ∈ (0, −2) where λ ∈ R.
So, when λ < 0 we have to choose
2
0 < ∆t <
−λ
for absolute stability.
un+1 = R(λ∆t)un
where we can get the stability function R directly as above, or by using the Butcher table notation
(1.16)
R(z) = 1 + zbT (I − zA)−1 1,
and 1 := (1, 1, . . . , 1)T . It is relatively easy to show that R(z) is a polynomial in z for explicit RK
methods, and that it is a rational polynomial function for implicit RK methods. We have
n
un = (R(λ∆t)) u0
and clearly we need |R(λ∆t)| < 1 to ensure that un → 0 as n → ∞ for absolute stability.
For k-step LMMs the requirement for absolute stability is that the roots z = zi , i = 1, . . . , k, of the
second characteristic equation
ρ(z) − ∆tλ σ(z) = 0 (1.21)
all satisfy |zi | < 1.
ρ(z) = z − 1, σ(z) = 1
and (1.21) has root z = 1 + ∆tλ, which is less than 1 in modulus for ∆tλ inside the disk described in
Example 1.7.
where u(t) is an exact solution of the ODE u′ = f (t, u) and we generate the k i starting with un = u(tn ),
the exact solution.
For general LMMs (1.18) it is defined similarly as
k k
1 X X
LTE(tn ) = αj u(tn+j ) − ∆t βj f (tn+j , u(tn+j ))
αk ∆t j=0 j=0
where again u(t) is an exact solution of the ODE u′ = f (t, u). This is neater if the standard normalisation
αk = 1 has been used to specify the scheme.
What we want to know is the behaviour of the LTE as ∆t → 0 since this tells us something about the
convergence properties of the scheme. The order of the LTE is the highest number p satisfying
LTE = O(∆tp ).
Very detailed multivariable Taylor expansion is needed for RK methods, and the following results are
available. For s stage implicit RK, methods of order p = 2s have been derived. However, for s stage
explicit RK, the maximum order is at best p = s, and often p < s as shown below.
Explicit RK stages s= 1 2 3 4 5 6 7 8 9 10
p
Best LTE is O(∆t ) with p= 1 2 3 4 4 5 6 6 7 7
For LMMs the Taylor expansion is much more straightforward. We use the ODE to replace the f by u′
(written as u(1) below) and get
k
X k
X
(αk ∆t) LTE(tn ) = αj u(tn+j ) − ∆t βj f (tn+j , u(tn+j ))
j=0 j=0
k
X k
X
= αj u(tn + j∆t) − ∆t βj u(1) (tn + j∆t)
j=0 j=0
k
(j∆t)2 (2) n
X
= αj u(tn ) + (j∆t)u(1) (tn ) + u (t ) + · · ·
j=0
2!
k
(j∆t)2 (3) n
X
−∆t βj u(1) (tn ) + (j∆t)u(2) (tn ) + u (t ) + · · · .
j=0
2!
Careful bookkeeping (or the use of Maple or Mathematica) gives the required results, and we get
where Cp+1 is simply a number that depends on the details of the scheme. If you really need to know
what it is (and sometimes that is useful), it is given by
k k
1 1 X 1 X
Cp+1 = j p+1 αj − j p βj .
αk (p + 1)! j=0 p! j=0
An alternative and equivalent way to establish the order of accuracy for LMMs is given by the following.
It uses the characteristic polynomials defined in Definition 1.1.
Theorem 1.2 (eg [6]) The multistep method is of order p ≥ 1 if and only if there exists c ̸= 0 such that
Once again, careful bookkeeping of Taylor expansions (this time expanding about z = 1) is required.
SMST C: Numerical Methods 1–15
LTE → 0 as ∆t → 0.
us → u(0) as ∆t → 0
for each s = 0, . . . , k − 1.
Initial data for RK schemes are consistent if they satisfy the above with k = 1. Of course this is trivial if
one does the obvious thing and uses u0 = u(0), the initial data supplied with the original ODE problem.
1.5.6 Convergence
This is the real goal. What we want is that the error in the approximate solution everywhere over the
fixed time interval t ∈ [0, T ] should tend to zero as the step size ∆t → 0. One could write this for a scalar
problem as
||uapprox (t) − u(t)||L∞ [0,T ] → 0 as ∆t → 0.
Fortunately for most well-behaved ODE systems, LMMs satisfy the Dahlquist Equivalence Theorem.
If the initial data are in addition accurate enough, then the rate of convergence is the order of the LTE.
See e.g. [1, Thm. 5.1] and [8, Thm. 12.5] for some technical details.
So checking two relatively “simple” properties of the approximation scheme is enough to establish con-
vergence, and it does not have to be tackled directly.
Similar results hold for RK methods (and one-step methods in general). See e.g. [1, Thm. 3.1] or the
comprehensive coverage in [4].
References
[1] U. Ascher and L. Petzold. Computer Methods for Ordinary Differential Equations and Differential
Algebraic Equations. SIAM, 1998.
[2] K. E. Atkinson. An Introduction to Numerical Analysis. Wiley, 2 edition, 1989.
[3] R. L. Burden and J. D. Faires. Numerical Analysis. Brooks Cole, 7 edition, 2001.
[4] J. C. Butcher. The Numerical Analysis of ODEs. Wiley-Interscience, 1987.
SMST C: Numerical Methods 1–16
[5] D. F. Griffiths and D. J. Higham. Numerical Methods for Ordinary Differential Equations. Springer,
2010.
[6] A. Iserles. A first course in the Numerical Analysis of Differential Equations. Cambridge texts in
applied mathematics. CUP, Cambridge, UK, 1996.
[7] A. M. Stuart and A. R. Humphries. Dynamical Systems and Numerical Analysis. Cambridge Mono-
graphs on Applied and Computational Mathematics. CUP, Cambridge, UK, 1998.
[8] E. Süli and D. Mayers. An Introduction to Numerical Analysis. CUP, 2003.
SMSTC (2024/25)
Numerical Methods
Lecture 2: Numerical Methods for ODEs 2
Dugald B Duncan, Heriot-Watt Universitya
www.smstc.ac.uk
Contents
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–2
2.2 Systems of ODEs and stiffness . . . . . . . . . . . . . . . . . . . . . . . . . . 2–2
2.2.1 Linear systems of ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–2
2.2.2 Nonlinear systems of ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–3
2.2.3 Stiffness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–3
2.2.4 A-stability and A(α)-stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–4
2.3 Computational effort and efficiency . . . . . . . . . . . . . . . . . . . . . . . 2–5
2.4 Practical global error estimation for fixed step schemes . . . . . . . . . . . 2–5
2.5 Error Estimation and Time Step Adaptivity . . . . . . . . . . . . . . . . . . 2–7
2.5.1 Local error estimation for RK methods . . . . . . . . . . . . . . . . . . . . . . 2–8
2.5.2 Local error estimation for implicit LMMs . . . . . . . . . . . . . . . . . . . . . 2–8
2.5.3 Time step control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–9
2.5.4 How is the global error related to the local error tolerance? . . . . . . . . . . . 2–10
2.6 What solvers are available and how do we use them? . . . . . . . . . . . . . 2–10
2.6.1 Explicit solvers for non-stiff problems . . . . . . . . . . . . . . . . . . . . . . . 2–11
2.6.2 Implicit LMM solvers for stiff problems . . . . . . . . . . . . . . . . . . . . . . 2–11
2.6.3 Other sources of ODE software . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–13
2.7 The Method of Lines for simple, time-dependent PDEs . . . . . . . . . . . 2–16
2.7.1 Output and discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–18
2.8 What has not been covered? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2–19
a D.B.Duncan@hw.ac.uk
2–1
SMST C: Numerical Methods 2–2
2.1 Introduction
These two lectures are about the accurate and efficient approximation of the solution of systems of
ordinary differential equations - hereafter abbreviated to “ODEs”. The aim is to introduce key ideas and
the language used to describe the attributes of numerical methods, as opposed to doing rigorous analysis.
There is usually no need to write your own solver for ODEs. There are lots of good packages available
that many people have devoted years of their life to. However it is useful to have a good idea of the
theory and ideas that underlie solving ODEs and hence understand the limitations and advantages of
packages.
This lecture aims to give an overview of practical issues related to using methods and software packages.
u′ = Au (2.1)
A = SDS −1
where S, D are matrices of the same dimension as A, and D is diagonal. If such S, D exist, then A and
D are said to be similar matrices and they have the same eigenvalues λµ (A), µ = 1, . . . , M . Hence the
diagonal elements of D are the eigenvalues of A.
Now define v(t) = S −1 u(t) for all t ≥ 0. It can be shown that applying a standard RK or LMM to (2.1)
is equivalent to applying the same scheme to the uncoupled system of ODEs
v′ = Dv, (2.2)
in the sense that the approximations generated satisfy vn = S −1 un , for all n ≥ 0. The scalar component
equations of (2.2) are
vµ′ = λµ (A)vµ , µ = 1, . . . , M (2.3)
where λµ (A) are the eigenvalues of A. Note that the eigenvalues are either real or in complex conjugate
pairs since A is real.
So the overall absolute stability of an approximation of the linear system (2.1) is determined by the
absolute stability of it applied to the eigencomponent ODEs (2.3). For the full system to be stable, all
of the eigencomponent problems must be stable and hence absolute stability is determined by the worst
cases in (2.3).
u′ = Au = SDS −1 u
where
−1 0 1 1
D= , S= .
0 −100 −0.8 0.2
The solution of the ODE is:
1 1
u(t) = c1 exp(−t) + c2 exp(−100t) (2.4)
−0.8 0.2
| {z } | {z }
slow decay fast decay
SMST C: Numerical Methods 2–3
1.5
U0 and U1
0.5
-0.5
-1
0 0.5 1 1.5 2 2.5 3 3.5 4
time t
Figure 2.1: Solution (u1 , u2 ) given by (2.4). Over a very short timescale we see decay from the fast
eigenvalue, then decay is dominated by the faster eigenvalue. For an explicit fixed step scheme the step
size is determined by the fast time scale. The fast transient at the start is like a boundary layer from the
earlier part of the course.
where c1 , c2 must be chosen to satisfy the initial condition u(0) = u0 . The solution is shown in Figure 2.1.
Stability for explicit Euler is restricted by the “fast” or big eigenvalue λ2 = −100 and we have
100∆t < 2 → ∆t < 2/100 = 0.02,
but the actual solution is completely dominated by the slow component after a short initial period. The
slow component would be reasonably well resolved with time steps
∆t = O(1/|λ1 |) = O(1)
but stability requires something much smaller. So the acceptable step size ∆t and overall effort is de-
termined by something that only contributes to the solution for a short time. After an initial period,
accuracy is determined only by the slow component.
Stiffness is showing up here in the large ratio of the timescales in the problem.
2.2.3 Stiffness
It is hard to get a clean definition of stiffness that covers all cases, because it can arise in different ways.
Stiff problems have timescales in the solution which differ by a large factor.
For the linear ODE u′ = Au where A has eigenvalues λj < 0, the traditional definition of stiffness
is that
def maxj |λj |
Stiffness Ratio = >> 1 .
minj |λj |
SMST C: Numerical Methods 2–4
u′ = A(u1 )u ,
where the matrix A is essentially tridiagonal and depends only on the first component of u. Typical
solutions evolve on time scales of O(1020 ) and the eigenvalues of the linearised problem are real and
negative with maxj |λj | ≈ 10.
The Euler stability limit is then ∆t < 0.2 implying that O(1021 ) steps would be required to compute
the solution. Unfortunately the CPU time per step ≈ 0.001 seconds on a powerful workstation, giving a
total CPU time of 1018 seconds or O(1010 ) years. One could wait until machines are 106 faster (in about
20 years) and do it on a 1000 processor parallel computer, but the calculation using explicit Euler would
still take about 10 years!
A good implicit package based on BDF methods takes only a few minutes.
The simplest A-stable methods are the implicit Euler method (1.8) (with R(z) = 1/(1 − z)) and
the Trapezoidal Rule (1.9) (with R(z) = (1 + z/2)/(1 − z/2)).
Explicit RK methods are not A-stable. This is because R(z) is a polynomial in z for an explicit
method, and polynomials of degree 1 or more are unbounded as |z| → ∞.
Even the BDF methods (1.19) which are designed for stiff problems cannot combine high order LTEs,
zero stability and A-stability, but they do satisfy a slightly less restrictive stability requirement that their
RAS contains the sector in the left half complex plane with angle [180◦ − α◦ , 180◦ + α◦ ]. This is called
A(α)-stability. When α > 0 this includes all of the negative real axis, which is useful for stiff problems
with pure real eigenvalues. Note that only BDF1—BDF6 are zero-stable and hence convergent/useful.
BDF # 1 2 3 4 5 6 ≥7
√ √ √ √ √ √
Zero-stable No
LTE order 1 2 3 4 5 6 N.A.
◦ ◦ ◦ ◦ ◦ ◦ ◦
α 90 90 88 73 51 18 N.A.
SMST C: Numerical Methods 2–5
Definition 2.1 We rewrite the approximate solution obtained with fixed step size ∆t to show the depen-
dence on ∆t as u∆t (tn ) ≡ un , where tn = t0 + n∆t for n = 0, 1, 2, . . ..
If we have a scheme with LTE = O(∆tp ) for some integer p ≥ 1, and the ODE it is applied to is
well-enough behaved, then the convergence result in Section 1.5.6 tells us that the Global Error
u(t) − u∆t (t) = O(∆tp ) as ∆t → 0
for all t = tn ∈ [0, T ], the finite time interval of interest. Another way to write this when ∆t is small
enough is
u∆t (t) = u(t) + O(∆tp ) ≈ u(t) + c(t)∆tp
where the exact solution u and c do not depend on ∆t. If we compare approximate solutions obtained
with different stepsizes, usually ∆t and ∆t/2, then
∆tp
p
u∆t (t) − u∆t/2 (t) ≈ (u(t) + c(t) ∆t ) − u(t) + c(t) p
2
1
= c(t) ∆tp 1 − p
2
1
≈ 1 − p (u(t) − u∆t (t)) .
2
So the difference between approximate solutions with two different step sizes is approximately proportional
to the Global Error: p
2
∥u(t) − u∆t (t)∥ ≈ ∥u∆t (t) − u∆t/2 (t)∥ (2.5)
2p − 1
using an appropriate norm for vector-valued solutions. Table 2.1 gives an example of this for the logistic
ODE.
SMST C: Numerical Methods 2–6
0
10
−2
10
−4
10
−6
10
−8
10
Euler
Huen
−10
10 RK4
AB4
−4 −3 −2 −1 0
10 10 10 10 10
Computer Time
Figure 2.2: Maximum relative error in the approximate solution of the logistic equation u′ = u(1 − u)
with u(0) = 0.02 for t ∈ [0, 10] by various explicit methods plotted against the CPU time required on
my old laptop. The step size decreases from ∆t = 10/4 in multiples of 2 to ∆t = 10/215 — result
shown by a marker symbol for each scheme. The most efficient scheme is the one that gives the required
error (say 10−4 ) with the least CPU time. For all errors less than about 0.001 in this example, it is
Adams-Bashforth 4 (AB4). We see AB4 and the Classical RK method (RK4) decreasing in parallel since
both are 4th order accurate methods. Huen decreases with about 1/2 the slope since it is 2nd order, and
Euler decreases with 1/2 the slope again since it is first order.
SMST C: Numerical Methods 2–7
log10(error)
4 1.25 0.670943530629 0
8 0.625 0.725665614442
16 0.3125 0.744405428442 −2
32 0.15625 0.749814551585
64 0.078125 0.751277482656
−4
128 0.039062 0.751659099945
256 0.019531 0.751756651338
512 0.0097656 0.751781318874 −6
−2 −1 0 1
1024 0.0048828 0.751787521467 log10(∆ t)
Table 2.1: The approximate solution at t = 5 for the well-behaved ODE u′ = u(1 − u) with u(0) = 0.02
using the Improved Euler method (1.14) with fixed step size ∆t = 5/N . The Figure shows the error
estimate (4/3)|u∆t (5) − u∆t/2 (5)| obtained from (2.5) with p = 2 as well as the actual error. There is
little difference except when the step size is large and the estimation process that gives (2.5) is inaccurate.
The slope of the graph in Table 2.1 asymptotes to 2 as ∆t → 0 because the scheme (1.14) is 2nd order
accurate. This sort of graph can act as a check that a fixed step size scheme is working correctly. It can
do this by showing that it converges with the correct rate (or not), by providing a global error estimate
and by indicating where the step sizes are small enough to enter the convergence zone so that we can
trust the error estimate and hence the approximate solution.
Definition 2.2 Given the time levels t0 , t1 , . . ., not necessarily equally spaced, the local time step size is
defined by
∆tn−1/2 = tn − tn−1
for n = 1, 2, . . .. Looking at this the other way, given the sequence of timestep sizes ∆t1/2 , ∆t3/2 , . . ., the
time levels tn are defined by
n
X
tn = tn−1 + ∆tn−1/2 = t0 + ∆tj−1/2 .
j=1
Variable time step sizes are used in virtually all modern codes for ODEs. The usual approach in ODE
codes is to use an estimate of the local error (LE) defined next to determine what step size to use.
Definition 2.3 The local error is defined to be the error over the step from t = tn to t = tn+1 assuming
that no errors have been made up to time t = tn . In other words, apply the scheme to the local exact
problem û′ = f (t, û) with initial condition û(tn ) = un and calculate the error in solving this local problem
over one step.
A consequence of this definition is that there is a simple relationship between local error (LE) and the
local truncation error (LTE) defined in Section 1.5.3:
LE = ∆t LTE,
SMST C: Numerical Methods 2–8
Embedded pairs
To cut down the computational effort, a pairs of RK methods of order p and p + 1 are constructed sharing
as many of the same stage calculations (the k in (1.12)) as possible. The simplest example is the forward
Euler method embedded in a modified trapezoidal rule:
k1 = ∆t f (tn , un )
k2 = ∆t f (tn + ∆t, un + k1 )
n+1
u = un + k1
1 1
un+1 = un + k + k2
aux
2
LE estimate = un+1 − un+1
aux .
Probably the most famous embedded formula is the explicit Fehlberg 4(5) pair which uses a method of
order 4 with an auxiliary scheme of order 5. It has 6 different stages (the k evaluations) instead of at least
9 for two arbitrarily selected explicit RK methods of these orders. The details are not very instructive but
can be found for example in [1]. The methods of Dormand and Prince are now probably more commonly
used. The 4(5) pair which is the basis of the Matlab code has 7 stages.
In practice embedded RK pairs are usually based on explicit schemes and so are not often used for stiff
problems.
and we need to solve the nonlinear system of algebraic equations, g(un+1 ) = 0 for un+1 at each timestep.
This is usually done using Newton’s method (Section 1.1.3) or a variant of it. The initial guess for un+1
may be obtained by using an explicit LMM called the predictor in the literature. The implicit method
we actually want to use is called the corrector. If we match the explicit and implicit LMMs to have the
same order of accuracy (say p) we get the following way to estimate the local error.
The predictor method can be chosen to play the part of the auxiliary scheme in the previous subsection
about RK methods. Typically a predictor of the same order of accuracy as the corrector is used. From
the LTE definition of Section (1.5.3), and from the relationship LE = ∆t LTE, we have
LE = Cp+1 ∆tp+1 û(p+1) (tn ) + O(∆tp+2 ), LEaux = C̃p+1 ∆tp+1 û(p+1) (tn ) + O(∆tp+2 )
for the main scheme (the corrector) and auxiliary scheme (the predictor) respectively where the exact
local solution is given by û′ = f (t, û) with û(tn ) = un . Then assuming that Cp+1 ̸= C̃p+1 , we follow the
approach of the previous subsection to get
un+1 − un+1
aux = (û(tn+1 ) − û(tn+1 )) + LE − LEaux
= (Cp+1 − C̃p+1 ) ∆tp+1 û(p+1) (tn ) + O(∆tp+2 )
!
Cp+1 − C̃p+1
= LE + O(∆tp+2 )
Cp+1
!
Cp+1 − C̃p+1
≈ LE
Cp+1
is an estimate of the local error and there exists constant C, independent of ∆t such that
Cp+1 p+1
∥LEest ∥ = ∥un+1 − un+1
aux ∥ ≈ C ∆t . (2.7)
Cp+1 − C̃p+1
∥LEest (∆t)∥
∥LEest (∆t)∥ ≈ C∆tp+1 ⇔ C≈
∆tp+1
and so
TOL TOL
∥LE(∆t∗ )∥ ≈ C∆tp+1
∗ ≈ TOL ⇔ ∆tp+1
∗ ≈ ≈ ∆tp+1 .
C ∥LEest (∆t)∥
SMST C: Numerical Methods 2–10
and the local error estimate is given by (2.6) for RK and (2.7) for LMMs. To be on the safe side of all
these estimates, in most computer codes the optimal step size is given by
1/p+1
TOL
∆t∗ = Fsafe ∆t (2.9)
∥LEest (∆t)∥
2–4. Set
!1/p+1
n+1/2 TOL
∆t∗ = Fsafe ∆t .
∥LEest (∆tn+1/2 )∥
2–5. If LEest > TOL, then the local error is too big.
Reset the step size to be ∆tn+1/2 := ∆t∗ .
Goto Algorithm Step 2–2 and try again.
2–6. If LEest ≤ TOL, then the local error is small enough.
Reset n := n + 1.
Set the new step size to be ∆tn+1/2 = ∆t∗ .
Goto Algorithm Step 2–2 and continue.
To make this algorithm work in real life one needs to add a stopping condition so that we stop the
calculation once we have approximated the solution all the way from t = 0 to t = T . Additionally one
might also add a mechanism to choose the step size to give outputs at pre-selected values of t of particular
interest.
Using a one-step scheme with a different size for each step is straightforward. However, you might have
noticed that k-step LMMs are based on having past information over k equal length steps and we lose
that if we have non-uniform step sizes. A workaround is required. We are not going to cover it here, but
is used routinely in ODE codes.
2.5.4 How is the global error related to the local error tolerance?
This was a hot topic in the 1980’s, but seems to be overlooked when discussing ODE solvers these days.
It is important, since specifying reltol and abstol in (2.8) does not give a guaranteed error bound for the
global error. It only directly controls the size local error estimate. The relationship between the local
error tolerance, and the global (or actual) error is not straightforward.
Fortunately the global error is usually more or less proportional to the local error tolerance. See Figure
2.3 for a simple example that works well. There we see that when reltol is small, the global error in each
case is roughly proportional to reltol, and is between 5 and 10 times reltol.
−5 −5
10 10
−10 −10
10 10
−10 −5 0 −2 −1 0 1
10 10 10 10 10 10 10
Relative Tolerance Computer Time
Figure 2.3: Global error in the solution of the logistic ODE u′ = u(1 − u) with u(0) = 0.02 for t ∈ [0, 10]
using the Matlabembedded pair explicit RK codes ode23 and ode45 with abstol = 10−30 . The left plot
shows the relationship between the specified relative tolerance reltol in (2.8) and the global error. The
right hand plot shows the computational cost. The 4th order code ode45 is more efficient than the second
order code ode23.
similar things in Python, C etc.. Matlab has a range of very reliable stiff and non-stiff solvers with
good documentation. They are easily interchangeable and deal automatically with sparse linear algebra.
They can deal with combinations of ODEs and algebraic equations (called DAEs) as well.
Filename: oscode.m
dudt = [u(2);
-(u(1)^2-4-sin(t))*u(2) - u(1)^3];
Filename: runosc.m
subplot(2,1,1)
plot(t,phi,’b-’,t,phiprime,’r--’) %%% plot the results
legend(’\phi’,’d\phi/dt’)
xlabel(’time t’,’fontsize’,14)
30
φ
20 dφ/dt
10
−10
−20
0 1 2 3 4 5 6 7 8 9 10
time t
which is obtained directly from (1.18) by substituting n := n − k + 1. Using Newton’s method 1.1.3 to
solve g(un+1 ) = 0 for un+1 on each time step, we get the iteration
−1
n+1,[ν+1] n+1,[ν] ∂g n+1,[ν]
u =u − (u ) g(un+1,[ν] ) for ν = 0, 1, 2 . . .
∂u
where the initial guess un+1,[0] is provided by a predictor method and we stop when ∥un+1,[ν+1] −un+1,[ν] ∥
is small enough. Note that the Jacobian matrix is
∂g ∂f
= αk I − βk ∆tn+1/2 ,
∂u ∂u
where I is the M × M identity matrix, all commensurate with the dimension of u′ = f (t, u).
Clearly need to specify the function f (t, u) to make any ODE solver work (otherwise how would it know
which ODE it was to solve?). Additionally for implicit methods we need a way to calculate the Jacobian
matrix, and that is usually best done by providing another M-file or subroutine to calculate all the entries
in ∂f /∂u.
Further, for large systems, the linear algebra required in the Newton iteration can dominate the computer
time and it makes sense to exploit all possible ways to speed this up, like using sparse solvers. Fortunately
Matlab deals with sparse matrices automatically if you make sure that the output from the routine you
supply to calculate the Jacobian produces a matrix of sparse type.
Example 2.3 Approximate the solution of the Robertson chemical reaction system [3, Ch. 12.2.3]
with u(0) = (u1 (0), u2 (0), u3 (0))T = (1, 0, 0)T using a stiff solver for t ∈ [0, 5].
First encode this in a function M-file to define the function f (t, u) in chemode.m of Table 2.3. Next create
function M-file chemjac.m to calculate the Jacobian matrix. Finally, create a script M-file runchem.m to
run ode15s and plot the results (see Figure 2.5). This choice of tout in runchem.m gives output at the
time levels used by the solver.
Python, or more specifically the SciPy library, has the code solve ivp which covers some of the
same methods as Matlab.
The Nag library has many F77/C callable routines for solving ODEs making use of a wide range
of special structures in the linear algebra.
The Netlib WWW repository www.netlib.org contains a mountain of software (usually F77
source) for solving all sorts of things. The most appropriate packages for ODEs/DAEs are under
ode and I suggest vode, vodpk for ODEs and DASSL and DASPK are well respected LMM codes
for differential algebraic equations.
RK Stiff codes publicly available include Radau5 [2] and Stride by Burrage, Butcher and Chipman.
Filename: chemode.m
xlabel(’time t’,’fontsize’,14)
−5 −5
x 10 x 10
4 4
3.8
3.5
3.6
3 3.4
Concentration of chemical 2
3.2
2.5
0 0.02 0.04
2
−5
x 10
3.7
1.5
3.65
1
3.6
0.5
3.55
0 3.5
0 1 2 3 4 5 0 0.002 0.004 0.006 0.008 0.01
time t time t
Figure 2.5: Results of Example 2.3. The markers show the time levels used by the adaptive time stepping
in the solver, and the smaller graphs focus on the region of small time steps. There is a fast transient at
the start. The problem is fairly stiff, and explicit methods do not work well for it.
SMST C: Numerical Methods 2–16
h2 h3
u(xj − h, t) − 2u(xj , t) + u(xj + h, t) = u − hux + uxx − uxxx + . . .
2! 3! (xj ,t)
−2u(xj , t)
h2 h3
+ u + hux + uxx + uxxx + . . .
2! 3! (xj ,t)
2h4
= h2 uxx (xj , t) + uxxxx (xj , t) + O(h6 )
4!
and so
u(xj − h, t) − 2u(xj , t) + u(xj + h, t) uj−1 (t) − 2uj (t) + uj+1 (t)
uxx (xj , t) ≈ ≈ .
h2 h2
The other parts of the PDE are easily approximated:
duj ∂u
(t) ≈ (xj , t) and g(uj (t)) ≈ g(u(xj , t))
dt ∂t
are obvious (but not unique) choices.
We put these three approximations together to define the MOL approximation
duj 2 uj−1 − 2uj + uj+1
=ε + g(uj )
dt h2
at the interior points j = 1, . . . , N−1, with u0 (t) = 1, uN (t) = 0. Now we write this in the standard form
u′ = f (t, u) ≡ Au + g(u) + b
where
is tridiagonal.
This specifies the ODE function f (t, u) = Au + g(u) + b, including the system size parameter N , matrix
A and boundary condition vector b as arguments of the function. This saves on recomputing them each
time the function is called.
Similar to the previous function, but this time specifying the Jacobian matrix
∂f
= A + diag(1 − 2uj : j = 1, . . . , N − 1),
∂u
which is a sparse N − 1 × N − 1 tridiagonal matrix.
%%%---------------------------------------------------------
%%% Problem settings
%%%---------------------------------------------------------
%%%---------------------------------------------------------
SMST C: Numerical Methods 2–18
A = (epsilon^2/dx^2)*A;
%%%---------------------------------------------------------
%%% Run the ODE solvers ode45 and ode15s
%%%---------------------------------------------------------
disp(’-------------------------------------’);
disp(’TEST ODE45’)
disp(’-------------------------------------’);
options=odeset(’stats’,’on’,’AbsTol’,ATOL,’RelTol’,RTOL);
disp(’ ’)
disp([’ODE solve by ode45 took ’,num2str(t1),’ seconds’]);
disp(’ ’)
%%%----------------------------------------------------------
disp(’-------------------------------------’);
disp(’TEST ODE15S’)
disp(’-------------------------------------’);
options=odeset(’stats’,’on’,’AbsTol’,ATOL,’RelTol’,RTOL,’Jacobian’, @rdjac);
disp(’ ’)
disp([’ODE solve by ode15s took ’,num2str(t2),’ seconds’]);
>> runrdodes
-------------------------------------
TEST ODE45
SMST C: Numerical Methods 2–19
-------------------------------------
4839 successful steps
314 failed attempts
30919 function evaluations
-------------------------------------
TEST ODE15S
-------------------------------------
246 successful steps
0 failed attempts
291 function evaluations
1 partial derivatives
38 LU decompositions
289 solutions of linear systems
We see that the implicit code ode15s was significantly faster than the explicit code ode45. The rest of
the diagnostic output above is produced as a result of setting stats to on in the options statement. We
see that ode45 did no linear algebra (since it is explicit), but used a large number of function evaluations.
On the other hand, ode15s did some linear algebra (LU decompositions and solutions of linear systems)
and a small number of function evaluations and the net effect is that it took much less time overall.
Both codes use schemes of roughly the same order of accuracy (order 4 and order up to 5 respectively), so
there is no obvious difference there. The main reason that ode15s is more efficient is that this problem is
stiff, and ode15s is able to take fewer, bigger steps than ode45, which is hampered by its linear stability
restriction. Indeed we can see that ode45 shows quite a few “failed attempts”. These are steps which
have to be done again because the step size predicted by the local time step control turned out to be too
big — a common indication that the problem is stiff.
Differential Algebraic Equations (DAEs) — these are ODEs combined with algebraic constraints
(see e.g. [1]). Matlab and other ODE software deals with this reasonably easily.
Delay Differential Equations — ODEs with a delay term. Some software (Matlab dde23 and
ddesd), but not so easy.
Event detection — at the simplest level, work out when the solution passes through a target value.
Matlab and other ODE software deals with this reasonably easily.
Hamiltonian systems of any kind (e.g. molecular dynamics, N-body gravtitional problems) and
systems with special invariants.
Highly oscillatory systems.
Boundary value problems — similar to elliptic PDE problems coming in later lectures.
Stochastic differential equations — coming in later lectures. Take care – don’t use standard ODE
solvers with stochastic forcing of any kind.
References
[1] U M Ascher and L R Petzold, Computer Methods for Ordinary Differential Equations and Differential-
Algebraic Equations, SIAM (1998).
SMST C: Numerical Methods 2–20
[2] E. Hairer and G. Wanner. Solving Ordinary Differential Equations II, Stiff and Differential Algebraic
Equations. Number 14 in Springer Computational Mathematics. Springer, Berlin, 1991.
[3] D J Higham and N J Higham, Matlab Guide, SIAM (2005).
[4] K Segeth, A posteriori error estimation with the finite element method of lines for a nonlinear parabolic
equation in one space dimension, Numer. Math. 83, 455-475 (1999).
[5] L F Shampine and M W Reichelt, The MATLAB ODE suite, SIAM J. Sci. Comput., 18, 1–22 (1997).