Numerical Method
Numerical Method
5.0 INTRODUCTION
One of the most important tasks in a study of dynamical systems is the numerical
calculation of the trajectories. Thus far we have considered the integration method to
be a black box into which we pop the system, initial conditions, method and time
range and out pops a plot of the trajectories. Although this approach is common in
courses on dynamical systems it obscures many of the pitfalls of numerical
integration.
It is not possible at the present state of the art to choose a best algorithm for the
calculation of trajectories. There are several types of numerical algorithm, each with
their own advantages and disadvantages. We shall consider a class of methods known
as discrete variable methods. These methods approximate the continuous time
dynamical system by a discrete time system. This means that we are not really
simulating the continuous system but a discrete system which may have different
dynamical properties. This is an extremely important point.
The discrete variable methods which we consider fall into two main types, Runge-
Kutta methods and Linear Multistep methods. Maple has implementations of both
types of method as well as a number of more sophisticated techniques designed to
overcome some of the pitfalls of numerical solution. The more sophisticated methods
still fall into the discrete variable category.
1
5.1 TYPES OF METHOD
Although the dynamical systems which we are simulating are usually in more than
one dimension we can, without loss, restrict our numercal anlaysis of the methods to
the single non-autonomous differential equation
bg
x f t , x
bg
subject to the initial condition x t 0 x0 . We shall usually refer to the differential
equation together with the initial condition as an initial value problem (IVP). Discrete
variable methods yield a series of approximations
X n x (tn )
t n 1 tn h, n 0,1 N
h2 h3
x(tn 1 ) x (tn ) hx(tn ) x tn ) (
( x tn )
2! 3!
h2 h3 h p ( p)
X n1 X n hX n X n X n Xn
2! 3! p!
2
dpX
where X ( p ) .
dt p
Runge-Kutta Methods
These methods are based on the notion of finding a formula which agrees with the
Taylor series as closely as possible without involving derivatives. For example
consider the possibility of matching the second order Taylor series method
2
h
X n 1 X n hX n Xn
2!
X n 1 X n h (t n , X n , h)
where
(t , x , h) f (t , x ) f (t ah, x bhf (t , x ))
h
(t , x , h) x(t ) (
x t)
2
h
f (t , x ) f t (t , x ) f x (t , x ) f (t , x )
2
3
Comparing the two expressions we see that
1, a 21 , b 21
1
1 , , a b
2
X n 1 X n
h
2
b
f (tn , X n ) f tn h, X n hf (tn , X n ) g
b
X n1 X n hf tn h2 , X n h2 f (tn , X n ) g
Unfortunately the terminology for naming second order Runge-Kutta methods is not
standardised and Maple calls the improved Euler method the Heun formula and the
modified Euler method the improved polygon method.
The procedure above can be extended to give higher order methods such as the
classical 4th order method
h
X n 1 X n ( k1 2 k 2 2 k 3 k 4 )
6
k1 f (t n , X n )
k 2 f (t n 21 h, X n 21 hk1 )
k 3 f (t n 21 h, X n 21 hk 2 )
k 4 f (t n h, X n hk 3 )
4
Linear Multistep Methods
bg
x f t , x
b g bg z f (t , x)dt
x t n 1 x t n
t n1
tn
b g bg zL
M
N
t t
x tn1 x tn
t t
t n1
tn
n
n 1
n 1
fn
t tn
tn1 tn
O
P
f n1 dt
Q
Integrating gives
b g bg h2 bf f g
x t n 1 x tn n n 1
X n 1 X n
h
2
b
f n f n 1 g
k k
j X n j h j f n j ,
j 0 j 0
n 0,1,2,
5
where j and j are constants, k 0 . This formula is called a linear k-step
Adams-Bashforth
These methods are explicit with methods of order k being k-step. Methods of order
from one to three have the formulae
b g
X n 1 X n hf tn , X n
m 3f b
t ,X g fbt ,X g r
h
X n 1 X n n n n 1 n 1
2
m 23 f b
t ,X g 16 f b 5f b
t ,X g gr
h
X n 1 X n n n t n 1 n 1 n2 , X n2
12
The first order method is more normally called the Euler method.
Adams-Moulton
These methods are implicit with methods of order k being k 1 -step. Methods of b g
order from one to three have the formulae
b g
X n 1 X n hf tn 1 , X n 1
m fbt ,X g fb
t ,X gr
h
X n 1 X n n 1 n 1 n n
2
m 5f b
t ,X g 8f b fb
t ,X g t ,X g
r
h
X n 1 X n n 1 n 1 n n n 1 n 1
12
The first order method is called the backward Euler formula and the second order
method is the Trapezoidal method.
6
Gear Methods
These methods are implicit with methods of order k being k-step. Methods of order
from one to three have the formulae
b
X n 1 X n hf tn 1 , X n 1 g
X n 1 1
3 l4 X X q 23h f bt , X g
n n 1 n 1 n 1
b g xbg
x f t , x , t x 0 0
All of these procedures are invoked using the Maple dsolve command with the
numeric option. The syntax for the command is
where deqns defines the system of differential equations and initial values, vars
defines the dependent and independent variables, type=numeric tells Maple to use a
numerical algorithm and options allows a choice of method, stepsize and other
options associated with the method. The default method is a Fehlberg fourth-fifth
order Runge-Kutta method.
7
Worked Example 1 - Van der Pol Equation
As an example consider the solution of the Van der Pol equation written as the first
order system
x y
c h
y x 1 x 2 y
> epsilon:=10:
> ivp := {diff(x(t),t)=y(t),diff(y(t),t)=-x(t)+epsilon*(1-x(t)^2)*y(t),
x(0)=0,y(0)=0.5}:
> fcns := {x(t), y(t)}:
Note that the output from the dsolve command is a procedure. In order to find the
numerical solution we need to evaluate the procedure at the appropriate value of t.
> p1(10);
We can us the Maple odeplot command to graph the solution. This command is in the
plots package so this has to be loaded first.
> with(plots):
8
Now we can plot the solution in various ways. Firstly here is a plot of the solution
components v t over the range [0,20]. numpoints controls the number of plotted points
which needs to be relatively high here to obtain a realistic plot.
9
Classical Methods
Maple contains a number of one-step methods for the numerical solution of initial
value problems. These are referred to as classical methods and are invoked by
including the option method=classical[type] in the call to dsolve. Here type can be
one of
10
Worked Example 2 - The Forward Euler Method
y 2 xy 2 , bg
y 0 1
> ivp:={diff(y(x),x)=-2*x*y(x)^2,y(0)=1}:
In this case Maple can find the exact solution using dsolve
> Exactsoln:=rhs(dsolve(ivp,y(x)));
1
Exactsoln :=
x 21
Now we use Euler's method to obtain the numerical solution. Note that this method
like all the other methods of type classical uses a fixed stepsize which we provide.
> es0:=dsolve(ivp,y(x),type=numeric,
method=classical[foreuler],stepsize=0.001):
> es0(0.4);
[ x.4, y( x ).8623085097414066 ]
> odeplot(es0,[x,y(x)],0..6,labels=[x,y]);
11
Now often we want to investigate how the solution behaves for differing values of the
stepsize h. In order to do this we can define a different version of the function for the
numerical solution which uses the stepsize as one of the input parameters
> es1:=h->dsolve(ivp,y(x),type=numeric,
method=classical[foreuler],stepsize=h):
> es1(0.001)(0.4);
[ x.4, y( x ).8623085097414066 ]
> es1(0.01)(0.4);
[ x.4, y( x ).8644849248890492 ]
> x:=k->k*0.1:
12
Now define a function which finds the approximate solution at a given output point
> EulerSoln:=(x,h)->rhs(es1(h)(x)[2]):
> ExactSoln:=x->1/(1+x^2);
1
ExactSoln := x
1x 2
Construct an array whose elements compare the exact solution to the numerical
solution for three different stepsizes.
> mm:=array(1..8,1..5):
mm[1,1]:=`x(k)`:mm[1,2]:=`Exactsoln`:mm[1,3]:=`h=0.1`:
mm[1,4]:=`h=0.01`:mm[1,5]:=`h=0.001`:
for i from 2 to 8 do
mm[i,1]:=0.1*(i-2):
mm[i,2]:=evalf(ExactSoln(x(i-2)),5):
for j from 3 to 5 do
mm[i,j]:=evalf(EulerSoln(x(i-2),10^(-j+2)),5)
od:
od:
> eval(mm);
13
Another possibility is to compare the errors at each stepsize. Firstly define a function
giving the error
> err:=(x,h)->ExactSoln(x)-EulerSoln(x,h):
> tt:=array(1..8,1..4):
tt[1,1]:=`x(k)`:tt[1,2]:=`h=0.1`:tt[1,3]:=`h=0.01`:tt[1,4]:=`h=0.001`:
for i from 2 to 8 do
tt[i,1]:=0.1*(i-2);
for j from 2 to 4 do
tt[i,j]:=evalf(err(x(i-2),10^(-j+1)),5);
od:
od:
> eval(tt);
y 4 y 4 x, bg
y 0 1
> IVP:={diff(y(x),x)=-4*y(x)+4*x,y(0)=1}:
14
The exact solution is given by
> dsolve(IVP,y(x));
1 5 ( 4 x )
y( x )x e
4 4
> rk2:=h->dsolve(IVP,y(x),type=numeric,method=classical[rk2],stepsize=h):
> x:=k->k*0.5:
> RK2Soln:=(x,h)->rhs(rk2(h)(x)[2]):
> ExactSoln:=x->x-1/4+5/4*exp(-4*x);
1 5 ( 4 x )
ExactSoln := xx e
4 4
> mm:=array(1..10,1..5):
mm[1,1]:=`x(k)`:mm[1,2]:=`Exactsoln`:mm[1,3]:=`h=0.25`:
mm[1,4]:=`h=0.5`:mm[1,5]:=`h=0.75`:
for i from 2 to 10 do
mm[i,1]:=0.5*(i-2):
mm[i,2]:=evalf(ExactSoln(x(i-2)),5):
for j from 3 to 5 do mm[i,j]:=evalf(RK2Soln(x(i-2),0.25*(j-2)),5) od;
od:
> eval(nm);
15
x(k) Exact soln h=0.25 h=0.5 h=0.75
0 1. 1. 1. 1.
.5 .41918 .56250 1.5000 1.5000
1.0 .77290 .82813 2. 2.3125
1.5 1.2531 1.2695 2.5000 9.0625
2.0 1.7504 1.7549 3. 9.5625
2.5 2.2501 2.2512 3.5000 12.016
3.0 2.7500 2.7503 4. 51.578
3.5 3.2500 3.2501 4.5000 52.078
4.0 3.7500 3.7500 5. 64.785
Note that as the stepsize is increased the numerical solution fails to represent the exact
solution accurately. Indeed for a stepsize of 0.75 the numerical solution 'blows up'.
This is due to non-convergence as a result of the numerical method becoming
unstable. We shall consider this phenomenon next.
16
Exercises 1
1. Use the classical numerical methods foreuler, heunform, rk3, rk4 and adambash
to attempt to obtain a numerical solution of the IVPs
(a)
dx
dt
bg
2tx 2 , x 0 1
(b)
dx
dt
b g bg
5x 1 x , x 0 0.5
Use a range of stepsizes in the interval [0,1]. At what approximate value of the
stepsize do the methods become unstable.
2. Use each of the methods above to solve the systems of differential equations
x x xy , x (0) 0.5
(a)
y y xy , y (0) 0.5
x y, x(0) 0
(b)
y x (1 x ) y,
2
y(0) 0.5
x ( y z), x (0) 1
(c) y x 0.2 y , y (0) 1
z 0.2 8z xz, z(0) 1
In each case use odeplot to obtain time series and phase plots.
17
5.3 LOCAL AND GLOBAL ERRORS
l q
The output of a discrete variable method is a set of points t n , X n and the output of
bg
the dynamical system is a continuous trajectory x t . For the numerical results to
provide a good approximation to the trajectory we require that the difference
bg
X N x tN
where is some defined error tolerance, at each solution point. This difference is
called the global error and is the accumulated error over all solution steps.
Unfortunately it is extremely difficult to accomplish this and we have to confine
ourselves to controlling the local error
~
bg
X n x tn
~
at each step where X n is the numerical solution obtained on the assumption that the
numerical solution at the previous solution point is exact.
There are two sources of local error,the roundoff error and the truncation error.
Roundoff Error
The roundoff error is the error which arises from the fact that numerical methods are
implemented on digital computers which only calculate results to a fixed precision
which is dependent on the computer system used. Note that since roundoff errors
depend only on the number and type of arithmetic operations per step and is thus
independent of the integration stepsize h.
Truncation Error
18
An important concept in the analysis of the truncation error is that of consistency.
Basically consistency requires that the discrete variable method becomes an exact
representation of the dynamical system as the stepsize h 0 . Consistency conditions
can be derived for both Linear Multistep and Runge-Kutta methods.
k k
j X n j h j f n j ,
j 0 j 0
n 0,1,2,
k
( ) i i
i 0
k
( ) i i
i 0
Runge-Kutta Methods
The general pth order Runge-Kutta method can be written in the form
19
p
Yn 1 Yn h c r k r
r 1
b g
k1 f t n , X n
kr
Ft ha , X
fG
p
n h brs k s ,
IJ r 2,3, p
H n r
s 1 K
Here we have
bg
1
m
(1) 0, cr 1
r (1)
Worked Example 4
4
1 1 1 1
(a) ( ) 1 c
r 1
r 1
6 3 3 6
( ) 1
thus
4
(1) 0 (1) 1 cr
r 1
20
3 1
(b) ( ) 2 ( )
2 2
( ) 2 1
thus
Worked Example 5
The order of consistency is determined by substituting the exact solution x n into the
formula of the numerical algorithm and expanding the difference between the two
sides of the formual by Taylor series. The result is then normalised by multiplying by
1
the scaling factor .
bg
1 h
1 1
( ) 1 ( )
2 2
( ) 1
thus
21
n
1 L
M b h
gO
P
xn 1 xn f n f n 1
bgN
1 h 2 Q
1L O
x x b x x g
h
M P
hN Q
n 1 n n n 1
2
R
| h
x hx
h2
x x
3
U
||
1|
n n n n
2 6
S V
h| hF h I | 2
|T x 2 G
Hx x hx 2 x J
n n n n
K|W n
h2
xn
12
The order is given by the highest power of h remaining. Hence the method is
consistent of order two.
22
5.4 ZERO STABILITY
This is a problem peculiar to consistent linear k-step methods in which a first order
dynamical system is integrated using a kth order difference equation. This leads to the
possible existence of spurious solutions of the difference equation which can swamp
the desired solution. In order to avoid this occuring we have to restrict the roots of the
bg
first characteristic polynomial to satisfy the root condition.
We say that a linear k-step method satisfies the root condition if the roots of the
characteristic polynomial ( ) all lie within or on the unit circle, those on the unit
circle being simple.
Note that the roots of ( ) may be complex hence the necessity of considering the
unit circle rather than the interval 11
, in the definition.
Theorem Zero-Stability
A a linear k-step method is zero stable if and only if it satisfies the root condition.
Theorem - Convergence
A discrete variable method is convergent if and only if it is both consistent and zero
stable.
Often it is desirable for the roots of ( ) to satisfy the strong root condition.
23
Definition Strong Root Condition
A linear k-step method is said to satisfy the strong root condition if the characteristic
polynomial has a simple root at 1 1 and all the remaining roots lie strictly within
the unit circle.
1 1 principal root
i , i 1, 2 i k spurious roots
i , i 1, 2 i m essential roots
i , i 1, m 1 i k non essential roots
Worked Example 6
X n 1 1
3 l4 X n q 23h f bt , X g
X n 1 n 1 n 1
is convegent.
X k 2 43 X k 1 13 X k
2h
3
b
f t k 2 , X k 2 g
Then
bg
2 43 13 , bg
23 2
24
Checking consistency
bg
1 1 43 13 0, bg bg
1 2 43 23 1
bg
The roots of are given by
bg
2 43 13 0
b b 1g
3 1g
1
3
1, 13
25
Exercises 2
X n 2 1 X n 1 X n
h
f n 2 1 f n1 f n
2
(b) Find the range of values of for which the method is zero stable.
X n4
8
X n3 X n1 X n 6h f n 4 4 f n3 4 f n1 f n
19 19
X n 3
1
18 X n 2 9 X n1 2 X n 6h f n3
11 11
26
5.4 ABSOLUTE STABILITY
So far we have considered the behaviour of numerical methods in the limit as the
stepsize h 0 . However in practice we must deal with finite stepsizes. To illustrate
the problems that might arise consider the mid-point method
X n 1 X n 1 2hf n
X n 2 X n 2hf n 1
thus
bg
2 1, 2 bg
Checking consistency
bg
1 12 1 0, bg bg
1 2 1 1
bg
The roots of are given by 1 hence the method is both consistent and zero-
stable and hence convergent.
x 2tx 2 , bg
x 0 1
> f:=(t,x)->-2*t*x(t)^2:x0:=1:
> IVP:={diff(x(t),t)=f(t,x),x(0)=x0}:
> FCN:={x(t)}:
27
We can find the exact solution
> Exact:=rhs(dsolve(IVP,FCN));
1
Exact :=
2
t 1
The mid-point method requires a starting value which can be obtained from the
classical fourth order Runge-Kutta method
> sv:=h->dsolve(IVP,FCN,type=numeric,method=classical[rk4],stepsize=h):
> SV:=(t,h)->rhs(sv(h)(t)[2]):
> plotmidpt:=proc(N,h)
local l,i;
l:=[];
for i from 1 to N do
l:=[op(l),i*h,midpt(i,h)];
od;
pointplot(l,connect=true);
end:
28
> with(plots):
> p1:=plotmidpt(50,0.1):
> p2:=plot(Exact,t=0..5):
> display({p1,p2});
Notice that the numerical solution becomes increasingly innacurate, oscillating about
the exact solution, as t increases. This behaviour arises because the behaviour of the
numerical solution does not mimic that of the exact solution. In this case the problem
arises because of a spurious solution of the difference equation corresponding to the
bg
root 1 of . However the problem can also arise in one-step methods which
have no spurious solutions.
k k
iYni h i f ni
i 0 i 0
y y
29
k k
iYni h iYni
i 0 i 0
Thus
k k
iYni h iYni 0
i 0 i 0
Let Yn n , then
k k
i i h i i 0
i 0 i 0
Hence
( ; h ) ( ) h ( ) 0
( ; h ) is called the stability polynomial of the method. Now one of the roots
1 (h ) will correspond to the true solution, the other roots will lead to spurious
solutions whose magnitude will have to be controlled to obtain stability.
A numerical method is said to be absolutely stable for a given h if all the roots of
( ; h ) lie within the unit circle.
30
Worked Example 7
( ; h ) (1 h )
Thus
1 h 1
R A is shown below
RA
-2 0
F
G h I F h I
H 2J
( ; h ) 1
K G
1 J
H 2K
Thus
31
h
1
2 1
h
1
2
RA
bg
( ; h ) 1 0 h 0 h
32
Worked Example 8
Find and sketch the absolute stability region for the second order Runge-Kutta method
Yn 1 Yn
h
2
b g
k1 k 2
where
b g
k1 f xn , Yn
k2 fb
x h, Y hk g
n n 1
b g
k1 f xn , Yn hYn
k2 fb
x h, Y hk g
n n hY h Y
1 n
2 2
n
Thus
h 2 2
Yn 1 Yn hYn Yn
2
F
G
1 h
h 2 2
Yn
IJ
H 2 K
Hence the stability polynomial is given by
F
G
( ; h ) 1 h
h2 2 IJ
H 2 K
For absolute stability we require that
h2 2
1 h 1
2
33
In order to draw the region of absolute stability consider the boundary R A of R A .
The locus of this boundary will be the set of complex numbers z such that
z2
1 z 1
2
Thus
z2
1 z ei , 0 2
2
In order to obtain the region we need to plot the roots of the quadratic equation
z 2 2z 2 2ei 0
for in the range 0 2 . This is best done on a computer. The resulting stability
region is shown below:
The method outlined above is an example of the boundary locus method which is
easily implemented for Linear Multistep methods as follows. The stability polynomial
is
34
( ; h ) ( ) h ( ) 0
and hence
( )
h
( )
but on R A
1, thus ei , 0 2
Hence the locus of the boundary R A is given by the set of complex numbers z
satisfying
z
c h,
ei
0 2
c
e h i
Worked Example 9
X n 1 1
3 l4 X n q 23h f bt , X g
X n 1 n 1 n 1
bg
2 43 13 , bg
23 2
( ; h ) 2 43 13 23 h 2 0
35
Substituting z h and solving
3 2 4 1
z 1
2
2
3e2i 4ei 1 3
z 1
2 2 i
2 2e i 21 e 2i
e
In order to determine whether R A is the interior or exterior of the closed curve choose
a point inside the curve, say z 1 and evaluate the roots of ( ; h ) .
2 43 13 23 2 0
2 4 1 0
4 42 4
2 3
2
and we see that on of the roots, 2 3 , has modulus greater than one and hence R A
must consist of the exterior of the closed curve.
36
Worked Example 10
b g
X n 1 X n 1 2hf tn , X n
From above
bg bg
2 1, 2
( ; h ) 2 1 2h 0
2 1
z
2
e2i 1 ei e i
z i sin
2ei 2
which does not bound any region of the complex plane. Hence R A is empty.
37
Exercises 3
X n4
8
X n3 X n1 X n 6h f n 4 4 f n3 4 f n1 f n
19 19
(a) X n 3 X n 2
h
23 f n 2 16 f n1 5 f n
12
(b) X n 2 X n 1
h
5 f n 2 8 f n1 f n
12
(c) X n 3
1
18 X n 2 9 X n1 2 X n 6h f n3
11 11
(d) X n4
1
48 X n3 36 X n 2 16 X n1 3 X n 12h f n 4
25 25
38
APPENDIX 1
39
Methods
Exercises 1
1. Use the method outlined in section to obtain tables comparing the exact and
numerical solutions for a variety of stepsizes.
2. Use the method outlined in section to obtain solutions and plots with the odeplot
command.
Exercises 2
bg bg
2. 4 198 3 198 1, 196 4 19 19
24 3 24
196 , hence
b
1g 0, b
1g 60
19 bg
1 and roots of are 1, 1, bg 4
19 345
19 i which all have
absolute value one.
bg
3. 3 18 2 9 2 6 3
bg bg
11 11 11 , 11 , hence 1 0, 1 11 1 and
6
bg bg
bg
roots of are 1, 7
22 39
22 i which all have absolute value less than or equal to
one.
Absolute Stability
Exercises 3
40
(b) Region inside
41
APPENDIX 2
42
Worked Example 5 - Determining The Order of a Numerical Method
> restart:
> x:=t->x(t):
> alias(x1=D(x),x2=(D@@2)(x),x3=(D@@3)(x),x4=(D@@4)(x),
x5=(D@@5)(x),x6=(D@@6)(x),x7=(D@@7)(x),x8=(D@@8)(x)):
> ts:=taylor(x(t),t=tn,9):
> tsh:=subs(t-tn=h,ts):
> p:=unapply(convert(tsh,polynom),h):
> dts:=taylor(x1(t),t=tn,8):
> dtsh:=subs(t-tn=h,dts):
> dp:=unapply(convert(dtsh,polynom),h):
Euler's Method
> simplify(p(h)-x(tn)-h*x1(tn));
Trapezoidal Rule
> simplify(p(h)-x(tn)-h*(x1(tn)+dp(h))/2);
Quade's Method
> simplify(p(4*h)-8*(p(3*h)-p(h))/19-x(tn)-
6*h*(dp(4*h)+4*dp(3*h)+4*dp(h)+x1(tn))/19);
43
Worked Example 9
> restart:
> with(plots):
> rho:=theta->theta^2-4*theta/3+1/3;
> sigma:=theta->2/3*theta^2;
> pi:=theta->rho(theta)-lambda*h*sigma(theta);
Substitute z = h*lambda;
> piz:=theta->rho(theta)-z*sigma(theta);
> rat:=solve(piz(theta),z);
> rat1:=subs(theta=exp(I*phi),rat);
> complexplot(rat1,phi=0..2*Pi,numpoints=500);
> rs:=solve(subs(z=1,piz(theta))=0,theta);
Must be outside.
44
Worked Example 10
> restart:
> with(plots):
> rho:=theta->theta^2-1;
> sigma:=theta->2*theta;
> pi:=theta->rho(theta)-lambda*h*sigma(theta);
> piz:=theta->rho(theta)-z*sigma(theta);
Solve for z
> rat:=solve(piz(theta),z);
> rat1:=subs(theta=exp(I*phi),rat);
> simplify(rat1);
45