You are on page 1of 108

Numerical solution of partial dierential

equations
Dr. Louise Olsen-Kettle
Earth Systems Science Computational Centre
School of Earth Sciences
The University of Queensland
Email: l.kettle1@uq.edu.au
Web: http://www.uq.edu.au/
~
uqlkett1/
Acknowledgements
Special thanks to Cinnamon Eliot who helped typeset these lecture notes in
L
A
T
E
X.
Contents
1 Overview of PDEs 8
1.1 Classication of PDEs . . . . . . . . . . . . . . . . . . . . . . 8
1.1.1 Elliptic . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1.2 Hyperbolic . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1.3 Parabolic . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 Implicit Vs Explicit Methods to Solve PDEs . . . . . . . . . . 9
1.3 Well-posed and ill-posed PDEs . . . . . . . . . . . . . . . . . . 9
I Numerical solution of parabolic equations 11
2 Explicit methods for 1-D heat or diusion equation 12
2.1 Analytic solution: Separation of variables . . . . . . . . . . . 12
2.2 Numerical solution of 1-D heat equation . . . . . . . . . . . . 14
2.2.1 Dierence Approximations for Derivative Terms in PDEs 14
2.2.2 Numerical solution of 1-D heat equation using the nite dierence method 15
2.2.3 Explicit Forward Euler method . . . . . . . . . . . . . 16
2.2.4 Stability criteria for forward Euler method . . . . . . . 19
2.3 Method of lines . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1
2 CONTENTS
3 Implicit methods for 1-D heat equation 22
3.1 Implicit Backward Euler Method for 1-D heat equation . . . . 22
3.1.1 Numerical implementation of the Implicit Backward Euler Method 23
3.1.2 Dirichlet boundary conditions . . . . . . . . . . . . . . 23
3.1.3 Mixed boundary conditions . . . . . . . . . . . . . . . 23
3.2 Crank-Nicolson Scheme . . . . . . . . . . . . . . . . . . . . . . 25
4 Iterative methods 27
4.1 Jacobi method . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.1.2 Using the Jacobi method . . . . . . . . . . . . . . . . . 29
4.2 Gauss-Seidel Method . . . . . . . . . . . . . . . . . . . . . . . 30
4.2.1 Example: using Gauss-Seidel method to solve a matrix equation 30
4.3 Relaxation Methods . . . . . . . . . . . . . . . . . . . . . . . 31
5 2-D Finite Dierence 32
5.1 2-D Poissons equation . . . . . . . . . . . . . . . . . . . . . . 32
5.2 2-D Heat (or Diusion) Problems . . . . . . . . . . . . . . . . 36
5.2.1 Alternating Direct/Implicit method for the 2-D heat equation 37
5.3 Cylindrical and spherical polar co-ordinates . . . . . . . . . . 38
5.3.1 Example: Temperature around a nuclear waste rod . . 40
II Numerical solution of hyperbolic equations 45
6 Analytical solutions to the 1-D Wave equation 46
6.1 1-D Wave equation . . . . . . . . . . . . . . . . . . . . . . . . 46
6.2 dAlemberts solution . . . . . . . . . . . . . . . . . . . . . . . 46
6.3 Separation of variables . . . . . . . . . . . . . . . . . . . . . . 47
CONTENTS 3
7 Flux conservative problems 49
7.1 Flux Conservative Equation . . . . . . . . . . . . . . . . . . . 49
7.2 Stability analysis of numerical solutions of the rst order ux conservative or 1-D advection
7.3 Forward Time Centred Space (FTCS) . . . . . . . . . . . . . . 50
7.3.1 von Neumann stability analysis of FTCS method . . . 50
7.4 Lax Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.4.1 von Neumann Stability Analysis of Lax Method . . . . 51
7.5 Courant Condition . . . . . . . . . . . . . . . . . . . . . . . . 52
7.6 von Neumann Stability Analysis For Wave Equation . . . . . . 53
7.6.1 Lax method . . . . . . . . . . . . . . . . . . . . . . . . 53
7.7 Other sources of error . . . . . . . . . . . . . . . . . . . . . . . 54
7.7.1 Phase Errors (through dispersion) . . . . . . . . . . . . 54
7.7.2 Dispersion in the numerical solution of the 1-D advection equation using the Lax metho
7.7.3 Error due to nonlinear terms . . . . . . . . . . . . . . . 57
7.7.4 Aliasing error . . . . . . . . . . . . . . . . . . . . . . . 58
8 Numerical Solution of 1-D and 2-D Wave Equation 59
8.1 Explicit Central Dierence for 1-D Wave Equation . . . . . . . 59
8.1.1 Example: plucking a string . . . . . . . . . . . . . . . 59
8.1.2 1-D Wave Equation with Friction . . . . . . . . . . . . 63
8.2 2-D Wave Equation . . . . . . . . . . . . . . . . . . . . . . . . 66
8.2.1 Example: vibrations of a thin elastic membrane xed at its walls 67
8.2.2 Examples of wave equation . . . . . . . . . . . . . . . . 69
9 Finite element method 71
9.1 An introduction to the Finite Element Method . . . . . . . . . 71
9.2 Comparing FEM solution to FD solution for our example . . . 75
9.2.1 FD solution . . . . . . . . . . . . . . . . . . . . . . . . 76
9.3 2-D Finite Element Method . . . . . . . . . . . . . . . . . . . 76
9.3.1 2-D hat functions . . . . . . . . . . . . . . . . . . . . 78
9.3.2 Example: 2-D Finite Element Method using eScript for elastic wave propagation from
4 CONTENTS
10 Spectral methods 81
10.1 An introduction to spectral methods . . . . . . . . . . . . . . 81
10.1.1 Example 1: Comparing the accuracy of solutions of a variable speed wave equation
10.1.2 Example 2 Comparing spectral and nite dierence methods with constant w
III Nonlinear partial dierential equations 87
11 Shock wave 88
11.1 Analytical solution: Method of characteristics . . . . . . . . . 88
11.1.1 Example 1: Using method of characteristics to solve the linear 1-D advection
11.1.2 Example 2: Using method of characteristics to solve the nonlinear inviscid Burgers
11.2 Numerical Solution for nonlinear Burgers Equation . . . . . . 93
11.2.1 Example I: Finite dierence solution with Lax Method 94
11.2.2 Example II: Solution using Method of Lines . . . . . . 95
11.2.3 Example III: Solution using Spectral Method . . . . . . 96
12 Korteweg-de Vries Equation 99
12.1 Solitons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
12.2 Analytical solution . . . . . . . . . . . . . . . . . . . . . . . . 99
12.3 Numerical solution of KdV Equation . . . . . . . . . . . . . . 102
12.3.1 Solving directly with Spectral Method . . . . . . . . . 103
12.3.2 Modifying U
xxx
term causing instabilities in direct spectral method104
12.3.3 Interacting Solitons . . . . . . . . . . . . . . . . . . . . 105
List of Figures
2.1 Initial conditions in (a) and matlab solution using Forward Euler method for temperature distribution
3.1 Initial conditions in (a) and matlab solution using Backward Euler method for temperature
4.1 Plot of residual using the Gauss-Seidel method after each iteration 31
5.1 3-D Cylindrical Co-ordinates . . . . . . . . . . . . . . . . . . . 39
5.2 3-D Spherical Polar Co-ordinates . . . . . . . . . . . . . . . . 39
5.3 Initial conditions in (a) and matlab solution using Backward Euler method for temperature
7.1 Solution at t = 1 using the Lax method with dierent time steps, (a) t = x/2c where disp
7.2 Aliasing error occurs when the mesh spacing x is too large to represent the smallest wavelength
8.1 Initial conditions in (a) and matlab solution using explicit central dierence method for 1D
8.2 DAlemberts solution in (a) and error using numerical matlab solution using explicit central
8.3 Initial conditions in (a) and matlab solution using explicit central dierence method for 1D
9.1 FEM mesh with triangles . . . . . . . . . . . . . . . . . . . . 72
9.2 FEM mesh with triangles . . . . . . . . . . . . . . . . . . . . 77
9.3 2D hat function (
j
(x
j
, y
j
) = 1,
j
(x
i
, y
l
) = 0 if i = j and j = l) 78
9.4 Plot of Euclidean normal of the displacement at t > 0 for a point source using eScript. 80
10.1 Numerical solution for 1D advection equation with initial conditions of a smooth Gaussian pulse
10.2 Numerical solution for 1D advection equation with initial conditions of a box pulse with a constan
11.1 The analytical solution U(x, t) = f(x Ut) is plotted to show how shock and rarefaction dev
5
6 LIST OF FIGURES
11.2 Initial conditions in (a) and solution for nonlinear Bugers equation using the Lax metho
11.3 Initial conditions in (a) and solution for nonlinear Bugers equation using the metho
11.4 Initial conditions in (a) and solution for nonlinear Bugers equation using the spectral
12.1 The initial conditions U(x, 0) = f(x) = Asech
2
(
_
A
12
x). . . . . 101
12.2 Initial conditions in (a) and solution for nonlinear KdV equation using the direct sp
12.3 Initial conditions and nal solution after one period in (a) and solution for nonlinear
12.4 The initial conditions U(x, 0) = f(x) is plotted to show the 2 solitions and their speeds
12.5 Initial conditions and nal solution after one period in (a) and solution for nonlinear
Numerical solution of parabolic
and hyperbolic PDEs
This document can be downloaded from
http://www.uq.edu.au/
~
uqlkett1/Louise_Olsen_Teaching_Info/MATH3203LectureNotes/math320
Code for the lecture examples can be downloaded from here:
http://www.uq.edu.au/
~
uqlkett1/
Louise_Olsen_Teaching_Info/MATH3203LectureNotes/math3203code.html.
References:
Applied Numerical Methods for Engineers using Matlab and C, R. J. Schilling
and S. L. Harris.
Computational Physics Problem solving with computers, R.H. Landau
and M. L. P aez.
An Introduction to Computational Physics, T. Pang.
Numerical Recipes in Fortran (2nd Ed.), W. H. Press et al.
Introduction to Partial Dierential Equations with Matlab, J. M. Cooper.
Numerical solution of partial dierential equations, K. W. Morton and
D. F. Mayers.
Spectral methods in Matlab, L. N. Trefethen
7
Chapter 1
Overview of PDEs
1.1 Classication of PDEs
The classication of PDEs is important for the numerical solution you choose.
A(x, y)U
xx
+ 2B(x, y)U
xy
+ C(x, y)U
yy
= F(x, y, U
x
, U
y
, U)
1.1.1 Elliptic
AC > B
2
For example, Laplaces equation:
U
xx
+ U
yy
= 0
A = C = 1, B = 0
1.1.2 Hyperbolic
AC < B
2
For example the 1-D wave equation:
U
xx
=
1
c
2
U
tt
A = 1, C = 1/c
2
, B = 0
8
1.2. IMPLICIT VS EXPLICIT METHODS TO SOLVE PDES 9
1.1.3 Parabolic
AC = B
2
For example, the heat or diusion Equation
U
t
= U
xx
A = 1, B = C = 0
1.2 Implicit Vs Explicit Methods to Solve PDEs
Explicit Methods:
possible to solve (at a point) directly for all unknown values in the
nite dierence scheme.
stable only for certain time step sizes (or possibly never stable!). Sta-
bility can be checked using Fourier or von Neumann analysis. Time
step size governed by Courant condition for wave equation.
Implicit Methods:
there is no explicit formula at each point, only a set of simultaneous
equations which must be solved over the whole grid.
Implicit methods are stable for all step sizes.
1.3 Well-posed and ill-posed PDEs
The heat equation is well-posed U
t
= U
xx
. However the backwards heat
equation is ill-posed: U
t
= U
xx
at high frequencies this blows up!
In order to demonstrate this we let U(x, t) = a
n
(t) sin(nx)
then:
U
xx
= a
n
(t)n
2
sin(nx), and U
t
= a
n
(t) sin(nx)
10 CHAPTER 1. OVERVIEW OF PDES
U
t
= U
xx
. .
Heat Equation
a
n
(t) sin(nx) = a
n
(t)n
2
sin(nx)
a
n
= a
n
n
2
a
n
(t) = a
n
(0)e
n
2
t
For the heat equation the transient part of the solution decays and this has
stable numerical solutions.
U
t
= U
xx
. .
Backwards Heat Equation
a
n
(t) sin(nx) = a
n
(t)n
2
sin(nx)
a
n
= a
n
n
2
a
n
(t) = a
n
(0)e
n
2
t
For the backwards heat equation the transient part of the solution blows up
and the numerical solution would fail! In general it is dicult or impossible
to obtain numerical solutions for ill-posed PDEs.
Part I
Numerical solution of parabolic
equations
11
Chapter 2
Explicit methods for 1-D heat
or diusion equation
We will focus on the heat or diusion equation for the next few chapters.
This is an example of a parabolic equation.
2.1 Analytic solution: Separation of variables
First we will derive an analtical solution to the 1-D heat equation. Consider
the temperature U(x, t) in a bar where the temperature is governed by the
heat equation, U
t
= U
xx
. The ends of the bar are cooled to 0

C and the
initial temperature of the bar is 100

C.

U(0, t) = 0

C

U(L, t) = 0

C
U(x, 0) = 100

C
`
We want to solve U
t
= U
xx
using separation of variables. We assume that
the solution can be written as the product of a function of x and a function
of t, ie. U(x, t) = X(x)T(t) then:
U
t
=
T
t
X = T

2
X
x
2
= U
xx
divide by XT
12
2.1. ANALYTIC SOLUTION: SEPARATION OF VARIABLES 13
1

(t)
T(t)
. .
function of t only
=
X

(x)
X(x)
. .
function of x only
=
2
. .
constant
(2.1)
The only way the LHS and RHS of equation 2.1 can be a function of t and
x respectively is if they are both equal to a constant which we dene to be

2
for convenience.
T

+
2
T = 0 T = e

2
t
X

+
2
X = 0 X = Asin x + Bcos x
Use boundary conditions U(t, 0) = 0 X(0) = 0 = B
U(t, L) = 0 X(L) = 0 = Asin
t =
n
=
n
L
, n = 1, 2, . . .
U(x, t) = X(x)T(t)
=

n=1
Ansin(
nx
L
)e

2
t
e

2
t
is a transient solution and decays in time to boundary conditions.
Use initial conditions U(0, x) = 100

C to nd A
n
:
U(0, x) = T
0
=

n=1
A
n
sin (nx/L)
Use orthogonality:
_
L
0
sin(
nx
L
) sin(
mx
L
)dx =
nm
and cos(m) 1 = 0, for m = 0, 2, 4, . . .
and cos(m) 1 = 2, for m = 1, 3, 5, . . .
A
m
= T
0
[L/m(cos(m) 1)]
=
2L
m
T
0
for m=1,3,5,. . .
14CHAPTER 2. EXPLICIT METHODS FOR 1-DHEAT OR DIFFUSIONEQUATION
2.2 Numerical solution of 1-D heat equation
2.2.1 Dierence Approximations for Derivative Terms
in PDEs
We consider U(x, t) for 0 x a, 0 t T
Discretise time and spatial variable x:
t =
T
m
, x =
a
n + 1
,
t
k
= kt, 0 k m x
j
= jx, 0 j n + 1
Let U
k
j
= U(x
j
, t
k
)
Consider Taylor series expansion for U
k+1
j
:
U
k+1
j
= U
k
j
+ t
U
k
j
t
+
t
2
2

2
U
k
j
t
2
+ 0(t
3
) (2.2)
If we only consider 0(t) terms in equation 2.2 then we arrive at the forward
dierence in time approximation for U
t
:
U
k
j
t
=
U
k+1
j
U
k
j
t
+ 0(t)
We can also derive a higher order approximation for U
t
if we consider the
Taylor series expansion for U
k1
j
as well:
U
k1
j
= U
k
j
t
U
k
j
t
+
t
2
2

2
U
k
j
t
2
+ 0(t
3
) (2.3)
2.2 2.3
U
k
j
t
=
U
k+1
j
U
k1
j
2t
+ 0(t
2
) leap-frog (or centred dierence) in time.
This gives higher order accuracy than forward dierence.
2.2. NUMERICAL SOLUTION OF 1-D HEAT EQUATION 15
We can also perform similar manipulations to arrive at approximations for
the second derivative U
tt
:
2.2 + 2.3 2U
k
j

2
U
k
j
t
2
=
U
k+1
j
+ U
k1
j
2U
k
j
t
2
+ 0(t
2
) central dierence
The nite dierence method makes use of the above approximations to solve
PDEs numerically.
2.2.2 Numerical solution of 1-D heat equation using
the nite dierence method
U
t
= U
xx
Initial conditions
U(0, x) = f(x)
Types of boundary conditions
Neumann boundary conditions
U
x
(t, 0) = g
1
(t)
U
x
(t, a) = g
2
(t)
Dirichlet boundary conditions
U(t, 0) = g
1
(t)
U(t, a) = g
2
(t)
Mixed boundary conditions
U(t, 0) = g
1
(t)
U
x
(t, a) = g
2
(t)
16CHAPTER 2. EXPLICIT METHODS FOR 1-DHEAT OR DIFFUSIONEQUATION
2.2.3 Explicit Forward Euler method
or FTCS (Foward Time Centred Space)
We want to solve the 1-D heat equation:
U
t
= U
xx
. (2.4)
We solve this PDE for points on a grid using the nite dierence method
where we discretise in x and t for 0 x a and 0 t T:
t
0
t
1
t
2
t
3
. . .
t
m1 t
m
= T
x
0
x
1
x
2
x
3
.
.
.
x
n
x
n+1
= a
We discretise in time with time step: t = T/m and in space with grid
spacing: x = a/(n + 1), and let t
k
= kt where 0 k m and x
j
= jx
where 0 j n + 1.
Let U
k
j
= U(x
j
, t
k
) then the nite dierence approximations for equation
(2.4) are given by:
U(x
j
, t
k
)
t
=
U
k+1
j
U
k
j
t
+ O(t), Forward Euler method for time derivative,

2
U(x
j
, t
k
)
x
2
=
U
k
j+1
2U
k
j
+ U
k
j1
x
2
+ O(x
2
), Central dierence method for spatial derivativ
Our discretised PDE (equation (2.4)) becomes:
U
k+1
j
U
k
j
t
=

x
2
_
U
k
j+1
2U
k
j
+ U
k
j1
_
,
or U
k+1
j
= s
_
U
k
j+1
+ U
k
j1
_
+ (1 2s)U
k
j
,
where s =
t
x
2
.
2.2. NUMERICAL SOLUTION OF 1-D HEAT EQUATION 17
U
k+1
j
is the solution for the temperature at the next time step.
Suppose we have initial conditions U(x, 0) = U
0
j
= f(x
j
), and mixed bound-
ary conditions:
Dirichlet boundary conditions at x = 0: U(0, t) = U
k
0
= g
1
(t
k
),
Neumann boundary conditions at x = a: U
x
(a, t) =
U
k
n+1
x
= g
2
(t
k
),
Numerical implementation of Explicit Forward Euler method
Solving equation (2.4): U
t
= U
xx
with:
initial conditions: U
0
j
= f(x
j
) = U(x, t = 0),
Dirichlet boundary conditions at x = 0: U(x = 0, t) = U
k
0
= g
1
(t
k
) and
Neumann boundary conditions at x = a: U(a, t)/x = U
k
n+1
/x = g
2
(t
k
).
To solve using the Neumann boundary condition we need an extra step:
U
k
n+1
x

U
k
n+1
U
k
n
x
= g
2
(t
k
),
or U
k
n+1
= xg
2
(t
k
) + U
k
n
. (2.5)
We can write out the matrix system of equations we will solve numerically
for the temperature U. Suppose we use 5 grid points x
0
, x
1
, x
2
, x
3
, x
4
= x
n+1
,
ie. n = 3 in this example:
x
0
= 0
x
1
x
2
x
3
x
4
= x
n+1
= a
We let:

U
k
=
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_, solution for temperature vector

U
k
at time t
k
.
The boundary conditions give U
k
0
= U(x = 0, t
k
) and U
k
n+1
= U
k
4
= U(x =
a, t
k
).
18CHAPTER 2. EXPLICIT METHODS FOR 1-DHEAT OR DIFFUSIONEQUATION
We can rewrite U
k+1
j
= s
_
U
k
j+1
+ U
k
j1
_
+ (1 2s)U
k
j
in matrix form:

U
k+1
=
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
_
_
_
1 2s s 0
s 1 2s s
0 s 1 2s
_
_
_
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_ +
_
_
_
sU
k
0
0
sU
k
4
_
_
_
(2.6)
Using boundary conditions: U
k
0
= g
1
(t
k
) and U
k
4
= xg
2
(t
k
) + U
k
3
equation
(2.6) becomes:

U
k+1
=
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
_
_
_
_
_
1 2s s 0
s 1 2s s
0 s 1 s
. .
Neumann bc
_
_
_
_
_
. .
A
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_ +
_
_
_
_
_
_
_
_
_
Dirichlet bc
..
sg
1
(t
k
)
0
sxg
2
(t
k
)
. .
Neumann bc
_
_
_
_
_
_
_
_
_
. .
b
or

U
k+1
= A

U
k
+

b (2.7)
The term (1 s) in the matrix A above and the term (sxg
2
(t
k
)) in vector

b above are from the Neumann boundary condition given using the approxi-
mation in equation (2.5).
Matlab code for Explicit Forward Euler method
The matlab code can be downloaded here for details of the numerical coding
of the example below:
Solving equation (2.4): U
t
= U
xx
with 0 x 1 and 0 t 12, 000, using
m = 600 time steps, and n = 39 for 41 grid points in x.
Initial conditions: U(x, t = 0) = 2x + sin(2x) + 1,
and Dirichlet boundary conditions at x = 0: U(x = 0, t) = 1 and Neumann
boundary conditions at x = 1: U(x = 1, t)/x = 2.
With these boundary conditions we can check that the numerical solution
approximates the steady state solution U(x, t) = 2x + 1 as t . Your
solution using the nite dierence code can also be checked using Matlabs
PDE solver (using pdex1).
2.2. NUMERICAL SOLUTION OF 1-D HEAT EQUATION 19
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
1.4
1.6
1.8
2
2.2
2.4
2.6
2.8
Initial condition for Temperature distribution
x
U
0
5000
10000
15000
0
0.2
0.4
0.6
0.8
1
1
1.5
2
2.5
3
t
Variation of Temperature distribution with time
x
U
Figure 2.1: Initial conditions in (a) and matlab solution using Forward Euler
method for temperature distribution along rod with time in (b)
Figure 2.1 shows the initial conditions in (a) and matlab solution for tem-
perature distribution along rod with time in (b). The numerical solution
matches the analytical solution reasonably well: U(x, t) = 2x+1 at the nal
time. However the Neumann boundary condition at x = 1 introduces error
through the approximation to the spatial derivative in this initial condition
so the numerical solution at x = 1 is not exactly U = 3 but close to it.
2.2.4 Stability criteria for forward Euler method
Suppose U(x, 0) = f(x) = cos (x/x) where these initial conditions data
oscillate with the same frequency as the grid, x = a/n +1, As before we
let x
j
= jx, 0 j n + 1
f(x
j
) = cos(j) = (1)
j
Using nite dierence discretisation of (2.4) U
t
= U
xx
:
U
k+1
j
= s(U
k
j+1
+ U
k
j1
) + (1 2s)U
k
j
At rst time step (k = 1):
U
1
j
= (1 2s)(1)
j
+ s [(1)
j+1
+ (1)
j1
]
. .
2(1)
j
= (1 4s)(1)
j
20CHAPTER 2. EXPLICIT METHODS FOR 1-DHEAT OR DIFFUSIONEQUATION
At k = 2:
U
2
j
= (1 2s)U
1
j
+ s(U
1
j+1
+ U
1
j1
)
U
2
j
= (1 2s)(1 4s)(1)
j
+ s [(1 4s)(1)
j+1
+ (1 4s)(1)
j1
]
. .
2(14s)(1)
j
= (1 4s)
2
(1)
j
Therefore at k = n
U
n
j
= (1 4s)
n
(1)
j
NB: the term (1 4s) determines stability.
This solution for U
n
j
will become unbounded as n if |1 4s| 1 or
s > 1/2.
We know the exact solution |U(x, t)| |U(x, t
0
)| = x, t.
The Forward Euler method is only stable if s (known as the gain parameter)
satises 0 s 1/2 or equivalently the time step satises: t x
2
/2.
You can check that using the matlab code that when the time step exceeds
this value that the numerical solution becomes unstable.
2.3 Method of lines
There are other explicit numerical methods that can be applied to the 1-
D heat or diusion equation such as the Method of Lines which is used
by Matlab and Mathematica. The trick with the Method of Lines is that
it replaces all spatial derivatives with nite dierences but leaves the time
derivatives. It is then possible to use a sti ordinary dierential equation
solver on the time derivatives in the resulting system.
2.3.1 Example
Download the matlab code for this example here and here.
We are solving the same system again with the method of lines: U
t
= U
xx
where the initial conditions are U(x, 0) = sin(2x) + 2x + 1
0 x 1, = 10
5
, 0 t 12, 000.
boundary conditions are U(0, t) = 1 and U
x
(1, t) = 2
Again we get:

U
t
= A

U +

b
2.3. METHOD OF LINES 21
How? Replace
U
xx
=
U
j+1
2U
j
+ U
j1
x
2
,
where U(x
j
, t) = U
j
(t), x
j
= jx, 0 j n + 1
x = a/(n + 1) = 1/(n + 1) (a = 1)
with boundary conditions: U(0, t) = U
0
(t) = 1
U
x
(1, t) =
U
n+1
x
(t)
U
n+1
U
n
x
= 2 U
n+1
= U
n
+ 2x
In matrix form for n = 3 elements:
x
0
= 0
x
1
x
2
x
3 x
4
= 1

U
t
=
_
_
_

U
1

U
2

U
3
_
_
_ =

x
2
_
_
_
2 1 0
1 2 1
0 1 1
_
_
_
_
_
_
U
1
U
2
U
3
_
_
_ +

x
2
_
_
_
1
0
2x
_
_
_

U = A

U +

b
We solve for

U using ode45 using matlab code downloaded from here and
here.
Chapter 3
Implicit methods for 1-D heat
equation
3.1 Implicit Backward Euler Method for 1-D
heat equation
Unconditionally stable (but usually slower than explicit methods).
implicit because it evaluates dierence approximations to derivatives
at next time step t
k+1
and not current time step we are solving for t
k
.
U
xx
(t
k+1
, x
j
) =
U
k+1
j+1
2U
k+1
j
+ U
k+1
j1
x
2
U
t
(t
k+1
, x
j
) =
U
k+1
j
U
k
j
t
U
t
= U
xx
becomes:
U
k
j
= U
k+1
j

t
x
2
[U
k+1
j+1
2U
k+1
j
+ U
k+1
j1
]
= (1 + 2s)U
k+1
j
s(U
k+1
j+1
+ U
k+1
j1
) (3.1)
where s =
t
x
2
as before.
We still need to solve for U
k+1
j
given U
k
j
is known This requires solving a
22
3.1. IMPLICIT BACKWARDEULER METHODFOR 1-DHEAT EQUATION23
tridiagonal linear system of n equations.
Again we let U
k
j
= U(x
j
, t
k
); x
j
= jx, j = 0, ..., n + 1, x =
a
n+1
; t
k
=
kt, k = 0, ..., m, and t =
T
m
.
3.1.1 Numerical implementation of the Implicit Back-
ward Euler Method
Again we are solving the same problem: U
t
= U
xx
, U(x, 0) = U
0
j
= f(x
j
)
3.1.2 Dirichlet boundary conditions
U(0, t) = 1 = U
k
0
, U(a, t) = U(1, t) = 3 = U
k
n+1
= U
k
4
For simplicity we consider only 4 elements in x in this example to nd the
matrix system we need to solve for:
x
0
= 0 x
1
= 0.25 x
2
= 0.5 x
3
= 0.75 x
4
= 1
Rewriting s[U
k+1
j+1
+ U
k+1
j1
] + (1 + 2s)U
k+1
j
= U
k
j
as a matrix equation:
_
_
_
1 + 2s s 0
s 1 + 2s s
0 s 1 + 2s
_
_
_
. .
Tridiagonal matrix
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_
. .
Solution U at next time step
=
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_ + s
_
_
_
U
k+1
0
0
U
k+1
4
_
_
_
. .
given from b.c.
A

U
k+1
=

U
k
+


U
k+1
= A
1
[

U
k
+

b]
3.1.3 Mixed boundary conditions
The matlab code can be downloaded here for this example.
24 CHAPTER 3. IMPLICIT METHODS FOR 1-D HEAT EQUATION
U(0, t) = 1 = U
k
0
, U
x
(1, t) = 2 =
U
k
n+1
x
Using a leap-frog approximation for the spatial derivative:
U
k
j
x
=
U
k
j+1
U
k
j1
2x
+ 0(x
2
)
This is more accurate than the forward approximation we used previously
(see section 2.2.1):
U
k
j
x
=
U
k
j+1
U
k
j
x
+ 0(x)
So for the Neumann boundary condition we have:
U
x
(1, t) =
U
k
n+1
x

U
k
n+2
U
k
n
2x
= 2 (3.2)
With n = 4, U
k
5
is called a ghost point because it lies outside the bar. So
using equation 3.2 we dene U
k
5
:
U
k
5
= 4x + U
k
3
Because we have Neumann boundary conditions at x = a(= 1), U
k
n+1
= U
k
4
is unknown, and given by equation 3.1:
U
k
4
= s[U
k+1
5
+ U
k+1
3
] + (1 + 2s)U
k+1
4
use U
k+1
5
= 4x + U
k+1
3
U
k
4
= s[4x + 2U
k+1
3
] + (1 + 2s)U
k+1
4
Our system of equations becomes:
_
_
_
_
_
_
_
_
1 + 2s s 0 0
s 1 + 2s s 0
0 s 1 + 2s s
0 0 2s
. .
Neumann b.c.
1 + 2s
_
_
_
_
_
_
_
_
. .
A
_
_
_
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
U
k+1
4
_
_
_
_
_
_
. .

U
k+1
=
_
_
_
_
_
U
k
1
U
k
2
U
k
3
U
k
4
_
_
_
_
_
. .

U
k
+
_
_
_
_
_
_
_
_
_
_
_
Dirichlet b.c.
..
sU
k+1
0
0
0
4sx
. .
Neumann b.c.
_
_
_
_
_
_
_
_
_
_
_
. .

b
Using an implicit solver means we have to invert the matrix A to solve for

U
k+1
which is a lot more computationally expensive than the matrix multiply
operation in equation 2.7 to nd

U
k+1
for explicit solvers in section 2.2.3.
A

U
k+1
=

U
k
+

b = c


U
k+1
= A
1
c
3.2. CRANK-NICOLSON SCHEME 25
This method is stable for s 0 so larger time steps can be used for implicit
methods than explicit methods.
Download the matlab code here.
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
1.4
1.6
1.8
2
2.2
2.4
2.6
2.8
3
Initial condition for Temperature distribution
x
U
0
5000
10000
15000
0
0.2
0.4
0.6
0.8
1
1
1.5
2
2.5
3
t
Variation of Temperature distribution with time using Backward Euler method
x
U
Figure 3.1: Initial conditions in (a) and matlab solution using Backward
Euler method for temperature distribution along rod with time in (b)
Figure 3.1 shows the initial conditions in (a) and matlab solution for tem-
perature distribution along rod with time in (b). The solution using the
Backward Euler method in gure 3.1 is stable even for large time steps and
this matlab code uses a time step 6 greater than the solution using the For-
ward Euler method in gure 2.1. So even though there is more work in each
time step (inverting a matrix) using the implicit Backward Euler method it
allows larger time steps than the explicit Forward Euler method.
3.2 Crank-Nicolson Scheme
Average of the explicit (forward Euler) and implicit (backward Euler)
schemes.
Uses:
U
t
=
U
k+1
j
U
k
j
t
U
xx
=
1
2
_

_
U
k+1
j+1
2U
k+1
j
+ U
k+1
j
x
2
. .
implicit
+
U
k
j+1
2U
k
j
+ U
k
j1
x
2
. .
explicit
_

_
26 CHAPTER 3. IMPLICIT METHODS FOR 1-D HEAT EQUATION
Often used for simple diusion problems.
Chapter 4
Iterative methods
Implicit methods are stable - however they can take much longer to compute
than explicit methods. We saw that the Backward Euler method requires a
system of linear equations to be solved at each time step:
A

U
k+1
= c (where: c =

U
k
+

b)
where A is a tridiagonal matrix. How can we speed up this calculation? By
using iterative methods.
Iterative methods:
improve the solution of Ax =

b.
use a direct method or a guess for an initial estimate of the solution.
useful for solving large, sparse systems (eg. tridiagonal matrix A in
Backward Euler scheme).
many dierent methods such as Jacobi, Gauss-Seidel, relaxation meth-
ods.
iterative methods are not always applicable and convergence criteria
need to be met before they can be applied. However they are ideal for
nite dierence methods (involving solution of large sparse matrices).
Iterative methods begin with an initial guess for the solution x
0
to the matrix
equation we are trying to solve: Ax =

b. Each iteration updates the new
k
th
estimate (x
k
) which converge on the exact solution x. Dierent methods
have dierent convergence times and for big inverse matrix problems are
much faster than direct matrix inverse methods.
27
28 CHAPTER 4. ITERATIVE METHODS
4.1 Jacobi method
A is decomposed into a sum of lower-triangular (L), diagonal (D) and upper-
triangular terms (U):
A = L + D + U
_
`

A = D
L
U

4.1.1 Example
A for Backward Euler Method with Dirichlet boundary conditions:
A =
_
_
_
_
_
_
_
_
_
1 + 2s s 0
s 1 + 2s s
.
.
.
.
.
.
.
.
.
s 1 + 2s s
0 s 1 + 2s
_
_
_
_
_
_
_
_
_
D =
_
_
_
_
_
_
1 + 2s 0
1 + 2s
.
.
.
0 1 + 2s
_
_
_
_
_
_
, L =
_
_
_
_
_
_
_
0 0
s
.
.
.
.
.
.
.
.
.
0 s 0
_
_
_
_
_
_
_
, U =
_
_
_
_
_
_
_
0 s 0
.
.
.
.
.
.
.
.
.
s
0 0
_
_
_
_
_
_
_
We want to solve Ax = b
(D + L + U)x =

b
or Dx = b (L + U)x
x =
_
_
_
_
_
_
x
1
x
2
.
.
.
x
n
_
_
_
_
_
_
4.1. JACOBI METHOD 29
If x
k
is k
th
estimate of solution Ax =

b then the (k + 1)
th
estimate is:
Dx
k+1
= b (L + U)x
k
(Jacobi Method)
Since D is diagonal (D
ij
=
ij
A
ij
), we can write the vector equation above
for x
k+1
for each component (x
k+1
1
, ...x
k+1
n
).
x
k+1
i
=
1
A
ii
..
D
1
_
_
_
_
_
_
_
_
b
i

j=i
A
ij
x
k
j
. .
L+U part
_
_
_
_
_
_
_
_
, 1 i n (4.1)
4.1.2 Using the Jacobi method
To start the scheme use an initial guess x
0
, (eg. x
0
=

0). The iterations are


repeated until Ax
k

b or the residual:
|

b Ax
k
| < error tolerance (eg.10
5
)
Jacobi method converges to correct solution x
k
x as k if :
D
1
(L + U) < 1 |A
ii
| >

j=i
|A
ij
|
. .
A is strictly diagonally dominant
, 1 i n
where B is the row-sum norm dened below:
B =
n

j=1
|B
ij
|
The degree to which the convergence criteria:
|A| >

j=i
|A
ij
|, 1 i n
holds is a measure of how fast the estimate x
k
converges to actual solution
x.
Look for matlab code on this free source website: http://www.netlib.org/
which implements the Jacobi method and try for yourself.
30 CHAPTER 4. ITERATIVE METHODS
4.2 Gauss-Seidel Method
improves convergence of Jacobi method by simple modication
in Jacobi method the new estimate, x
k+1
i
is computed using only the
current estimate, x
k
j
Gauss-Seidel method uses all the possible new estimates (j i 1)
x
k+1
i1
, x
k+1
i2
, ..., x
k+1
1
, x
k+1
0
when updating the new estimate x
k+1
i
:
x
k+1
i
=
1
A
ii
_
_
b
i

i1

j=1
A
ij
x
k+1
j

n

j=i+1
A
ij
x
k
j
_
_
, 1 i n.
This is an improvement over the Jacobi method because it uses the new
estimate x
k+1
j
when it can. In vector form: x
k+1
= D
1
(bLx
k+1
Ux
k
)
or (D + L)x
k+1
= b Ux
k
.
solution converges x
k
x as k if: (D + L)
1
U 1.
4.2.1 Example: using Gauss-Seidel method to solve a
matrix equation
Download matlab code for this example here. Solve Ax =

b for Res =
|b Ax
k+1
| < 1e
3
with:
A =
_
_
_
5 0 2
3 5 1
0 3 4
_
_
_, b =
_
_
_
7
2
4
_
_
_, x
0
=
_
_
_
0
0
0
_
_
_
with initial guess x
0
=
_
_
_
0
0
0
_
_
_ takes 9 iterations. The Jacobi method needs
17 iterations to converge so takes nearly twice as long as the Gauss-Seidel
method.
Figure 4.1 shows the residual using the Gauss-Seidel method after each iter-
ation, it takes 9 iterations for the residual error to be less than 0.001.
4.3. RELAXATION METHODS 31
0 1 2 3 4 5 6 7 8 9
0
1
2
3
4
5
6
7
8
9
no of iterations
R
e
s
i
d
u
a
l
Residual after each iteration using GaussSeidel method
Figure 4.1: Plot of residual using the Gauss-Seidel method after each itera-
tion
4.3 Relaxation Methods
Relaxation methods generalise Gauss-Seidel method by introducing a relax-
ation factor, > 0. If is optimised for the system this can increase the
rate of convergence of the solution x
k
by modifying the size of the correction:
x
k+1
i
= x
k
i
+

A
ii
_
_
b
i

i1

j=1
A
ij
x
k+1
j

n

j=i
A
ij
x
k
j
_
_
, 1 i n. (4.2)
This is called the successive relaxation (SR) method and for:
0 < < 1 under-relaxation
= 1 Gauss-Seidel method
> 1 over-relaxation
We can re-write equation 4.2:
x
k+1
i
= (1 )x
k
i
+

A
ii
_
_
b
i

i1

j=1
A
ij
x
k+1
j

n

j=i+1
A
ij
x
k
j
_
_
, 1 i n
Solution converges, ie x
k
x as k if: (D+L)
1
[(1)DU] < 1.
Chapter 5
2-D Finite Dierence
5.1 2-D Poissons equation
Solving Laplaces (f = 0) or Poissons equation in 2-D:
U
xx
+ U
yy
= f (5.1)
We discretise in x and y-directions:
x
0
x
1
x
2
x
3
. . .
x
m
x
m+1
= a
y
0
y
1
y
2
y
3
.
.
.
y
n
y
n+1
= b
We discretise in x-direction with grid spacing: x = a/(m + 1) and in
y-direction with grid spacing: y = b/(n + 1), and let x
k
= kx where
0 k m + 1 and y
j
= jy where 0 j n + 1. We let U
kj
= U(x
k
, y
j
)
32
5.1. 2-D POISSONS EQUATION 33
and f
kj
= f(x
k
, y
j
) We are solving equation (5.1) using Dirichlet boundary
conditions:
U(0, y) = U
0,j
= g
0,j
(y
j
)
U(a, y) = U
m+1,j
= g
m+1,j
(y
j
)
U(x, 0) = U
k,0
= g
k,0
(x
k
)
U(x, b) = U
k,n+1
= g
k,n+1
(x
k
)
Using central dierence approximations for U
xx
and U
yy
then the nite dif-
ference approximations for equation (5.1) are given by:

2
U
x
2
= U
xx
(x
k
, y
j
) =
U
k+1,j
2U
k,j
+ U
k1,j
x
2
,

2
U
x
2
= U
yy
(x
k
, y
j
) =
U
k,j+1
2U
k,j
+ U
k,j1
y
2
.
Our discretised PDE (equation 5.1) becomes (if x = y = h):
U
k+1,j
2U
k,j
+ U
k1,j
h
2
+
U
k,j+1
2U
k,j
+ U
k,j1
h
2
= f
k,j
,
or U
k+1,j
+ U
k1,j
4U
k,j
+ U
k,j+1
+ U
k,j1
= h
2
f
k,j
. (5.2)
Since we have Dirichlet boundary conditions: the outer boundaries of the
region we are solving for are known: U
0,j
, U
m+1,j
, U
k,0
, U
k,n+1
, and we need to
nd the interior values: U
k,j
for 1 k m and 1 j n.
For example: m = 3 and n = 3:
34 CHAPTER 5. 2-D FINITE DIFFERENCE
x
0
x
1
x
2
x
3
x
4
= x
m+1
= a
y
0
y
1
y
2
y
3
y
4
= y
n+1
= b



U
11
U
21
U
31
U
12
U
22
U
32
U
13
U
23
U
33
Thus we need to solve for the interior values marked with a circle above as
the boundary values are already given. We let the vector of interior values
we are solving for be dened as:

U =
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
U
11
U
12
U
13
U
21
U
22
U
23
U
31
U
32
U
33
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
, vector of interior values we are solving for.
Thus the matrix system for

U using equation (5.2):
5.1. 2-D POISSONS EQUATION 35
U
k+1,j
+ U
k1,j
4U
k,j
+ U
k,j+1
+ U
k,j1
= h
2
f
k,j
becomes:
k = 1
j = 1
k = 1
j = 2
k = 1
j = 3
k = 1
j = 1
k = 1
j = 2
k = 2
j = 3
k = 2
j = 1
k = 3
j = 2
k = 3
j = 3
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
4 1 0 1 0 0 0 0 0
U
k,j
U
k,j+1
U
k+1,j
1 4 1 0 1 0 0 0 0
U
k,j1
U
k,j
U
k,j+1
U
k+1,j
0 1 4 0 0 1 0 0 0
U
k,j1
U
k,j
U
k+1,j
1 0 0 4 1 0 1 0 0
U
k1,j
U
k,j
U
k,j+1
U
k+1,j
0 1 0 1 4 1 0 1 0
U
k1,j
U
k,j1
U
k,j
U
k,j+1
U
k+1,j
0 0 1 0 1 4 0 0 1
U
k1,j
U
k,j1
U
k,j
U
k+1,j
0 0 0 1 0 0 4 1 0
U
k1,j
U
k,j
U
k,j+1
0 0 0 0 1 0 1 4 1
U
k1,j
U
k,j1
U
k,j
U
k,j+1
0 0 0 0 0 1 0 1 4
U
k1,j
U
k,j1
U
k,j
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
U
11
U
12
U
13
U
21
U
22
U
23
U
31
U
32
U
33
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
+
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
U
10
+ U
01
U
k,j1
+ U
k1,j
U
02
U
k1,j
U
14
+ U
03
U
k,j+1
+ U
k1,j
U
20
U
k,j1
0
U
24
U
k,j+1
U
30
+ U
41
U
k,j1
+ U
k+1,j
U
42
U
k+1,j
U
34
+ U
43
U
k,j+1
+ U
k+1,j
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
+ h
2
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
f
11
f
12
f
13
f
21
f
22
f
23
f
31
f
32
f
33
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
36 CHAPTER 5. 2-D FINITE DIFFERENCE
ie:
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
4 1 0 1 0 0 0 0 0
1 4 1 0 1 0 0 0 0
0 1 4 0 0 1 0 0 0
1 0 0 4 1 0 1 0 0
0 1 0 1 4 1 0 1 0
0 0 1 0 1 4 0 0 1
0 0 0 1 0 0 4 1 0
0 0 0 0 1 0 1 4 1
0 0 0 0 0 1 0 1 4
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
U
11
U
12
U
13
U
21
U
22
U
23
U
31
U
32
U
33
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
=
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
h
2
f
11
g
10
g
01
h
2
f
12
g
02
h
2
f
13
g
14
g
03
h
2
f
21
g
20
h
2
f
22
h
2
f
23
g
24
h
2
f
31
g
30
g
41
h
2
f
32
g
42
h
2
f
33
g
34
g
43
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
,
or A

U =

f. (5.3)
If m n 100 then direct matrix elimination methods can be used. Oth-
erwise iterative methods such as Jacobi, Gauss-Seidel, relaxation methods
should be used to solve for

U, as discussed in previous chapter 4. Because A
is sparse and diagonally dominant, iterative solutions are ideal here.
However we see in the next section 5.2.1 that when we solve 2-D parabolic
equations (2-D heat or diusion equations) that the numerical solution re-
quires tweaking since the matrix A is no longer tridiagonal for 2-D as it was
for 1-D.
5.2 2-D Heat (or Diusion) Problems
We consider the 2-D heat equation:
U
t
= (U
xx
+ U
yy
) for 0 x a, 0 y b and 0 t T.
with initial conditions: U(0, x, y) = f(x, y) and boundary conditions: U(t, x, y) =
g(t, x, y) for (x, y) on boundary.
5.2. 2-D HEAT (OR DIFFUSION) PROBLEMS 37
If we use explicit forward Euler scheme as we did for the 1-D heat
equation the stability criteria is even stricter than for 1-D:
t
x
2
+ y
2
8
this is less attractive because the time step is much smaller.
if we use backward Euler or the Crank-Nicolson methods they are no
longer as attractive because the matrix systems to be solved are much
larger and no longer tridiagonal.
We will look at an alternative nite dierence method specically tai-
lored to the 2-D heat equation: the alternating direction implicit (ADI)
method.
5.2.1 Alternating Direct/Implicit method for the 2-D
heat equation
U
t
= (U
xx
+ U
yy
)
We let t = T/m, x = a/(n + 1), y = b/(p + 1) t
k
= kt, 0 k
m, x
i
= ix, 0 i n + 1, y
j
= jy, 0 j p + 1, and:
U(t
k
, x
i
, y
j
) = U
k
ij
The time derivative, U
t
, is approximated using a leap-frog step in time about
the mid-point t
k+1/2
, where t
k+1/2
= (t
k
+t
k+1
)/2 using a time step of t/2:
U
k+1/2
ij
t
=
U
k+1
ij
U
k1
ij
t
at time t
k+1/2
.
The spatial derivatives, U
xx
and U
yy
, are approximated using central dier-
ences:

2
U
k
ij
x
2
=
U
k
i+1,j
2U
k
ij
+ U
k
i1,j
x
2
. .
EARLY STEP
at time t
k
,

2
U
k+1
ij
y
2
=
U
k+1
i,j+1
2U
k+1
ij
+ U
k+1
i,j1
y
2
at time t
k+1
.
38 CHAPTER 5. 2-D FINITE DIFFERENCE
This introduces an early bias which evaluates U
xx
(t
k
) at an earlier time
than U
yy
(t
k+1
). This bias is compensated by also evaluating U
xx
(t
k+2
) at
t
k+2
; U
yy
(t
k+1
) again at t
k+1
and U
t
(t
k+3/2
) at mid-point between t
k+1
and
t
k+2
:
U
k+3/2
ij
t
=
U
k+2
ij
U
k+1
ij
t
,

2
U
k+2
ij
x
2
=
U
k+2
i+1,j
2U
k+2
ij
+ U
k+2
i1,j
x
2
. .
LATE STEP
and

2
U
k+1
ij
y
2
as before.
The boundary conditions specify U
k
oj
, U
k
m+1,j
, U
k
io
, U
k
i,p+1
, and initial condi-
tions specify U
0
ij
. So we are solving for 1 k m, 1 i m, 1 j p.
ie. for each interior point at time t
k
.
We solve the problem for both time steps t
k+1
and t
k+2
at the same time
using early and late denitions. If we let s
x
= t/x
2
, s
y
= t/y
2
then U
t
= (U
xx
+ U
yy
) becomes:
Early step:
U
k+1
ij
(1 + 2s
y
) s
y
[U
k+1
i,j+1
+ U
k+1
i,j1
] = U
k
ij
(1 2s
x
) + s
x
[U
k
i+1,j
+ U
k
i1,j
]
This is solved rst for i
th
row of U
k+1
matrix, for 1 i n.
Late step:
U
k+2
ij
(1 + 2s
x
) s
x
[U
k+2
i+1,j
+ U
k+2
i1,j
] = U
k+1
ij
(1 2s
y
) + s
y
[U
k+1
i,j+1
+ U
k+1
i,j1
]
This is solved for j
th
column of U
k+2
matrix, for 1 j p.
These are solved using LU decomposition. (For more details see Schilling
and Harris, p. 445).
5.3 Cylindrical and spherical polar co-ordinates
Spherical and cylindrical symmetry in problems are often exploited to
reduce 2-D 1-D or 3-D 1-D.
5.3. CYLINDRICAL AND SPHERICAL POLAR CO-ORDINATES 39
3-D Cylindrical Co-ordinates
x = r cos
y = r sin
z = z
r =

x
2
+ y
2
= arctan(
y
x
)
Figure 5.1: 3-D Cylindrical Co-ordinates
3-D Spherical Polar Co-ordinates
x = r sin cos
y = r sin sin
z = r cos
Figure 5.2: 3-D Spherical Polar Co-ordinates
40 CHAPTER 5. 2-D FINITE DIFFERENCE
2-D Polar Co-ordinates
x = r cos
y = r sin
r =

x
2
+ y
2
= arctan(
y
x
).

x
=
r
x

r
+

x

= cos

r

sin
r

y
=
r
y

r
+

y

= sin

r
+
cos
r

5.3.1 Example: Temperature around a nuclear waste


rod
The matlab code for this example is available from here.

_
`
y


r
r = a
Nuclear rod buried in ground
We consider the temperature increase due to storage of nuclear rods which
release heat due to radioactive decay:
1

T
t
(r, t)
2
T(r, t)
. .
2 -D heat equation
= S(r, t)
. .
source term
where the source term due to the radioactive decay of rod is given by:
S(r, t) =
_
T
rod
e
t/
0
/a
2
for r a
0 elsewhere.
where a = 25cm, = 2 10
7
cm
2
/year, T
rod
= 1K,
0
= 100years, r
c
=
100cm, T
E
= 300K, 0 < r < 100cm and 0 < t < 100years. Initially
T(r, t = 0) = 300K.
5.3. CYLINDRICAL AND SPHERICAL POLAR CO-ORDINATES 41
Because the problem has circular symmetry (ie. no dependence) 2-D
problem in (x, y) reduced to 1-D problem in r.
2
T = T
xx
+ T
yy
is 2-D in
Cartesian co-ordinates. However if we choose to use polar co-ordinates then
the temperature, T(r, t) is a function of r only because the rod circularly
symmetric and there is no dependence. This reduces the original 2-D
problem to 1-D!
How do we evaluate
2
T in polar co-ordinates?
T
xx
=

2
T
x
2
= (cos

r

sin
r

)(cos
T
r

sin
r
T

)
= cos
2
T
rr

2 sin cos
r
T
r
+
sin
2

r
T
r
+
2 cos sin
r
T

+
sin
2

r
T

T
yy
= (sin

r
+
cos
r

)(sin
T
r
+
cos
r
T

)
= sin
2
T
rr
+
2 cos sin
r
T
r

2 cos sin
r
2
T

+
cos
2

r
T
r
+
cos
2

r
2
T

and T
xx
+ T
yy
= T
rr
+
1
r
T
r
+
1
r
2
T

using cos
2
+ sin
2
= 1.
Since the temperature T(r, t) has no dependence then T

= 0 and we are
solving the 1-D heat equation in polar co-ordinates:
1
K
T
t


2
T
r
2

1
r
T
r
= S(r, t)
We know that in the steady state solution eventually the nuclear rod is no
longer radioactive and stops releasing heat: S(r, t) 0 as t , and
further enough away from the rod the temperature equals the environment
temperature, T(r = r
c
, t) = 300K. So the solution should approach the
environmental temperature T(r, t) = 300K once rod has nished radioactive
decaying.
We use nite dierences to solve:
1
K
T
t


2
T
r
2

1
r
T
r
= S(r, t) (5.4)
We observe that there is a singularity at r = 0 in the above equation where
special care needs to be taken so that the numerical solution is stable.
42 CHAPTER 5. 2-D FINITE DIFFERENCE
Initial conditions T(r, 0) = 300K.
Neumann boundary conditions at r = 0 (temperature cannot ow into r = 0
region)
T
r
(r = 0, t) = 0
Dirichlet boundary conditions at r = r
c
T(r = r
c
, t) = 300K
Again we discretise space and time: r = r
c
/(n + 1), t = T
f
/m, r
j
=
jr, 0 j n + 1, t
k
= kt, 0 k m, T(r
j
, t
k
) = T
k
j
, and S(r
j
, t
k
) =
S
k
j
.
Discrete Neumann boundary conditions at r = 0 become:
T
k
j
t
(r = 0, t) =
T
k
0
t
= 0
T
k
1
T
k
0
t
T
k
0
T
k
1
Discrete Dirichlet boundary conditions at r = r
c
become:
T
k
j
(r = r
c
, t) = T
k
n+1
= 300
We will use the backward Euler method (implicit) to solve the PDE. This
means evaluating the spatial derivatives in r at the future time step t
k+1
:
T
t
(t
k+1
, r
j
) =
T
k+1
j
T
k
j
t
T
rr
(t
k+1
, r
j
) =
T
k+1
j+1
2T
k+1
j
+ T
k+1
j1
r
2
(centred dierence at t
k+1
)
T
r
(t
k+1
, r
j
) =
T
k+1
j+1
T
k+1
j1
2r
(leap-frog in space)
Using r
j
= jr our discretised PDE 5.4 becomes:
1

T
t
T
rr

1
r
T
r
= S(r, t)
1
t
[T
k+1
j
T
k
j
]
. .
T
t
/
[
T
k+1
j+1
2T
k+1
j
+ T
k+1
j1
r
2
]
. .
T
rr

1
jr
[
T
k+1
j+1
T
k+1
j1
2r
]
. .
T
r
/r
= S
k
j
We let s = t/r
2
and we arrive at:
T
k+1
j+1
[s
s
2j
] + T
k+1
j1
[s +
s
2j
] + T
k+1
j
[1 + 2s] = T
k
j
+ S
k
j
t (5.5)
This is a tridiagonal matrix for 1 j n.
5.3. CYLINDRICAL AND SPHERICAL POLAR CO-ORDINATES 43
Numerical solution of the 1-D heat equation in polar co-ordinates
using the Backward Euler method
For n = 3:
r
0
= 0
r
1
r
2
r
3
r
n+1
= r
c
= r
4

r
The boundary conditions give T
k
0
T
k
1
using
T
r
(r = 0, t) = 0) and T
k
4
=
300K using (T(r = r
c
, t) = 300) and the initial conditions are T
0
j
= 300K.
We solve equation 5.5 for T
k
1
, T
k
2
, T
k
3
at each time step (t
k
):
_
_
_
_
1 + 2s (s
s
2j
) 0
(s +
s
2j
) 1 + 2s (s
s
2j
)
0 (s +
s
2j
) 1 + 2s
_
_
_
_
_
_
_
T
k+1
1
T
k+1
2
T
k+1
3
_
_
_ +
_
_
_
_
(s +
s
2j
)T
k+1
0
0
(s
s
2j
)T
k+1
4
_
_
_
_
=
_
_
_
T
k
1
T
k
2
T
k
3
_
_
_ + t
_
_
_
S
k
1
S
k
2
S
k
3
_
_
_
Using the boundary conditions: T
k+1
0
T
k+1
1
, T
k+1
4
= 300K
_
_
_
_
(1 + s +
s
2j
) (s
s
2j
) 0
(s +
s
2j
) (1 + 2s) (s
s
2j
)
0 (s +
s
2j
) (1 + 2s)
_
_
_
_
_
_
_
T
k+1
1
T
k+1
2
T
k+1
3
_
_
_
=
_
_
_
T
k
1
T
k
2
T
k
3
_
_
_ + t
_
_
_
S
k
1
S
k
2
S
k
3
_
_
_
_
_
_
0
0
(s
s
2j
)T
k+1
4
_
_
_

_
_
_
(1 + s +
s
2
) (s
s
2
) 0
(s +
s
4
) (1 + 2s) (s
s
4
)
0 (s +
s
6
) (1 + 2s)
_
_
_
_
_
_
T
k+1
1
T
k+1
2
T
k+1
3
_
_
_
=
_
_
_
T
k
1
T
k
2
T
k
3
_
_
_ + t
_
_
_
S
k
1
S
k
2
S
k
3
_
_
_
_
_
_
0
0
(s
s
6
)300
_
_
_
Or to simplify we are solving the following matrix equation for the vector of
unknown temperatures

T
k+1
:
A

T
k+1
=

T
k
+ t

S
k
+

b
44 CHAPTER 5. 2-D FINITE DIFFERENCE
Download the matlab code here and check that solution for T 300K as
t (steady state approaches environment temperature, 300K).
0 10 20 30 40 50 60 70 80 90 100
299
299.2
299.4
299.6
299.8
300
300.2
300.4
300.6
300.8
301
Initial condition for Temperature distribution
r
T
e
m
p
e
r
a
t
u
r
e
0 10 20 30 40 50 60 70 80 90 100
300
300.1
300.2
300.3
300.4
300.5
300.6
300.7
300.8
300.9
301
r
T
e
m
p
e
r
a
t
u
r
e


Temp after 1 year
Temp after 10 years
Temp after 50 years
Temp after 100 years
Figure 5.3: Initial conditions in (a) and matlab solution using Backward
Euler method for temperature distribution near nuclear rod at dierent time
intervals in (b)
Figure 5.3 shows the initial conditions and temperature distribution near the
nuclear rod at dierent time intervals.
Part II
Numerical solution of
hyperbolic equations
45
Chapter 6
Analytical solutions to the 1-D
Wave equation
6.1 1-D Wave equation
U
tt
c
2
U
xx
= 0
or (

t
c

x
)(

t
+ c

x
)U = 0 (6.1)
This is a hyperbolic equation since A = 1, C = c
2
, B = 0 so that AC < B
2
6.2 dAlemberts solution
We introduce a change of variables:
= x + ct
= x ct
Then:

=
x

x
+
t

t
=

x
+
1
c

=
x

x
+
t

t
=

x

1
c

t
So equation 6.1 becomes:
c

(c

)U = c
2
U

= 0
46
6.3. SEPARATION OF VARIABLES 47
U(, ) = g() + f()
= g(x + ct) + f(x ct)
g(x + ct) denes waves that travel in left direction with speed c
f(x ct) denes waves that travel in right direction with speed c.
The pulses move without dispersion and the initial pulse breaks into a
left and right pulse.
6.3 Separation of variables
This time we will derive the analytical solution using the separation of vari-
ables technique as we did for the 1-D heat equation in section 2.1. We want
to solve the 1-D heat equation:
U
tt
= c
2
U
xx
(6.2)
with periodic boundary conditions U(0, t) = 0 = U(L, t)
Again we assume U(x, t) = X(x)T(t) then substitute into equation 6.2:
X(x)T

(t) = c
2
X

(x)T(t)
then divide by XT
T

T
..
function of t only
= c
2
X

X
. .
function of x only
=
2
(constant)
Solving X

= k
2
X, where k = /c for X(x) gives:
X = Asin(kx) + Bcos(kx)
The boundary conditions give X(0) = B = 0 and X(L) = Asin kL = 0
k = k
n
= n/L, n = 0, 1, . . .. Thus the general solution for X(x) is:
X(x) =

n
a
n
sin(
nx
L
)
Similarly if we solve T

=
2
n
T (where
n
= ck
n
) we nd the general
solution:
T(t) = C sin(
n
t) + Dcos(
n
t)
48CHAPTER 6. ANALYTICAL SOLUTIONS TOTHE 1-DWAVE EQUATION
. So the solution for U(x, t) is:
U(x, t) = X(x)T(t)
=

n
[a
n
sin(
n
t) + b
n
cos(
n
t)] sin(k
n
x)
where a
n
, b
n
are given by initial conditions:
U(x, 0) = U
0
(x),
U
t
(x, 0) = V
0
(x)
U
0
=

n
b
n
sin(k
n
x) V
0
=

n
a
n

n
sin(k
n
x)
using orthogonality of sine functions:
_
L
0
sin(kmx) sin(knx)dx =
nm

b
m
=
2
L
_
L
0
U
0
(x) sin(kmx)dx
a
m
=
2
w
m
L
_
L
0
V
0
(x) sin(kmx)dx
Chapter 7
Flux conservative problems
7.1 Flux Conservative Equation
A large class of PDEs can be cast into the form of a ux conservative equation:

U
t
=
f
x
(

U,

U
x
,

U
xx
, ...)
Example: ux conservative form for the wave equation
We consider the 1-D wave equation U
tt
= c
2
U
xx
. If we let:
w =
_
r
s
_
, where r = c
U
x
, and s =
U
t
.
This means that:
w
t
=
_
r
t
s
t
_
=
_
c
s
x
c
r
x
_
or
w
t
=

x
_
0 c
c 0
_
w =

x
f( w)
7.2 Stability analysis of numerical solutions
of the rst order ux conservative or 1-D
advection equation
U
t
= c
U
x
(7.1)
49
50 CHAPTER 7. FLUX CONSERVATIVE PROBLEMS
We introduce a change of variable = x ct and:

t
=

t

= c

,

x
=

x

We see that equation 7.1 holds: c


U

= c
U

. So, U(x, t) = U() =


f(x ct) is the analytic general solution of equation 7.1, which is a wave
propagating in the right (positive x) direction.
We study the stability of dierent nite dierence schemes in solving the ux
conservative or 1-D advection equation:
U
t
= cU
x
, x
0
x x
1
, t
0
t T
Again we discretise problem x =
x
1
x
0
n+1
, t =
Tt
0
m
and let x
j
= x
0
+
jx, j = 0, . . . , n + 1, t
k
= t
0
+ kt, k = 0, . . . , m, and U
k
j
= U(x
j
, t
k
).
7.3 Forward Time Centred Space (FTCS)
Forward Euler method in time:
U
k
j
t
=
U
k+1
j
U
k
j
t
+ O(t)
Leap-frog or centred dierence in space:
U
k
j
x
=
U
k
j+1
U
k
j1
2x
+ O(x
2
)
Using FTCS method: U
t
= cU
x
gives:
U
k+1
j
= U
k
j

ct
2x
[U
k
j+1
U
k
j1
] (7.2)
7.3.1 von Neumann stability analysis of FTCS method
FTCS is unstable! Why?
We assume that independent solutions (eigenmodes) of equation 7.2 (or any
dierence equation) are of the form:
U
k
j
=
k
e
ipjx
(7.3)
where p is a real spatial wavenumber and = (p) is a complex number that
depends on p.
7.4. LAX METHOD 51
Equation 7.3 shows that the time dependence of a single eigenmode U
k
j
is
only through successive powers of (
k
). Dierence equations are unstable
if |(p)| > 1 for some p. is called the amplication factor.
To nd (p) for FTCS method substitute U
k
j
=
k
e
ipjx
into equation 7.2:

k+1
e
ipjx
=
k
e
ipjx
_
_
_
_
1
ct
2x
(e
ipx
e
ipx
)
. .
2i sin(px)
_
_
_
_
(p) = 1
ict
x
sin(px)
and |(p)| 1 p FTCS scheme is unconditionally unstable for solving
U
t
= cU
x
.
7.4 Lax Method
Again we are solving the ux conservative equation: U
t
= cU
x
. The in-
stability in the FTCS method is removed in the Lax method by using the
average for U
k
j
=
U
k
j+1
+U
k
j1
2
instead of U
k
j
in approximating U
t
:
U
k
j
t
=
U
k+1
j

1
2
[U
k
j+1
+ U
k
j1
]
t
and centred dierence again for U
x
. Then U
t
= cU
x
becomes:
U
k+1
j
=
1
2
[U
k
j+1
+ U
k
j1
]
ct
2x
[U
k
j+1
U
k
j1
] (7.4)
7.4.1 von Neumann Stability Analysis of Lax Method
The Lax method is conditionally stable. To see substitute U
k
j
=
k
e
ipjx
into
equation 7.4:

k+1
e
ipjx
=
k
e
ipjx
_
_
_
_
_
1
2
[e
ipx
+ e
ipx
]
. .
cos(px)

ct
2x
(e
ipx
e
ipx
)
. .
2i sin(px)
_
_
_
_
_
= cos(px) i
ct
x
sin(px)
52 CHAPTER 7. FLUX CONSERVATIVE PROBLEMS
Lax method stable when ||
2
1 :
| cos
2
(px) +
c
2
t
2
x
2
sin
2
(px)| 1
or |1 (1
c
2
t
2
x
2
) sin
2
(px)| 1
1
c
2
t
2
x
2
0
or
c
2
t
2
x
2
1
or t
x
c
. .
COURANT CONDITION
(c > 0)
7.5 Courant Condition
The Courant condition means Lax method is stable when t x/c
The physical meaning is that value U
k+1
j
is computed from information
at points j 1 and j + 1 at time k in a stable scheme, where the
wave speed is less that the mesh spacing divided by time. ie. in a
continuum wave equation information propagates at maximum speed
c, so Lax method is stable when
x
t
c. This is shown in the plot below:

`
, , , , ,
, , , , ,
, , , , ,
U
k
j1
U
k
j
U
k
j+1
U
k+1
j
`
`
`
`
`
`
`
`
`

t
x

x
`

t
wave speed c

7.6. VONNEUMANNSTABILITYANALYSIS FOR WAVE EQUATION53
Unstable schemes arise when
x
t
c ie. when the time step t be-
comes too large because U
k+1
j
requires information from points outside
[U
k
j1
, U
k
j+1
] as shown in the plot below. (see Press et al, Numerical
Recipes, p. 825-830)

`
, , , , ,
, , , , ,
U
k
j1
U
k
j
U
k
j+1
U
k+1
j
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
`
``


t
x

x
`

t
wave speed c

7.6 von Neumann Stability Analysis For Wave
Equation
U
tt
= c
2
U
xx
let w =
_
r
s
_
=
_
cU
x
U
t
_
We saw earlier that:
w
t
=
_
r
t
s
t
_
=
_
c
s
x
c
r
x
_
= c

x
_
s
r
_
7.6.1 Lax method
We will solve the wave equation using the Lax method:
r
t
= cs
x
becomes:
r
k+1
j
=
1
2
[r
k
j1
+ r
k
j+1
] +
ct
2x
(s
k
j+1
s
k
j1
) (7.5)
54 CHAPTER 7. FLUX CONSERVATIVE PROBLEMS
s
t
= cr
x
becomes:
s
k+1
j
=
1
2
[s
k
j1
+ s
k
j+1
] +
ct
2x
(r
k
j+1
r
k
j1
) (7.6)
where r
k
j
= r(x
j
, t
k
) and s
k
j
= s(x
j
, t
k
)
For von Neumann stability analysis assume eigen-modes for r
k
j
and s
k
j
are of
the form:
_
r
k
j
s
k
j
_
=
k
e
ipjx
_
r
0
j
s
0
j
_
solutions stable if || 1
Equation 7.5 and 7.6 give:
_
cos(px)
ict
x
sin(px)

ict
x
sin(px) cos(px)
__
r
0
s
0
_
=
_
0
0
_
This has a solution only if determinant = 0.
This gives = cos(px) i
ct
x
sin(px).
This is stable if ||
2
1 which gives same Courant condition t
x
c
.
7.7 Other sources of error
7.7.1 Phase Errors (through dispersion)
Fourier analysis of the Lax method shows how phase errors arise.
The Fourier mode U(x, t) = e
i(px+t)
is an exact solution of U
t
= cU
x
if and p satisfy the dispersion relation = cp, then U(x, t) =
e
ip(xct)
= f(x ct) gives the exact solution of U
t
= cU
x
.
ie. this mode is completely undamped and the amplitude is constant (no
dispersion) for the numerical solution using a time step which satisifes
this dispersion relation.
We will show the eects of phase errors by studying the numerical
solution of the 1-D advection equation using dierent time steps which
lead to dispersion being absent or present in section 7.7.2.
7.7. OTHER SOURCES OF ERROR 55
Dispersion relation for the Lax Method
The dispersion relation is only satised if: t =
x
c
.
Why? Consider U
k
j
=
k
e
ipjx
In section 7.4.1 we found:
= cos(px) i
ct
x
sin(px)
= e
ipx
+ i(1
ct
x
) sin(px)
If we let t =
x
c
= e
ipx
and U
k
j
=
k
e
ipjx
= e
ip(kx+jx)
When we substitute x
j
= jx, t
k
= kt and the dipsersion relation x =
ct then: U
k
j
= e
ip(ckt+jx)
= e
ip(x
j
ct
k
)
= f(x
j
ct
k
)
. .
exact solution
.
Thus the Lax method has no dispersion present when the time step satises
the dispersion relation exactly: t =
x
c
. We will show this in the next
section.
7.7.2 Dispersion in the numerical solution of the 1-D
advection equation using the Lax method
The matlab code can be downloaded from here.
Use Lax Method to solve:
U
t
+ U
x
= 0, 0 x 2 , 0 t 1
initial conditions:
U(x, 0) =
_
1, 0.2 x 0.4
0, otherwise
= U
0
(x)
boundary conditions:
U(0, t) = U(2, t) = 0
56 CHAPTER 7. FLUX CONSERVATIVE PROBLEMS
Exact solution

t = 0
initial pulse
t = 1
nal pulse
0.2 0.4 1.2 1.4

U(x, t) = U
0
(x ct)
= U
0
(x t) (c = 1)
In the next section we compare the above exact solution with the numerical
solution using the Lax method with dierent time steps:
t =
x
c
no dispersion matches analytic solution.
t =
x
2c
dispersion present but pulse matches speed of wave.
t =
1.001x
c
courant condition not met unstable!
Lax Method for U
t
+ U
x
= 0
Equation 7.4 gives: U
k+1
j
=
1
2
[U
k
j+1
+ U
k
j1
]
ct
2x
[U
k
j+1
U
k
j1
]
let s =
ct
x
U
k+1
j
=
1
2
(1 s)U
k
j+1
+ U
k
j1
+
1
2
(1 + s)U
k
j1
Again for simplicity we only consider 4 elements in x:
x
0
x
1
x
2
x
3
x
4
Solve for U
k+1
j
for 0 k m, 1 j 3 with boundary conditions: U
k
0
= 0,
U
k
4
= 0. We have:
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
_
_
_
0
1
2
(1 s) 0
1
2
(1 + s) 0
1
2
(1 s)
0
1
2
(1 + s) 0
_
_
_
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_ +
_
_
_
1
2
(1 + s)U
k
0
0
1
2
(1 s)U
k
4
_
_
_
or

U
k+1
= A

U
k
+

b
7.7. OTHER SOURCES OF ERROR 57
Dispersion means the initial pulse changes shape (unlike analytical solution)
because wave components with dierent frequencies travel at dierent speeds.
Downlad the matlab code here to see how the numerical solution changes for
dierent time steps depending on whether or not the scheme is stable or
dispersion is present. Figure 2.1 shows how the solution changes for dierent
time steps depending on whether or not the scheme is stable or dispersion is
present.
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0
0.2
0.4
0.6
0.8
1
Final solution at t=1
x
U
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0
0.2
0.4
0.6
0.8
1
Final solution at t=1
x
U
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0
0.2
0.4
0.6
0.8
1
Final solution at t=1
x
U
Figure 7.1: Solution at t = 1 using the Lax method with dierent time
steps, (a) t = x/2c where dispersion is present but the pulse matches
the analytical solution for the speed of the wave, (b) t = x/c where
no dispersion is present and numerical solution matches analytical solution
exactly, and (c) t = 1.001x/c where the Courant condition is not met
and solution is becoming unstable.
7.7.3 Error due to nonlinear terms
Example
Shock wave equation:
U
t
+ UU
x
. .
nonlinear term
= 0
nonlinear term causes wave prole to steepen resulting in a shock.
schemes stable for linear problems can become unstable.
this will be discussed later in chapter 11
58 CHAPTER 7. FLUX CONSERVATIVE PROBLEMS
7.7.4 Aliasing error
Example
-1.5
-1
-0.5
0
0.5
1
0 1 2 3 4 5 6
x
x

2
Figure 7.2: Aliasing error occurs when the mesh spacing x is too large to
represent the smallest wavelength
1
and misinterprets it as a longer wave-
length oscillation
2
Alising error occurs when a short wavelength (1) is not represented well by
the mesh-spacing (x), and may be misinterpreted as a longer wavelength
oscillation (2).
Chapter 8
Numerical Solution of 1-D and
2-D Wave Equation
8.1 Explicit Central Dierence for 1-D Wave
Equation
U
tt
= c
2
U
xx
, 0 t T, 0 x a
Discretise: t =
T
m
, x =
a
n+1
,
t
k
= kt, 0 k m, x
j
= jx and 0 j n + 1.
8.1.1 Example: plucking a string
The matlab code can be downloaded from here.
0 0.8a
a
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
`
`
`
`
`
`
`
`
A string is initially plucked or lifted from rest:
59
60CHAPTER 8. NUMERICAL SOLUTIONOF 1-DAND2-DWAVE EQUATION
boundary conditions: U(0, t) = 0, U(a, t) = 0 or U
k
0
= 0, U
k
n+1
= 0
initial conditions: string is plucked or lifted 1mm at x = 0.8a:
U(x, t = 0) = f(x) =
_
1.25x
a
, for x 0.8a
5(1
x
a
), for x > 0.8a
Plucked string is released from rest:
U
t
(x, 0) = g(x) = 0
U(x, t = 0) = f(x) U
0
j
= f
j
= f(x
j
)
U
t
(x, t = 0) = g(x)
U
0
j
t

U
1
j
U
1
j
2t
. .
leap-frog in time
= g
j
= g(x
j
)
We can solve for ghost point U
1
j
:
U

j
1 = U
1
j
2tg(x
j
)
We approximate U
tt
and U
xx
using central dierences:
U
tt
=
U
k+1
j
2U
k
j
+ U
k1
j
t
2
U
xx
=
U
k
j+1
2U
k
j
+ U
k
j1
x
2
Using U
tt
= c
2
U
xx
and s =
c
2
t
2
x
2
, we solve for U
k+1
j
at time step k + 1:
U
k+1
j
= U
k1
j
. .
solution at t
k1
+ 2U
k
j
(1 s) + s(U
k
j+1
+ U
k
j1
)
. .
solution at t
k
In order to nd U
2
j
we need to know U
0
j
and U
1
j
.
We consider n = 3:
x
0
x
1
x
2
x
3
x
4
8.1. EXPLICIT CENTRAL DIFFERENCE FOR 1-DWAVE EQUATION61
boundary conditions: U
k
0
= 0, U
k
4
= 0
initial conditions: U
0
j
= f
j
, U
1
j
= U
1
j
2tg(x
j
) = U
1
j
, since g(x
j
) = 0.
First nd

U
1
=
_
_
_
U
1
1
U
1
2
U
1
3
_
_
_

U
1
=
_
_
_
U
1
1
U
1
2
U
1
3
_
_
_ =
_
_
_
2(1 s) s 0
s 2(1 s) s
0 s 2(1 s)
_
_
_
. .
A
_
_
_
U
0
1
U
0
2
U
0
3
_
_
_ + s
_
_
_
U
0
0
0
U
0
4
_
_
_
. .
b

_
_
_
U
1
1
U
1
2
U
1
3
_
_
_
Use U
0
j
= f
j
and U
1
j
= U
1
j
2tg
j

U
1
=
_
_
_
U
1
1
U
1
2
U
1
3
_
_
_ =
1
2
_
_
_
2(1 s) s 0
s 2(1 s) s
0 s 2(1 s)
_
_
_
_
_
_
f
1
f
2
f
3
_
_
_ +
s
2
_
_
_
U
0
0
0
U
0
4
_
_
_ + t
_
_
_
g
1
g
2
g
3
_
_
_

U
1
=
1
2
A

U
0
+
1
2

b +

d
For this example, U
0
0
= 0, U
0
4
= 0 and:
U
0
j
t
(x, t = 0) = g(x
j
) = 0

d =

0
for

U
2
, . . . ,

U
m
we have:
U
k+1
j
= 2U
k
j
(1 s) + s(U
k
j+1
+ U
k
j1
) U
k1
j
for 1 k m:

U
k+1
=
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
_
_
_
2(1 s) s 0
s 2(1 s) s
0 s 2(1 s)
_
_
_
. .
A
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_ + s
_
_
_
U
k
0
0
U
k
4
_
_
_
. .
b

_
_
_
U
k1
1
U
k1
2
U
k1
3
_
_
_

U
k+1
= A

U
k
+

b

U
k1
The matlab code can be downloaded from here.
In our example U
k
0
= 0, U
k
4
= 0

b =

0, since U
k
0
= 0 = U
k
4
At xed boundaries U(0, t) = 0 = U(a, t) wave is reected. We plot the
numerical solution in gure 8.1.
62CHAPTER 8. NUMERICAL SOLUTIONOF 1-DAND2-DWAVE EQUATION
0 5 10 15 20 25 30 35 40 45 50
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Initial condition for plucked string
x
U
Figure 8.1: Initial conditions in (a) and matlab solution using explicit central
dierence method for 1D wave equation in (b)
We can compare with DAlemberts solution which gives:
U(x, t) =
1
2
[f(x ct) + f(x + ct)] since U
t
(x, 0) = 0
where U(x, 0) = f(x) (initial conditions) for < x <
What if we want to solve the wave equation for 0 x a, with xed bound-
ary condition U(t, 0) = 0 = U(t, a)? We can extend DAlemberts general so-
lution for U
tt
= c
2
U
xx
with initial conditions:U(x, 0) = f(x) U
t
(x, 0) = g(x):
U(t, x) =
f(x + ct) + f(x ct)
2
+
1
2c
_
x+ct
xct
g(z)dz
for x
In our example:
U
t
(x, 0) = 0, U(x, 0) = f(x) =
_
1.25x
a
, 0 x 0.8a
5(1
x
a
), for x 0.8a
0 x a
with xed boundary conditions:
The boundary condition U(0, t) = 0 is equivalent to f and g being odd
8.1. EXPLICIT CENTRAL DIFFERENCE FOR 1-DWAVE EQUATION63
functions:
U(0, t) = 0 f(x) = f(x)
g(x) = g(x)
(f and g are odd functions)
The boundary condition U(a, t) = 0 is equivalent to f and g being periodic
with period 2a:
U(a, t) = 0 f(x + 2a) = f(x)
g(x + 2a) = g(x)
(f and g are periodic with period 2a)
Since U
t
(x, 0) = g(x) = 0 the analytical solution for our example:
U(t, x) =
f(x + ct) + f(x ct)
2
and we can compare the analytical solution with the numerical solution in
gure 8.2.
Figure 8.2: DAlemberts solution in (a) and error using numerical matlab
solution using explicit central dierence method for 1D wave equation in (b)
8.1.2 1-D Wave Equation with Friction
The matlab code can be downloaded from here.
64CHAPTER 8. NUMERICAL SOLUTIONOF 1-DAND2-DWAVE EQUATION
We consider friction due to viscosity of medium and density of string. Sup-
pose we are solving:

U + 2

U = c
2
U
xx
, 0 x a = 50, 0 t T = 20
The friction term opposes motion of string and means that eventually
vibrations decay with time.
Suppose string is initially plucked in 2 places:
0
a
0.1a 0.3a 0.7a 0.9a

We have initial conditions:


U(x, 0) =
_

_
0, 0 x 0.1a
5(10x a), 0.1a x 0.2a
5(10x + 3a), 0.2a x 0.3a
0, 0.3a x 0.7a
5(10x 7a), 0.7a x 0.8a
5(10x + 9a), 0.8a x 0.9a
0, x 0.9a
U
t
(x, 0) = 0
and boundary conditions: U(x, 0) = 0, U(x, a) = 0.
Again we use central dierence for U
xx
and U
tt
as in section 8.1.1.
We use a leap-frog step for U
t
U
k
j
t
=
U
k+1
j
U
k1
j
2t
Now we substitute dierence approximations into U
tt
+ 2U
t
= c
2
U
xx
U
k+1
j
2U
k
j
+ U
k1
j
t
2
+
U
k+1
j
U
k1
j
t
=
c
2
(U
k
j+1
2U
k
j
+ U
k
j1
)
x
2
8.1. EXPLICIT CENTRAL DIFFERENCE FOR 1-DWAVE EQUATION65
let s =
c
2
t
2
x
2
Rearranging for U
k+1
j
gives:
U
k+1
j
=
1
1 + t
_
2(1 s)U
k
j
(1 t)U
k1
j
+ s(U
k
j+1
+ U
k
j1
)
_
Special care is again needed to solve for U
1
j
which needs U
0
j
and the ghost
point, U
1
j
. To nd U
1
j
we use initial condition:
U
t
(x, t = 0) =
U
0
j
t
= 0 =
U
1
j
U
1
j
2t
or U
1
j
= U
1
j
(since U
t
(x, 0) = 0)
We evaluate U
1
j
:
U
1
j
=
1
1 + t
_

_
2(1 s)U
0
j
(1 t) U
1
j
..
=U
1
j
+s(U
0
j+1
U
0
j1
)
_

2
1 + t
U
1
j
=
1
1 + t
_
2(1 s)U
0
j
+ s(U
0
j+1
U
0
j1
)
_
U
1
j
=
1
2
_
2(1 s)U
0
j
+ s(U
0
j+1
U
0
j1
)
_
Example n = 3
x
0
= 0
x
1
x
2
x
3
x
4
= a
U
k
0
= 0 = U(0, t), U
k
n+1
= U
k
4
= U(a, t)
Again we solve for time step k = 1,

U
1
rst:

U
1
=
_
_
_
U
1
1
U
1
2
U
1
3
_
_
_ =
1
2
_
_
_
2(1 s) s 0
s 2(1 s) s
0 s 2(1 s)
_
_
_
_
_
_
U
0
1
U
0
2
U
0
3
_
_
_ +
s
2
_
_
_
U
0
0
0
U
0
4
_
_
_
and the solution for time steps, k 1,

U
k+1
are given by:
66CHAPTER 8. NUMERICAL SOLUTIONOF 1-DAND2-DWAVE EQUATION

U
k+1
=
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
1
1 + t
_
_
_
2(1 s) s 0
s 2(1 s) s
0 s 2(1 s)
_
_
_
. .
A
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_
+
s
1 + t
_
_
_
U
k
0
0
U
k
4
_
_
_
. .
b

1 t
1 + t
. .
e
_
_
_
U
k1
1
U
k1
2
U
k1
3
_
_
_
= A

U
k
+

b e

U
k1
The numerical solution is plotted in gure 8.3 below.
0 5 10 15 20 25 30 35 40 45 50
0
50
100
150
200
250
Initial condition for plucked string
x
U
Figure 8.3: Initial conditions in (a) and matlab solution using explicit central
dierence method for 1D wave equation with friction in (b)
The matlab code can be downloaded from here.
8.2 2-D Wave Equation
U
tt
= (U
xx
+ U
yy
), 0 x a, 0 y b, 0 t T
8.2. 2-D WAVE EQUATION 67
8.2.1 Example: vibrations of a thin elastic membrane
xed at its walls
We discretise in x and y-directions:
x
0
x
1
x
2
x
3
. . .
x
n
x
n+1
= a
y
0
y
1
y
2
y
3
.
.
.
y
p
y
p+1
= b
We discretise: t =
T
m
, x =
a
n+1
, y =
b
p+1
, t
k
= kt, x
i
= ix, y
j
=
jy0 k m, 0 i n + 1, 0 j p + 1, and let U
k
ij
= U(t
k
, x
i
, y
j
)
Suppose we solve for n = 3 and p = 3 and have Dirichlet boundary conditions:
U(0, y, t) = 0 = U
k
oj
, U(a, y, t) = 0 = U
k
n+1,j
= U
k
4j
, U(x, 0, t) = 0 =
U
k
i0
, U(x, b, t) = 0 = U
k
i,p+1
= U
k
i4
and initial conditions:
U(x, y, 0) = f(x, y) = f
ij
U
t
(x, y, 0) = g(x, y) = g
ij
.
Since we have Dirichlet boundary conditions: the outer boundaries of the
region we are solving for are known: U
k
0,j
, U
k
n+1,j
, U
k
i,0
, U
k
i,p+1
, and we need to
nd the interior values: U
k
i,j
for 1 i n and 1 j p.
68CHAPTER 8. NUMERICAL SOLUTIONOF 1-DAND2-DWAVE EQUATION
x
0
x
1
x
2
x
3
x
4
= x
n+1
= a
y
0
y
1
y
2
y
3
y
4
= y
p+1
= b



U
11
U
21
U
31
U
12
U
22
U
32
U
13
U
23
U
33
We will use the 2-D Central Dierence Method
U
tt
=
U
k+1
ij
2U
k
ij
+ U
k1
ij
t
2
,
U
xx
=
U
k
i+!,j
2U
k
ij
+ U
k
i1,j
x
2
,
U
yy
=
U
k
i,j+1
2U
k
ij
+ U
k
i,j+1
y
2
We let s
x
=
t
2
x
2
, s
y
=
t
2
y
2
and substitute the central dierence approxima-
tions into our PDE, U
tt
= (U
xx
+ U
yy
) we solve for U
k+1
ij
:
U
k+1
ij
= 2U
k
ij
(1 s
x
s
y
) U
k1
ij
+ s
x
(U
k
i+1,j
+ U
k
i1,j
) + s
y
(U
k
i,j+1
+ U
k
i,j1
)
computing

U
k+1
uses the solution at

U
k
and

U
k1
.
For rst time step U
1
ij
needs U
0
ij
and U
1
ij
. Again we need to use the initial
conditions to nd the ghost point, U
1
ij
:
U
0
ij
t
= U
t
(x, y, 0) =
U
1
ij
U
1
ij
2t
= g(x
i
, y
j
) = g
ij
U
1
ij
= U
1
ij
2tg
ij
8.2. 2-D WAVE EQUATION 69
Solution at rst time step k = 1:
U
1
ij
= U
0
ij
(1 s
x
s
y
) + tg
ij
+
s
x
2
(U
0
i+1,j
+ U
0
i1,j
) +
s
y
2
(U
0
i,j+1
+ U
0
i,j1
)
If we let

U
k
=
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
U
k
11
U
k
12
U
k
13
U
k
21
U
k
22
U
k
23
U
k
31
U
k
32
U
k
33
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
then for time steps, k > 1, the solution is:
U
k+1
ij
= 2U
k
ij
(1 s
x
s
y
) U
k1
ij
+ s
x
(U
k
i+1,j
+ U
k
i1,j
) + s
y
(U
k
i,j+1
+ U
k
i,j1
)
and we can write this in vector form:

U
k+1
= A

U
k
+

b

U
k1
where A =:
_
_
_
_
_
_
2(1 s
x
s
y
) s
y
0 s
x
0 0 0 0 0
s
y
2(1 s
x
s
y
) s
y
0 s
x
0 0 0 0
0 s
y
2(1 s
x
s
y
) 0 0 s
x
0 0 0
s
x
0 0 2(1 s
x
s
y
) s
y
0 s
x
0 0
0 s
x
0 s
y
2(1 s
x
s
y
) s
y
0 s
x
0
0 0 s
x
0 s
y
2(1 s
x
s
y
) 0 0 s
x
0 0 0 s
x
0 0 2(1 s
x
s
y
) s
y
0
0 0 0 0 s
x
0 s
y
2(1 s
x
s
y
) s
y
0 0 0 0 0 s
x
0 s
y
2(1 s
x
s
y
b =
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
s
x
U
k
01
+ s
y
U
k
10
s
x
U
k
02
s
x
U
k
03
+ s
y
U
k
14
s
y
U
k
20
0
s
y
U
k
24
s
x
U
k
41
+ s
y
U
k
30
s
x
U
k
42
s
x
U
k
43
+ s
y
s
k
34
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
8.2.2 Examples of wave equation
1. Elastic wave propagation through rocks in 1-D

xx,x
= U
tt
(8.1)
70CHAPTER 8. NUMERICAL SOLUTIONOF 1-DAND2-DWAVE EQUATION
where

xx
= E
xx
,
xx
= stress,
xx
= strain
= E
U
x
8.1 EU
xx
= U
tt
or U
tt
=
E

U
xx
elastic waves propagate with speed
_
E

2. Electromagnetic Wave Equation


c
2

2
E =

E and c
2

2
B =

B (8.2)
From Maxwells equations where E is electric eld, B is magnetic eld.
Derived using:
E =

0
, E =
B
t
(8.3)
B = 0, B =
0

0
E
t
(8.4)
taking curl of 8.3 and 8.4 and using (

V ) = (

V )
2

V
and ( E) =
_

0
_
= 0, ( B) = 0 gives Equation 8.2 where
c =
_
1

0
= 3 10
8
m/s.
3. Schrodingers Wave Equation
i h

t
= H
for a wavefunction of a quantum system dened by Hamiltonian,
H.
eg.H = KE + PE =
h
2
2m
+ V (r)
numerical solutions also need to satisfy
_

|(x)|
2
dx
= 1
Chapter 9
Finite element method
9.1 An introduction to the Finite Element
Method
Finite dierence (FD) method is an approximation to the dierential
equation.
Finite element method (FEM) is an approximation to its solution.
FD methods are usually based on the assumption of regular domains
eg line in 1-D, rectangle in 2-D with regular elements
FEM is better for irregular regions as the domain can be partitioned
into any simple subregion such as triangles or rectangles in 2-D or
bricks and tetrahedra in 3-D. Figure 9.1 shows a nite element mesh
with triangles for an irregular domain.
Example: Solving Poissons equation in 1-D using FEM
U
xx
= q, 0 x L (9.1)
We consider Dirichlet boundary conditions: U(0) = U(L) = 0. A weak
solution of (9.1) considers the variational form of (9.1):
_
L
0
U
xx
(x)(x)dx +
_
L
0
q(x)(x) = 0, (9.2)
71
72 CHAPTER 9. FINITE ELEMENT METHOD
X
Y
Z
Figure 9.1: FEM mesh with triangles
where (x) satisfy the boundary conditions: (0) = (L) = 0.
We can integrate the rst term by parts:
_
L
0
U
xx
(x)(x)dx = U
x
(x)(x)]
x=L
x=0

_
L
0
U
x
(x)
x
(x)dx
=
_
L
0
U
x
(x)
x
(x)dx
using (0) = (L) = 0.
Then (9.2) becomes:
_
L
0
U
x
(x)
x
(x)dx =
_
L
0
q(x)(x)dx (9.3)
Equation (9.3) holds for all functions (x) which are piece-wise continous
and satisfy the bc: (0) = (L) = 0.
To solve equation (9.3) using the FEM we again introduce a mesh (as in FD)
on the interval [0, L] with mesh points x
j
= jx, j = 0, . . . , n + 1 where
x =
L
n+1
. To complete the discretisation we must choose a basis for (x).
The most common basis chosen for (x) are the hat functions,
j
(x).
We solve (9.3) using these:
(x) =
n

j=1
a
j

j
(x)
9.1. AN INTRODUCTION TO THE FINITE ELEMENT METHOD 73
where

j
(x) =
_

_
0, for 0 x x
j1
1
x
(x x
j1
), for x
j1
x x
j
1
1
x
(x x
j
), for x
j
x x
j+1
0, for x x
j+1
`

x
j1
x
j
x
j+1

j
(x)
`
`
`
`
`
`
`
`
`
1

x
with this construction:
j
(x
i
) =
ij
and:

j
(x) =

j
x
=
_

_
0, for 0 < x < x
j1
1
x
, for x
j1
< x < x
j

1
x
, for x
j
< x < x
j+1
0, for x > x
j+1
We let (x) =

n
j=1
a
j

j
(x) and (x
i
) = a
i
for i = 1, . . . , n, and (0) =

1
(0) = 0 and (L) =
n
(L) = 0 so that (x) satises boundary conditions.
The hat functions are advantageous as a basis as they are nearly orthonor-
mal, ie.
_
L
0

j
(x)
k
(x)dx = 0 when |j k| > 1.
Using FEM we seek an approximate solution to (9.3) which is satised for
all basis functions,
i
(x), for i = 1, . . . , n:
_
L
0
U
x
(x)
x
(x)dx =
_
L
0
q(x)(x)dx
and require that 9.3 be satised for =
i
, i = 1, . . . , n. We also expand
the solution U(x) using the hat functions
i
as a basis:
U(x) U
h
(x) =
n

j=1
b
j

j
(x)
74 CHAPTER 9. FINITE ELEMENT METHOD
This simplies equation 9.3 and we solve for =
i
, i = 1, . . . , n:
ie.
_
L
0
U

h
(x)

i
(x)dx =
_
L
0
q(x)
i
(x)dx, for i = 1, . . . , n
where f

(x) =
f
x
.
LHS =
_
L
0
U

h
(x)

i
(x)dx
=
_
L
0
n

j=1
b
j

j
(x)

i
(x)dx
=
n

j=1
C
i,j
b
j
where C
i,j
=
_
L
0

j
(x)

i
(x)dx. C
i,j
is known as the stiness matrix in me-
chanics.
To nd the coecients b
j
which dene our solution U(x) we must solve n
linear equations:
LHS =
n

j=1
C
i,j
b
j
= RHS =
_
L
0
q(x)
i
(x)dx = q
i
(9.4)
for i = 1, . . . , n with q
i
=
_
L
0
q(x)
i
(x)dx).
We approximate the solution by expanding in the basis of hat functions:
U(x)

n
j=1
b
j

j
(x). Thus we only need to know the coecients b
j
to
dene our solution U(x) and FEM solves the following equation for vector

b = (b
1
, . . . , b
n
):
n

j=1
b
j
_
L
0

j,x
(x)
i,x
(x)dx =
_
L
0
q(x)
i
(x)dx,
or
n

j=1
b
j
C
i,j
= q
i
for i = 1, . . . , n.
We will show that the stiness matrix C is tridiagonal for this example. We
are solving the above system for coecients b
j
, thus we are solving C

b = q
and can use iterative methods in FEM solutions too.
9.2. COMPARINGFEMSOLUTIONTOFDSOLUTIONFOR OUR EXAMPLE75
We can show that the stiness matrix is tridiagonal:
C
ij
=
_
L
0

j,x
(x)
i,x
(x)dx =
_

_
1
x
, i = j 1
2
x
, i = j
1
x
, i = j + 1
0, elsewhere
We approximate q
i
using:
q
i
=
_
L
0
q(x)
i
(x)dx q(x
i
)
_
L
0

i
(x)dx
= q(x
i
)
_
_
x
j
x
j1
1
x
(x x
j1
)dx +
_
x
j+1
x
j
1
1
x
(x x
j
)dx
_
= xq(x
i
)
We can substitute the above simplications into equation 9.4 and arrive at
C

b = xq or
1
x
C

b = q:
_
_
_
_
_
_
_
_
2
x
2
1
x
2
0 . . .
1
x
2
2
x
2
1
x
2
.
.
.
0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
x
2
2
x
2
_
_
_
_
_
_
_
_
_
_
_
_
_
_
b
1
b
2
.
.
.
b
n
_
_
_
_
_
_
=
_
_
_
_
_
_
q
1
q
2
.
.
.
q
n
_
_
_
_
_
_
Thus the matrix system to solve is the same as FD solution in this example
and the solution involves inverting the stiness matrix C:

b = C
1
xq
Iterative methods are useful in FEM too as it involves inverting large, sparse
matrices.
Once

b is known, the solution U to the PDE is given by:


U(x)
n

j=1
b
j

j
(x)
This is a weak solution of the PDE U
xx
= q.
9.2 Comparing FEM solution to FD solution
for our example
U
xx
= q, 0 x L, U(0) = U(L) = 0
76 CHAPTER 9. FINITE ELEMENT METHOD
9.2.1 FD solution
Discretise using x
j
= jx, j = 0, 1, . . . , n + 1 where x =
L
n+1
, U
0
= 0 =
U
n+1
(using boundary conditions).
We let U(x
j
) = U
j
, q(x
j
) = q
j
. The central dierence approximation to the
PDE is:
U
xx
=
U
j+1
2U
j
+ U
j1
x
2
and U
xx
= q becomes

U
j+1
+ 2U
j
U
j1
x
2
= q
j
We solve for U
1
, . . . , n since U
0
and U
n+1
given from boundary conditions
and we can rewrite in matrix form:
_
_
_
_
_
_
_
_
2
x
2
1
x
2
0 . . .
1
x
2
2
x
2
1
x
2
.
.
.
0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
x
2
2
x
2
_
_
_
_
_
_
_
_
. .
same coecient matrix for FD as FEM
_
_
_
_
_
_
U
1
U
2
.
.
.
U
n
_
_
_
_
_
_
+
_
_
_
_
_
_
_

U
0
(=0)
x
2
0
.
.
.

U
n+1
=0
x
2
_
_
_
_
_
_
_
=
_
_
_
_
_
_
q
1
q
2
.
.
.
q
n
_
_
_
_
_
_
In this example FEM and FD methods solve the same matrix system.
9.3 2-D Finite Element Method
We consider a triangular mesh (could also be rectangular) shown in gure
9.2 where G is the domain inside the circle and G is the domains boundary.
We are solving:

2
U = q in G (9.5)
with boundary conditions, U = 0 on G.
The weak solution for U satises the variational form for Equation 9.5:

_ _
G

2
U(x, y)(x, y)dxdy =
_ _
G
q(x, y)(x, y)dxdy (9.6)
9.3. 2-D FINITE ELEMENT METHOD 77
X
Y
Z
Figure 9.2: FEM mesh with triangles
where (x, y) = 0 on G (satises boundary conditions).
Using Greens rst identity:
_ _
G
(
2
U)dxdy =
_
G
(U n)dS
. .
=0 since =0 on G

_ _
G
( U)dxdy
=
_ _
G
( U)dxdy
Thus equation 9.6 becomes:
_ _
G
( U)dxdy =
_ _
G
qdxdy (9.7)
Which holds G where = 0 on G.
Similarly to the 1-D case we seek an approximate solution to equation 9.7
by expanding U(x, y) in a basis of 2-D hat functions:
U(x, y) U
h
(x, y) =
n

j=1
b
j

j
(x, y)
where U
h
(x
i
, y
i
) = b
i
and U
h
= 0 on G.
78 CHAPTER 9. FINITE ELEMENT METHOD
9.3.1 2-D hat functions
Figure 9.3: 2D hat function (
j
(x
j
, y
j
) = 1,
j
(x
i
, y
l
) = 0 if i = j and j =
l)
The 2-D hat functions satisfy
j
(x
j
, y
j
) = 1,
j
(x
i
, y
l
) = 0 if i = j and j =
l at all other vertices. The 2D hat function is plotted in gure 9.3.
We require that equation 9.7 holds for all (x, y) and solve for =
1
,
2
, . . . ,
n
:
_ _
G
U
h

i
dxdy =
_ _
G
q
i
dxdy, for i = 1, . . . , n (9.8)
LHS =
_ _
G
U
h

i
dxdy =
n

j=1
C
i,j
b
j
where C
i,j
=
_ _
G

j

i
dxdy is called the stiness matrix.
Equation 9.8 becomes:
n

j=1
C
i,j
b
j
= q
i
, for i = 1, . . . , n where q
i
=
_ _
G
q
i
dxdy.
If C is symmetric and positive denite then the system has a unique solution.
9.3.2 Example: 2-D Finite Element Method using eS-
cript for elastic wave propagation from a point
source.
eScript is a general linear solver written in python
(see http://www.uq.edu.au/esscc/escript-finley)
9.3. 2-D FINITE ELEMENT METHOD 79
eScript can be applied to any problem of the form:
(A
jl
a
,l
+ B
j
a)
,j
+ C
l
a
,l
+ Da = X
j,j
+ Y
where a is the scalar we are solving for in this example.
(eScript can also solve for a vector a)
We are using Einstein notation and according to this convention if an index
appears twice in a single term it implies we are summing over all possible
values:
a
i
f
,ii
= a
i

2
f
i
x
2
i
= a
i

2
f
1
x
2
1
+ a
2

2
f
2
x
2
We will see that the FEM takes care of spatial derivative in the problem
below. However we still need to approximate time derivatives.
We want to solve the 2-D wave equation for a point source:

tt
= V
2
p
(
xx
+
yy
) + F
PS
where p is the wave speed, is the wave-eld and F
PS
is the force due to
the point source.
In eScript this becomes:
D
a
= X
j,j
+ Y
where a =
tt
D = 1
X = V
2
p

,j
Y = F
PS
We solve for a
k
at each time step t
k
. Once a
k
is known we use it to calculate
the solution at the next time step,
k+1
using the central dierence formula:
a
k
=

2

k
t
2


k+1
2
k
+
k1
t
2
or

k+1
= 2
k

k1
t
2
a
k
The eScript python code can be downloaded from here and the output from
this code is shown in gure 9.4.
80 CHAPTER 9. FINITE ELEMENT METHOD
Figure 9.4: Plot of Euclidean normal of the displacement at t > 0 for a point
source using eScript.
Chapter 10
Spectral methods
10.1 An introduction to spectral methods
remove spurious dispersion and are highly accurate
exponential convergence for smooth functions (smooth functions have
rapidly decaying Fourier transforms)
usually involve calling a Fast Fourier Transform (t) subroutine.
good for smooth solutions.
Like the FEM, the spectral method also approximates the solution
U(x).
FEM approximates the solution as a linear combination of piece-wise
functions that are non-zero only on small subdomains (hat functions)-
local approach.
spectral methods approximate the solution as a linear combination of
continuous functions that are generally nonzero throughout the domain
(usually sinusoids or Chebychev polynomials)- global approach.
We will show an example using the spectral method where U(x) is expanded
as a Fourier series and this series and its spatial derivatives are then substi-
tuted into the PDE resulting in a system of ODEs in time.
81
82 CHAPTER 10. SPECTRAL METHODS
10.1.1 Example 1: Comparing the accuracy of solu-
tions of a variable speed wave equation with
either the spectral or nite dierence method
Spectral method for variable speed wave equation
In this example we will compare the accuracy of either the spectral or nite
dierence method when solving the 1D advection equation with a variable
wave speed, c(x) =
1
5
+ sin
2
(x 1). First we will derive the solution using
the spectral method:
U
t
+ c(x)U(x) = 0, 0 x 2 and 0 t 9
U(x, 0) = exp(100(x 1)
2
)
c(x) =
1
5
+ sin
2
(x 1)
U(0, t) = U(2, t) periodic boundary condition
The matlab code can be downloaded from here.
Again we discretise in space and time: x =
2
2n
=

n
, t =
T
m
x
j
= jx, j = 0, 1, 2, . . . , 2n 1
t
k
= kt, k = 0, 1, 2, . . . , m
U(x
j
, t
k
) = U
k
j
The spectral method uses the discrete Fourier transform of U(x
j
, t):

= F(U),
=
2n1

j=0
U(x
j
, t) exp(ix
j
)
=
2n1

j=0
U(x
j
, t) exp(i2j/(2n)), using x
j
= jx = j2/2n
=
2n1

j=0
U(x
j
, t) exp(ij/n)
for = n + 1, . . . , n.
U(x
j
, t) is then dened as the inverse discrete Fourier transform of

U

:
U(x
j
, t) = U
j
= F
1
(

U),
=
1
2n
n

=n+1

exp(ix
j
)
=
1
2n
n

=n+1

exp(i2j/(2n)
10.1. AN INTRODUCTION TO SPECTRAL METHODS 83
where j = 0, . . . , 2n 1.
With this denition the spatial derivatives are:
U(x
j
, t)
x
=
1
2n
n

=n+1
i

U

exp(ix
j
)
= F
1
(i

U)
= F
1
(iF(U))
We solve the advective equation with variable wave speeds and compare the
solution with either FD or spectral method:
U
t
+ c(x)U
x
= 0
where c(x) =
1
5
+ sin
2
(x 1)
ic: U(x, 0) = exp(100(x 1)
2
)
periodic bc: U(0, t) = U(, t)
The central dierence approximation is used for U
t
and spectral method for
U
x
:
U
k+1
j
U
k1
j
2t
. .
leap-frog for U
t
+ c(x
j
) F
1
(iF(U
k
j
))
. .
spectral method for U
x
= 0
or U
k+1
j
= U
k1
j
+ 2tc(x
j
)F
1
(iF(U
k
j
))
For U
1
j
need U
0
j
and U
1
j
U(x, 0) = U
0
j
= exp(100(x 1)
2
)
since c(x)
1
5
we can assume a constant wave speed of 1/5 to calculate
U
1
j
at t = t.
U
1
j
= U(x, t) = U(x + c(x)t) U
0
(x +
1
5
t) = exp(100(x +
t
5
1)
2
)
The matlab code can be downloaded from here.
84 CHAPTER 10. SPECTRAL METHODS
Comparing accuracy of solution with spectral method vs. nite
dierence method
Solve again using nite dierence.
U
t
+ c(x)U
x
= 0
This matlab code can be downloaded from here.
The Lax method is used for U
t
and central dierence method for U
x
:
U
t
=
U
k+1
j

1
2
(U
k
j1
+ U
k
j+1
)
t
U
x
=
U
k
j+1
U
k
j1
2x
c(x
j
) = c
j
.
Plug the formulas into the PDE:
U
k+1
j

1
2
(U
k
j1
+ U
k
j+1
)
t
. .
U
t
+c(x
j
)
U
k
j+1
U
k
j1
2x
. .
U
x
= 0
or U
k+1
j
=
1
2
(1 + sc
j
)U
k
j1
+
1
2
(1 sc
j
)U
k
j+1
where s = t/x. Using 4 elements:
x
0
x
1
x
2
x
3
x
4
U
k
0
= 0 = U
k
4
given by boundary conditions
U
0
j
= U(x, 0) given by initial conditions

U
k+1
=
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
1
2
_
_
_
0 1 sc
1
0
1 + sc
2
0 1 sc
2
0 1 + sc
3
0
_
_
_
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_ +
1
2
_
_
_
(1 + sc
1
)U
k
0
0
(1 sc
3
)U
k
4
_
_
_
or

U
k+1
= A

U
k
+

b
10.1. AN INTRODUCTION TO SPECTRAL METHODS 85
0 1 2 3 4 5 6 7
0
2
4
6
8
10
0.5
0
0.5
1
1.5
Variation of U with time
x
U
t
0 1 2 3 4 5 6 7
0
5
10
0
0.2
0.4
0.6
0.8
1
Variation of U with time
x
t
U
Figure 10.1: Numerical solution for 1D advection equation with initial condi-
tions of a smooth Gaussian pulse with variable wave speed using the spectral
method in (a) and nite dierence method in (b)
Figure 10.1(b) shows that the solution using nite dierences is much worse
than the spectral method because dispersion is introduced in FD method
when a variable wave speed is applied. However gure 10.1(a) shows the
spectral method performs well when smooth initial conditions of a Gaussian
pulse are used and there is very little dispersion present.
10.1.2 Example 2 Comparing spectral and nite dif-
ference methods with constant wave speed con-
ditions and initial conditions of a non-smooth
pulse
We solve the advective equation with constant wave speed (c = 1) with initial
conditions of a box pulse and compare the solution with either FD or spectral
method:
U
t
+ U
x
= 0
ic: U(x, 0) =
_
1, 0.5 x 1.
0, otherwise
periodic bc: U(0, t) = U(, t)
The matlab code for the spectral solution can be downloaded from here.
The matlab code for the FD solution can be downloaded from here. In
this code we have chosen the time step carefully so that no dispersion
is present for a constant wave speed of c = 1. Please see section 7.7.2
for a discussion on dispersion in nite dierence methods.
86 CHAPTER 10. SPECTRAL METHODS
0
1
2
3
4
5
6
7
0
1
2
3
4
5
0.5
0
0.5
1
1.5
Variation of U with time
x
t
U
0 1
2 3 4
5 6 7
0
1
2
3
4
5
0
0.2
0.4
0.6
0.8
1
Variation of U with time
x
t
U
Figure 10.2: Numerical solution for 1D advection equation with initial con-
ditions of a box pulse with a constant wave speed using the spectral method
in (a) and nite dierence method in (b)
Figure 10.2(a) shows that the solution with an initial condition which is not
smooth like the box pulse we used here using the spectral method is much
worse than the nite dierence method. This is because the spectral method
uses Fourier series to approximate the initial conditions and is unable to
approximate non-smooth initial conditions accurately. It is important to
note that for the numerical solution using the FD method in gure 10.2(b)
that we were able to remove dispersion in this example by carefully choosing
t = x/c (see section 7.7.2).
Part III
Nonlinear partial dierential
equations
87
Chapter 11
Shock wave
11.1 Analytical solution: Method of charac-
teristics
Analytical solution to the shock wave equation is given by the method of
characteristics. We will illustrate this method rst for a linear rst-order
PDE:
can be applied to rst order PDEs:
a(x, t)U
x
+ b(x, t)U
t
+ c(x, t)U = 0 (11.1)
with initial conditions U(x, 0) = f(x)
We change co-ordinates from (x, t) to (x
0
, s) so that our PDE 11.1 becomes
an ODE for certain characteristic curves in the x-t plane. The new variable,
s, will vary along the characteristic curves, whereas x
0
will remain constant.
How does it work?
We let:
dx
ds
= a(x, t),
dt
ds
= b(x, t)
Then
dU
ds
=
dx
ds
U
x
+
dt
ds
U
t
= aU
x
+ bU
t
(11.2)
We substitute 11.2 into 11.1:

dU
ds
+ c(x, t)U = 0
88
11.1. ANALYTICAL SOLUTION: METHOD OF CHARACTERISTICS89
This is an ODE along the characteristic curves satisfying the characteristic
equations:
dx
dt
= a(x, t) and
dt
ds
= b(x, t)
11.1.1 Example 1: Using method of characteristics to
solve the linear 1-D advection equation
U
t
+ cU
x
= 0
initial conditions: x(s = 0) = x
0
, t(s = 0) = 0
U(x, t = 0) = f(x) or U(s = 0) = f(x
0
)
dx
ds
= c x = cs + k
1
, use x(0) = x
0
= k
1
x = cs + x
0
dt
ds
= 1 t = s + k
2
, use t(0) = 0 = k
2
t = s
and since t = s x = ct + x
0
Solve for U:
dU
ds
=
dt
ds
U
t
+
dx
ds
U
x
= U
t
+ cU
x
= 0
ie
dU
ds
= 0 U = k
3
(U is constant along characteristic curves).
Use initial conditions U(s = 0, x
0
) = f(x
0
) = k
3
ie U = f(x
0
) = f(x ct) since x
0
= x ct.
This is the same as DAlemberts solution for a wave moving to the right at
speed c. The characteristic curves are given by: x = x
0
+ct or t =
1
c
(xx
0
).

slope (=
1
c
) and U are constant along curves
t
x
90 CHAPTER 11. SHOCK WAVE
11.1.2 Example 2: Using method of characteristics to
solve the nonlinear inviscid Burgers equation
Shock waves result when solving the nonlinear inviscid Burgers equation:
U
t
+ UU
x
= 0
initial conditions: x(s = 0) = x
0
, t(s = 0) = 0
U(x, t = 0) = f(x) or U(s = 0) = f(x
0
)
Now the wave speed is not constant but depends on the amplitude U(x, t).
The characteristic equations are:
dt
ds
= 1 t = s (using t(0) = 0)
dx
ds
= U x = Ut + x
0
(using x(0) = x
0
and t = s)
Again:
dU
ds
=
dt
ds
U
t
+
dx
ds
U
x
= U
t
+ UU
x
= 0
U = k
3
= f(x
0
) = f(x Ut)
So U = f(x Ut) is given implicitly since U is a function of itself. The
characteristic curves given by
t =
1
U
(x x
0
) =
1
f(x
0
)
(x x
0
)
The characteristic curves no longer have constant slope - they may cross
(meaning U is multiply dened shock waves) or be discontinuous (regions
with no solution for U expansion waves) as we will see in the next example.
Example
Solving U
t
+ UU
x
= 0 with the following initial conditions:
U(x, t = 0) = f(x) =
_
U
1
, x > 0
U
2
, x < 0
t =
_
1
U
1
(x x
0
), x > 0 or x = U
1
t + x
0
1
U
2
(x x
0
), x < 0 or x = U
2
t + x
0
11.1. ANALYTICAL SOLUTION: METHOD OF CHARACTERISTICS91
2 cases
U
1
< U
2
- compression wave shock
U
1
> U
2
- expansion wave rarefaction
Case 1: Shock wave U
1
< U
2

>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
t
x
x < 0
t =
1
U
2
(x x
0
)
x > 0
t =
1
U
1
(x x
0
)
x = U
1
t

x = U
2
t

In the fan bounded by x = U


1
t and x = U
2
t the characteristic curves are
multi-valued leading to shocks (breaking waves). We illustrate this below:

`
t = 0
U
x
U
2
U
1

t > 0
U
x
U
2
U
1

_
shock
centred compression wave
with overlap
92 CHAPTER 11. SHOCK WAVE
Case 2: Rarefaction or expansion wave U
1
> U
2

`
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>

t
x
x < 0
t =
1
U
2
(x x
0
)
x > 0
t =
1
U
1
(x x
0
)
no solution

x = U
1
t x = U
2
t

The solution is single-valued for t > 0 unlike the shock wave case. However
in wedge between x = U
2
t and x = U
1
t there is no information. We assume
x = U
t
in wedge since U
2
t x U
1
t and speeds vary U
2
U U
1
and add
solution to the wedge.
Adding solution to wedge:

`
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>
>
>
>
>
>
>
>
>
>
>
>
>>

t
x

x = Ut

x = U
1
t x = U
2
t

Thus U =
_

_
U
2
,
x
t
< U
2
x
t
, U
2
<
x
t
< U
1
U
1
,
x
t
> U
1
11.2. NUMERICAL SOLUTIONFOR NONLINEAR BURGERS EQUATION93

`
t = 0
U
x
U
2
U
1

t > 0
U
x
U
2
U
1

_
rarefaction
centred expansion wave
11.2 Numerical Solution for nonlinear Burgers
Equation
U
t
+ UU
x
= 0, 0 x 1, 0 t 1
U(x, 0) = f(x) = exp(10(4x 1)
2
)
Solution given implicitly by U(x, t) = f(x Ut) so speed depends on ampli-
tude, U.
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
0 0.1 0.2 0.3 0.4 0.5
U
x
rarefaction shock
t=0
t>0
Figure 11.1: The analytical solution U(x, t) = f(x Ut) is plotted to show
how shock and rarefaction develop for this example
We study the numerical solution using 3 methods but we will see in each
case that the numerical solution fails to produce a shock wave because we
are unable to produce multi-valued solutions.
94 CHAPTER 11. SHOCK WAVE
11.2.1 Example I: Finite dierence solution with Lax
Method
The matlab code can be downloaded from here. We are solving:
U
t
+ UU
x
= 0
U
k
j
t
=
U
k+1
j

1
2
(U
k
j1
+ U
k
j+1
)
t
(Lax method for U
t
)
U
k
j
x
=
U
k
j+1
U
k
j1
2x
(leap-frog for U
x
)
The Courant condition only holds for linear wave equation. A good guess is
t <<
x
max(U)
.(Waves travel at a maximum wave speed U = 1.)
Put dierence equations into PDE:
U
t
+ UU
x
= 0 becomes:
U
k+1
j
=
1
2
_
U
k
j+1
(1 sU
k
j
) + U
k
j1
(1 + sU
k
j
)
_
(11.3)
Where s =
t
x
Use boundary conditions U(0, t) = U(1, t) = 0 and for 4 elements:
x
0
x
1
x
2
x
3
x
4
So U
k
0
= 0 = U
k
4
given by boundary conditions and we can rewrite Equation
11.3 as a matrix system of equations:

U
k+1
=
_
_
_
U
k+1
1
U
k+1
2
U
k+1
3
_
_
_ =
1
2
_
_
_
0 1 sU
k
1
0
1 + sU
k
2
0 1 sU
k
2
0 1 + sU
k
3
0
_
_
_
_
_
_
U
k
1
U
k
2
U
k
3
_
_
_
+
1
2
_
_
_
(1 + sU
k
1
)U
k
0
0
(1 sU
k
3
)U
k
4
_
_
_
=
1
2
A

U
k
+
1
2

b
A varies with time because of U
k
j
term in matrix!
11.2. NUMERICAL SOLUTIONFOR NONLINEAR BURGERS EQUATION95
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.2
0.4
0.6
0.8
1
Initial condition for U
x
U
Figure 11.2: Initial conditions in (a) and solution for nonlinear Bugers equa-
tion using the Lax method in (b)
We can compare the dierence between the matlab code for the linear 1D advection equation
(U
t
+ U
x
= 0) in section 7.7.2 and the shock wave equation (U
t
+ UU
x
= 0)
above.
When we compare the analytical solution given by the method of character-
istics to the numerical solution given by the Lax method we can see that the
numerical solution is accurate for the linear 1D advection equation (see nu-
merical solution in gure 7.1) but fails to give a shock wave for the nonlinear
Burgers equation in gure 11.2. The Lax method introduces dispersion into
the numerical solution and in the nonlinear case this removes the shock
wave instability and attens the wave front.
11.2.2 Example II: Solution using Method of Lines
The matlab code can be downloaded from here and here.
U
t
= UU
x
Using the method of lines solution (as demonstrated in section 2.3) we only
replace spatial derivative U
x
with FD approximation.
U
j
x
=
U
j+1
U
j1
2x
,
U
j
t
= U
j
_
U
j+1
U
j1
2x
_
and again we show the case with 4 elements:
96 CHAPTER 11. SHOCK WAVE
x
0
x
1
x
2
x
3
x
4
U
0
= 0 = U
4
from boundary conditions.

U
t
=
_
_
_

U
1

U
2

U
3
_
_
_ =
1
2x
_
_
_
0 U
1
0
U
2
0 U
2
0 U
3
0
_
_
_
_
_
_
U
1
U
2
U
3
_
_
_ +
1
2x
_
_
_
U
1
U
0
0
U
3
U
4
_
_
_
or

U =
1
2x
A(U)

U +

b
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Initial condition for U
x
U
Figure 11.3: Initial conditions in (a) and solution for nonlinear Bugers equa-
tion using the method of lines in (b)
When the shock develops in gure 11.3(b) the numerical solution becomes
unstable using the method of lines.
11.2.3 Example III: Solution using Spectral Method
The matlab code can be downloaded from here.
U
t
= UU
x
, 0 x 2
(we can change variable: =
x
2
later so that the range for is 0 1)
11.2. NUMERICAL SOLUTIONFOR NONLINEAR BURGERS EQUATION97
Spectral method
We let U(x
j
, t
k
) = U
k
j
, x
j
= jx, j = 0, 1, . . . , 2n 1, t
k
= kt, k =
0, 1, . . . , m, and t =
T
m
.
Take the discrete Fourier transform of U:

= F(U) =
2n1

j=0
U(x
j
, t) exp(ix
j
) for = n + 1, . . . , n
where x
j
= jx =
j
n
then
U
j
= F
1
(

U) =
1
2n
n

=n+1

exp(ix
j
)
for j = 0, 1, . . . , 2n 1
and
U
k
j
x
=
1
2n
n

=n+1
i

U

exp(ix
j
)
= F
1
(i

U)
= F
1
(i F(U))
Use leap-frog for U
t
:
U
t
=
U
k+1
j
U
k1
j
2t
then U
t
= UU
x
becomes:
U
k+1
j
= U
k1
j
2tU
k
j
F
1
(i F(U
k
j
))
To nd U
1
j
for spectral method we assume wave speed 1 and:
U
1
j
= U(x, t) = U
0
(x Ut) = f(x Ut)
f(x + t) = exp(10(4(x + t) 1)
2
)
Again as in the method of lines the numerical solution becomes unstable as
shock develops in gure 11.4 using the spectral method.
98 CHAPTER 11. SHOCK WAVE
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.2
0.4
0.6
0.8
1
Initial condition for U
x
U
0
0.2
0.4
0.6
0.8
1
0
0.02
0.04
0.06
0.08
0.1
0
0.2
0.4
0.6
0.8
1
x
Variation of U with time
t
U
Figure 11.4: Initial conditions in (a) and solution for nonlinear Bugers equa-
tion using the spectral method in (b)
Chapter 12
Korteweg-de Vries Equation
12.1 Solitons
solitons or solitary waves result from solution of the KdV equation
KdV equation is a model for shallow water waves:
U
t
+ UU
x
+ U
xxx
= 0 nonlinear PDE
analytical solutions exist
solitons move in isolation and propagate without changing form. Ve-
locity is amplitude dependent (linearly proportional to maximum am-
plitude).
the nonlinear term causes waves to steepen (UU
x
the dispersive term causes waves to disperse (U
xxx
)
these eects are in exact balance for solitons waveform maintains
its size, shape and speed as it travels.
solitons pass through each other without change of form except shifted.
12.2 Analytical solution
U
t
+ UU
x
+ U
xxx
= 0
99
100 CHAPTER 12. KORTEWEG-DE VRIES EQUATION
Let U = f() = f(x V t) where = x V t then:

t
=

t

= V

,

x
=

x

Let f

() =
df

then U
t
+ UU
x
+ U
xxx
= 0 becomes: (using U = f() =
f(x V t))
V f

+ ff

+ f

= 0
We integrate once: (use ff

=
d
d
(
f
2
2
))
V
_
f

d +
_
d
d
(
f
2
2
)d +
_
f

d = 0
V f +
f
2
2
+ f

= C (12.1)
Multiply by f

and integrate again:

_
V ff

d +
_
f

f
2
2
d +
_
f

d =
_
Cf

d + c
0
Term 1 =
_
V ff

d = V f
2

_
V ff

d
_
V ff

d =
V
2
f
2
Term 2 =
_
f

f
2
2
d =
f
3
2

_
f
2
f

d
_
f

f
2
2
d =
f
3
6
Term 3 =
_
f

d = (f

)
2

_
f

d
_
f

d =
f
2
2

So multiplying 12.1 by f

and integrating again gives:


V
2
f
2
+
f
3
6
+
f
2
2
= Cf + C
0
(12.2)
We assume boundary conditions f() 0, f

() 0 as .
So Equation 12.2C
0
= 0 and Equation 12.1C = 0.
12.2. ANALYTICAL SOLUTION 101
0
10
20
30
40
50
60
70
80
90
100
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
f
(
x
)
x
Figure 12.1: The initial conditions U(x, 0) = f(x) = Asech
2
(
_
A
12
x).
We assume initial conditions for soliton:
U(x, 0) = f(x) = Asech
2
(

A
12
x)
So f(0) = A and f

(0) = 0 and f

() 0 for 0.
Rearranging Equation 12.2 gives
f
2
= V f
2

f
3
3
(12.3)
Use
f(0) = A, f

(0) = 0 0 = A
2
(V
A
3
)
V =
A
3
Since f

() 0 for 0 we take the negative square root of 12.3:


f

=
f

3
_
A f
This can be integrated analytically by making a change of variable if we let
f = Asech
2
then df = 2Asech
2
tanh d and integrating:
f

=
f

3
_
A f
102 CHAPTER 12. KORTEWEG-DE VRIES EQUATION
Since we assumed 0 we integrate from = 0 in integration limits:

_

0
df
d
d
f

A f
=
1

3
_

0
d

3
=
_
f()
f(0)
df
f

A f
=
_
f
A
df
f

A f
now substitute f = Asech
2

=
_

0
2Asech
2
tanh d
Asech
2

_
A Asech
2

=
_

0
2 tanh d

A
_
1 sech
2

now use 1 sech


2
= tanh
2

_

0
2

A
d =
2

A
=

3
=

A
12

U(x, t) = f() = Asech


2

= Asech
2
_
_

A
12

_
_
U(x, t) = Asech
2
_
_

A
12
(x
A
3
t)
_
_
. .
soliton travelling to right
(using V =
A
3
)
where sechx =
1
coshx
and coshx =
1
2
(e
x
+ e
x
).
12.3 Numerical solution of KdV Equation
U
t
+ UU
x
+ U
xxx
= 0, 0 x 2
with periodic boundary conditions U(0) = U(2).
and initial conditions:
U(x, 0) = Asech
2
(

A
12
(x )), A = 100
12.3. NUMERICAL SOLUTION OF KDV EQUATION 103
The analytical solution is:
U(x, t) = Asech
2
_
_

A
12
(x
A
3
t)
_
_
If we apply the spectral method directly we nd that the linear term, U
xxx
,
involves high frequencies making the numerical solution unstable as we will
see in section 12.3.1. Section 12.3.2 shows how to modify this term to gain
stability using a modied spectral method.
12.3.1 Solving directly with Spectral Method
U
t
+ UU
x
+ U
xxx
= 0
The matlab code can be downloaded from here.
Take discrete Fourier transform of U:

= F(U) =
2n1

j=0
U(x
j
, t) exp(ix
j
), for = n + 1, . . . , n
and the inverse discrete Fourier transform of

U:
U
j
= F
1
(

U) =
1
2n
n

=n+1

exp(ix
j
), for j = 0, . . . , 2n 1
We calculate spatial derviatives using spectral method:
U
x
=
U
k
j
x
=
1
2n
n

=n+1

(i) exp(ix
j
)
= F
1
(i

U) = F
1
(iF(U))
and:
U
xxx
=

3
U
k
j
x
3
=
1
2n
n

=n+1

(i
3
) exp(ix
j
)
= F
1
(i
3

U) = F
1
(i
3
F(U))

At high wavenumbers, , this term causes instabilities in solution.


We use a leap-frog approximation for U
t
:
U
k
j
t
=
U
k+1
j
U
k1
j
2t
104 CHAPTER 12. KORTEWEG-DE VRIES EQUATION
Plug approximations into PDE: U
t
+ UU
x
+ U
xxx
= 0,
U
k+1
j
= U
k1
j
2t
_
U
k
j
F
1
(iF(U)) + F
1
(i
3
F(U))
_
solution blows up!
0 1 2 3 4 5 6 7
0
10
20
30
40
50
60
70
80
90
100
Initial condition for U
x
U
0
2
4
6
8
0
1
2
3
x 10
5
300
200
100
0
100
200
300
t
x
Variation of U with time
U
Figure 12.2: Initial conditions in (a) and solution for nonlinear KdV equation
using the direct spectral method in (b)
Figure 12.2 shows that the numerical solution for the KdV equation blows
up using a direct spectral method. In the next section we modify the U
xxx
term causing instabilities.
12.3.2 Modifying U
xxx
term causing instabilities in di-
rect spectral method
The matlab code can be downloaded from here.
The direct method solves:
U
k+1
j
= U
k1
j
+ 2t[U
k
j
F
1
(ivF(U)) + F
1
(iv
3
F(U))]

The last term approximating U


xxx
makes PDE very sti at high wavenumbers
To remove this instability for high wavenumbers we replace the last term
with:
sin(v
3
t) v
3
t + 0(t
3
) as t 0 this is satised
Using sin x = x
x
3
3!
+
x
5
5!
. . . and re-solve with this approximation:
U
k+1
j
= U
k1
j
+ 2tU
k
j
F
1
(ivF(U)) + 2F
1
(i sin(v
3
t)F(U))
12.3. NUMERICAL SOLUTION OF KDV EQUATION 105
This numerical solution is stable!
(See Fornberg and Whitham, Philos. Trans. Roy. Soc. London (1974))
Again use the same initial conditions:
U
1
j
= U(x
j
, t) = U
0
_
x +
A
3
t
_
= f
_
x +
A
3
t
_
= Asech
2
(

A
12
_
x +
A
3
t )
_
0 1 2 3 4 5 6 7
0
20
40
60
80
100
120
x
U


Final solution at t=one period
Initial Condition at t=0
0
1
2
3
4
5
6
7
8
0
0.05
0.1
0.15
0.2
0
20
40
60
80
100
x
Variation of U with time
t
U
Figure 12.3: Initial conditions and nal solution after one period in (a) and
solution for nonlinear KdV equation using a modied spectral method in (b)
Figure 12.3 shows that the numerical solution for the KdV equation is sta-
ble using a spectral method where we have modied the U
xxx
term causing
instabilities.
The method of integrating factors can also be used to remove the instability
due to U
xxx
term (see Trefethen).
12.3.3 Interacting Solitons
The matlab code can be downloaded from here.
When 2 solitons travelling at dierent speeds collide their waveform main-
tains same size, shape and speed but the smaller (and slower) soliton is
backward shifted and the taller (and faster) soliton is forward shifted.
106 CHAPTER 12. KORTEWEG-DE VRIES EQUATION
To show this feature of solitons we begin with intial conditions of two solitons
with speeds of V = 2A/3 and V = A/3:
U(x, 0) = f(x) = Asech
2
_
_

A
12
(x
3
2
)
_
_
+ 2Asech
2
_
_

2A
12
(x

2
)
_
_
where A = 100.
0
50
100
150
200
0 1 2 3 4 5 6
f
(
x
)
x
V=2A/3
V=A/3
Figure 12.4: The initial conditions U(x, 0) = f(x) is plotted to show the 2
solitions and their speeds
Figure 12.5 shows the numerical solution for the KdV equation for 2 interact-
ing solitons using the modied spectral method. In gure 12.5(a) we plot the
initial conditions and nal solution after one period. We see that after the
interaction the smaller soliton is backward shifted and taller soliton forward
shifted in time.
0 1 2 3 4 5 6 7
50
0
50
100
150
200
250
x
U


Final solution at t=one period
Initial Condition at t=0
0
2
4
6
8
0
0.05
0.1
0.15
0.2
50
0
50
100
150
200
250
x
Variation of U with time
t
U
Figure 12.5: Initial conditions and nal solution after one period in (a) and
solution for nonlinear KdV equation for two interacting solitons in (b)

You might also like