GniCodes { Matlab Programs for
Geometri Numeri al Integration
Ernst Hairer
1
and Martin Hairer
2
1
Se tion de math�ematiques, Univ. Gen�eve, CH-1211 Gen�eve 24, Switzerland
2
Mathemati s Institute, Univ. Warwi k, Coventry CV4 7AL, England
Abstra t. Geometri numeri al integration is synonymous with stru ture-preser-
ving integration of ordinary di�erential equations. These notes, prepared for the
Durham summer s hool 2002, are omplementary to the monograph of Hairer,
Lubi h and Wanner [12℄. They give an introdu tion to the subje t, and they dis uss
and explain the use of Matlab programs for experimenting with stru ture-preserving
algorithms.
We start with presenting some typi al lasses of problems having properties
that are important to be onserved by the dis retization (Se tion 1). The ow of
Hamiltonian di�erential equations is symple ti and possesses onserved quanti-
ties. Conservative systems have a time-reversible ow. Di�erential equations with
�rst integrals and problems on manifolds are also onsidered. We then introdu e
in Se tion 2 simple symple ti and symmetri integrators, (partitioned) Runge-
Kutta methods, omposition and splitting methods, linear multistep methods, and
algorithms for Hamiltonian problems on manifolds. We brie y dis uss their sym-
ple ti ity and symmetry. The improved performan e of su h geometri integrators
is best understood with the help of a ba kward error analysis (Se tion 3). We ex-
plain some impli ations for the long-time integration of Hamiltonian systems and
of ompletely integrable problems.
Se tion 4 is devoted to a presentation and explanation of Matlab odes for
impli it Runge-Kutta, omposition, and multistep methods. The �nal Se tion 5
gives a omparison of the di�erent methods and illustrates the use of these programs
at some typi al interesting situations: the omputation of Poin ar�e se tions, and
the simulation of the motion of two bodies on a sphere. The Matlab odes as well
as their Fortran 77 ounterparts an be downloaded at
http://www.unige. h/math/folks/hairer
under the item \software".
1 Problems to be Solved
For the numeri al solution of ordinary di�erential equations there exist well-
developed theories, and ex ellent general purpose odes are available and
widely used. If the ow of the di�erential equation has a parti ular stru -
ture, then its preservation by the dis retization s heme an improve on-
siderably its performan e and its qualitative behaviour. This arti le fo uses
on stru ture-preserving algorithms for some important lasses of problems {
Hamiltonian systems and reversible di�erential equations.
2 Ernst Hairer and Martin Hairer
1.1 Hamiltonian Systems
For a smooth fun tion H(p; q) de�ned on an open set D � R
d
� R
d
we
onsider the di�erential equation
_p
i
= �
�H
�q
i
�
p; q
�
; _q
i
=
�H
�p
i
�
p; q
�
; i = 1; : : : ; d: (1.1)
The dimension d of the ve tors p and q is alled the `degree of freedom' of
the system. We also use the more ompa t notation
_p = �r
q
H(p; q); _q = r
p
H(p; q);
or
_y = J
�1
rH(y); y =
�
p
q
�
; J =
�
0 I
�I 0
�
; (1.2)
where r
p
H;r
q
H;rH denote the olumn ve tors of partial derivatives with
respe t to the omponents of p; q; y, respe tively. The matrix J is the stru -
ture matrix of Hamiltonian systems in anoni al form.
Throughout this arti le we denote by '
t
(y) the exa t ow of the system
(1.2), i.e., '
t
(y
0
) = y(t) is the solution at time t of the problem (1.2) with
initial value y(0) = y
0
.
Example 1.1 (Classi al Me hani al Systems). Consider a me hani al system
that an be des ribed with (minimal) oordinates q 2 R
d
. Denote its kineti
energy by T (q; _q) =
1
2
_q
T
M(q) _q (with a symmetri positive de�nite matrix
M(q)) and its potential energy by U(q). The motion of the system is then
given as the solution of the Euler-Lagrange equations
d
dt
�
M(q) _q
�
=
�
�q
�
T (q; _q)� U(q)
�
(1.3)
orresponding to the variational problem
R �
T (q; _q) � U(q)
�
dt ! min. In-
trodu ing the new variables p :=M(q) _q (momenta or Poisson variables) the
di�erential equation (1.3) is equivalent to the Hamiltonian system (1.1) with
H(p; q) =
1
2
p
T
M(q)
�1
p+ U(q): (1.4)
This is an immediate onsequen e from omputing the partial derivatives of
this fun tion H(p; q). A simple example, often used for illustrations, is the
mathemati al pendulum for whi h the Hamiltonian is
H(p; q) =
1
2
p
2
� os q: (1.5)
Due to their spe ial stru ture, Hamiltonian systems have several interest-
ing properties:
GniCodes { Matlab Programs for Geometri Numeri al Integration 3
−3 −2 −1 0 1 2 3 4
−2
−1
1
2
−3 −2 −1 0 1 2 3 4
−2
−1
1
2
A
'
�=3
(A)
Fig. 1.1. Level urves H(p; q) = onst for the pendulum problem (left pi ture),
and area-preservation of its exa t ow (right pi ture).
� the Hamiltonian H(p; q) is onstant along solutions of (1.1); for lassi al
me hani al systems this means that the total energy (sum of kineti and
potential energies) is a onserved quantity.
� for systems with one degree of freedom the ow '
t
is area-preserving; for
the general ase it is volume-preserving. This means that
�
�
'
t
(A)
�
= �(A) for t � 0 (1.6)
for any ompa t set A � R
d
�R
d
.
� the ow '
t
is a symple ti transformation, i.e.,
'
0
t
(y)
T
J '
0
t
(y) = J for t � 0; (1.7)
where the prime in '
0
t
(y) denotes the derivation with respe t to y.
The �rst property is immediately veri�ed by di�erentiating
d
dt
H
�
p(t); q(t)
�
=
: : : = 0. The solutions of the mathemati al pendulum are therefore on the
level urves of the Hamiltonian (1.5); see Fig. 1.1. The se ond property is
a onsequen e of the third, be ause (1.7) and the ontinuous dependen e of
'
0
t
(y) on t imply det'
0
t
(y) = 1. This together with the transformation formula
for multiple integrals proves (1.6). The right pi ture of Fig. 1.1 illustrates the
area-preservation of the exa t ow for the pendulum equation.
The symple ti ity ondition (1.7) has a ni e geometri interpretation. It
is equivalent to the property that
!
�
'
t
(A)
�
= !(A) for t � 0
holds for any two-dimensional sub-manifoldA ofR
d
�R
d
, where !(A) denotes
the sum of the oriented areas of the proje tions of A onto the (p
i
; q
i
)-plane.
The important feature is that this property is hara teristi for Hamiltonian
systems ( f. [12, hap.VI℄), whi h means that whenever the ow of a di�er-
ential equation _y = f(y) is symple ti for all t and all y, then f(y) is lo ally
of the form f(y) = J
�1
rH(y). This hara teristi property of Hamiltonian
systems motivates the sear h for dis retizations that are symple ti .
4 Ernst Hairer and Martin Hairer
1.2 Reversible Di�erential Equations
Consider �rst a me hani al system for whi h the equations of motion are given
by the se ond order di�erential equation (1.3). Sin e T (q; _q) is quadrati in
_q, they are equivalent to the system
_q = v; _v = g(q; v); (1.8)
satisfying g(q;�v) = g(q; v). This implies the time-reversibility of the system
and means that whenever
�
q(t); v(t)
�
is a solution of (1.8), also
�
q(�t);�v(�t)
�
is a solution. For example, in the study of planetary motion, the same dif-
ferential equation permits us to investigate the future and the past, one only
has to hange the sign of the velo ity ve tor v.
More generally, we onsider a di�erential equation _y = f(y) and a linear
invertible transformation �. We all the di�erential equation �-reversible if
(� Æ f)(y) = �(f Æ �)(y): (1.9)
For the previous situation we have y = (q; v), �(q; v) = (q;�v), and the ve tor
�eld f(y) =
�
v; g(q; v)
�
indeed satis�es (1.9) whenever g(q;�v) = g(q; v).
This is illustrated in the left pi ture of Fig. 1.2 at the hand of the perturbed
pendulum equation _q = v; _v = � sin q � v
2
=5, whi h is still �-reversible with
respe t to �(q; v) = (q;�v), but whi h is no longer Hamiltonian.
The ow of a �-reversible di�erential equation has a remarkable property:
� it is �-reversible, i.e., it satis�es (see the right pi ture of Fig. 1.2)
(� Æ '
t
)(y) = ('
�1
t
Æ �)(y) for all t and all y. (1.10)
The proof of this statement is straightforward. One he ks by di�erentiation
that (�Æ'
t
)(y) and ('
�1
t
�)(y) = ('
�t
�)(y) are both solutions of the same
di�erential equation _z = �f(z), and are identi al for t = 0. Formula (1.10)
−2 −1 0 1 2
−1
1
� �
f(y)
�f(y)
f(�y)
−2 −1 0 1 2
−1
1
� �
'
t
'
�1
t
Fig. 1.2. The �-reversibility of the ve tor �eld f(q; v) = (v;� sin q � v
2
=5)
T
(left
pi ture), and the �-reversibility of the orresponding ow.
GniCodes { Matlab Programs for Geometri Numeri al Integration 5
thus follows from the uniqueness of the solution of an initial value problem.
Analogous to the situation of Hamiltonian problems, this property is hara -
teristi for �-reversible di�erential equations. This means that whenever the
ow of a di�erential equation _y = f(y) satis�es (1.10), then (1.9) holds. It is
thus natural to look for numeri al methods that share this property.
Example 1.2 (Kepler Problem). The relative motion of two bodies whi h at-
tra t ea h other is des ribed by the di�erential equation
_q
1
= v
1
; _q
2
= v
2
; _v
1
= �
q
1
(q
2
1
+ q
2
2
)
3=2
; _v
2
= �
q
2
(q
2
1
+ q
2
2
)
3=2
: (1.11)
Sin e it an be onsidered as a lassi al me hani al system, it is �-reversible
for �(q
1
; q
2
; v
1
; v
2
) = (q
1
; q
2
;�v
1
;�v
2
). However, there are more symmetries
in this problem, and it is seen to be �-reversible also for �(q
1
; q
2
; v
1
; v
2
) =
(q
1
;�q
2
;�v
1
; v
2
).
Example 1.3 (Se ond Order Di�erential Equations). Many problems of pra -
ti al appli ations lead to �q = g(q), or equivalently,
_q = v; _v = g(q): (1.12)
For example, all lassi al me hani al systems for whi h M(q) =M is a on-
stant matrix are of this form. The di�erential equation (1.12) is �-reversible
for �(q; v) = (q;�v) independent of the form of g(q). It is Hamiltonian only
if g(q) = �r
q
U(q) for some potential fun tion U(q).
1.3 Hamiltonian and Reversible Systems on Manifolds
It is often diÆ ult to �nd suitable minimal oordinates for des ribing the
motion of me hani al systems. Moreover, minimal oordinates are in general
de�ned only lo ally and frequent hanges of harts may be ne essary. To avoid
this diÆ ulty we onsider oordinates q 2 R
d
that are subje t to onstraints
g(q) = 0. Expressing the Euler-Lagrange equations and their Hamiltonian
formulation in terms of these oordinates, we are led to a system of the form
_p = �r
q
H(p; q)�r
q
g(q)�
_q = r
p
H(p; q); 0 = g(q);
(1.13)
where the additional term with the Lagrange multiplier � for es the solution
to satisfy g(q) = 0. Here, p and q are ve tors inR
d
, g(q) = (g
1
(q); : : : ; g
m
(q))
T
is the ve tor of onstraints, and r
q
g =
�
r
q
g
1
; : : : ;r
q
g
m
�
is the transposed
Ja obian matrix of g(q).
Di�erentiating the onstraint 0 = g
�
q(t)
�
with respe t to time yields
0 = r
q
g(q)
T
r
p
H(p; q) (1.14)
6 Ernst Hairer and Martin Hairer
(the so- alled hidden onstraint) whi h is an invariant of the ow of (1.13).
A se ond di�erentiation gives the relation
0 =
�
�q
�
r
q
g(q)
T
r
p
H(p; q)
�
r
p
H(p; q)
�r
q
g(q)
T
r
2
p
H(p; q)
�
r
q
H(p; q) +r
q
g(q)�
�
;
(1.15)
whi h allows us to express � in terms of (p; q), if the matrix
r
q
g(q)
T
r
2
p
H(p; q)r
q
g(q) is invertible (1.16)
(r
2
p
H denotes the Hessian matrix of H). Inserting the so-obtained fun tion
�(p; q) into (1.13) gives a di�erential equation for (p; q) on the manifold
M =
�
(p; q) j g(q) = 0; r
q
g(q)
T
r
p
H(p; q) = 0
: (1.17)
This interpretation allow us to dedu e the existen e and uniqueness of the
solution from the standard theory for ordinary di�erential equations, provided
that the initial values satisfy (p
0
; q
0
) 2M.
Important properties of the system (1.13) that should be onserved by a
dis retization are the following:
� for (p
0
; q
0
) 2 M the solution stays on the manifold M for all t; hen e,
the ow is a mapping '
t
:M!M.
� the ow '
t
is a symple ti transformation on M whi h means that
('
0
t
(y)�)
T
J '
0
t
(y)� = �
T
J � for �; � 2 T
y
M; (1.18)
here, the produ t '
0
t
(y)� has to be interpreted as the dire tional deriva-
tive.
� for Hamiltonians satisfying H(�p; q) = H(p; q) the ow '
t
is �-reversible
for �(p; q) = (�p; q) in the sense that (1.10) holds for y = (p; q) 2 M.
Example 1.4 (Two-Body Problem on the Sphere). We are interested in the
motion of two bodies whi h attra t ea h other, but whi h are restri ted to
stay on a sphere. Using Cartesian oordinates q
1
; q
2
2 R
3
for the positions of
the two bodies and p
1
; p
2
2 R
3
for their velo ities, the Hamiltonian be omes
(after a suitable normalization)
H(p
1
; p
2
; q
1
; q
2
) =
1
2
�
p
T
1
p
1
+ p
T
2
p
2
�
+ U(q
1
; q
2
); (1.19)
and the onstraint equations g(q
1
; q
2
) = 0 with g : R
6
! R
2
are given by
q
T
1
q
1
� 1 = 0; q
T
2
q
2
� 1 = 0: (1.20)
GniCodes { Matlab Programs for Geometri Numeri al Integration 7
Fig. 1.3. A solution of the two-body problem on the sphere; initial values are
indi ated by larger symbols; the geodesi onne tion between the two bodies is
plotted at every se ond time step in the right pi ture.
A ording to Kozlov and Harin [18℄, we hoose U(q
1
; q
2
) = � os#= sin# as
potential, where # is the distan e between the two bodies along a geodesi s.
We have os# = q
T
1
q
2
, so that the equations of motion be ome
_q
1
= p
1
; _p
1
= f(q
T
1
q
2
)q
2
� �
1
q
1
;
_q
2
= p
2
; _p
2
= f(q
T
1
q
2
)q
1
� �
2
q
2
;
(1.21)
together with (1.20), where
f( ) =
1
(1�
2
)
3=2
:
The initial values have to lie on the manifold
M =
�
(p
1
; p
2
; q
1
; q
2
) ; q
T
1
q
1
= 1; q
T
2
q
2
= 1; q
T
1
p
1
= 0; q
T
2
p
2
= 0
;
and the solution stays on M for all t.
A parti ular solution is plotted in Fig. 1.3. We have hosen
q
i
=
�
os�
i
sin �
i
; sin�
i
sin �
i
; os �
i
�
T
with (�
1
; �
1
) = (0:8; 0:6) and (�
2
; �
2
) = (0:5; 1:5) as initial values for the
positions, and
p
i
=
�
�
_
�
i
sin�
i
sin �
i
+
_
�
i
os�
i
os �
i
;
_
�
i
os�
i
sin �
i
+
_
�
i
sin�
i
os �
i
;�
_
�
i
sin �
i
)
with (
_
�
1
;
_
�
1
) = (1:1;�0:2) and (
_
�
2
;
_
�
2
) = (�0:8; 0:0) as initial values for the
velo ities. The two bodies are indi ated by small squares in di�erent olors.
The right pi ture of Fig. 1.3 also shows the geodesi onne tion between the
two bodies.
8 Ernst Hairer and Martin Hairer
Example 1.5 (Rigid Body Simulation). The motion of a rigid body with a
�xed point hosen at the origin an be des ribed by an orthogonal matrix
Q(t). Denoting by I
1
; I
2
; I
3
the moments of inertia of the body, its kineti
energy is
T =
1
2
�
I
1
2
1
+ I
2
2
2
+ I
3
2
3
�
;
where the angular velo ity = (
1
;
2
;
3
)
T
of the body is de�ned by
b
=
0
�
0 �
3
2
3
0 �
1
�
2
1
0
1
A
= Q
T
_
Q;
(see [2, Chap. 6℄). In terms ofQ, the kineti energy on the manifold fQ jQ
T
Q =
Ig be omes
T =
1
2
tra e (
b
D
b
T
) =
1
2
tra e (Q
T
_
QD
_
Q
T
Q) =
1
2
tra e (
_
QD
_
Q
T
);
where D = diag (d
1
; d
2
; d
3
) is given by the relations I
1
= d
2
+d
3
, I
2
= d
3
+d
1
,
and I
3
= d
1
+ d
2
. With P = �T=�
_
Q =
_
QD, we are thus on erned with
H(P;Q) =
1
2
tra e (PD
�1
P
T
) + U(Q);
and the onstrained Hamiltonian system be omes
_
P = �r
Q
U(Q)�Q�;
_
Q = PD
�1
; 0 = Q
T
Q� I;
(1.22)
where � is a symmetri matrix onsisting of Lagrange multipliers. This is of
the form (1.13) and satis�es the regularity ondition (1.16).
2 Symple ti and Symmetri Integrators
A numeri al integrator is a family �
h
(y) of maps on the phase spa e that
approximates the exa t ow '
h
(y) of the di�erential equation. It is the aim
of `geometri integration' to onstru t and to study methods for whi h the
numeri al solution, given by y
n+1
= �
h
(y
n
), preserves the stru ture of the
problem. We are mainly interested in methods for whi h �
h
is symple ti or
�-reversible, when it is applied to a Hamiltonian or �-reversible di�erential
equation, respe tively.
2.1 Simple Symple ti Methods
The simplest numeri al methods for general di�erential equations _y = f(y)
are the expli it Euler method
y
n+1
= y
n
+ hf(y
n
) (2.1)
GniCodes { Matlab Programs for Geometri Numeri al Integration 9
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
expli it Euler impli it Euler
symple ti Euler (2.3) symple ti Euler (2.4)
Fig. 2.1. Numeri al ow with step size h = �=3 for the four `Euler methods' of
Se t. 2.1. The exa t ow is in luded as a white shadow.
and the impli it Euler method
y
n+1
= y
n
+ hf(y
n+1
): (2.2)
Here, h is the step size, and y
n
is an approximation to the solution y(t) at
time t = nh. For Hamiltonian systems (1.1) we onsider the method
p
n+1
= p
n
� hr
q
H(p
n+1
; q
n
); q
n+1
= q
n
+ hr
p
H(p
n+1
; q
n
); (2.3)
whi h treats the p-variable by the impli it Euler method and the q-variable
by the expli it Euler method. Similarly, we also onsider
p
n+1
= p
n
� hr
q
H(p
n
; q
n+1
); q
n+1
= q
n
+ hr
p
H(p
n
; q
n+1
): (2.4)
Both methods are alled symple ti Euler method.
Example 2.1. We apply all four methods to the pendulum problem whi h is
Hamiltonian with H(p; q) given by (1.5), and we onsider initial values in the
set A of Fig. 1.1. The numeri al solution obtained with the large step size
h = �=3 is illustrated in Fig. 2.1. Neither the expli it nor the impli it Euler
methods are area-preserving (i.e., symple ti ). We shall see in the following
theorem that both `symple ti Euler methods' are area-preserving (hen e
the name symple ti ). Due to the large step size, however, the numeri al
solution di�ers signi� antly from the exa t solution whi h is in luded as a
white shadow in the pi tures ( ompare with Fig. 1.1).
10 Ernst Hairer and Martin Hairer
Theorem 2.1. For the numeri al s hemes (2.3) and (2.4) the mapping
�
h
:
�
p
n
q
n
�
7!
�
p
n+1
q
n+1
�
is a symple ti transformation.
The proof of this theorem is straightforward (de Vogelaere [42℄ and [12,
p. 176℄). One omputes the Ja obian of �
h
by impli it di�erentiation, and
one he ks the identity (1.7).
2.2 Simple Reversible Methods
We next onsider �-reversible di�erential equations (i.e., � Æ f = �f Æ �) as
dis ussed in Se t. 1.2.
Theorem 2.2. If a numeri al method �
h
satis�es
� Æ �
h
= �
�h
Æ � and �
h
= �
�1
�h
; (2.5)
then it is �-reversible, i.e., � Æ �
h
= �
�1
h
Æ �.
This statement is obvious. The interest of this theorem lies in the fa t that
the se ond ondition of (2.5) is independent of �, whereas the �rst ondition of
(2.5) is easy to he k and satis�ed by all `reasonable' methods. For example,
the expli it Euler dis retization (2.1) yields
(� Æ �
h
)(y
n
) = �y
n+1
= �y
n
+ h�f(y
n
) = �y
n
� hf(�y
n
) = (�
�h
Æ �)(y
n
);
and a similar simple omputation shows that the impli it Euler method and
all (expli it and impli it) Runge-Kutta methods satisfy the �rst ondition of
(2.5). For partitioned Runge-Kutta methods, su h as the symple ti Euler
s heme, this is true for transformations � whi h are of the form �(q; v) =
(�
1
(q); �
2
(v)).
If �
h
(y) represents a numeri al method of order at least one, i.e., �
h
(y) =
y + hf(y) +O(h
2
), then also �
�1
�h
(y) = y + hf(y) +O(h
2
) and
�
�
h
:= �
�1
�h
(2.6)
is a numeri al method of order at least one. It is alled the adjoint method of
�
h
. Whenever an integrator satis�es
�
�
h
= �
h
; (2.7)
it is alled a symmetri method. The se ond ondition in (2.5) of Theorem 2.2
is thus equivalent to the symmetry of the method �
h
.
Ex hanging h$ �h and y
n
$ y
n+1
in (2.1) shows that the adjoint of the
expli it Euler method is the impli it Euler method and vi e versa. Similarly,
GniCodes { Matlab Programs for Geometri Numeri al Integration 11
the adjoint of the symple ti Euler method (2.3) is the method (2.4). None
of these methods is symmetri .
Using the notion of the adjoint method it is easy to onstru t symmet-
ri methods: let
h
be an arbitrary method of order at least one, then the
ompositions
h=2
Æ
�
h=2
and
�
h=2
Æ
h=2
(2.8)
are symmetri methods of order at least two. The symmetry follows from the
properties (�
h
Æ
h
)
�
=
�
h
Æ �
�
h
and (�
�
h
)
�
= �
h
, and order at least two is a
onsequen e of the fa t that symmetri method always have an even order.
For example, if we let
h
be the expli it Euler method, then the methods
of (2.8) are
y
n+1
= y
n
+ h f
�
y
n
+ y
n+1
2
�
; (2.9)
the impli it midpoint rule, and
y
n+1
= y
n
+
h
2
�
f(y
n
) + f(y
n+1
)
�
; (2.10)
the trapezoidal rule, respe tively.
2.3 St�ormer/Verlet S heme
We next onsider Hamiltonian systems (1.1) and the symple ti Euler method
(2.3) in the role of
h
. The ompositions (2.8) then yield
q
n+1=2
= q
n
+
h
2
r
p
H(p
n
; q
n+1=2
)
p
n+1
= p
n
�
h
2
�
r
q
H(p
n
; q
n+1=2
) +r
q
H(p
n+1
; q
n+1=2
)
�
q
n+1
= q
n+1=2
+
h
2
r
p
H(p
n+1
; q
n+1=2
)
(2.11)
and
p
n+1=2
= p
n
�
h
2
r
q
H(p
n+1=2
; q
n
)
q
n+1
= q
n
+
h
2
�
r
p
H(p
n+1=2
; q
n
) +r
p
H(p
n+1=2
; q
n+1
)
�
p
n+1
= p
n+1=2
�
h
2
r
q
H(p
n+1=2
; q
n+1
)
(2.12)
respe tively. For the important spe ial ase H(p; q) =
1
2
p
2
+ U(q), method
(2.12) redu es to (after elimination of the p-variable)
q
n+1
� 2q
n
+ q
n�1
= �h
2
r
q
U(q
n
): (2.13)
This dis retization of �q = �r
q
U(q) is attributed to Newton ( f. [13℄), De-
lambre ( f. [25℄), En ke, St�ormer [36℄, and Verlet [41℄. The methods (2.11)
and (2.12) are nowadays often alled St�ormer/Verlet s heme.
Let us olle t the most important properties of the St�ormer/Verlet s heme:
12 Ernst Hairer and Martin Hairer
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
St�ormer/Verlet (2.11) St�ormer/Verlet (2.12)
Fig. 2.2. Numeri al ow with step size h = �=3 for the two versions of the
St�ormer/Verlet method. The exa t ow is in luded as a white shadow.
� the method is of order two,
� it is a symple ti method,
� it is a symmetri method,
� for separable Hamiltonians T (p) + U(q) the method is expli it,
� the method exa tly onserves quadrati �rst integrals p
T
Cq, e.g., the
angular momentum in N -body problems.
The �rst four statements are immediate onsequen es of the above dis us-
sions. A proof of the last property is given in [12, p. 98℄.
In Fig. 2.2 we repeat the experiment of Example 2.1, but this time with
the two versions of the St�ormer/Verlet method. We learly observe the higher
a ura y ( ompared to the �rst order methods) and the area-preservation.
The St�ormer/Verlet s heme is an ex ellent geometri integrator and it
is widely used, in parti ular in mole ular dynami s where a orre t qual-
itative simulation is of utmost importan e. For long-time omputations in
astronomy, where a very high a ura y is demanded, the order two of the
St�ormer/Verlet s heme is too low.
2.4 Splitting Methods
A di�erent approa h for onstru ting simple geometri integrators is based
on the idea of splitting the ve tor �eld as
_y = f
[1℄
(y) + f
[2℄
(y): (2.14)
If by han e the exa t ows '
[1℄
t
and '
[2℄
t
of the systems _y = f
[1℄
(y) and
_y = f
[2℄
(y) an be al ulated exa tly, we an onsider
�
h
= '
[1℄
h
Æ '
[2℄
h
(2.15)
as simple numeri al integrator. It follows from Taylor expansion that this
method is of order one. Even more important is the symmetri (se ond order)
omposition
�
h
= '
[1℄
h=2
Æ '
[2℄
h
Æ '
[1℄
h=2
(2.16)
GniCodes { Matlab Programs for Geometri Numeri al Integration 13
whi h is usually alled Strang splitting . These splitting methods have the
following obvious properties:
� if both, f
[1℄
(y) and f
[2℄
(y), are Hamiltonian ve tor �elds, then the om-
positions (2.15) and (2.16) are symple ti integrators;
� if both, f
[1℄
(y) and f
[2℄
(y), are �-reversible, then the symmetri method
(2.16) is �-reversible.
For some situations the splitting (2.14) is obvious. For example, if a Hamil-
tonian system has H(p; q) = T (p) + U(q) as Hamiltonian, then the ows
orresponding to H
[1℄
(p; q) = T (p) and H
[2℄
(p; q) = U(q) are given expli itly
by
'
[1℄
t
(p; q) =
�
p; q + tr
p
T (p)
�
; '
[2℄
t
(p; q) =
�
p� tr
q
U(q); q
�
:
The resulting splitting methods (2.15) and (2.16) are then equivalent to the
symple ti Euler method (2.3) and to the St�ormer/Verlet s heme (2.12), re-
spe tively. In general, however, it is an art to �nd a suitable splitting ( f. [25℄).
2.5 High Order Geometri Integrators
We start this se tion with a numeri al experiment that motivates the sear h
for high order symple ti and symmetri numeri al integrators. We onsider
the Kepler problem whi h is Hamiltonian with
H(p
1
; p
2
; q
1
; q
2
) =
1
2
(p
2
1
+ p
2
2
)�
1
p
q
2
1
+ q
2
2
; (2.17)
and we take as initial values
q
1
(0) = 1� e; q
2
(0) = 0; p
1
(0) = 0; p
2
(0) =
p
(1 + e)(1� e)
�1
;
su h that the solution is an ellipse with e entri ity e = 0:6. Figure 2.3 shows
the work pre ision diagrams (global error at the endpoint after 200 revolu-
tions against the required number of fun tion evaluations and the omputer
time, respe tively) for the se ond order St�ormer/Verlet s heme as well as for
various methods of order eight. It learly demonstrates that for high a u-
ra y requirements (say 10 digits) the low order method annot ompete with
the high order ones. It would need about 1000 times more pu time. The
irregularities at the right bottom orner of the pi tures are due to round-o�.
Impli it Runge-Kutta Methods In the end of the 19th and the beginning
of the 20th entury Runge [30℄ and Kutta [19℄ introdu ed generalizations of
the expli it Euler method with the aim of getting more a urate numeri al
approximations. These expli it methods an neither be symple ti nor sym-
metri as follows from the hara terizations given below. Mu h more impor-
tant for our purpose are impli it Runge-Kutta methods, introdu ed mainly
14 Ernst Hairer and Martin Hairer
105 10610−12
10−9
10−6
10−3
100
10−1 10010−12
10−9
10−6
10−3
100
e
r
r
o
r
f n. eval.
omp
irk2
lmm2
verlet
e
r
r
o
r
pu time
irk2
omp
lmm2
verlet
Fig. 2.3. Work pre ision diagrams for the St�ormer/Verlet s heme and for three
methods of order eight; impli it Runge-Kutta method (irk2), omposition method
( omp), and linear multistep method (lmm2).
in the work of But her [5℄. For Hamiltonian systems or for general partitioned
di�erential equations
_q = f(q; v); _v = g(q; v) (2.18)
we onsider so- alled partitioned Runge-Kutta methods, whi h treat the om-
ponents of q and those of v by possibly di�erent impli it Runge-Kutta meth-
ods. They are de�ned by
k
i
= f
�
q
n
+ h
s
X
j=1
a
ij
k
j
; v
n
+ h
s
X
j=1
ba
ij
`
j
�
;
`
i
= g
�
q
n
+ h
s
X
j=1
a
ij
k
j
; v
n
+ h
s
X
j=1
ba
ij
`
j
�
;
q
n+1
= q
n
+ h
s
X
i=1
b
i
k
i
; v
n+1
= v
n
+ h
s
X
i=1
b
b
i
`
i
:
(2.19)
The equations for k
i
; `
i
(i = 1; : : : ; s) are nonlinear and have to be solved by
�xed-point iteration, provided that the step size h is suÆ iently small.
It turns out that the method (2.19) is symple ti for general Hamiltonian
systems, if the following relations are satis�ed:
b
i
ba
ij
+
b
b
j
a
ji
= b
i
b
b
j
for i; j = 1; : : : ; s;
b
i
=
b
b
i
for i = 1; : : : ; s:
(2.20)
It is symmetri , if
a
s+1�i;s+1�j
+ a
ij
= b
j
for all i; j,
ba
s+1�i;s+1�j
+ ba
ij
=
b
b
j
for all i; j.
(2.21)
GniCodes { Matlab Programs for Geometri Numeri al Integration 15
If the method does not ontain super uous stages and if the stages are suit-
ably ordered, the onditions (2.20) and (2.21) are also ne essary for symple -
ti ity and symmetry, respe tively. These hara terizations have been obtained
originally by Lasagni [22℄, Sanz-Serna [32℄ and Suris [37℄ for symple ti ity,
and by Stetter [35℄ and Wanner [43℄ for symmetry. They are dis ussed in
detail in Chapters V and VI of [12℄.
For the important spe ial ase �q = g(q), i.e., f(q; v) = v in (2.18) and
g(q; v) independent of v, the variables k
i
an be eliminated expli itly and the
method (2.19) redu es to
`
i
= g
�
q
n
+ h
i
v
n
+ h
2
s
X
j=1
ea
ij
`
j
�
; i = 1; : : : s;
q
n+1
= q
n
+ hv
n
+ h
2
s
X
i=1
e
b
i
`
i
; v
n+1
= v
n
+ h
s
X
i=1
b
b
i
`
i
:
(2.22)
where
i
=
P
s
j=1
a
ij
, and
e
b
i
, ea
ij
are the oeÆ ients of
e
b
T
= b
T
b
A and
e
A = A
b
A.
Example 2.2 (Method Used as `irk2' in Fig. 2.3). The most important sym-
ple ti impli it Runge-Kutta methods are the so- alled Gauss methods. They
are built on the Gaussian quadrature (b
i
;
i
)
s
i=1
, whi h is interpolatory and
for whi h
1
; : : : ;
s
are the zeros of the sth shifted Legendre polynomial
d
s
dx
s
�
x
s
(x� 1)
s
�
:
The oeÆ ients a
ij
are omputed from the linear system
s
X
j=1
a
ij
k�1
j
=
k
i
k
for i; k = 1; : : : ; s.
We let
b
b
i
= b
i
and ba
ij
= a
ij
in (2.19), so that all omponents of the di�erential
equation are treated by the same method.
The method obtained in this way has originally been introdu ed by But her
[6℄, and it has many ni e properties. It is of order 2s (whi h is maximal among
all s-stage Runge-Kutta methods), it is symple ti and symmetri , so that
it is extremely well suited in the ontext of geometri integration. The only
disadvantage is that even for simple situations su h as �q = g(q), it gives an
impli it dis retization. In the experiment of Fig. 2.3 we use this method with
s = 4. The `2' in `irk2' indi ates that the ode is for se ond order di�erential
equations �q = g(q) only, and that it is implemented as (2.22).
Partitioned Multistep Methods Another extension of the Euler methods
are linear multistep methods, originally introdu ed by Adams in 1855 and
published in Bashforth [3℄. Neither expli it nor impli it lassi al multistep
16 Ernst Hairer and Martin Hairer
methods have been su essful in geometri integration. Lambert and Watson
[21℄ onsidered spe ial lasses for se ond order di�erential equations �q =
g(q), whi h have been revived by Quinlan and Tremaine [27℄ for the long-
time integration of planetary orbits. For partitioned di�erential equations
(2.18), whi h are more general than _q = v; _v = g(q), these methods an be
interpreted as partitioned linear multistep methods, de�ned by
k
X
j=0
�
j
q
n+j
= h
k
X
j=0
�
j
f(q
n+j
; v
n+j
);
b
k
X
j=0
b�
j
v
n+j
= h
b
k
X
j=0
b
�
j
g(q
n+j
; v
n+j
):
(2.23)
It is not evident to dis uss symple ti ity and symmetry of multistep methods,
be ause we are on erned with an algorithm (y
n
; : : : ; y
n+k�1
) 7! y
n+k
and
not with a one-step method y
n+1
= �
h
(y
n
) whi h is a transformation on
the phase spa e. However, Kir hgraber [17℄ showed that to every onsistent
stri tly stable multistep method one an asso iate a so- alled underlying one-
step method �
h
whi h has the same long-time dynami s. More pre isely, it
satis�es the following properties:
� for every y
0
, the sequen e de�ned by y
n+1
= �
h
(y
n
) is a solution of the
multistep method;
� for an arbitrary starting approximation y
0
; : : : ; y
k�1
, the numeri al ap-
proximation of the multistep method tends exponentially fast to a par-
ti ular solution obtained by the underlying one-step method.
The existen e of an underlying one-step method (as a formal series in powers
of h) satisfying the �rst of these properties, an be shown for general on-
sistent methods (2.23); see [12, Chap.XIV℄. The se ond property annot be
ful�lled by methods that are not stri tly stable. Assuming that for arbitrary
starting approximations the multistep solution remains lose to that obtained
by the underlying one-step method, it is natural to all a method (2.23) sym-
ple ti and symmetri , if the underlying one-step method is symple ti and
symmetri , respe tively.
Unfortunately, it turns out that partitioned multistep methods annot
be symple ti (Tang [39℄). However, they an be symmetri . In terms of the
oeÆ ients of the method (2.23), the symmetry of the underlying one-step
method is equivalent to (assuming irredu ibility of the methods)
�
j
= ��
k�j
; �
j
= �
k�j
for j = 0; : : : ; k,
b�
j
= �b�
b
k�j
;
b
�
j
=
b
�
b
k�j
for j = 0; : : : ;
b
k.
(2.24)
For stable symmetri multistep methods the zeros of the generating poly-
nomials �(�) =
P
k
j=0
�
j
�
j
and b�(�) =
P
b
k
j=0
b�
j
�
j
have to lie on the unit
GniCodes { Matlab Programs for Geometri Numeri al Integration 17
ir le. Su h methods annot be stri tly stable, and for this reason symmetri
multistep methods have been disregarded for a long time.
Also for this lass of methods we are mainly interested in the numeri al
solution of se ond order di�erential equations �q = g(q). Elimination of the
v-variables in (2.23) yields the formula
K
X
j=0
A
j
q
n+j
= h
2
K
X
j=0
B
j
g(q
n+j
); (2.25)
where the generating polynomials R(�) =
P
k
j=0
A
j
�
j
and S(�) =
P
k
j=0
B
j
�
j
are obtained from those of (2.23) by
R(�) = �(�) � b�(�); S(�) = �(�) � b�(�):
Here, �(�); b�(�); �(�); b�(�) are the generating polynomials of �
j
; b�
j
; �
j
;
b
�
j
,
respe tively. We re all that method (2.25) is of order p, if
R(e
h
)� h
2
S(e
h
) = O(h
p+2
) for h! 0: (2.26)
Formula (2.25) does not involve derivative approximations v
n
. If they are
needed, they an be obtained by �nite di�eren es from the position approxi-
mations q
n
.
Example 2.3 (Method Used as `lmm2' in Fig. 2.3). We put K = 8 and we let
R(�) = (� � 1)(�
7
� 1) = (� � 1)
2
(�
6
+ �
5
+ �
4
+ �
3
+ �
2
+ � + 1);
so that all zeros lie on the unite ir le and, apart from � = 1, all zeros are
simple. To get a method of order p = 8, the polynomial S(�) has to satisfy
S(�) = R(�)= log
2
� +O
�
(� � 1)
p
�
( f. ondition (2.26)). Expanding the right-hand expression into a Taylor
series at � = 1 and trun ating to get a polynomial of degree 7, we obtain the
generating polynomial
S(�) =
13207
8640
�
�
7
+ �) �
8934
8640
�
�
6
+ �
2
) +
42873
8640
�
�
5
+ �
3
)�
33812
8640
�
4
:
The resulting method (2.25) is of order 8 for problems �q = g(q), symmet-
ri , and expli it (be ause B
K
= 0). An approximation to the derivative is
obtained by symmetri di�eren es as
_y
n
=
1
840h
�
672 (y
n+1
� y
n�1
)� 168 (y
n+2
� y
n�2
)
+ 32 (y
n+3
� y
n�3
)� 3 (y
n+4
� y
n�4
)
�
:
18 Ernst Hairer and Martin Hairer
Composition Methods We onsider the omposition of a given basi one-
step method �
h
(y) with di�erent step sizes:
h
= �
s
h
Æ : : : Æ �
2
h
Æ �
1
h
: (2.27)
The aim is to in rease the order (and hen e the a ura y) while preserving
desirable properties (symple ti ity, symmetry) of the basi method. This idea
has mainly been developed in the papers of Suzuki [38℄, Yoshida [44℄, and
M La hlan [24℄. For a re ent omprehensive survey see [25℄ and Chapters II,
III, and V of [12℄.
The reason of the su ess of omposition methods within geometri inte-
grators are the following properties:
� if �
h
is symple ti , then the omposition method
h
is symple ti ;
� if �
h
is symmetri and if the step size parameters
i
satisfy
i
=
s+1�i
,
then the omposition
h
is symmetri .
The main problem onsists in �nding parameters
i
su h that the omposition
h
is of a given order. Suzuki [38℄ and Yoshida [44℄ propose general simple
pro edures that allow one to onstru t omposition methods of arbitrarily
high order. For orders higher than four they are, however, not very eÆ ient.
One is therefore obliged to investigate and to solve the set of order onditions
for the
i
whi h guarantee that the method
h
of (2.27) has a ertain order.
Example 2.4 (Method Used as ` omp' in Fig. 2.3). From the many published
examples of omposition methods, let us present the oeÆ ients of a method
of order 8 with s = 17 steps:
1
=
17
= 0:13020248308889008087881763
2
=
16
= 0:56116298177510838456196441
3
=
15
= �0:38947496264484728640807860
4
=
14
= 0:15884190655515560089621075
5
=
13
= �0:39590389413323757733623154
6
=
12
= 0:18453964097831570709183254
7
=
11
= 0:25837438768632204729397911
8
=
10
= 0:29501172360931029887096624
9
= �0:60550853383003451169892108
0 1
This set of oeÆ ients is due to Kahan and Li [15℄. The little pi ture to
the right illustrates the 17 steps ne essary for obtaining order 8. The zig-zag
behaviour is typi al for omposition methods. It is impossible to get high
order without negative step sizes.
For the omputations of Fig. 2.3 we use the St�ormer/Verlet s heme (2.12)
as basi integrator. The resulting omposition method is symple ti and (due
to
i
=
18�i
) symmetri .
GniCodes { Matlab Programs for Geometri Numeri al Integration 19
2.6 Rattle for Constrained Hamiltonian Systems
Let us explain here, how the St�ormer/Verlet method (2.12) an be generalized
to solve onstrained Hamiltonian systems of the form (1.13). Without taking
mu h are of velo ity approximations Ry kaert, Ci otti and Berendsen [31℄
show how onstraints g(q) = 0 an be in luded in the formulation (2.13).
Anderson [1℄ reformulates their method and in ludes a velo ity approxima-
tion that satis�es the hidden onstraint (1.14). The resulting algorithm, still
for separable Hamiltonians, is alled `Rattle'. Later, Jay [16℄ and Rei h [28℄
observed that the Rattle algorithm an be extended to general Hamiltonians.
Re all that the exa t ow of a onstrained Hamiltonian system lies on
the manifold M, de�ned in (1.17). Assume therefore that an approximation
(p
n
; q
n
) 2M is given. One step of the algorithm is de�ned as
p
n+1=2
= p
n
�
h
2
�
r
q
H(p
n+1=2
; q
n
) +r
q
g(q
n
)�
n
�
q
n+1
= q
n
+
h
2
�
r
p
H(p
n+1=2
; q
n
) +r
p
H(p
n+1=2
; q
n+1
)
�
0 = g(q
n+1
)
p
n+1
= p
n+1=2
�
h
2
�
r
q
H(p
n+1=2
; q
n+1
) +r
q
g(q
n+1
)�
n
�
0 = r
q
g(q
n+1
)
T
r
p
H(p
n+1
; q
n+1
):
(2.28)
For �xed �
n
, the �rst two equations de�ne uniquely p
n+1=2
and q
n+1
, if h is
suÆ iently small. The parameter �
n
has to be hosen to satisfy g(q
n+1
) = 0.
This is possible if the matrix (1.16) is invertible. In the last two equations,
�
n
has to be hosen to satisfy the onstraint for p
n+1
.
Similar to the St�ormer/Verlet method for un onstrained Hamiltonian sys-
tems, this algorithm has many ni e properties that are useful within geometri
integration:
� the numeri al solution stays on the manifold M; i.e., the method (2.28)
de�nes a numeri al ow �
h
:M!M;
� the numeri al ow �
h
:M!M is a symple ti transformation on M;
� the method is symmetri ;
� the method is onvergent of order two.
The symple ti ity of the numeri al ow has �rst been shown by Leimkuhler
and Skeel [23℄. The other properties are easy onsequen es of the de�nition
of the method. This integrator is an ideal andidate as basi method for
ompositions of the form (2.27). For elaborate proofs and for extensions to
higher orders we refer to Se t. VII.1 of [12℄.
3 Theoreti al Foundation of Geometri Integrators
Intuitively, it is quite obvious that a symple ti method should be preferred
for the integration of Hamiltonian systems. Similarly, symmetri (more pre-
isely, �-reversible) integrators should be preferred for �-reversible di�erential
20 Ernst Hairer and Martin Hairer
equations. This is motivated by the fa t that the symple ti ity of the ow is
hara teristi for Hamiltonian systems, and the �-reversibility of the ow is
hara teristi for �-reversible di�erential equations.
In this se tion we give some more pre ise statements on the long-time be-
haviour of geometri integrators. In parti ular, we dis uss the idea of ba kward
error analysis whi h is the key for a deeper understanding of most numeri-
al phenomena. This idea was ommon to many numeri al analysts already
before a systemati study started with the work of Feng [8℄, Sanz-Serna [33℄,
Yoshida [45℄, Hairer [9℄ and many others.
3.1 Ba kward Error Analysis
Consider an ordinary di�erential equation
_y = f(y) (3.1)
and a numeri al method y
n+1
= �
h
(y
n
). The idea of ba kward error analysis
onsists in sear hing and studying a modi�ed di�erential equation
_y = f(y) + hf
2
(y) + h
2
f
3
(y) + : : : ; (3.2)
su h that the exa t time-h ow e'
h
(y) of (3.2) is equal to the numeri al ow
�
h
(y). Already simple examples (e.g., trapezoidal rule applied to a quadrature
problem _y = f(t)) show that the series in (3.2) annot be expe ted to onverge
in general. The pre ise statement is the following:
Theorem 3.1. Consider the di�erential equation (3.1) with an in�nitely dif-
ferentiable ve tor �eld f(y). Assume that the numeri al ow admits a Taylor
series expansion of the form
�
h
(y) = y + hf(y) + h
2
d
2
(y) + h
3
d
3
(y) + : : : : (3.3)
Then, there exist unique ve tor �elds f
j
(y) su h that for any N � 1
�
h
(y) = e'
h;N
(y) +O(h
N+1
);
where e'
t;N
is the exa t ow of the trun ated modi�ed equation
_y = f(y) + hf
2
(y) + : : :+ h
N�1
f
N
(y)
(noti e that the ow e'
t;N
also depends on h, be ause h is a parameter in the
modi�ed di�erential equation).
Let us outline a onstru tive proof . Without taking are of onvergen e
we expand the exa t ow of (3.2) into a Taylor series
e'
h
(y) = y + h ey
0
(0) +
h
2
2!
ey
00
(0) +
h
3
3!
ey
000
(0) + : : :
= y + h
�
f(y) + hf
2
(y) + h
2
f
3
(y) + : : :
�
+
h
2
2!
�
f
0
(y) + hf
0
2
(y) + : : :
��
f(y) + hf
2
(y) + : : :
�
+ : : :
(3.4)
GniCodes { Matlab Programs for Geometri Numeri al Integration 21
(where the prime denotes derivation with respe t to time) and ompare like
powers of h in the expressions (3.4) and (3.3). This yields re urren e relations
for the fun tions f
j
(y), namely,
f
2
(y) = d
2
(y)�
1
2!
f
0
f(y) (3.5)
f
3
(y) = d
3
(y)�
1
3!
�
f
00
(f; f)(y) + f
0
f
0
f(y)
�
�
1
2!
�
f
0
f
2
(y) + f
0
2
f(y)
�
:
Example 3.1. We onsider the pendulum equation _q = p; _p = � sin q and
apply the expli it Euler dis retization (2.1). We have d
j
(y) = 0 for all j � 2,
so that (3.5) yields for the modi�ed equation
�
_q
_p
�
=
�
p
� sin q
�
+
h
2
�
sin q
p os q
�
+
h
2
12
�
�4 p osq
(p
2
+ 4 os q) sin q
�
+ : : : : (3.6)
For the impli it Euler method (2.2) we get (3.6) with h repla ed by �h. A
similar omputation yields for the symple ti Euler method (2.3) the modi�ed
di�erential equation
�
_q
_p
�
=
�
p
� sin q
�
+
h
2
�
� sin q
p os q
�
+
h
2
12
�
2 p os q
(p
2
� 2 os q) sin q
�
+ : : : ; (3.7)
whereas the same equation with h repla ed by �h is obtained for the method
(2.4). The four pi tures of Fig. 3.1 show the exa t ow of the modi�ed dif-
ferential equations (trun ated after the O(h
2
) term) orresponding to these
four Euler methods together with the numeri al solution for the initial value
(p
0
; q
0
) = (�1:2; 0:7). We observe a surprisingly good agreement. This �gure
should be ompared to the exa t ow of the unperturbed system ( f. Fig. 1.1).
The O(h) perturbation in (3.6) provokes the origin to be ome a sour e for
the expli it Euler method, and a sink for the impli it Euler method. For the
two symple ti dis retizations we observe that the solutions of the modi�ed
equation are periodi , and that the numeri al approximation lies near a losed
urve. It has thus the orre t qualitative behaviour. This is explained by the
fa t that the di�erential equation (3.7) is Hamiltonian with
e
H(p; q) =
1
2
p
2
� os q �
h
2
p sin q +
h
2
12
(p
2
� os q) os q + : : : ;
so that the exa t solutions stay on the level urves of
e
H(p; q).
3.2 Properties of the Modi�ed Equation
The previous example demonstrates that the numeri al solution is extremely
lose to the exa t solution of the modi�ed di�erential equation. To study
properties of the numeri al solution, it is therefore justi�ed to investigate
instead the orresponding properties of the modi�ed di�erential equation,
and this is often mu h simpler. Let us olle t some properties valid for general
di�erential equations:
22 Ernst Hairer and Martin Hairer
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
−2 0 2 4
−2
2
expli it Euler impli it Euler
symple ti Euler (2.3) symple ti Euler (2.4)
Fig. 3.1. Numeri al solution with step size h = 0:4 for the four `Euler methods' of
Se t. 2.1 ompared to the exa t ow of their modi�ed di�erential equations trun-
ated after the O(h
2
) term.
� if the method is of order r, i.e., �
h
(y)� '
h
(y) = O(h
r+1
), then we have
f
j
(y) = 0 for j = 2; : : : ; r ;
� if h
r+1
Æ
r+1
(y) is the leading term of the lo al trun ation error, i.e.,
�
h
(y)�'
h
(y) = h
r+1
Æ
p+1
(y)+O(h
r+2
), then we have f
r+1
(y) = Æ
r+1
(y);
� if �
h
(y) has the modi�ed equation (3.2), then the adjoint method has
f
�
j
(y) = (�1)
j+1
f
j
(y) as oeÆ ient fun tions of the modi�ed equation;
� for symmetri methods the modi�ed equation is an expansion in even
powers of h; i.e., f
2k
(y) = 0 for all k.
We now turn our attention to Hamiltonian systems and to �-reversible dif-
ferential equations.
Theorem 3.2 (Lo al Modi�ed Hamiltonian). Consider a Hamiltonian
system (1.1) with smooth Hamiltonian H : D ! R (D � R
2d
) and apply
a symple ti numeri al method �
h
(y). Then, the ve tor �elds f
k
(y) of the
modi�ed di�erential equation are lo ally Hamiltonian, i.e., lo ally we have
f
k
(y) = J
�1
rH
k
(y).
The proof is by indu tion on k. Its ideas an be tra ed ba k to Moser
[26℄, and it an be found in Benettin & Giorgilli [4℄, Tang [40℄, Rei h [29℄,
and in Chapter IX of [12℄. Sin e the idea of proof is appli able to many other
situations, we outline it shortly.
GniCodes { Matlab Programs for Geometri Numeri al Integration 23
We assume (by indu tion) that the trun ated modi�ed equation
_y = f(y) + hf
2
(y) + : : :+ h
k�1
f
k
(y) (3.8)
is Hamiltonian. Its ow e'
t;k
(y) satis�es
�
h
(y) = e'
h;k
(y) + h
k+1
f
k+1
(y) +O(h
k+2
):
Sin e �
h
and e'
h;k
are symple ti transformations,
J = �
0
h
(y)
T
J�
0
h
(y) = J + h
k+1
�
f
0
k+1
(y)
T
J + Jf
0
k+1
(y)
�
+O(h
k+2
)
holds. Consequently, the matrix Jf
0
k+1
(y) is symmetri and the existen e of
H
k+1
(y) satisfying f
k+1
(y) = J
�1
rH
k+1
(y) follows from the integrability
lemma. ut
If H and �
h
are both de�ned and smooth on the whole of R
2d
or on a
simply onne ted domain D, the fun tions H
k
of the modi�ed Hamiltonian
are globally de�ned. However, as shown by the following example, the fun -
tions H
k
are in general not globally de�ned, and the above theorem annot
be used for the study of the long-time behaviour of numeri al solutions.
Example 3.2. For the harmoni os illator _p = �q; _q = p, onsider the dis-
retization
p
n+1
= p
n
� hq
n
� h
2
p
n+1
; q
n+1
= q
n
+ hp
n+1
� h
2
q
n
(3.9)
where = 0:25=(p
2
n+1
+q
2
n
). It is aO(h
2
) perturbation of the symple ti Euler
method and therefore it is a method of order 1. Its symple ti ity follows from
the fa t that it an be written as
p
n+1
= p
n
� hr
q
S(p
n+1
; q
n
); q
n+1
= q
n
+ hr
p
S(p
n+1
; q
n
)
with S(p; q) =
1
2
(p
2
+q
2
)�
h
4
arg(q+ip). Its numeri al approximation, plotted
in the right pi ture of Fig. 3.2, is disappointing and does not show the orre t
qualitative behaviour. This is due to the fa t that S(p; q), and hen e H
2
(p; q),
are not globally de�ned.
Theorem 3.3 (Global Modi�ed Hamiltonian). Consider a Hamiltonian
system (1.1) with smooth Hamiltonian H : D ! R (D � R
2d
) and apply the
symple ti method
p
n+1
= p
n
� hr
q
S(p
n+1
; q
n
); q
n+1
= q
n
+ hr
p
S(p
n+1
; q
n
)
with generating fun tion
S(p; q) = S
1
(p; q) + hS
2
(p; q) + h
2
S
3
(p; q) + : : : ;
where all S
k
(p; q) are globally de�ned on D. Then, the ve tor �elds f
k
(y)
of the modi�ed di�erential equation are globally Hamiltonian, i.e., we have
f
k
(y) = J
�1
rH
k
(y) with smooth H
k
: D ! R.
24 Ernst Hairer and Martin Hairer
expli it Euler impli it Euler symple ti Eulersympl. meth. (3.9)
Fig. 3.2. Numeri al solution of di�erent �rst order methods applied to the harmoni
os illator with step size h = 0:15.
The proof of this theorem is based on the Hamilton-Ja obi di�erential
equation ( f. Se t. IX.3.2 of [12℄). Let us mention that all previous meth-
ods (symple ti Euler, St�ormer/Verlet, symple ti partitioned Runge-Kutta
methods, omposition methods) satisfy the assumption of Theorem 3.3.
Theorem 3.4 (�-Reversible Modi�ed Ve tor Field). Consider a �-re-
versible di�erential equation ( f. Se t. 1.2) and apply a �-reversible numeri al
method �
h
(y). Then, the ve tor �elds f
k
(y) of the modi�ed di�erential equa-
tion are �-reversible, i.e., they satisfy (1.9).
The proof uses the same ideas as that of Theorem 3.2.
3.3 Long-Time Behaviour of Geometri Integrators
Using ba kward error analysis and in parti ular the results of Theorems 3.3
and 3.4, we shall show that symple ti integrators (for Hamiltonian systems)
and �-reversible integrators (for �-reversible di�erential equations) have an
improved long-time behaviour. We study the onservation of the Hamiltonian
and of general �rst integrals, and the error growth for integrable systems.
Conservation of the Hamiltonian We know that the HamiltonianH(p; q)
is onstant along exa t solutions of the Hamiltonian system (energy onser-
vation for me hani al systems). Sin e the lo al error of a rth order integrator
is of size O(h
r+1
), we have H(p
n+1
; q
n+1
)�H(p
n
; q
n
) = O(h
r+1
). Summing
up these errors, we obtain H(p
n
; q
n
) � H(p
0
; q
0
) = O(nh
r+1
) = O(th
r
) for
t = nh, be ause no an ellation of errors an be expe ted for general integra-
tors. For symple ti integrators, however, we have the mu h more favourable
estimate
H(p
n
; q
n
)�H(p
0
; q
0
) = O(h
r
) for nh � T (3.10)
with an extremely large T (in pra ti e it an be onsidered as in�nity), pro-
vided that the numeri al solution stays in a ompa t set. This an be ex-
plained with the help of Theorem 3.3 as follows: the modi�ed di�erential
equation is Hamiltonian with
e
H(p; q) = H(p; q) + h
r
H
r+1
(p; q) + h
r+1
H
r+2
(p; q) + : : : : (3.11)
GniCodes { Matlab Programs for Geometri Numeri al Integration 25
The exa t ow of the modi�ed equation, and hen e also the numeri al solu-
tion, keep the modi�ed Hamiltonian
e
H(p; q) exa tly onstant. If the numeri al
solution stays in a ompa t set, the fun tions H
j
(p; q) are bounded along the
numeri al solution so that (3.10) holds. This argument is not yet rigorous,
be ause the series (3.11) usually does not onverge. If one trun ates the series
suitably, one an rigorously prove (3.10) on exponentially long time intervals,
i.e., for T = O(e
=h
) with some positive ( f. [4℄, [11℄, and Se t. IX.7 of [12℄).
It is natural to study whether also other �rst integrals an be well on-
served by numeri al integrators. Re all that I(y) is a �rst integral of _y =
f(y), if it is onstant along all solutions of the di�erential equations, i.e., if
I
0
(y)f(y) = 0 vanishes identi ally.
Example 3.3. Consider the Kepler problem (1.11). Besides the Hamiltonian
(2.17), it has also the angular momentum
L(p
1
; p
2
; q
1
; q
2
) = q
1
p
2
� q
2
p
1
: (3.12)
and the so- alled Runge-Lenz-Pauli ve tor
A(p; q) =
0
�
p
1
p
2
0
1
A
�
0
�
0
0
q
1
p
2
� q
2
p
1
1
A
�
1
p
q
2
1
+ q
2
2
0
�
q
1
q
2
0
1
A
(3.13)
as �rst integrals. As numeri al s heme we take the St�ormer/Verlet method.
We apply it to the Kepler problem with initial values as in Se t. 2.5, and we
use the step size h = 0:02. Figure 3.3 shows the values of H(p
n
; q
n
)�H(p
0
; q
0
)
and of the �rst two omponents of A(p
n
; q
n
)�A(p
0
; q
0
) along the numeri al
solution. The angular momentum L(p; q) is exa tly preserved by the method
and therefore not visible in the �gure. We see that, in agreement with (3.10),
the error in the Hamiltonian is bounded by O(h
2
) on the whole interval of
integration. The Runge-Lenz-Pauli ve tor (3.13), however, is not preserved.
The lower pi ture of Fig. 3.3, where the errors obtained with step size h = 0:04
are in luded in grey, indi ates that they behave like e(h
2
t) +O(h
2
).
Completely Integrable Systems The example above demonstrated that
it is diÆ ult to predi t the onservation of general �rst integrals by numeri al
methods (even when they are symple ti ). There is, however, an important
spe ial ase for whi h more information an be obtained. We mention some
fa ts and refer the reader to Chapters X and XI of [12℄.
We all a Hamiltonian system (1.1) ompletely integrable, if there exists
a symple ti transformation
(p; q) = (a; �); 2�-periodi in �; (3.14)
su h that the Hamiltonian be omes
H(p; q) = H
�
(a; �)
�
= K(a): (3.15)
26 Ernst Hairer and Martin Hairer
100 200 300 400
−.004
−.002
.000
.002
.004
5000 10000 15000
−1.0−.5.0.5
1.0
H
A
1
A
2
onservation of �rst integrals
A
1
A
2
Fig. 3.3. Kepler problem: the Hamiltonian and the �rst two omponents of
the Runge-Lenz-Pauli ve tor along the numeri al solution of the St�ormer/Verlet
method with step sizes h = 0:02 (bla k) and h = 0:04 (grey).
The new variables (a; �) are alled a tion-angle variables. Suppose we know
expli itly the transformation . Sin e it is symple ti , the Hamiltonian sys-
tem (1.1) be omes in the new variables
_a
i
= 0;
_
� = !
i
(a); i = 1; : : : ; d
with !
i
(a) = �K=�a
i
(a). This system an be readily solved, and gives a
i
(t) =
a
i0
, �
i
(t) = �
i0
+ !
i
(a
0
)t, so that
�
p(t); q(t)
�
=
�
a
0
; �
0
+ !(a
0
)t
�
:
The gives a periodi or quasi-periodi ow on the torus de�ned by a = onst .
Among the problems seen in this survey arti le, Hamiltonian systems with
one degree of freedom (harmoni os illator, pendulum) and the Kepler prob-
lem are ompletely integrable. Under some additional te hni al assumptions
(see the general referen e [12℄), symple ti numeri al integrators applied to
su h ompletely integrable Hamiltonian systems have the following interest-
ing properties:
� the global error grows at most linearly with time, more pre isely, for
t = nh we have
p
n
� p(t) = O(h
r
t); q
n
� q(t) = O(h
r
t);
� �rst integrals that only depend on the a tion variables are well preserved
on exponentially long time intervals; i.e., if I(p; q) is su h that I( (a; �))
is independent of �, then
I(p
n
; q
n
)� I(p
0
; q
0
) = O(h
r
) for nh � T
with T as in (3.10).
GniCodes { Matlab Programs for Geometri Numeri al Integration 27
This result has to be seen in ontrast to general methods, where the global
error in reases typi ally quadrati ally with time, and where the error in �rst
integrals drifts linearly from the orre t value.
We �nally mention that the notion of omplete integrability an be re-
interpreted for �-reversible di�erential equations (not ne essarily Hamilto-
nian), and the same results (linear error growth, onservation of a tion vari-
ables) hold for �-reversible integrators applied to su h systems. Let us illus-
trate this at an interesting example.
Example 3.4 (Toda latti e). Let us onsider parti les on a line intera ting
pairwise with exponential for es, and suppose periodi boundary onditions
q
d+1
= q
1
. The Hamiltonian is given by
H(p; q) =
d
X
k=1
�
1
2
p
2
k
+ exp(q
k
� q
k+1
)
�
:
The orresponding Hamiltonian system has the interesting property that the
d eigenvalues of the matrix
L =
0
B
B
B
B
B
B
�
a
1
b
1
b
d
b
1
a
2
b
2
0
b
2
.
.
.
.
.
.
0
.
.
.
a
d�1
b
d�1
b
d
b
d�1
a
d
1
C
C
C
C
C
C
A
;
a
k
= �
1
2
p
k
b
k
=
1
2
exp
�
1
2
(q
k
� q
k+1
)
�
are �rst integrals. This Hamiltonian system is ompletely integrable within
the lass of Hamiltonian systems and also within the lass of �-reversible sys-
tems, and the a tion variables are related to the eigenvalues of the matrix L.
We onsider the ase d = 3, and we apply an impli it Runge-Kutta
method (Lobatto IIIB, s = 3, order r = 4) whi h is symmetri but not
symple ti . The upper pi ture of Fig. 3.4 shows the Eu lidean norm of the
ve tor of errors in the eigenvalues of L. It is of size O(h
4
) on the whole in-
terval of integration. The lower pi ture shows the norm of the global error,
and we ni ely observed the linear error growth. This on�rms the statement
about integrable systems of this se tion.
4 Matlab Programs of `GniCodes'
We explain a few Matlab programs that implement the most important geo-
metri integrators of the previous se tions. They are olle ted in the Matlab
pa kage GniCodes whi h is available (together with short installing instru -
tions) on the web at the address
http://www.unige. h/math/folks/hairer
28 Ernst Hairer and Martin Hairer
50 100 150 200.0000.0002.0004.0006.0008
50 100 150 200.0
.2
.4
error in the eigenvalues of L
Lobatto IIIB, h = 0:1
global error
Lobatto IIIB, h = 0:1
Fig. 3.4. The Toda latti e: error in the eigenvalues of L (upper pi ture), and global
error of the numeri al solution for an impli it Runge-Kutta method.
Fortran 77 versions of the programs are also available at the same address.
Another Matlab pa kage related to geometri integration is DiffMan of [7℄.
The philosophy of our pa kage, however, is ompletely di�erent and it is
loser to the standard Matlab ODE suite (ode45, ode23, et ) of Shampine
and Rei helt [34℄.
4.1 Standard Call of Integrators
We give an overview of how to use the three lasses of geometri integrators
that are implemented in the pa kage GniCodes for the moment. For the
solution of se ond order initial value problems
�q = g(q); q(0) = q
0
; _q(0) = _q
0
(4.1)
all these methods have the same syntax, and are usually alled as
[T,P,Q℄ = gni_meth('g',tspan,y0,options,...);
where gni meth has to be repla ed by gni irk2 for the impli it Runge-Kutta
method, by gni lmm2 for the linear multistep method, and by gni omp for
the omposition method based on the St�ormer/Verlet s heme. The `2' in irk2
and in lmm2 expresses the fa t that these programs are appli able only to se -
ond order di�erential equations (4.1). For the omposition method gni omp
there is the possibility to de�ne the basi integrator by altering the options
stru ture, so that the method an be used for the solution of any di�erential
equation. The relevant syntax will be explained in Se t. 4.6 below.
The meaning of the arguments in a all of gni meth is as follows:
'g' This argument must be a string ontaining the name of a Matlab �le
des ribing the problem. The syntax for su h a �le is des ribed in Se t. 4.2
below.
GniCodes { Matlab Programs for Geometri Numeri al Integration 29
tspan should ontain the time span over whi h the problem is to be solved.
It has to be given in the form [t0,tf℄.
y0 This is a ve tor ontaining the initial values for q and _q. The initial values
for q are given by the �rst d omponents and those for _q by the remaining
d omponents of y0.
options This argument should ontain a GNI options stru ture reated by
gniset (the syntax of gniset is the same as for the standard odeset
fun tion). This option stru ture ontains additional instru tions for the
integrator.
... After options, an arbitrary number of optional arguments an be given.
These arguments are passed over to the fun tion F.
The list of available options di�ers slightly from the standard ODE suite.
Some of these options are also fun tion-dependent and will be explained in
the sequel. The following options are available for every integrator:
'OutputF n' This is a string ontaining the name of an output fun tion. The
format for the output fun tions is the same as for the standard ODE suite,
in parti ular the standard odeplot output fun tion an be used (and will
be used as the default output fun tion). The ve tor passed to the output
fun tion ontains in its d �rst omponents the value of the solution and
in its d remaining omponents the values for its time-derivatives. If this
parameter is set to phaseplot, the solution is drawn in the phase spa e
orresponding to the �rst omponents of (q; _q).
'OutputSel' As for the standard ODE suite, this ontains a ve tor of indi es
determining whi h omponents of the solution will be passed to the out-
put fun tion. By default, all the indi es (in luding those orresponding
to the time-derivative of the solution) are passed through.
'OutputSteps' tells the integrator whi h steps to take into a ount for the
output. For example, if OutputSteps is equal to 10, only every 10th step
generates some output. Putting OutputSteps equal to 0, output is made
available only at the beginning and the end of the integration interval.
'Ve torized' has the same meaning as for the standard ODE suite.
'Events' If this option is set to 'on', event lo ation is enabled. See Se t. 4.3
for an explanation of how to use event lo ation.
'StepSize' Size h of one integration step. It is slightly altered by the ode,
if the length of the integration interval is not an integer multiple of h.
'NumSteps' is the number of integration steps. This option is only used when
StepSize is not spe i�ed. If neither is spe i�ed, a warning is issued and
the default step size h = 0:01 is used.
'Method' allows to sele t the type of method to use. The list of available
methods depends on the integrator and is listed in the orresponding
se tions below.
Note that (like for ode45 for example) the arguments tspan, y0, and options
are optional and an be de�ned in the �le F.m instead.
30 Ernst Hairer and Martin Hairer
On output, gni meth returns three ve tors [T,Q,P℄, ontaining the times
at whi h the solution was evaluated, as well as the values of q and _q at these
times. If event lo ation is turned on, additional return values are given as
des ribed in Se t. 4.3 below.
4.2 Problem Des ription
The problem to be solved should be des ribed in a .m �le. In the most simple
ase, this �le only returns the right-hand side of the se ond-order di�erential
equation. E.g., in order to solve the equation �q = �q
3
, one may reate a �le
trivial.m ontaining the following:
fun tion out = trivial(t,q)
out = -q^3;
From the ommand line, one would then use it for example as
options = gniset('StepSize', 0.1);
gni_meth('trivial', [0 10℄, [0 2.5℄, options);
An additional parameter flags an be used by the integrator to retrieve
default parameters for the problem. Assume we want to solve the previous
problem between t = 0 and t = 10, using a step size of 0:1 and with initial
values q(0) = 0 and _q(0) = 2:5. We ould then de�ne the �le trivial.m as
fun tion [out,out2,out3℄ = trivial(t,q,flags)
if (nargin < 3) | isempty(flags)
out = -q^3;
else
swit h flags
ase 'init',
out = [0 10℄;
out2 = [0 2.5℄;
out3 = gniset('StepSize', 0.1);
end
end
and all it from the ommand line in the most simple possible way as
gni_meth('trivial');
For a system of di�erential equations, out and q are olumn ve tors. If the
option 'Ve torized' is set to 'on' in the GNI options stru ture, the inte-
grator may request to evaluate the right-hand side of the problem for several
values of t and q in one all. If the problem is of dimension d and the inte-
grator requests m values, t is a line ve tor of size m and q is a d�m matrix.
The right-hand side is expe ted to be also a d�m matrix. If 'Ve torized'
is set to 'off', one an safely assume that m = 1.
GniCodes { Matlab Programs for Geometri Numeri al Integration 31
When ve torized orre tly, the .m �le for the Kepler problem with initial
values as in Se t. 2.5 looks like
fun tion [out,out2,out3℄ = kepler(t,q,flags,e )
if (nargin < 3) | isempty(flags)
rad=q(1,:).*q(1,:)+q(2,:).*q(2,:);
rad=rad.*sqrt(rad);
out(1,:)=-q(1,:)./rad;
out(2,:)=-q(2,:)./rad;
else
swit h flags
ase 'init',
if (e < 0) | (e >= 1)
error('The e entri ity must lie between 0 and 1');
end
out = [0 2*pi℄;
out2 = [1-e ,0,0,sqrt((1+e )/(1-e ))℄;
out3 = gniset('NumSteps',50,'Ve torized','on','Events','off',...
'OutputF n','phaseplot','OutputSel',[1,2℄);
end
end
Noti e that this problem depends on the e entri ity e , whi h has been
appended to the end of the parameter list. To solve this problem with e =
0:6, just type
gni_meth('kepler',[ ℄,[ ℄,[ ℄,0.6);
Entering [ ℄ in the parameter list tells the integrator to use the default
values of the problem de�nition �le instead. The parameter e is again
simply appended at the end of the parameter list.
4.3 Event Lo ation
In many situations (for example the omputation of Poin ar�e se tions), it is
useful to know at whi h times some event fun tion g(t; q(t); p(t)) vanishes.
This is usually referred to as event lo ation.
Event lo ation is implemented in the GNI suite in a way that is again
very similar to the standard ODE suite implementation. It an be enabled
by spe ifying the value 'on' for the 'Events' sele tor of the GNI options
stru ture.
When event lo ation is turned on, the integrator an be alled as
[T,P,Q,TE,PE,QE,IE℄ = gni_meth('g',tspan,y0,options,...);
The output ve tor TE ontains the times at whi h events o urred. The ve -
tors PE and QE ontain the values of the solution and its derivative at these
times. If more than one event fun tion is de�ned, the ve tor IE ontains the
index of the event fun tion that triggered the event.
32 Ernst Hairer and Martin Hairer
When event lo ation is turned on, the problem des ription �le is expe ted
to respond to the flags set to 'events' by returning in the �rst output
argument a ve tor of event fun tions. Furthermore it is supposed to return in
the se ond and third output arguments ve tors telling the integrator whether
the orresponding event is terminal or not and whi h types of zero- rossings
to onsider. When a terminal event is en ountered, the integration stops,
whether the end of the integration interval has been rea hed or not. The
following example shows how to de�ne a problem des ription �le that allows
to retrieve the times at whi h the solution either rosses 1 upwards or 0 in any
dire tion. The integration stops whenever the solution rosses�2 downwards.
fun tion [out,out2,out3℄ = trivial(t,q,flags)
if (nargin < 3) | isempty(flags)
out = -q^3;
else
swit h flags
ase 'init',
out = [0 10℄;
out2 = [-1 5℄;
out3 = gniset('StepSize', 0.1,'Events','on');
ase 'events',
out = [q(1)-1,q(1),q(1)+2℄;
out2 = [0 0 1℄;
out3 = [1 0 -1℄;
end
end
4.4 Program gni irk2
The program gni irk2 uses an impli it Runge-Kutta s heme to solve se ond
order di�erential equations �q = g(q). The following sele tors of the GNI
options stru ture are spe i� to gni irk2:
'Method' This sele tor allows to spe ify whi h s heme is to be used. The
a epted values for 'Method' are 'G4', 'G8', and 'G12'. The letter `G'
refers to the fa t that all of these methods are Gauss methods ( f. Example
2.2), and the number that follows indi ates the order of the orresponding
method. If no method is spe i�ed, 'G12' is used.
'MaxIter' Sin e the s hemes are impli it, a non-linear system of equation
has to be solved at every integration step. This is a hieved through a
�xed point iteration. This sele tor allows to spe ify the maximal number
of iterations that are performed. The default value is 50.
The oeÆ ients of the di�erent methods are ontained in the �le oeff irk2.
New methods an easily be in orporated.
Let us shortly explain the meaning of the required oeÆ ients. The arrays
C, B, BC orrespond to the ve tors with oeÆ ients
i
,
b
b
i
,
e
b
i
of (2.22), the 2-
dimensional array AA to the matrix
e
A. Further oeÆ ients are needed for an
GniCodes { Matlab Programs for Geometri Numeri al Integration 33
eÆ ient solution of the nonlinear Runge-Kutta equations of (2.22), whi h are
equivalent to
Q
i
= q
n
+ h
i
v
n
+ h
2
s
X
j=1
ea
ij
g(Q
j
); i = 1; : : : ; s: (4.2)
We solve this system by �xed point iteration and we use
Q
0
i
= q
n
+ h
i
v
n
+ h
2
s+3
X
j=1
e
ij
g(Q
j;n�1
) (4.3)
as starting guess, where Q
1;n�1
; : : : ; Q
s;n�1
are the internal stage values of
the previously omputed step, Q
s+1;n�1
= q
n�1
, Q
s+2;n�1
= q
n
, and
Q
s+3;n�1
= q
n
+
s
X
i=1
�
i
�
Q
i;n�1
� q
n�1
�
+ h�
s+1
v
n�1
+ h�
s+2
v
n
is an approximation to the solution at t = t
n�1
+ �h. The oeÆ ients �,
�
i
, e
ij
(stored in the arrays SM, AM, and E) are determined su h that (4.3)
oin ides as far as possible with the Taylor series of the solution of (4.2). We
refer to [12, Se t. VIII.6.1℄ and [20℄ for more details.
4.5 Program gni lmm2
Linear multistep methods (2.25) for se ond order di�erential equations are
implemented in the ode gni lmm2. Sin e these methods are not self-starting,
we have to provide starting approximations. This is done by a all to gni irk2
with 'Method' set to 'G12'. For the moment we have implemented the three
methods of Table 1, and the 'Method' options are '801', '802', and '803',
respe tively.
Table 1. Symmetri multistep methods for se ond order problems.
method 801 method 802 method 803
i C
i�1
12096B
i
C
i�1
120960B
i
C
i�1
8640B
i
1 1 17671 1 192481 1 13207
2 0 �23622 2 6582 1 �8934
3 1 61449 3 816783 1 42873
4 1 �50516 3:5 �156812 1 �33812
The oeÆ ients of the methods are stored in the separate �le oeff lmm2
as follows. The generating polynomial R(�) has � = 1 as a double zero, and
therefore it an be written as
R(�) = (� � 1)
2
(C
0
+ C
1
� + C
2
�
2
+ : : :+ C
k�2
�
k�2
):
34 Ernst Hairer and Martin Hairer
Sin e for expli it symmetri methods these oeÆ ients satisfy B
K�i
= B
i
(with B
0
= 0) and C
K�2�i
= C
i
, only those given in Table 1 have to be
spe i�ed. The oeÆ ients C
i
and B
i
uniquely determine the method (2.25).
4.6 Program gni omp
This program allows to easily implement general omposition methods. A
omposition method (2.27) is hara terized by the set of oeÆ ients f
i
g and
by the basi method �
h
. They are ontrolled by the following two options:
'Method' This option allows to hoose between several prede�ned sets of
oeÆ ients
i
. The available methods are '21', '43', '45', '67', '69',
'815', '817', and '1033'. These methods are of order 2, 4, 6, 8, and 10
respe tively.
'PartialFlow' This option allows the user to spe ify the name of a Matlab
�le de�ning the basi method �
h
. The default method is the St�ormer/Verlet
method.
For reasons of eÆ ien y we assume the basi method to be of the form
�
h
= !
h=2
Æ �
h=2;h;h=2
Æ �
h=2
; (4.4)
where the following simpli� ation formula holds:
�
�;�;ha
Æ �
ha
Æ !
h
Æ �
h ;�;�
= �
�;�;ha+h
Æ �
ha+h ;�;�
: (4.5)
Every one-step method an be written in the form (4.4) by hoosing !
h=2
=
�
h=2
= id (the identity), and �
h ;hb;ha
= �
hb
. But this is not the reason for
writing �
h
in this apparently ompli ated form. The advantage of the repre-
sentation (4.4) is that in many important situations a large part of the work
for evaluating �
h
an be put into �
h=2
and !
h=2
and, by the simpli� ation
formula (4.5), this part an be avoided unless at grid points where an output
of the solution is required.
The ode for the basi method must have the following stru ture:
fun tion [outP,outQ℄ = basi (t,P,Q,ode,ha,hb,h ,first,last,flags,args)
if isempty(flags)
if (first)
apply �
ha
to the ve tors P and Q
end
apply �
h ;hb;ha
if (last)
apply !
h
end
else
swit h flags
ase 'init',
perform some initialization
ase 'done',
GniCodes { Matlab Programs for Geometri Numeri al Integration 35
perform some leanup
end
end
For example, the St�ormer/Verlet method (2.12) for �q = g(q), onsidered as a
splitting method (2.16), an be written as (4.4) with !
h=2
= id, �
h ;hb;ha
=
'
[1℄
h
Æ'
[2℄
hb
, and �
ha
= '
[1℄
ha
. This presentation satis�es ondition (4.5), be ause
'
[1℄
t
has the group property. The Matlab program for this basi method is
fun tion [outP,outQ℄ = stverl(t,P,Q,ode,ha,hb,h ,first,...
last,flags,varargin)
if isempty(flags)
if (first)
Q = Q + ha*P;
end
F = feval(ode,t,Q,varargin{:});
outP = P + hb*F;
outQ = Q + h *outP;
end
The a tual implementation uses ompensated summation (to redu e round-
o� error) and it is the default method used by gni omp.
5 Some Typi al Appli ations
Let us �nally illustrate the use of our programs at some typi al examples,
where geometri integrators are re ommended. We start with a omparison
of geometri integrators for se ond order ordinary di�erential equations. We
then show how Poin ar�e se tions an be omputed, and we terminate with a
slightly more sophisti ated use of omposition methods.
5.1 Comparison of Geometri Integrators
Often it is diÆ ult to de ide whi h integrator is the best for a given problem.
The impli it Runge-Kutta and omposition methods have a sound theoreti al
basis, but they typi ally need more fun tion evaluations per step than linear
multistep methods. On the other hand, multistep methods have larger lo al
error, so that smaller step sizes are required. The best hoi e is in general
problem dependent.
Consider �rst the Kepler problem with e entri ity e = 0:6. The �le
kepler.m, ontaining the problem des ription, is explained in Se t. 4.2. We
ompute the solution over the interval [0; 400�℄ with many di�erent step sizes.
As we have seen in Fig. 2.3 the eÆ ien y of all three lasses of integrators
(impli it Runge-Kutta, multistep, omposition) is about the same for this
problem. This need not always be the ase.
As another example onsider the 6-body problem (sun and the �ve outer
planets) with data and initial values as in Chap. I of [12℄ on a relatively short
36 Ernst Hairer and Martin Hairer
time interval [0; 500 000℄. Similar as in Fig. 2.3 we show in Fig. 5.1 the work
pre ision diagram of the di�erent methods. It is somewhat surprising that
for this problem (with orbits of very small e entri ity) the linear multistep
method is the most eÆ ient integrator. The pu times in Fig. 2.3 and in
Fig. 5.1 are obtained with Fortran implementations of the odes.
5.2 Computation of Poin ar�e Se tions
Consider the H�enon-Heiles Hamiltonian
H(p
1
; p
2
; q
1
; q
2
) =
1
2
(p
2
1
+ p
2
2
) +
1
2
(q
2
1
+ q
2
2
) + q
2
1
q
2
�
1
3
q
3
2
: (5.1)
The orresponding Hamiltonian system is integrable for suÆ iently small
energy, e.g., for the initial values p
1
(0) = p
2
(0) = q
1
(0) = q
2
(0) = 0:18,
whi h we take for our omputations. This means that the solution stays on
a two-dimensional torus in the four-dimensional phase spa e. Its interse tion
with the hyperplane q
1
= 0 (Poin ar�e se tion) thus gives a losed urve in the
phase spa e. We study the proje tion of this urve onto the (q
2
; p
2
)-plane.
The left pi ture of Fig. 5.2 shows the Poin ar�e se tion for the numeri al
solution obtained by dop853 (an expli it Runge-Kutta method of order eight
with step size ontrol, see [14, Appendix℄) and toleran e Atol = Rtol = 10
�5
on the interval [0; 100 000℄. The pi ture learly demonstrates that the nu-
meri al solution is qualitatively wrong as it does not remain on a losed
urve. The same experiment with the three geometri integrators gni lmm2,
gni irk2, and gni omp gives a orre t simulation of the system, and it an-
not be distinguished from a pi ture for the exa t solution (right pi ture of
Fig. 5.2). If we use step sizes su h that the error of the Hamiltonian remains
104 105
10−9
10−6
10−3
100
10−1 100
10−9
10−6
10−3
100
e
r
r
o
r
f n. eval.
omp
irk2
lmm2
verlet
e
r
r
o
r
pu time
omp
irk2
lmm2
verlet
Fig. 5.1. Work pre ision diagrams for the St�ormer/Verlet s heme and for three
methods of order eight; impli it Runge-Kutta method (irk2), omposition method
( omp), and linear multistep method (lmm2), applied to the outer solar system.
GniCodes { Matlab Programs for Geometri Numeri al Integration 37
below 10
�5
, the ode gni lmm2 (with h = 0:22) requires 454 716 fun tion
evaluations, gni irk2 (with h = 1:5) needs 3 731 867 fun tion evaluations,
and gni omp (with h = 1:2) 1 416 661 fun tion evaluations. For omparison,
the ode dop853 requires 1 216 680 evaluations of the ve tor �eld, but the
error in the Hamiltonian in reases linearly with time. The high number of
fun tion evaluations for gni irk2 is due to the fa t that for low a ura y
requirements (large step size) the onvergen e of the �xed point iterations
for solving the nonlinear Runge-Kutta equations is rather slow.
For the omputation of the Poin ar�e se tion (Fig. 5.2) we have used the
following program:
fun tion [out,out2,out3℄ = henon(t,q,flags)
if (nargin < 3) | isempty(flags)
out(1,:)=-q(1,:).*(1+2*q(2,:));
out(2,:)=-q(2,:).*(1-q(2,:)) - q(1,:).^2;
else
swit h flags
ase 'init',
out = [0 100000℄;
out2 = [0.18 0.18 0.18 0.18℄;
out3 = gniset('StepSize',0.22,'Ve torized','on',...
'Events','on','OutputSteps',0);
ase 'events',
out = [q(1)℄;
out2 = [0℄;
out3 = [0℄;
end
end
dop853 gni lmm2
Fig. 5.2. Poin ar�e se tion for the H�enon-Heiles problem: dop853 with toleran e
Atol = Rtol = 10
�5
(left pi ture) and gni lmm2 with step size h = 0:22 (right
pi ture); integration interval [0; 100 000℄.
38 Ernst Hairer and Martin Hairer
The plot of the Poin ar�e se tion is then obtained with
[T,Q,P,TE,QE,PE℄=gni_lmm2('henon');
plot(QE(:,2),PE(:,2),'.');
5.3 `Rattle' as Basi Integrator for Composition
As a �nal example, we present a Matlab implementation of the Rattle algo-
rithm (2.28) applied to the two-body problem on the sphere as introdu ed in
Example 1.4. We follow the des ription of Se t. 4.6 and we do it in su h a way
that it an be used as basi integrator for the omposition method gni omp.
A possible implementation is the following program:
fun tion [outP,outQ℄ = rattwo(t,P,Q,gradpot,ha,hb,h ,first,last,...
flags,varargin)
if isempty(flags)
F = feval(gradpot,t,Q,varargin{:});
EP = P - ha*F;
EQ = Q + hb*EP;
EE1 = EQ(1:3)'*EQ(1:3);
EQ1 = EQ(1:3)'*Q(1:3);
EE2 = EQ(4:6)'*EQ(4:6);
EQ2 = EQ(4:6)'*Q(4:6);
BET1 = 1 - EE1;
ALAM1 = -BET1/(hb*(EQ1+sqrt(BET1+EQ1^2)));
BET2 = 1 - EE2;
ALAM2 = -BET2/(hb*(EQ2+sqrt(BET2+EQ2^2)));
outP = EP - [ALAM1*Q(1:3);ALAM2*Q(4:6)℄;
outQ = Q + hb*outP;
if (last)
F = feval(gradpot,t,outQ,varargin{:});
outP = outP - h *F;
AMU1 = sum(outP(1:3).*outQ(1:3));
AMU2 = sum(outP(4:6).*outQ(4:6));
outP = outP - [AMU1*outQ(1:3);AMU2*outQ(4:6)℄;
end
end
We remark that, due to the simple stru ture of the Hamiltonian, the method
is expli it in p
n+1=2
, q
n+1
and p
n+1
, and it is impli it only in the Lagrange
multipliers. Sin e the onstraints are quadrati , we are only on erned with
the solution of a s alar quadrati equation for ea h of the omponents of �
n
.
This is why no iterations are involved in the above program. Sin e r
q
H(p; q)
does not depend on p, the �rst equation of (2.28) an be ombined with the
fourth equation of the pre eeding step into one formula to give p
n+1=2
=
p
n�1=2
+ : : : . This is the reason for putting the omputation of p
n+1
into
!
h=2
of the de omposition (4.4).
GniCodes { Matlab Programs for Geometri Numeri al Integration 39
500 1000 1500
−.000008
−.000004
.000000
.000004
Fig. 5.3. Error in the Hamiltonian of an 8th order omposition method with Rattle
as basi integrator; step size h = 0:15
The argument gradpot in the fun tion rattwo is a fun tion that omputes
the gradient of the potential (i.e., r
q
U(q) = r
q
H(p; q)). For the two-body
problem on the sphere it is given by
fun tion [out,out2,out3℄ = twobodysphere(t,q,flags)
if (nargin < 3) | isempty(flags)
prod = q(1:3)'*q(4:6);
out = -q([4:6,1:3℄)/(1-prod^2)^(3/2);
else
swit h flags
ase 'init',
out = [0 10℄;
phi = [1.3 -2.1℄;
theta = [2.1 -1.1℄;
out2([1 4℄) = os(phi).*sin(theta);
out2([2 5℄) = sin(phi).*sin(theta);
out2([3 6℄) = os(theta);
dphi = [1.2 0.1℄;
dtheta = [0.1 -0.5℄;
out2([7 10℄) = -dphi.*sin(phi).*sin(theta) ...
+ dtheta.* os(phi).* os(theta);
out2([8 11℄) = dphi.* os(phi).*sin(theta) ...
+ dtheta.*sin(phi).* os(theta);
out2([9 12℄) = -dtheta.*sin(theta);
out3 = gniset('StepSize',0.02,'Ve torized','off',...
'Events','off','PartialFlow','rattwo','OutputF n',...
'sphereplot','OutputSteps',5,'Method','817');
end
end
Here, the option sphereplot permits us to get a 3-dimensional plot of the
solution. The problem is then simply solved by alling
gni_ omp('twobodysphere');
The experiment of Fig. 5.3 on�rms the statement of Theorem 3.3 for the
Rattle algorithm applied to onstrained Hamiltonian systems. We have plot-
40 Ernst Hairer and Martin Hairer
ted the error of the Hamiltonian for the omposition method of Example 2.4,
applied with step size h = 0:15 to the interval [0; 2000℄. As expe ted for a
symple ti integrator, there is no drift in the error of the Hamiltonian. This
is also on�rmed by integrations over mu h longer time intervals.
Hints for the implementation of the rigid body problem of Example 1.5 an
be found in [10℄, where also the extension of Theorem 3.3 to numeri al meth-
ods for Hamiltonian systems on manifolds (in luding the Rattle algorithm)
is proved.
A knowledgement
E.H. is grateful to the organizers of the 2002 Durham summer s hool, in
parti ular to James F. Blowey, to Alan W. Craig, and to the lo al expert
Sebastian Rei h, for providing a sympathi al and stimulating athmosphere.
We also wish to thank Christian Lubi h, Gerhard Wanner and numerous
other olleagues for their useful dis ussions on the topi of this work.
Referen es
1. H.C. Andersen (1983), Rattle: a \velo ity" version of the Shake algorithm for
mole ular dynami s al ulations, J. Comput. Phys. 52:24{34.
2. V.I. Arnold (1989), Mathemati al Methods of Classi al Me hani s, Se ond Edi-
tion, Springer-Verlag.
3. F. Bashforth (1883), An attempt to test the theories of apillary a tion by om-
paring the theoreti al and measured forms of drops of uid. With an explanation
of the method of integration employed in onstru ting the tables whi h give the
theoreti al form of su h drops, by J.C.Adams. Cambridge Univ. Press.
4. G. Benettin & A. Giorgilli (1994), On the Hamiltonian interpolation of near
to the identity symple ti mappings with appli ation to symple ti integration
algorithms, J. Statist. Phys. 74:1117{1143.
5. J.C. But her (1963), CoeÆ ients for the study of Runge-Kutta integration pro-
esses, J. Austral. Math. So . 3:185{201.
6. J.C. But her (1964), Impli it Rung-Kutta pro esses, Math. Comput. 18:50-64.
7. K. Eng�, A. Marthinsen and H.Z. Munthe-Kaas (2001), Di�Man: an obje t-
oriented MATLAB toolbox for solving di�erential equations on manifolds. Spe-
ial issue: Themes in geometri integration. Appl. Numer. Math. 39:349{365.
8. K. Feng (1991), Formal power series and numeri al algorithms for dynami al
systems. In Pro eedings of international onferen e on s ienti� omputation,
Hangzhou, China, Eds. Tony Chan & Zhong-Ci Shi, Series on Appl. Math.
1:28{35.
9. E. Hairer (1994), Ba kward analysis of numeri al integrators and symple ti
methods, Annals of Numeri al Mathemati s 1:107{132.
10. E. Hairer (2002), Global modi�ed Hamiltonian for onstrained symple ti in-
tegrators. Numer. Math. to appear.
11. E. Hairer and Ch. Lubi h (1997), The life-span of ba kward error analysis for
numeri al integrators, Numer. Math. 76:441{462.
GniCodes { Matlab Programs for Geometri Numeri al Integration 41
12. E. Hairer, C. Lubi h and G. Wanner (2002), Geometri Numeri al Integration.
Stru ture-Preserving Algorithms for Ordinary Di�erential Equations. Springer
Series in Computational Mathemati s 31, Springer, Berlin.
13. E. Hairer, C. Lubi h and G. Wanner (2003), Geometri numeri al integration
illustrated by the St�ormer/Verlet method. A ta Numeri a to appear.
14. E. Hairer, S.P. N�rsett and G. Wanner (1993), Solving Ordinary Di�erential
Equations I. Nonsti� Problems, 2nd edition. Springer Series in Computational
Mathemati s 8, Springer, Berlin.
15. W. Kahan and R.-C. Li (1997), Composition onstants for raising the orders
of un onventional s hemes for ordinary di�erential equations, Math. Comput.
66:1089{1099.
16. L. Jay (1994), Runge-Kutta type methods for index three di�erential-algebrai
equations with appli ations to Hamiltonian systems, Thesis No. 2658, 1994,
Univ. Gen�eve.
17. U. Kir hgraber (1986), Multi-step methods are essentially one-step methods,
Numer. Math. 48:85{90.
18. V.V. Kozlov and A.O. Harin (1992), Kepler's problem in onstant urvature
spa es, Celestial Me h. Dynam. Astronom. 54:393{399.
19. W. Kutta (1901), Beitrag zur n�aherungsweisen Integration totaler Differential-
glei hungen, Zeits hr. f�ur Math. u. Phys. 46:435{453.
20. M.P. Laburta (1998), Constru tion of starting algorithms for the RK-Gauss
methods, J. Comput. Appl. Math. 90:239{261.
21. J.D. Lambert and I.A. Watson (1976), Symmetri multistep methods for peri-
odi initial value problems, J. Inst. Maths. Appli s. 18:189{202.
22. F.M. Lasagni (1988), Canoni al Runge-Kutta methods, ZAMP 39:952{953.
23. B.J. Leimkuhler and R.D. Skeel (1994), Symple ti numeri al integrators in
onstrained Hamiltonian systems, J. Comput. Phys. 112:117{125.
24. R.I. M La hlan (1995), On the numeri al integration of ordinary di�erential
equations by symmetri omposition methods, SIAM J. S i. Comput. 16:151{
168.
25. R.I. M La hlan and G.R.W. Quispel (2002), Splitting methods. A ta Numeri a
2002:341{434.
26. J. Moser (1968), Le tures on Hamiltonian systems,Mem. Am. Math. So . 81:1{
60.
27. G.D. Quinlan and S. Tremaine (1990), Symmetri multistep methods for the
numeri al integration of planetary orbits, Astron. J. 100:1694{1700.
28. S. Rei h (1994), Momentum onserving symple ti integrators, Phys. D 76:375{
383.
29. S. Rei h (1999), Ba kward error analysis for numeri al integrators, SIAM J.
Numer. Anal. 36:1549{1570.
30. C. Runge (1895), Ueber die numeris he Au �osung von Di�erentialglei hungen,
Math. Ann. 46:167{178.
31. J.-P. Ry kaert, G. Ci otti and H.J.C. Berendsen (1977), Numeri al integration
of the artesian equations of motion of a system with onstraints: mole ular
dynami s of n-alkanes, J. Comput. Phys. 23:327{341.
32. J.M. Sanz-Serna (1988), Runge-Kutta s hemes for Hamiltonian systems, BIT
28:877{883.
33. J.M. Sanz-Serna (1992), Symple ti integrators for Hamiltonian problems: an
overview, A ta Numeri a 1:243{286.
42 Ernst Hairer and Martin Hairer
34. L.F. Shampine and M.W. Rei helt (1997), The MATLAB ODE suite. SIAM J.
S i. Comput. 18:1{22.
35. H.J. Stetter (1973), Analysis of Dis retization Methods for Ordinary Di�eren-
tial Equations, Springer-Verlag, Berlin.
36. C. St�ormer (1907), Sur les traje toires des orpus ules �ele tris�es. Ar h. s i.
Phys. nat. Gen�eve 24:5{18, 113{158, 221{247.
37. Y.B. Suris (1988), On the onservation of the symple ti stru ture in the nu-
meri al solution of Hamiltonian systems (in Russian), In: Numeri al Solution of
Ordinary Di�erential Equations, ed. S.S. Filippov, Keldysh Institute of Applied
Mathemati s, USSR A ademy of S ien es, Mos ow, 1988, 148{160.
38. M. Suzuki (1990), Fra tal de omposition of exponential operators with appli-
ations to many-body theories and Monte Carlo simulations, Phys. Lett. A
146:319{323.
39. Y.-F. Tang (1993), The symple ti ity of multi-step methods, Computers Math.
Appli . 25:83{90.
40. Y.-F. Tang (1994), Formal energy of a symple ti s heme for Hamiltonian sys-
tems and its appli ations (I), Computers Math. Appli . 27:31{39.
41. L. Verlet (1967), Computer \experiments" on lassi al uids. I. Thermodynam-
i al properties of Lennard-Jones mole ules. Phyis al Review 159:98{103.
42. R. de Vogelaere (1956), Methods of integration whi h preserve the onta t
transformation property of the Hamiltonian equations. Report Nr. 4, Dept.
Math. Univ. of Notre Dame, Notre Dame, Ind.
43. G. Wanner (1973), Runge-Kutta-methods with expansion in even powers of h,
Computing 11:81{85.
44. H. Yoshida (1990), Constru tion of higher order symple ti integrators, Phys.
Lett. A 150:262{268.
45. H. Yoshida (1993), Re ent progress in the theory and appli ation of symple ti
integrators, Celestial Me h. Dynam. Astronom. 56:27{43.