Finite Difference Methods Notes
Finite Difference Methods Notes
Equations II
Finite-Difference Methods
Dr Jon Shiach
2019 – 2020
2 Finite-Difference Approximations 11
2.1 The Taylor series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 “Big-oh” notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Deriving finite-difference approximations . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 The finite-difference toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.5 Example of a finite-difference scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
i
Contents
References 113
In part I of this unit you were introduced to methods used find the analytical solution to a PDE.
Unfortunately, for most practical applications attempting to find the analytical solution is either very
difficult or not possible so we need to use a numerical approach instead. There are several different
numerical methods that can be used to solve a PDE and in this unit we will be focusing on two of
the most common of these: finite-difference methods (taught in term 1) and finite-volume methods
(taught in term 2).
iii
Chapter 1
This chapter will introduce the concept of partial differential equations, the different types and the
general methodology used to solve partial differential equations using numerical methods.
1.1 Definitions
1.1.1 Partial derivatives
A partial derivative of a function of several variables is the derivative of that function with respect to
one of those variables, the other variables being treated as constants. The partial derivative is denoted
by the symbol ∂ instead of the ‘d’ that is used for the derivative of a single variable function. The
same rules for derivatives of single variable functions apply to partial derivatives.
For example, let U (x, y) = x2 + xy be a two-variable differential function then U can be differentiated
with respect to x and y, e.g.,
∂U
= 2x + y,
∂x
∂U
= x.
∂y
The partial derivatives are commonly denoted using subscripts, i.e.,
∂U
= Ux = ∂x U.
∂x
Higher order partial derivatives can be represented by repeating the variable in the subscript, i.e.,
∂2U
= Uxx = ∂xx U.
∂x2
Mixed partial derivatives can be formed by differentiating a derivative with respect to a different
variable, i.e.,
∂ ∂U ∂ ∂U ∂2U
= = = Uxy = Uyx = ∂xy U = ∂yx U.
∂x ∂y ∂y ∂x ∂x∂y
here U is an unknown function of the independent variables x and y and a, b, c, d, e and f are either
known functions of x and y or known constants.
1
Chapter 1. Partial Differential Equations
aUx + bUy = 0,
is a second-order PDE.
is a linear PDE.
1.1.5 Domain
The domain is defined as the set of all values of the independent variables for which there exists a
solution to a PDE. In most cases the domain can be split into the spatial domain which is the subset
of the domain for the spatial variables, x, y, z etc., and the time domain which is the subset of the
domain for the difference values of the time variable t. Often the spatial domain is simply referred to
as the domain even if time is also an independent variable of the PDE.
Example 1
Ut = kUxx , (1.1)
Solution: We need to show that the function in equation (1.2) satisfies the PDE in equa-
tion (1.1), the initial conditions and the boundary conditions. The PDE is expressed in terms
of the derivatives Ut and Uxx , evaluating these gives
πx
2kπ 2 kπ 2 t
Ut = − 2 sin exp − 2 ,
L L L
2
2π πx kπ 2 t
Uxx = − 2 sin exp − 2 .
L L L
since LHS = RHS then the function in equation (1.2) satisfies the PDE in equation (1.2). Now
consider the initial conditions
πx
kπ 2 (0)
U (t = 0, x) = 2 sin exp −
L L2
πx
= 2 sin exp(0)
L
πx
= 2 sin ,
L
therefore equation (1.2) satisfies the initial conditions. Finally consider the boundary conditions
π.0 kπ 2 t kπ 2 t
U (t, x = 0) = 2 sin exp − 2 = 2 sin(0) exp − 2 = 0,
L L L
π.L kπ 2 t kπ 2 t
U (t, x = L) = 2 sin exp − 2 = 2 sin(π) exp − 2 = 0,
L L L
which are both satisfied. Since equation (1.2) satisfies the PDE, the initial conditions and the
boundary conditions then we can say that this is a solution to the PDE in equation (1.1).
The terminology used to classify PDEs comes from that used to classify conic sections (Hoffman, 2001)
(figure 1.1). Conic sections are described by the equation
It should be obvious that there are similarities between the types of PDEs and the different conic
sections. However, there is no significance between the names of the types of PDEs and the behaviour
of their solutions, they are simply names given to the different types and do not share any properties
with the different conic sections.
Example 2
For each of the PDEs given below, classify them as either an elliptic, parabolic or hyperbolic
PDE.
(i) Uxx + Uyy = 0;
(ii) Ut = kUxx ;
(iii) Utt = c2 Uxx .
Solution:
(i) Comparing the PDE to equation (1.3) we have a = 1, b = 0 and c = 1 therefore
b2 − 4ac = 0 − 4 = −4 < 0,
b2 − 4ac = 0 + 0 = 0,
b2 − 4ac = 0 + c2 > 0
Uxx + Uyy = 0,
Ut = αUxx ,
where α is some heat diffusion coefficient that determines the conductivity of a material. Since t is an
independent variable the solution to this equation will evolve over time.
Utt = c2 Uxx ,
where c is the speed of sound and is used in the modelling of acoustic waves and the advection equation
Ut + vUx = 0,
where v is the advection velocity which is used to transport of a pollutant along a channel.
The differences between the three types of PDEs defined in section 1.2 can be illustrated by considering
what is known as the domain of dependence. Let U (t, x) be the solution of a PDE at a given point in
space x and timet.
The domain of dependence is defined by the set of all points in the domain that influences the
value of U (t, x).
The range of influence is defined as the set of all points in the domain for which U (t, x) is
a member of the domain of dependence, i.e., the set of all points that the value of U (t, x)
influences.
Consider an elliptic PDE where the spatial domain is defined in the xy-plane with a ≤ x ≤ b and
c ≤ y ≤ d (figure 1.2). As mentioned in section 1.2 elliptic PDEs have a steady state solution and do
not change over time. As such, for elliptic PDEs the domain of dependence is the same as the range
of influence and includes all points in the domain.
y boundary
range of influence
domain of dependence
c x
a b
boundary
Figure 1.2: Domain of dependence and range of influence for an elliptic PDE.
The solution to a parabolic PDE must satisfy the PDE, the boundary conditions and, because parabolic
PDEs include a time variable, the initial conditions for the PDE. Since parabolic PDEs model propa-
gation, the solution U (t, x) is dependent upon the solution at previous time values. Also, the solution
across the spatial domain depends upon all other points at the same time level. Therefore the domain
of dependence for a parabolic PDEs is bounded by the spatial boundaries x = a and x = b and the
initial conditions at t = tmin (figure 1.3). The range of influence is the set of all points at later time
levels up until t = tmax .
t
tmax
tmin x
a b
initial conditions
Figure 1.3: Domain of dependence and range of influence for a parabolic PDE.
Hyperbolic PDEs model propagation of a quantity across the spatial domain but unlike elliptic and
parabolic PDEs, the solution of U (t, x) is not influenced by all points in the spatial domain at once. For
example, if a disturbance occurs at a particular point, the effect of the disturbance will propagate away
from this point causing wave like structures. Only those points within the region of the propagating
waves after a certain amount of time will be affected by the disturbance. Therefore the domain of
dependence of a hyperbolic PDE is dependent upon the speed of propagation. Let c be the fastest
propagation speed permitted by the hyperbolic PDE, then the domain of dependence is bounded by
two lines called characteristics that extend back through time from U (t, x) to the initial conditions at
t = tmin . The range of influence is also bounded by the characteristics and extends forward in time
until t = tmax (figure 1.4).
t
tmax
range of
influence boundary
tn U (t, x)
x0 + ct x1 − ct
boundary domain of
dependence
tmin x
a x0 x1 b
initial conditions
Figure 1.4: Domain of dependence and range of influence for a hyperbolic PDE.
the dependent variables provide information of the state of the system. In almost all instances,
the PDEs that are used in modelling have already been derived previously and are then applied
to the specific case.
Mathematical modelling usually begins with a very simplified approach that provides a basic
model. This is then improved by including additional terms or equations that enable the model
to include other properties of the system. For example, the modelling of water waves may begin
with a model that can approximate the depth and velocity of the water over a flat bed surface,
this can then be improved by including terms that model bed topography, bed friction, wave
breaking etc.
2. Derive the numerical scheme – the numerical method chosen is based upon the PDE being
solved, the geometry of the physical domain, the accuracy required, the computational resources
available and the behaviour of the phenomena being modelled. There are several different
methods currently available to do this, examples include finite-difference methods, finite-volume
methods, finite-element methods, spectral methods and particle based methods. The complete
numerical model may use a single method or combination of two or more methods.
3. Write the program – a computer program is written to perform the calculations of the numer-
ical scheme. This can be time consuming and frustrating step. Do not expect to write a working
program straight away, even experienced computer programmers rarely write an error-free pro-
grams on the first attempt. The process of writing a program should be done in steps, once a
new part of the program is written it should be tested to see whether it performs as required.
Only when you are satisfied that the new part performs as required you should proceed to write
the next step in the program.
4. Verify the program – just because a program is working and produces an output does not
mean that the output is what the numerical scheme should produce. Minor errors called ‘bugs’
in the program will affect the numerical solution and render the model useless. To verify that
the program is performing as expected, the numerical solution is obtained using pen and paper
(know as performing a ‘hand calculation’) and compared to the results from the program. Since
performing the calculations by hand can be a time consuming process, the program is tested
using a small number of solution points. Care should be taken to ensure that every variable
and array calculated in the program matches that of the hand calculation. It is important that
you know what every line in the program does and understand the structure of the program.
It can be difficult to work with a program written by someone else but by stepping through
the program line-by-line will give you an understanding of what it does (this is when a well
commented program is useful).
5. Validate the model – once the program is performing as expected the next step is to validate
the model by comparing the output against real world data, either from controlled experiments
or real life measurements. Remember that a mathematical model is only an approximation of a
real world scenario and the assumptions and compromises made in developing the model may
mean that the model solutions may not exactly replicate the real world data. What is important
is that the behaviour of what is being modelled is replicated.
6. Present the model – the final step in the modelling process is to present the model, the results
and any conclusions that have been arrived at during the investigation. This usually takes the
form of a report or a paper published in a journal or conference proceedings. It is important
that the model is explained in enough detail so that the reader can reproduce the results for
themselves and apply the model to other cases. This step can be daunting when first encountered
and takes practice to get good at it. In this unit you will be asked to produce reports on work
in the computer labs as part of formative assessment in addition to your summative coursework
assessment. Guidance material on how to write mathematical reports is provided on Moodle.
Figure 1.5: An Eulerian method calculates the solution at points in the domain whose
positions are fixed.
Lagrangian methods calculate the solution at points that move around the domain based on the
flow conditions of the solution (figure 1.6). The values of the variables such as pressure, density,
temperature etc. are calculated at these points and the velocities are used to determine the position.
Examples of Lagrangian methods, which are also known as mesh free methods include Smoothed
Partial Hydrodynamics (SPH) and Moving Particle Semi-implicit (MPS) methods.
y Ω
Figure 1.6: An Lagrangian method calculates the solution at points in the domain whose
positions change depending on the flow conditions.
more complicated derivations and more computational resources. In practice we often reach a
compromise between the accuracy of our chosen method and the effort required to derive an
implement a more accurate method.
• The number of discrete points where we are calculating the solution. The more points we use
to calculate a solution the more accurate our solution will be. We are limited to the number of
points by the computational resources available.
• Computational rounding errors. There is a limit to the accuracy of a floating point operation
(i.e., operations involving decimals) performed by a computer. Computational rounding errors
can compound to affect the accuracy of a numerical solution.
Finite-Difference Approximations
Finite-difference methods are one of the simplest and most common methods used to solve PDEs.
Approximations of partial derivatives that use the difference of the function value at two or more
points are derived from the well known Taylor series. The derivatives in the PDE are replaced by these
difference approximations to form an algebraic system, the solution of which gives an approximation
to the solution of the PDE. This chapter will introduce finite-differences, show how to derive finite-
difference approximations and how we can use them to solve PDEs.
where f(n) (x) denotes the nth derivative of f (x) with respect to x. Writing out the first few terms
gives
Forward Taylor Series
h2 h3 h4
f (x + h) = f (x) + hfx (x) + fxx (x) + fxxx (x) + f(4) (x) + · · · (2.1)
2! 3! 4!
The quantity h is known as the step length since we are approximating the function f a small step
along the number line from x. As h is a positive quantity then x + h is said to be forward of x so
equation (2.1) is colloquially known as the forward Taylor series. We can also approximate f at x − h
simply by replacing h by −h in equation (2.1) to give
h2 h3 h4
f (x − h) = f (x) − hfx (x) + fxx (x) − fxxx (x) + f(4) (x) + · · · (2.2)
2! 3! 4!
Since x − h is a backwards step along the number line from x equation (2.2) is known as the backwards
Taylor series. Note how in the backwards Taylor series the odd order terms have negative coefficients
whilst the even order terms have positive coefficients, i.e.,
∞
X hn
f (x − h) = (−1)n f (x).
n! (n)
n=0
11
Chapter 2. Finite-Difference Approximations
In most cases we cannot calculate an exact value of the truncation error (if we could do that there
would be no need to truncate the Taylor series in the first place) but we can examine the behaviour
of the truncation error. Consider the truncation error for the first-order forward Taylor series
h2 h3
f (x + h) = f (x) + hfx (x) + fxx (x) + fxxx (x) + · · · .
| {z } |2! 3!
{z }
1st order approximation truncation error
Notice that the values of the denominators are factorials so the first term omitted will have a much
larger value than any of the other terms and is therefore the dominant component of the truncation
error. For example, consider the first-order Taylor series approximation of cos(0.1) using equation (2.3)
with x = 0 and h = 0.1
cos(0 + 0.1) ≈ cos(0) − 0.1 sin(0) = 1.
Since cos(0.1) = 0.995004 (correct to 6 decimal places) then our truncation error is |0.995004 − 1| =
0.004996. The absolute value of the lowest order omitted term is
2 2
h ∂ 0.12
2! ∂x2 cos(x) = − 2! cos(0) = 0.005,
so the error attributed to the other omitted terms is just |0.004996 − 0.005| = 4 × 10−6 .
The truncation error is dominated by lowest order term omitted we can use this to express the
truncation error as a polynomial function of h such that
E(h) only includes the largest term from those omitted in the truncation of the Taylor series the actual
truncation error will always be less than E(h). Therefore we can say that
“As h → 0 the truncation error will tend to zero at least as fast as E(h).”
The truncation errors for the first-order Taylor series approximation of cos(x + h) using values of
h = 0.5, 0.25, 0.125, 0.0625 and the function E(h) for a first-order approximation have been plotted in
figure 2.1. Note is that as the value of the step length h decreases, so does the truncation error. This
is an important result that applies to all numerical methods, i.e.,
“The smaller the step length, the more accurate the approximation.”
0.12
truncation error
E(h) = |− 12 h2 cos(1)|
0.1
truncation error
8 · 10−2
6 · 10−2
4 · 10−2
2 · 10−2
Figure 2.1: Plot of truncation errors for the first-order Taylor series expansion of cos(x + h)
for h = 0.5, 0.25, 0.125 and 0.0625 compared with E(h).
The truncation errors demonstrate quadratic behaviour as they tend to zero as h decreases. This is
because the dominant term in the truncation error is a quadratic polynomial in terms of h. Finally
the truncation errors are always less than the values of E(h). In most practical cases it will not be
possible to calculate the truncation error so we use E(h) as a worst possible scenario to examine the
behaviour of the truncation errors in numerical schemes.
Definition 3: O(hn )
Let f (h) be a function of h and define f (h) = O(hn ) such that the following is satisfied
f (h)
lim = C, (2.4)
h→0 hn
What this tells us is that since C > 0, the values of f (h) will always be less than that of Chn and “as
h → 0, f (h) → 0 at least as fast as hn → 0”. If this is true then we can write f (h) = O(hn ) and we say
that “f (h) is order h to the n”. For example, consider the function f (h) = 2h3 , using equation (2.4)
2h3
lim = C,
h→0 hn
and since C is a scalar quantity n must be 3 in order to cancel out the h in the numerator so we can
say that 2h3 = O(h3 ). Also consider g(h) = sin(h), using the series expansion of sin(h) we have
h3 h5 h7
sin(h) h− + + + ···
lim = lim 3! 5! 7! =C
h→0 hn h→0 hn
h3 h5 h7
∴h− + + + · · · ≈ Chn .
3! 5! 7!
h3 h5 h7
Since h − + + + · · · < h then n = 1 and we can say that sin(h) = O(h).
3! 5! 7!
h2 hn
f (x + h) = f (x) + hfx (x) + fxx (x) + · · · + f (x) + O(hn+1 ).
2! n! (n)
For example, a first-order Taylor series expansion, n = 1 and the truncation error is represented by
O(h2 ), i.e.,
f (x + h) = f (x) + hfx (x) + O(h2 ),
and similarly for a second-order Taylor series
h2
f (x + h) = f (x) + hfx (x) + fxx (x) + O(h3 ).
2
This means that for a first-order Taylor expansion, reducing the step length h by a factor of 21 will
result in the error being reduced by a factor of at least h2 = ( 21 )2 = 14 . For a second-order Taylor
series expansion, reducing h by the same factor will result in the the error being reduced by a factor
of at least h3 = ( 12 )3 = 81 .
kO(hn ) = O(hn ).
• When summing series expansions of different orders, the truncation error of the lower order
expansion dominates, i.e., if m > n
O(hn )
= O(hn−m ). (2.5)
hm
f (h) m
Proof. Assuming equation (2.5) is true, let = O(hn ) and using equation (2.4)
h
f (h)
m f (h) f (h)
lim hn−m
= lim n−m+m = lim n = C.
h→0 h h→0 h h→0 h
h
x
f (x − 2h) f (x − h) f (x) f (x + h) f (x + 2h)
Figure 2.2: Neighbouring values are used to approximate the derivative of f (x).
f (x + h) − f (x)
fx (x) = + O(h). (2.6)
h
Equation (2.6) is known as the first-order forward difference approximation of fx (x). Alternatively
we can also use the first-order backwards Taylor series
f (x − h) = f (x) − hfx (x) + O(h2 ),
and doing similar gives the first-order backward difference approximation of fx (x)
f (x) − f (x − h)
fx (x) = + O(h). (2.7)
h
which can be rearranged to give the second-order central difference approximation of fx (x).
f (x + h) − f (x − h)
fx (x) = + O(h2 ). (2.9)
2h
h2 h3
f (x + h) = f (x) + hfx (x) + fxx (x) + fxxx (x) + O(h4 ),
2 6
h2 h3
f (x − h) = f (x) − hfx (x) + fxx (x) − fxxx (x) + O(h4 ).
2 6
Here we want to approximation fxx (x) so we need to eliminate the fx (x) and fxxx (x) terms. Since
these have different signs in the forward and backward Taylor series expansions we can do this by
summing the two, i.e.,
f (x − h) − 2f (x) + f (x + h)
fxx (x) = + O(h2 ). (2.10)
h2
This is the second-order symmetric difference approximation of fxx (x). Note that when summing the
forward and backwards Taylor series expansion the odd order terms cancel out. This means that using
an odd order Taylor series expansion will result in an approximation that has an order of accuracy
one less than the order of expansion. For this reason, apart from the first-order approximations, the
order of finite-difference approximations increase as even numbers.
The forward, backward and central difference formulae have been used to approximate the slope of the
tangent to a function f (x) in figure 2.3. Note that the forward and backward differences give a poor
approximation of the tangent whereas the central difference is much more accurate. This is because
the central difference is second-order accurate whereas the forward and backward differences are only
first-order accurate.
f (x) f (x) f (x)
xi xi + h x xi − h xi x xi − h xi xi + h x
Figure 2.3: Difference approximations of the tangent slope of f (x) at x = xi (dashed line).
Example 3
Use first and second-order accurate finite-differences to approximate the first and second deriva-
tive of f (x) = cos(x) at x = π4 using a step length of h = 0.1. Calculate the absolute error
between your approximations and the exact value.
Solution: Using the first-order forward difference from equation (2.6) we have
π π
π cos + 0.1 − cos
fx ≈ 4 4 = −0.741255.
4 0.1
Using the first-order backward difference from equation (2.7) we have
π π
π cos( − cos − 0.1
fx ≈ 4 4 = −0.670603.
4 0.1
Using the second-order central difference formula from equation (2.9) we have
π π
π cos + 0.1 − cos − 0.1
fx ≈ 4 4 = −0.705929.
4 2(0.1)
The actual value is fx ( π4 ) = − sin( π4 ) = −0.707107 (correct to 6 decimal places) so the error
for the first-order forward approximation is 0.034148, the error for the first-order backward
approximation is 0.036504 and the error for the second-order approximation is 0.001178.
The second-order approximation of fxx (x) is (a first-order approximation does not exist)
f (x − h) − 2f (x) + f (x + h)
fxx (x) ≈ ,
h2
so we have
π π π
π cos − 0.1 − 2 cos + cos + 0.1
fxx ≈ 4 4 4 = −0.706518,
4 0.12
which when compared to the actual value of fxx ( π4 ) = − cos( π4 ) = −0.707107 (correct to 6
decimal places) gives an error of 0.000589.
which is a linear function. Therefore we can approximate n in O(hn ) by calculating the gradient of
log |E(h)| for a series approximation for the different values of h, i.e., if hmax and hmin are the largest
and smallest values of h for which the series approximation has been calculated then
For example consider the forward, backward and central difference approximations of the first deriva-
tive of f (x) = cos(x) at x = π4 using step lengths h = 0.1, 0.05, 0.25 and 0.0125 shown in table 2.1.
π
Table 2.1: Finite-difference approximations of the first derivative of f (x) = cos(x) at x = 4
using step lengths h = 0.1, 0.05, 0.25 and 0.0125.
The absolute errors between the finite-difference approximations and the exact value of − sin( π4 ) =
−0.707107 are given in table 2.2.
Table 2.2: Truncation errors for the Taylor series approximations of cos(1 + h) for h = 0.5,
0.25, 0.125 and 0.0625.
log(0.034148) − log(0.004401)
n≈ = 0.985305 ≈ 1,
log(0.1) − log(0.0125)
so we can say that the truncation error for the forward difference finite-difference approximation is
O(h). Doing similar for the backward and central difference approximations we get n ≈ 1.0134 and
n ≈ 1.9998 which suggests truncation errors of O(h) and O(h2 ) respectively.
A plot of the errors for the forward, backward and central difference approximations using a loglog
scale is shown in figure 2.4. By using a loglog scale we can easily compare the behaviour of the
errors as h decreases. Note that the forward and backward approximations have very similar errors
where as the central difference approximation is significantly more accurate. Furthermore the line
representing errors in the central difference approximation has a gradient of 2 and is steeper than
those of the forward and backward differences which have gradients of 1, this shows that as the step
length decreases, the central difference approximation will converge to the exact value at a faster rate
than the forward and backward difference approximations.
Figure 2.4: Plot of the error for the forward, backward and central difference approximations.
Let ∆x and ∆y be the step lengths for the x and y variables respectively and using a first-order
forwards approximation for fx (x, y) and fy (x, y)
f (x + ∆x, y) − f (x, y)
fx (x, y) = + O(∆x),
∆x
f (x, y + ∆y) − f (x, y)
fy (x, y) = + O(∆y),
2∆y
then
∂
fxy (x, y) = (fy (x, y))
∂x
fy (x + ∆x, y) − fy (x, y)
= + O(∆x)
∆x
f (x + ∆x, y + ∆y) − f (x + ∆x, y) f (x, y + ∆y) − f (x, y)
−
∆y ∆y
= + O(∆x, ∆y)
∆x
f (x + ∆x, y + ∆y) − f (x + ∆x, y) − f (x, y + ∆y) + f (x, y)
= + O(∆x, ∆y)
∆x∆y
where ci are the undetermined coefficients. Substituting in the second-order forwards and backwards
Taylor series in place of f (x + h) and f (x − h) and factorising gives
h2 h2
f(n) (x) = c1 f (x) − hfx (x) + fxx (x) + c2 f (x) + c3 f (x) + hfx (x) + fxx (x) + O(h3 )
2 2
1
= (c1 + c2 + c3 )f (x) + (−c1 + c3 )hfx (x) + (c1 + c3 )h2 fxx (x) + O(h3 ).
2
If we require a finite-difference approximation of fx (x) then we need the right-hand side to equal fx (x)
so the coefficients of f (x) and fxx (x) should be zero and the coefficient of fx (x) should be 1. This
leads to the following linear system of equations
c1 + c2 + c3 = 0,
1
−c1 + c3 = ,
h
c1 + c3 = 0.
1 1 O(h3 ) f (x + h) − f (x − h)
fx (x) = − f (x − h) + f (x + h) + = + O(h2 ),
2h 2h 2h 2h
which is the same as equation (2.9). Alternatively if we required a finite-difference approximation of
fxx (x) then the coefficients of f (x) and fx (x) need to be zero and the coefficient of fxx (x) should be
1, i.e.,
c1 + c2 + c3 = 0,
−c1 + c3 = 0,
2
c1 + c3 = 2 .
h
Solving gives c1 = 1
h2
, c2 = − h22 and c3 = 1
h2
so the second-order finite-difference approximation of
fxx (x) is
f (x − h) − 2f (x) + f (x + h)
fx (x) = + O(h2 ),
h2
which is the same as equation (2.10).
The method of undetermined coefficients also allows us to derive skewed or one-sided finite-difference
approximations which use more of the values of the neighbouring nodes to one side of f (x) than the
other. These can be useful for approximating derivatives close to the boundaries of the domain.
Example 4
Solution: A one-sided finite-difference approximation means that the other nodes used in the
approximation are only on one side of the f (x) node, e.g.,
Here we need the Taylor expansion of f (x + 2h) which is easily obtained by substituting 2h in
place of h in the second-order forward Taylor series
(2h)2
f (x + 2h) = f (x) + 2hfx (x) + fxx (x) + O(h3 )
2
= f (x) + 2hfx (x) + 2h2 fxx (x) + O(h3 ).
Substituting in the second-order Taylor series for f (x + h) and f (x + 2h) and factorising gives
h2
f(n) (x) = c1 f (x) + c2 f (x) + hfx (x) + fxx (x) + c3 f (x) + 2hfx (x) + 2h2 fxx (x)
2
+ O(h3 )
1
= (c1 + c2 + c3 )f (x) + (c2 + 2c3 )hfx (x) + c2 + 2c3 h2 fxx (x) + O(h3 )
2
c1 + c2 + c3 = 0,
1
c2 + 2c3 = ,
h
1
c2 + 2c3 = 0,
2
which can be written in matrix form as
1 1 1 c1 0
0 1 2 c2 = 1 .
h
0 21 2 c3 0
3 2 1 O(h3 )
fx (x) = − f (x) + f (x + h) − f (x + 2h) +
2h h 2h h
−3f (x) + 4f (x + h) − f (x + 2h)
= + O(h2 ).
2h
Finite-difference methods solve PDEs by replacing the partial derivatives with finite-difference ap-
proximations. The type and order of the finite-difference approximations used will depend upon the
accuracy required and the computational resources available. We can build what is known as the
finite-difference toolkit of common derivatives for convenience (see table 2.4) and we can pick and
choose the approximations in order to suit our needs. Sometimes a non-symmetric finite-difference
approximation is required, this usually happens near to the boundaries where the number of nodes each
side of f (xi ) may not be the same. In these cases we need to derive a finite-difference approximation
using the method of undetermined coefficients (section 2.3.6).
We will consider PDEs written in terms of the function f (t, x) where t usually denotes time and x
denotes spatial positioning. If one of the independent variables is fixed, the partial derivative of f (t, x)
can be approximated using finite-difference approximations. For example, consider the approximation
of fx (t, x) using the second-order central difference approximation in x with a step length of h = ∆x
results in
∂ f (t + ∆t, x) − f (t − ∆t, x)
f (t, x) = .
∂t 2∆t
For convenience, a subscript/superscript notation is employed. Let the subscript i denote the spatial
positioning and the superscript n denote the time level, i.e.,
fin = f (tn , xi )
∂ f n − fi−1
n
f (tn , xi ) = i+1 ,
∂x 2∆x
∂ f n+1 − fin−1
f (tn , xi ) = i .
∂t 2∆t
Finite-Difference Toolkit
Order
Derivative Finite-difference approximation
of accuracy
fi+1 − fi fi − fi−1
1st (forward) or (backward)
h h
fx fi+1 − fi−1
2nd (central)
2h
fi−2 − 8fi−1 + 8fi+1 − fi+2
4th
12h
fi−1 − 2fi + fi+1
2nd (symmetric)
fxx h2
−fi−2 + 16fi−1 − 30fi + 16fi+1 − fi+2
4th
12h2
−fi−2 + 2fi−1 − 2fi+1 + fi+2
2nd
fxxx 2h3
fi−3 − 8fi−2 + 13fi−1 − 13fi+1 + 8fi+2 − fi+3
4th
8h3
fi−2 − 4fi−1 + 6fi − 4fi+1 + fi+2
2nd
fxxxx h4
−fi−3 + 12fi−2 − 39fi−1 + 56fi − 39fi+1 + 12fi+2 − fi+3
4th
6h4
Ut + vUx = 0. (2.12)
The independent variables are t that denotes time and x that denotes space. The function U = U (t, x)
is the concentration of some substance and v is the velocity that the substance travels along the domain
which is defined by a ≤ x ≤ b. In order to solve equation (2.12), we need the initial conditions that
define the values of U at the start of the solution (t = 0), i.e.,
The solution of the advection equation is the function U (t, x) which satisfies:
An exact solution is defined at an infinite number of values for the independent variables t and x. We
will construct a Finite-Difference Scheme (FDS) to approximate U at a finite set of discrete points in
t and x.
a a + ∆x a + 2∆x a + (N − 2)∆x b
x
x0 x1 x2 ··· xN xN −1
∆x
Figure 2.5: The x domain discretised using a uniform grid of finite-difference nodes
Using a uniform finite-difference grid means that the step length ∆x and the number of nodes N are
related. Since we require xN +1 = b then
a + (N − 1)∆x = b
which gives
b−a
∆x = . (2.14)
N −1
If however the spatial step length ∆x is defined then it is easy to see that
b−a
N =1+ .
∆x
Ut + vUx = 0
Uin+1 − Uin Uin − Ui−1
n
+v = 0.
∆t ∆x
We now rearrange this equation so that Uin+1 is the subject
v∆t n
Uin+1 = Uin − (U − Ui−1
n
). (2.15)
∆x i
This is a FDS to solve the advection equation equation (2.12). Since the finite-difference approxima-
tions for derivatives in space and time are O(∆x) and O(∆t) respectively we can say that this FDS
has formal accuracy of first-order in space and time. A FDS of the form of equation (2.15) is called a
time marching scheme since the solution at the next time level, n + 1, is calculated using values from
the current time level, n. Since U (t, x) is only known exactly at the initial time level, equation (2.15)
is rewritten replacing Uin with uni to reflect that it is an approximation of the exact solution
v∆t n
un+1 = uni − (u − uni−1 ) + O(∆t, ∆x). (2.16)
i
∆x i
It is of interest to know which nodes are required to calculate the value of un+1
i for a given FDS.
In equation (2.16) the calculation of the value of the node un+1
i is dependent upon two nodes at the
current time level uni and the neighbouring node to the left uni−1 . This information can be represented
in a diagram known as a finite-difference stencil shown in figure 2.6.
un+1
i
n+1
∆t
uni−1 uni
n
∆x
x
i−1 i i+1
Figure 2.6: The stencil the first-order finite-difference scheme, equation (2.16), used to solve
the advection equation.
U (t, a) = U (t, b) = 0.
boundary boundary
computational nodes
u0 u1 u2 u3 ··· uN −2 uN −1 x
boundary boundary
node node
Figure 2.7: The boundary nodes are removed from the calculation of the FDS.
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x
For this FDS, ∆t, ∆x and v are constant so we can pre-calculate v∆t ∆x in order to save time later on. It
is good practice to pre-calculate any values that you know will not change over the course of applying
a numerical scheme
v∆t 1 × 0.05
= = 0.5,
∆x 0.1
so the FDS is
un+1
i = uni − 0.5(uni − uni−1 ).
Calculating the solution at the first time step for the first few nodes
u11 = u01 − 0.5(u01 − u00 ) = 0.0001 − 0.5(0.0001 − 0) = 0.0001,
u12 = u02 − 0.5(u02 − u01 ) = 0.0183 − 0.5(0.0183 − 0.0001) = 0.0092,
u13 = u03 − 0.5(u03 − u02 ) = 0.3679 − 0.5(0.3679 − 0.0183) = 0.1931,
The values of the first three iterations of the FDS are given in table 2.3.
∗
When we say we are going to perform a hand calculation it means that we are going to perform each calculation
using pen and paper and the help of a calculator.
i 0 1 2 3 4 5 6 7 8 9 10
xi 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
u0i 0.0000 0.0001 0.0183 0.3679 1.0000 0.3679 0.0183 0.0001 0.0000 0.0000 0.0000
27
u1i 0.0000 0.0001 0.0092 0.1931 0.6839 0.6839 0.1931 0.0092 0.0001 0.0000 0.0000
u2i 0.0000 0.0000 0.0046 0.1012 0.4385 0.6839 0.4385 0.1012 0.0046 0.0000 0.0000
u3i 0.0000 0.0000 0.0023 0.0529 0.2698 0.5612 0.5612 0.2698 0.0529 0.0023 0.0000
Finite-Difference Methods
Chapter 2. Finite-Difference Approximations
Chapter 2. Finite-Difference Approximations
start
initialise variables
yes
n > nsteps end
no
calculate one step of the FDS
for each computational node
and store the values in un+1
i
output un
Figure 2.8: Flow chart for the FDS used to solve the advection equation.
• Lines 6 and 7 – since this is an m-file it has access to any previously defined variables or arrays
which might cause errors in the program, therefore it is always a good idea to clear the memory
using clear and the command window using clc. Note that this step isn’t necessary if you have
used a function instead of an m-file.
• Lines 10 to 15 – defines all of the variables used in the program at once in the beginning. It is
good practice to use comments to explain what each variable represents.
• Lines 18 and 19 – calculates the spatial step and uses an implicit for loop to generate an array
of x values going from xmin to xmax in steps of dx
• Line 22 – calculates the initial values of u0i using equation (2.17) and stores them in the array u.
• Lines 25 to 31 – outputs the column headings using formatted output. This is purely cosmetic
and not really necessary of the successful calculation of the solution, however sometimes it is
useful to be able to output the solution in a readable format.
• Line 34 – The value of v∆t
∆x is pre-calculated since it is constant throughout. This should be done
for all values that do not change over the iterations since computational effort is not wasted
calculating the values when they are already known.
• Line 37 – a for loop is used to calculate 3 iterations of the FDS. The variable n is the iteration
counter.
• Lines 40 and 41 – the values of the boundary nodes are calculated using the boundary conditions
for the current problem.
• Lines 44 to 47 – a for loop is used to loop through the nodes and calculate the value of un+1
i
using the FDS in equation (2.16). Since array indices in MATLAB begin at 1 rather that 0,
the first computational node u1 is the second node in u therefore we add 1 to the array indices,
i.e., i = 2 : N - 1 in the code represents the computational nodes i = 1, · · · , N − 2 in the
finite-difference discretisation.
• Lines 50 and 51 – u is updated by setting it equal to unew ready for the next iteration. t is also
updated by incrementing by dt, although not used in this program it is useful to keep track of
the t value for output purposes.
Elliptic PDEs are used to model problems where the independent variables are restricted to spatial co-
ordinates only. Since time is not a variable the solution of an elliptic PDE is constant and referred to
as steady state solutions. Examples of elliptic equations are Laplace’s equation and Poisson’s equation
that are used to model electrical and gravitational fields. Laplace’s equation is the simplest elliptic
PDE and was derived by French mathematician Pierre-Simon Laplace (1749–1827). The class of
analytical solutions to Laplace’s equation are called harmonic functions which have applications in
many fields of science such as magnetism, astronomy and fluid dynamics. Laplace’s equation is
Laplace’s equation
where x and y are the spatial co-ordinates. Poisson’s equation named after French mathematician
Siméon Denis Poisson (1781–1840) is an extension of Laplace’s equation
Poisson’s equation
where f (x, y) is some function of x and y. This chapter will focus on the solution of Laplace’s equation
using finite-difference methods and introduces the reader to the discretisation of a two-dimensional
spatial domain, calculation of boundary conditions and solving linear systems.
31
Chapter 3. Elliptic Partial Differential Equations
y
ui−1,j+1 ui,j+1 ui+1,j+1
j+1
boundary boundary
computational nodes
u0 u1 u2 u3 ... uN −2 uN −1 x
boundary boundary
node node
Figure 3.2: When using Dirichlet boundary conditions the nodes on the boundaries are
excluded from the computation and determined by some known scalar or function.
∗
Be careful to avoid confusing the node indexing convention used for finite-differences grids with the indexing used
for matrices.
u0 = α,
uN −1 = β,
where u0 and uN denotes the boundary nodes and α and β are constants. This is used in
cases where the solution on the boundary is known and constant. For example, modelling heat
diffusion across a domain where the boundary temperature is always zero.
• Values on the boundaries adhere to some function, e.g.,
u0 = f (t, x, y),
uN −1 = g(t, x, y),
where f (t, x, y) and g(t, x, y) are some functions of the independent variables. These are used
in cases where the solution at the boundary is not constant but known to behave in a certain
way. For example, modelling wave propagation where the boundary condition is used to generate
waves passing into the domain.
• Boundaries are used to model periodic behaviour, e.g.„
u0 = uN −2 ,
uN −1 = u1 ,
where u1 and uN −2 are the values of the first and last computational nodes (figure 3.3). This
is used where we want to model an infinite domain. For example, modelling wave propagation
where waves passing out through the right-hand boundary will pass in from the left boundary.
x
u0 u1 u2 u3 ... uN −2 uN −1
boundary boundary
ghost node ghost node
x
u−1 u0 u1 u2 ... uN −2 uN −1 uN
computational nodes
Figure 3.4: Neumann boundary conditions use the derivative across the boundary to deter-
mine the values of the ghost nodes.
To specify a Neumann boundary condition we need to approximate the derivative across the boundary
using a finite-difference. Consider the Neumann boundary condition
Ux = f (U ),
so the value of the ghost node u−1 is an expression involving the second computational node u1 and
f (U ). There are two main uses for Neumann boundary conditions:
• Transmissive boundary conditions: the derivative across the boundary is zero, i.e., f (U ) = 0 so
u−1 = u1 ,
uN = uN −2 .
This has the effect that any flow will simply pass out of the boundary as if it wasn’t there. For
example, consider the modelling of water waves down a channel, by specifying a transmissive
boundary at one end the waves will simply pass through as if the channel had an infinite length.
• Reflective (solid) boundary conditions: no flow through a reflective boundary. This used where
one of the dependent variables of the PDE is velocity and the velocity of the ghost node is set
to equal that of the velocity of a computational node with the sign reversed, e.g.,
v−1 = −v1 ,
vN = −vN −2 ,
Uxx + Uyy = 0
ui−1,j − 2ui,j + ui+1,j ui,j−1 − 2ui,j + ui,j+1
+ = 0.
∆x2 ∆y 2
We want an expression that gives the solution for the node ui,j , therefore rearranging to make ui,j the
subject results in
Second-order finite-difference scheme to solve Laplace’s equation
This is a second-order accurate FDS that approximates the solution to Laplace’s equation. The stencil
for this FDS is shown in figure 3.5.
ui,j+1
ui,j
ui−1,j ui+1,j
ui,j−1
Figure 3.5: The finite-difference stencil for the second-order FDS used to solve Laplace’s
equation
In order to demonstrate the solution of Laplace’s equation using finite-differences we are going to use
a simple example with a coarse grid shown in figure 3.6. The domain is 0 ≤ x ≤ 4 and 0 ≤ y ≤ 3
has been discretised using 5 nodes in the x direction and 4 nodes in the y direction so that ∆x =
∆y = 1. Dirichlet boundary conditions are implemented at all four boundaries so that the values of
the boundary nodes are constant. We need to calculate the solution to Laplace’s equation using the
FDS in equation (3.3) for the six computational nodes u1,1 , u2,1 , u3,1 , u1,2 , u2,2 and u3,2 .
Using equation (3.3) and considering the computational nodes going from left-to-right along each row
This linear system can be solved using direct methods such matrix inversion or Gaussian elimination
but these are inefficient for large systems. Instead we can use indirect methods which use an iterative
process where an initial estimate of the solution at the nodes u is improved to form a better estimate.
This estimate in turn is improved and the process continues until two successive estimates agree to a
required accuracy.
Note that all of the values on the right-hand side of equation (3.4) have superscript k meaning that
they are all known values from the current iteration.
Example 5
Perform two iterations of the Jacobi method to solve Laplace’s equation for the finite-difference
grid shown in figure 3.7 with zero starting values for the computational nodes.
Figure 3.7: The values of the initial conditions and boundary nodes for solving Laplace’s
equation.
Solution: The values of the computational nodes after one iteration of the Jacobi method are
Figures 3.8 and 3.9 contain the values of the computational nodes after the first and second iteration
Figure 3.8: The values of the nodes for the first iteration of the Jacobi method.
Figure 3.9: The values of the nodes for the second iteration of the Jacobi method.
This provides a single value that can be used for the error at each iteration. There are two main ways
of calculating an error using the L2 norm: the absolute error and the relative error. The absolute error
is a measure of distance between two estimates of the solution
The relative error is the measure of the distance between two sets of solutions relative to the most
recent estimation
∥uk+1 − uk ∥
errrel = . (3.7)
∥uk+1 ∥
The choice of whether to use the absolute or relative error will depend on the size of the numbers you
are dealing with. For example, consider err calculated using the values u0 = 0.01 and u1 = 0.001,
So the absolute error between these values is small suggesting convergence but the relative error is
large. Doing the same of the values u0 = 100000 and u1 = 100100
In this case the absolute error is large whereas the relative error is small.
The general process of calculating the solution to a linear system using an indirect method is sum-
marised in the flow chart shown in figure 3.10. Once the variables and arrays have been initialised a
while loop is required to test for convergence. If the solution hasn’t converged yet, the new solution
uk+1 is calculated, then the error between the current solution and new solution and then the current
solution is updated by setting it equal to the new solution.
start
initialise variables
calculate ∆x,
∆y, xi , yj and u0
no
err > tol end
yes
calculate uk+1
i,j for each
computational node
update uk ← uk+1
and k ← k + 1
Figure 3.10: Flow chart for solving an elliptic PDE using an indirect method.
The MATLAB code for calculating the Jacobi iterations for the Laplace equation example is given in
Listing 3.1.
Listing 3.1: MATLAB program to calculate the Jacobi iterations for the Laplace equation
example.
1 % laplace . m by Jon Shiach
2 %
3 % This program calculates the solution to the Laplace equation using the
4 % Jacobi method
5
6 % define variables
7 Nx = 5; % no . of nodes in the x direction ( inc ghost nodes )
8 Ny = 4; % no . of nodes in the y direction ( inc ghost nodes )
9 lenx = 4; % length of the x domain
10 leny = 3; % length of the y domain
11 err = 1; % error between successive iterations
12 tol = 1d -6; % convergence tolerance
13 k = 0; % iteration counter
14
15 % Calculate x and y node co - ordinates
16 dx = lenx / ( Nx - 1) ;
17 dy = leny / ( Ny - 1) ;
18 x = 0 : dx : lenx ;
19 y = 0 : dy : leny ;
20 [x , y ] = meshgrid (x , y ) ;
21
• Lines 7 to 13 – initialises the variables that define the problem. It is good practice to list all
of the variables at the top of the program and use comments to explain what each variable
represents.
• Lines 16 to 20 – calculates the spatial steps and the node co-ordinates. Note the use of the
meshgrid command to produce two-dimensional arrays for plotting purposes.
• Lines 23 to 27 – defines the initial values for the finite-difference nodes. Note that this array is
inverted using the flipud command so that it is consistent with matrix indexing (i.e., element
u(1,1) corresponds to node u1,1 ). This isn’t strictly necessary and only done here so that we
can directly compare the results to a hand calculation.
• Lines 30 to 39 – prints the column headings and initial values to the command window. This
code includes fprintf commands that outputs the current iteration values. It isn’t strictly nec-
essary to do this but it can help in verifying the code, debugging and analysing the convergence
behaviour of the method.
• Line 42 – a while loop is used to iterate the Jacobi method until err > tol.
• Lines 45 to 50 – the values of uk+1 are calculated using row-by-column order. Note that the
subscript notation used in the FDS is not the same as standard matrix subscript notation, i.e.,
u(j, i) corresponds to the element ui,j .
• Lines 56 and 57 – the values of u and k are updated ready for the next iteration.
• Lines 60 to 62 – the iteration counter, current estimate and absolute L2 error are outputted to
the command window.
The Jacobi method took 33 iterations to converge to the solution shown in figure 3.11 using tol = 10−6 .
The iteration values for the Jacobi method are shown in table 3.1.
Figure 3.11: The final values of the nodes calculated using Jacobi method with tol = 10−6 .
Table 3.1: Iteration values for the Jacobi method applied to solve the Laplace equation
example.
If the values of ui,j are calculated along each row from left to right before moving up to the next row,
the nodes below and to the left of ui,j have already been updated in the current iteration. Using these
updated values in equation (3.4) gives
∆y 2 (uk+1 k 2 k+1 k
i−1,j + ui+1,j ) + ∆x (ui,j−1 + ui,j+1 )
uk+1
i,j = , (3.8)
2(∆x2 + ∆y 2 )
Perform two iterations of the Gauss-Seidel method to solve Laplace’s equation for the finite-
difference grid shown in figure 3.7 with zero starting values for the computational nodes. Cal-
culate the absolute L2 error after each iteration.
Solution: The first iteration of the Gauss-Seidel method for this problem is
The iteration values for the Gauss-Seidel method for this problem are shown in table 3.2. The Gauss-
Seidel method converges to the solution significantly faster than the Jacobi method. For this example,
18 iterations were required for the Gauss-Seidel method to converge using tol = 10−6 compared to 33
iterations for the Jacobi method.
Table 3.2: Iteration values for the Gauss-Seidel method applied to solve the Laplace equation
example.
where ω is called the relaxation parameter. The value of the relaxation parameter is restricted to the
range 0 < ω < 2. When ω = 1 it is easy to see that equation (3.9) reduces to the Gauss-Seidel method.
When 0 ≤ ω < 1 the convergence of the Gauss-Seidel method is slowed down, this is called under
relaxation and is used when the convergence behaviour of the indirect method oscillates about the
solution. When 1 < ω ≤ 2 the convergence of the Gauss-Seidel method increased, this is called over
relaxation and is used to accelerate the convergence when the convergence behaviour is monotonic.
The optimal value of ω will depend upon the problem being solved.
Equation (3.10) is known as the point SOR method since it updates each point in the finite-difference
grid at a time. The choice of ω will determine the convergence rate of the point SOR method. There
are methods that can be used to find the optimal value of ω for Laplace’s and Poisson’s equations (see
Yang and Gobbert (2009) as an example) but these are outside the scope of this unit. A numerical
investigation where solutions are calculated using different values of ω and choosing the one that uses
the fewest iterations can be used to approximate an optimal ω value (but this may be different when
using a different number of nodes).
For this example the optimal value was found to be ω = 1.12 and the point SOR method required 10
iterations to converge to tol = 10−6 (table 3.3).
Table 3.3: Iteration values for the point SOR method applied to solve the Laplace equation
example.
uki,j+1
j+1
sweep by rows
uk+1
i−1,j uk+1
i,j uk+1
i+1,j
bottom to top
j solving for the
nodes on each
uk+1 row
i,j−1
j−1
x
i−1 i i+1
Figure 3.12: The line SOR method solves for each row of the finite-difference at a time.
Consider the finite-difference grid in figure 3.12. If we want to update each row of nodes going from
bottom to top then the nodes ui,j , ui−1,j and ui+1,j will be updated for the (k+1)th iteration. The node
ui,j−1 has already been updated in the previous row and node ui,j+1 is yet to be updated. Rewriting
equation (3.10) for ∆x = ∆y in this way gives
!
uk+1 k+1 k+1 k
i−1,j + ui+1,j + ui,j−1 + ui,j+1
i,j = (1 − ω)ui,j + ω
uk+1 k
.
4
Rearranging so that the nodes in row j with the superscript k + 1 are on the left-hand side gives
−ωuk+1
0,j + 4u1,j − ωu2,j = 4(1 − ω)u1,j + ω(u1,j−1 + u1,j+1 ),
k+1 k+1 k k+1 k
−ωuk+1
1,j + 4u2,j − ωu3,j = 4(1 − ω)u2,j + ω(u2,j−1 + u2,j+1 ),
k+1 k+1 k k+1 k
..
.
−ωuk+1
N −4,j + 4uN −3,j − ωuN −2,j = 4(1 − ω)uN −3,j + ω(uN −3,j−1 + uN −3,j+1 ),
k+1 k+1 k k+1 k
−ωuk+1
N −3,j + 4uN −2,j − ωuN −1,j = 4(1 − ω)uN −2,j + ω(uN −2,j−1 + uN −2,j+1 ).
k+1 k+1 k k+1 k
−ωuk+1
1,j + 4u2,j − ωu3,j = 4(1 − ω)u2,j + ω(u2,j−1 + u2,j+1 ),
k+1 k+1 k k+1 k
..
.
N −4,j + 4uN −3,j − ωuN −2,j = 4(1 − ω)uN −3,j + ω(uN −3,j−1 + uN −3,j+1 ),
−ωuk+1 k+1 k+1 k k+1 k
N −3,j + 4uN −2,j = 4(1 − ω)uN −2,j + ω(uN −2,j−1 + uN −2,j+1 + uN −1,j ).
−ωuk+1 k+1 k k+1 k k+1
k+1
4 −ω u1,j 4(1 − ω)uk1,j + ω(uk+1 k k+1
1,j−1 + u1,j+1 + u0,j )
−ω 4 −ω uk+1 4(1 − ω)uk2,j + ω(uk+1 k
2,j
2,j−1 + u2,j+1 )
.. .. .. .. . .
. . . . = ..
k+1
−ω 4 −ω uN −3,j 4(1 − ω)uN −3,j + ω(uN −3,j−1 + uN −3,j+1 )
k k+1 k
−ω 4 uk+1
N −2,j 4(1 − ω)u k
N −2,j + ω(u k+1
N −2,j−1 + u k
N −2,j+1 + u k+1
N −1,j )
This matrix equation can be solved using any linear solver but since it is a tridiagonal system, the
Thomas algorithm is by far the most efficient for doing this.
where ai , bi and ci are elements of the lower, main and upper diagonals respectively. The Thomas
algorithm proceeds by performing a forward sweep modifying the upper diagonal coefficients ci and
where A = D − L − U , i.e., D are the diagonal elements of A and L and U are the a lower and upper
triangular elements of A respectively with their signed reversed, ρ(TJ ) the spectral radius of A which
is defined as the largest absolute eigenvalue of TJ .
Since A that is used to calculate ωopt contains ω we have an implicit relationship. In order to determine
ωopt , we first calculate equation (3.11) using some starting value of ω and then use this to calculate
A and the next estimate of ωopt . The iterations continue until two successive estimates agree to some
tolerance. For this Laplace equation example, ωopt = 1.036 and the line SOR method took just 8
iterations to converge to the solution (table 3.4).
Table 3.4: Iteration values for the line SOR method applied to solve the Laplace equation
example.
Table 3.5 contains an analysis of the rate of convergence for each indirect method discussed in this
chapter. The CPU time was recorded by placing the tic and toc MATLAB commands at each end
of the program and taking the average time over several runs. The normalised column was calculated
by dividing the CPU time taken for each method by the time taken for the Jacobi method. The
Jacobi method is the most computationally expensive with the Gauss-Seidel, point SOR and line SOR
methods taking 1.7%, 1.3% and 1.3% of the time taken by the Jacobi method respectively. Usually it
is expected that the line SOR method would offer more of a saving than the point SOR method but
in this example there aren’t enough nodes in each row of the finite-difference grid for the increased
efficiency to take effect.
Table 3.5: Analysis of the rate of convergence of different indirect methods applied to the
Laplace equation example.
Example 7
4 3 2 1
3 u1,2 u2,2 1
2 u1,1 u2,1 1
1 1 1 1 x
Compute the first two iterations of the Jacobi, Gauss-Seidel and point SOR methods (with
ω = 1.1) for this problem.
Solution: Using the second-order symmetric differences from the finite-difference toolkit to
approximate Uxx and Uyy ,
ui−1,j − 2ui,j + ui+1,j
Uxx ≈ ,
∆x2
ui,j−1 − 2ui,j + ui,j+1
Uyy ≈ ,
∆y 2
then Poisson’s equation becomes
Uxx + Uyy = x + y
ui−1,j − 2ui,j + ui+1,j ui,j−1 − 2ui,j + ui,j+1
+ = x i + yj .
∆x2 ∆y 2
1
Since ∆x = ∆y = 3 then multiplying both sides by ∆x2 = ( 13 )2 = 1
9 gives
x i + yj
−4ui,j + ui−1,j + ui+1,j + ui,j−1 + ui,j+1 = ,
9
and rearranging to make ui,j the subject gives
ui−1,j + ui+1,j + ui,j−1 + ui,j+1 xi + yj
ui,j = − .
4 36
The Jacobi method for solving this finite-difference scheme is
The values of uki,j for the first two iterations are shown in the table below.
uk+1 k k+1 k
i−1,j + ui+1,j + ui,j−1 + ui,j+1 xi + yj
uk+1
i,j = − .
4 36
Calculating the first iteration:
2 + u02,1 + 1 + u01,2 1
+ 13 2+0+1+0 2
u11,1 = − 3
= − = 0.731481,
4 36 4 108
u1,1 + 1 + 1 + u02,2
1 2
+1 0.731481 + 1 + 1 + 0 1
u12,1 = − 3 3 = − = 0.655093,
4 36 4 36
3 + u02,2 + u11,1 + 3 1
+2 3 + 0 + 0.731481 + 3 1
u11,2 = − 3 3 = − = 1.655093,
4 36 4 36
u11,2 + 1 + u12,1 + 2 2
+2 1.655093 + 1 + 0.655093 + 2 4
u12,2 = − 3 3 = − = 1.290509.
4 36 4 108
The values of uki,j for the first two iterations are shown in the table below.
The values of uki,j for the first two iterations are shown in the table below.
k u1,1 u2,1 u1,2 u2,2 L2 error
0 0.000000 0.000000 0.000000 0.000000 -
1 0.804630 0.740718 1.840718 1.494154 2.610906
2 1.434061 1.250632 2.240632 1.594941 0.909003
Hyperbolic PDEs are used to model propagation problems where a quantity travels across a domain.
Common applications are the modelling of water flow, weather forecasting, blast wave analysis and
aerodynamics. Unlike elliptic equations where the solutions are steady state, hyperbolic PDEs contain
a time derivative term so the solution will evolve over time from a set of initial conditions. Any
perturbation in the dependent variables will propagate outward over time, however, unlike elliptic
PDEs, not every point in the domain will feel this disturbance at once. For example, consider dropping
a stone in a pool of water initially at rest. Waves caused by the disturbance will propagate out from
where the stone was dropped and not all parts of the pool will be affected by the disturbance at once.
As the waves propagate across the surface of the pool, the region that is affected by the disturbance
expands until all of the pool has been affected. This chapter will discuss the use of FDS to solve the
advection equation and introduces the concept of the stability of a numerical scheme.
The simplest example of a hyperbolic PDE is the one-dimensional advection equation that models
pure advection along a one-dimensional channel. The one-dimensional advection equation is written
as
Ut + vUx = 0 (4.1)
where t and x are independent variables denoting time and space respectively and v is the velocity.
If the initial conditions are described by the function, U (0, x), then the exact solution at time t is
U (t, x − vt).
Consider figure 4.1 where an initial profile is defined that consists of a triangular perturbation (dashed
red line). If v is the advection velocity, then at time t the triangular profile has travelled distance vt
in the x direction. Note that the triangular perturbation has not deformed through any other process.
53
Chapter 4. Hyperbolic Partial Differential Equations
U (t, x) vt
x0 x1 x2 x0 + vt x1 + vt x2 + vt x
Figure 4.1: Pure advection of an initial profile. Note that the shape is unchanged is it
propagates along the x domain.
The advection equation is useful for developing FDSs to solve hyperbolic PDEs since it is simple to
calculate an exact solution for a problem and discontinuities which are challenging for a numerical
scheme to resolve can be easily specified. Any numerical scheme that performs poorly when solving
an advection problem will be unsuitable for other applications involving more complicated hyperbolic
PDEs.
v∆t n
un+1 = uni − (u − uni−1 ) + O(∆t, ∆x). (4.2)
i
∆x i
Assuming that v in equation (4.1) is positive then the direction of flow is from left-to-right along the
x domain. To capture this flow, the discretisation of Ux needs to use the neighbouring node in the
upwind direction (the node to the left). Therefore equation (4.2) is known as the first-order upwind
scheme. If v is negative, then flow travels from right-to-left, therefore the spatial discretisation uses
the node to the right, and the FOU is
The FOU scheme for the advection equation when v < 0
v∆t n
un+1 = uni − (u − uin ) + O(∆t, ∆x). (4.3)
i
∆x i+1
Equations (4.2) and (4.3) are examples of time marching schemes where the solution of u(t, x) is
marched forward in time by one time step of ∆t through a single application of the scheme. The
stencil for the FOU scheme when used to solve the advection equation can be seen in figure 4.2.
un+1
i un+1
i
start
calculate x and u0
no
t < tmax end
yes
calculate boundary conditions
output un
Figure 4.3: Flow chart for applying a time marching scheme to solve the advection equation.
Example 8
The concentration of a pollutant in a river is to be modelled using the advection equation. The
stretch of river under study is of length 10m with an average flow velocity of v = 5ms−1 . At
time t = 0s a pollutant is released into the river such that the concentration of pollutant in the
The domain is discretised using a finite-difference grid of six nodes and first-order zero gradient
boundary conditions are used to calculate the ghost nodes such that
u−1 = u0 , uN = uN −1 .
Use the FOU scheme with ∆t = 0.25 to solve the advection equation to model the concentration
of the pollutant after t = 1s.
Solution: The first thing we need to do is discretise the spatial domain. Since 0m ≤ x ≤ 10m
and we are using 6 computational nodes the spatial step is
10
∆x = = 2m,
6−1
so the co-ordinates of the nodes are
x0 = 0, x1 = 2, x2 = 4, x3 = 6 x4 = 8, x5 = 10.
The finite-difference grid for this problem can be represented using the diagram below. The
computational nodes are represented by filled circles and the ghost nodes u−1 and u6 are repre-
sented by unfilled circles. The finite-difference schemes will be applied to calculate the solutions
for the computational nodes whereas the ghost nodes will be calculated using the zero gradient
boundary condition given above.
u−1 u0 u1 u2 u3 u4 u5 u6
x
0 2 4 6 8 10
The concentration of the pollutant at t = 0s is given by a case statement where the initial values
of the nodes is 1 where the node co-ordinates are between 2 and 4 inclusive and 0 elsewhere,
therefore
and the ghost nodes are u−1 = u0 = 0 and u6 = u5 = 0. We now need to compute the
solution using the FOU scheme for each computational node. Since v, ∆t and ∆x are constant
throughout we can precalculate v∆t
∆x
v∆t 5(0.25)
= = 0.625.
∆x 2
The first step of the FOU is
The values for the computational nodes for the first four time steps are shown in the table
below.
To validate a numerical scheme (test whether it provides suitable approximations) we can make use
of test cases. These are often simplified scenarios where problems are defined using idealised values
that can often mean we have a known exact solution. To validate the FDS used to solve the advection
equation, a test case that involves the advection of a Gaussian curve profile along a channel is used.
The spatial domain is defined as 0 ≤ x ≤ 1 and the initial conditions are described by
which produces a Gaussian curve centred at x = 0.25. The exact solution for the advection equation
is U (t, x − vt) so for this test case
The FDS is calculated using 101 nodes so that ∆x = 0.01 and a time step of ∆t = 0.01 is used.
Transmissive boundary conditions are employed at either end of the domain so that a quasi-infinite
domain is modelled. This is achieved by using the following values for the ghost nodes
u−1 = u1 , uN = uN −2 ,
The velocity is set at v = 0.5 and the FDS is marched forward in time until t = 1 when the centre of
the Gaussian curve is at x = 0.75.
The computed solutions for the FOU scheme are plotted against the exact solution in figure 4.4 for
increasing values of t. The numerical solutions show a marked reduction in the height and width of
the Gaussian curve which is due to the FOU scheme only being first-order accurate in space. This
phenomena is called numerical dissipation and is a common feature, although undesirable, of first-order
methods.
Figure 4.4: The solution of the advection equation using the FOU scheme.
The MATLAB code used to perform the calculations is given in listing 4.1.
Listing 4.1: MATLAB code for the FOU scheme used to solve the advection equation.
1 % advection . m by Jon Shiach
2 %
3 % This program solves the advection equation using the FOU scheme
4
5 % Clear workspaces
6 clear
7 clc
8
9 % Define variables
• Lines 19 and 20 – calculates the spatial step ∆x and the x co-ordinates of the finite-difference
nodes. Note that the co-ordinates of the ghost nodes (x−1 = xmin − ∆x and xN +1 = xmax + ∆x)
have also been included in the x() array
• Line 23 – initialises the solution array u() using a Gaussian curve function f(x) that is defined
at the bottom of the m-file.
• Line 29 – the value of dt is checked to ensure that t does not exceed tmax.
• Lines 32 and 33 – the values of the ghost nodes are calculated so that transmissive boundary
conditions are employed.
• Lines 36 to 40 – a for loop is used to loop through the values of u() and calculate unew().
Since we have added two ghost nodes to the finite-difference grid and arrays in MATLAB start
at 1, the index 2 : N + 1 correspond to nodes 0, . . . , N − 1.
• Lines 43 and 44 – u are t are updated ready for the next iteration.
• Line 47 – the exact solution to the advection equation is calculated using the same function that
was used to define the initial conditions.
• Lines 50 to 59 – the numerical and exact solutions are plotted for the current time step. This
produces an animation which shows the evolution of the wave which will loop back around and
finish where it started due to the use of the periodic boundary conditions. Plotting the solution
at every time step is useful for seeing the evolution of the solution over time but can cause the
program to slow down considerably.
• Lines 67 to 72 – a function f(x) is defined that calculates the initial profile given the array
containing the node co-ordinates x(). This function is used twice in the main program, first
it is used to define the initial values of u() and then again to calculate the exact solution
U (t, x) = f (x − vt) at each time step.
ui−1 + ui+1
Ux = + O(∆x2 ),
2∆x
and substituting this along with a forward difference approximation for Ut into equation (4.1) gives
v∆t n
un+1 = uni − (u − uni−1 ) + O(∆t, ∆x2 ), (4.4)
i
2∆x i+1
This FDS is known as the Forward-Time Central Space (FTCS) scheme. The FTCS scheme is first-
order accurate in time and second-order in space. The stencil for the FTCS scheme is given in
figure 4.5.
un+1
i
A plot of solution for the FTCS scheme when applied to solve the test problem given in section 4.3
at t = 1 is shown in figure 4.6. Here it is clear that something has gone wrong and we are seeing
the solution oscilate behind the Gaussian curve. If the calculations were allowed to continue these
oscilations would increase and the program would eventually crash. This is an example of an unstable
scheme and it is well known that the FTCS scheme is unstable when applied to the advection equation.
It was included here to demonstrate that not all FDS are applicable for certain PDEs. It is important
that we can determine whether a FDS is stable for a particular PDE.
Figure 4.6: The numerical and exact solutions of the advection equation at t = 0.1 using
the FTCS scheme.
There are three important characteristics that a FDS must have in order for it to be suitable for
solving a PDE. These are consistency, stability and convergence.
Definition 7: Consistency
A FDS is said to be consistent if as ∆t, ∆x → 0 then the numerical scheme is equivalent to the
PDE.
Definition 8: Stability
A FDS is said to be stable if the errors at each step of the time marching scheme caused by
the finite-difference approximations and computational rounding remain bounded and do not
increase.
Definition 9: Convergence
These three characteristics are related by the Lax equivalence theorem which is stated below
Theorem 1: Lax equivalence theorem
Convergence is essential if we want to apply our FDS to solve a PDE, therefore we must check to see
if a FDS is both consistent and stable.
Show that the FOU scheme is consistent for the advection equation and has formal accuracy
O(∆t, ∆x).
uni = U,
∆x2
uni−1 = U − ∆xUx + Uxx + O(∆x3 ),
2
∆t2
un+1
i = U + ∆tUt + Utt + O(∆t3 ).
2
Simplifying
∆t v∆x
Utt + O(∆t2 ) = −vUx +
Ut + Uxx + O(∆x2 )
2 2
∆t v∆x
Ut + vUx + Utt + O(∆t2 ) − Uxx + O(∆x2 ) = 0.
2 2
For the FOU scheme to be consistent we need this equation to be equivalent to the advection
equation plus the additional truncation errors. As it stands we have two terms involving second-
order derivatives Utt and Uxx , however, these can be omitted by increasing the truncation errors
from O(∆t2 ) and O(∆x2 ) to O(∆t) and O(∆x) so we now have
We have shown that the FOU scheme is consistent for the advection equation and is first-order
accurate in time and space.
t x0 + vt
x0 x
(a) Gaussian curve. (b) Characteristic curve.
Figure 4.7: The advection of a Gaussian curve in the xt domain and its corresponding
characteristic curve.
∗
Named after German mathematicians Richard Courant (1888–1972), Kurt Otto Friedrichs (1901–1982) and Hans
Lewy (1904–1988).
|v|∆t ≤ ∆x,
|v|∆t
C= ≤ 1, (4.5)
∆x
which according to the CFL condition must satisfy C ≤ 1.
The numerical domain of dependence is a set of all nodes that are required to compute the
value of the node uni for a given finite-difference scheme.
uni
un−1
i−1 un−1
i
Consider figure 4.8 that shows the numerical domain of dependence for the FOU scheme used to solve
the advection equation where node uni has been calculated using the two nodes un−1
i−1 and ui
n−1
which
are each calculated using two nodes at the previous time step and so on. For the CFL condition to
be satisfied, the characteristic represented by the dashed line must be contained within the numerical
domain of dependence.
The CFL condition is useful for calculating a value of the time step. Rearranging equation (4.5) gives
∆x
∆t ≤ . (4.6)
|v|
The stability of a numerical scheme can be analysed using von Neumann stability analysis named after
Hungarian mathematician John von Neumann (1903–1957). This assumes that the PDE is linear and
periodic boundary conditions are employed but its implementation is often applied to other non-linear
PDEs with non-periodic boundary conditions.
where C = v∆t∆x is the Courant number. Note that the subscripts are written in terms of j and not
i as used elsewhere so to avoid confusion when using complex numbers. The error in the numerical
approximation, εnj = Ujn − unj , of the approximations also satisfy the FTCS scheme, i.e.,
C n
εn+1 = εnj − (ε − εnj−1 ). (4.7)
j
2 j+1
For linear PDEs with periodic boundary conditions, assuming that the errors grow or shrink expo-
nentially over time then we can write the error at each node as a Fourier mode†
C at ikm (x+∆x)
ea(t+∆t) eikm x = eat eikm x − (e e − eat eikm (x−∆x) )
2
C ikm ∆x
ea∆t = 1 − (e − e−ikm ∆x ) (4.9)
2
The term on the left-hand side is known as the growth factor which gives the growth in the error over
one time step of ∆t so we use G = ea∆t for convenience. For stability we require
|G| ≤ 1, (4.10)
i.e., the errors do not increase over a single step. Using the exponential trigonometric identity
eix − e−ix
sin(x) = ,
2i
we can combine equations (4.9) and (4.10) to give the following condition for stability
1 ≥ |1 − iC sin(km ∆x)|.
√
The modulus of a complex number is defined by |a + bi| = a2 + b2 so squaring both sides gives
Since sin2 (x) is bounded by the range [0, 1] and C 2 is always positive then this inequality only holds
when C = 0 = v∆t ∆x . This would imply that ∆t = 0 which means there is no feasible value of the
time step for which the FTCS scheme remains stable. We have shown that the FTCS scheme is
unconditionally unstable when applied to the advection equation (note that this is not a general result
and the FTCS scheme may be stable for other PDEs).
†
I have skipped over some explanations here for brevity. Readers seeking further explanation are advised to read
Hirsch (1994).
In summary, the von Neumann stability analysis is carried out using the following steps:
1. Replace each instance of unj in the FDS with its corresponding Fourier mode, e.g.,
unj = eat eikm x .
3. The condition G ≤ 1 to determine for what values of C is the scheme stable. This can
be a tricky stage requiring use of trigonometric identities and/or properties of moduli. If
equation (4.10) cannot be satisfied for C > 0 then the FDS is unconditionally unstable;
else if it is satisfied for certain values of C then the FDS is conditionally stable; and if it
is satisfied for all values of C then the FDS is unconditionally stable.
Note that it is not recommended in practice to use the largest value of C for which a scheme remains
stable in a computer program. This is because small errors due to the limit of machine precision for
floating point operations can cause the calculation of C to exceed the limit for stability. For example,
if a particular scheme is stable for C ≤ 1 then we could multiply the maximum value of ∆t for which
a FDS is stable by a safety factor of 0.9 to ensure that we always have a stable scheme.
Example 10
∆x
Use von-Neumann stability analysis to show that the FOU scheme is stable for ∆t ≤ .
|v|
The Lax-Friedrichs scheme is a first-order in space and time with the stencil given in figure 4.9 (note
that even though a central difference was used to approximate Ux , the Lax-Friedrichs scheme is still
only first-order accurate in space). Use of von Neumann stability analysis shows that the Lax-Friedrichs
scheme is stable for C ≤ 1.
uni+1
The Lax-Friedrichs solution to the test problem is shown in figure 4.10. Here the solution has dissipated
even more than the FOU scheme due the averaging of the uni term used to remove the oscillations in
the FTCS scheme.
Figure 4.10: The numerical solution of the advection equation using the Lax-Friedrichs
scheme compared to the exact solution.
The Leapfrog scheme is a second-order scheme in time and space derived by approximating both the
time and space derivatives using second-order accurate central differences i.e.,
v∆t n
un+1 = un−1 − (u − uni−1 ) + O(∆t2 , ∆x2 ). (4.12)
i i
∆x i+1
The stencil for the Leapfrog scheme is shown in figure 4.11. Note that it is the node at the previous
time level un−1
i that is updated to calculate un+1
i and not uni so we say that uni has been ‘leapfrogged’
hence the name of the scheme. Using von Neumann stability analysis it can be shown that the Leapfrog
scheme is stable when C ≤ 1.
un+1
i
un−1
i
We have a problem with the Leapfrog scheme in that attempting to calculate the solution at the first
time level, u1i , we need values from a time level prior to the initial conditions, u−1
i , which do not exist.
A solution to this problem is to solve the first time step using a first-order in time scheme which only
requires the values at one time level u0i , and then continue with the Leapfrog scheme for all other time
levels. The FTCS scheme can be used to calculate u1i , even though it is unconditionally unstable since
we are only using it for one step.
The solution to the test problem using the Leapfrog scheme is shown in figure 4.12. Here the numerical
solution quite closely replicates the exact solution and the dissipation that was evident for the first-
order in time schemes is not present. However, the Leapfrog scheme is notoriously prone to oscillations
in the solution for problems with rapidly varying profiles (the Gaussian curve used here has a relatively
gentle change in the gradient). Also, the calculation of the Leapfrog scheme requires the values of
nodes at two different time steps which requires a computer program to keep track of two solution
arrays, un−1 and un , as opposed to just one.
Figure 4.12: The numerical solution of the advection equation using the Leapfrog scheme
compared to the exact solution.
un+1
i
n+ 1 n+ 1
ui− 12 ui+ 12
2 2
Equations (4.13) and (4.14) can be combined to give a single expression for the Lax-Wendroff scheme
v∆t n+ 21 n+ 21
un+1
i = uni − ui+ 1 − ui− 1
∆x 2 2
v∆t 1 n v∆t n 1 n v∆t n
= uni − (u + ui+1 ) −
n
(u − ui ) − (ui−1 + ui ) +
n n
(u − ui−1 )
n
∆x 2 i 2∆x i+1 2 2∆x i
v∆t n v 2 ∆t2 n
= uni − (ui + uni+1 − uni−1 − uni ) + (u − uni − uni + uni−1 )
2∆x 2∆x2 i+1
v∆t n v 2 ∆t2 n
= uni − ui+1 − uni−1 + 2
ui−1 − 2uni + uni+1 .
2∆x 2∆x
Analysis of the consistency of the Lax-Wendroff scheme shows that it is second-order in time and
space
v∆t n v 2 ∆t2 n
un+1
i = uni − ui+1 − uni−1 + 2
ui−1 − 2uni + uni+1 + O(∆t2 , ∆x2 ). (4.15)
2∆x 2∆x
The stencil for the Lax-Wendroff scheme is shown in figure 4.14. The solution to the test problem
using the Lax-Wendroff scheme is shown in figure 4.15. The numerical solution closely approximates
the exact solution and there is no numerical dissipation of the curve.
un+1
i
Figure 4.14: Finite-difference stencil for the single step Lax-Wendroff scheme.
Figure 4.15: The numerical solution of the advection equation using the Lax-Wendroff
scheme compared to the exact solution.
un+1 − ui un − uni−1
i
+ v i+1 = 0, (FTCS), (4.16)
∆t 2∆x
un+1 − ui un+1 − un+1
i
+ v i+1 i−1
= 0, (BTCS). (4.17)
∆t 2∆x
The Crank-Nicolson scheme uses the average of equations (4.16) and (4.17)
!
v uni+1 − uni−1 ui+1 − ui−1
n+1 n+1
un+1 − uni
i
=− + ,
∆t 2 2∆x 2∆x
Equation (4.18) is an implicit equation where the solution at the next time step un+1
i is dependent on
values of nodes from the current time level, ui±1 and from those at the next time level, un+1
n
i±1 which
v∆t
are unknown (see the stencil in figure 4.16). Let r = 4∆x and moving all un+1 terms to the left-hand
side of equation (4.18) gives
− run+1 n+1
i−1 + ui i+1 = rui−1 + ui − rui+1 .
+ run+1 n n n
(4.19)
un+1
i−1 un+1
i
un+1
i+1
The linear system in equation (4.19) can be solved to determine the values of the nodes within the
domain, un+1
i where i = 1 . . . N − 1. The nodes on the boundaries, un+1
0 and un+1
N , need to take
into account the boundary condition that is applied at that boundary. For example, consider the
calculation of equation (4.19) for the node on the left-hand boundary
− run+1 n+1
−1 + u0 + run+1
1 = run−1 + un0 − run1 . (4.20)
−run1 + un+1
0 + run+1
1 = run1 + un0 − run1
∴ un+1
0 + run+1
1 = un0 + run1 .
−run+1
N −2 + ruN −1 = −ruN −2 + uN −1 .
n+1 n n
The solution of the advection equation using Crank-Nicolson scheme given in equation (4.19) with
transmissive boundary conditions at each end can be written as the following tridiagonal matrix
equation
1 r un+1
0 1 r un0
n+1 n
−r 1 r u1 r 1 −r u1
.. .. .. . ..
. . . .. = . . . . . . . . . . .
−r 1 r n+1 1 −r n
uN −2 r uN −2
−r 1 un+1
N −1 −r 1 unN −1
This linear system can be solved using the Thomas algorithm discussed in section 3.10.1 on page 47
to calculate the values of un+1 . Performing a von Neumann stability analysis shows that the Crank-
Nicolson scheme is unconditionally stable for all values of ∆t. This means if we wanted to we could
use a single step to update the solution to any value of t. However, since the approximation of the
time derivative is still second-order, choosing a high value for ∆t will increase errors even though the
scheme remains stable.
The Crank-Nicolson scheme was used to model the propagation of the Gaussian curve is shown in
figure 4.17.
Figure 4.17: The numerical solution of the advection equation using the Crank-Nicolson
scheme compared to the exact solution.
a numerical scheme since there is no abrupt changes in the gradient. A tougher test of a numerical
scheme is the propagation of a ‘top hat’ function defined using the following case statement
(
1, |x − vt − 0.25| ≤ 0.1,
U (t, x) = (4.21)
0, otherwise.
The FDS presented in this chapter with that exception of the FTCS scheme have all been applied to
solve this problem and the solutions are shown in figure 4.18.
The first-order in time schemes both exhibit numerical dissipation but it is more pronounced in the
Lax-Friedrichs scheme (figures 4.18(a) and 4.18(b)). This is a common feature of first-order methods.
The second-order in time schemes all exhibit over/undershoots where the profile makes an abrupt
change in the gradient. This is known as Gibbs phenomenon and is characteristic of second-order
methods. The Leapfrog and Crank-Nicolson schemes exhibit severe oscillations in the upwind region
behind the tophat (figures 4.18(c) and 4.18(e)) whereas the Lax-Wendroff scheme (figure 4.18(d))
does not have these oscillations and is clearly the best performing of all of the methods seen here. A
summary of the FDS described in this chapters is given in table 4.1.
(e) Crank-Nicolson
Figure 4.18: The solutions of the various FDS used to model the propagation of a ‘top hat’
profile
Parabolic PDEs combine second-order spatial derivatives seen in elliptic PDEs with first-order deriva-
tives in time and space that model propagation in hyperbolic PDEs. Parabolic PDEs are most com-
monly used to model heat diffusion and acoustic problems. This chapter uses the heat diffusion
equation to demonstrate the solution of one and two-dimensional parabolic PDEs in addition to per-
forming a grid refinement study.
Ut − αUxx = 0, (5.1)
k
α= , (5.2)
ρcp
and k is the thermal conductivity of the material, ρ is the density and cp is the specific heat capacity.
insulation
U (t, 0) = 0 U (t, L) = 0
x=0 x=L
insulation
75
Chapter 5. Parabolic Partial Differential Equations
The initial temperature profile across the length of the bar is given by the function
πx
U (0, x) = sin . (5.3)
L
It can be shown that the exact solution to this problem with initial conditions given by equation (5.3)
is
πx
απ 2 t
U (x, t) = sin exp − 2 . (5.4)
L L
therefore
α∆t n
un+1 = uni + (u − 2uni + uni+1 ) + O(∆t, ∆x2 ). (5.5)
i
∆x2 i−1
This is the FTCS scheme for the heat diffusion equation. In order to determine the value for ∆t for
α∆t
which equation (5.5) is stable we need to conduct a von Neumann stability analysis. Let r = ∆x2 then
equation (5.5) can be written in compact form as
un+1
i = (1 − 2r)uni + r(uni−1 + uni+1 ).
Substituting in the Fourier modes and rearranging gives (see section 4.5.3)
∆x2
∆t ≤ . (5.6)
2α
Note that unlike the advection equation where the FTCS scheme was unconditionally unstable, the
FTCS scheme is conditionally stable for the heat diffusion equation.
The test problem has been solved using the FTCS scheme. The length of the bar was set at L = 1
and thermal diffusivity α = 1 was used. The domain was discretised using 11 nodes so that N = 10
and ∆x = 0.1. Dirichlet boundary conditions U (t, 0) = U (t, 1) = 0 were employed at both ends of
the domain. The FTCS scheme was iterated until t = 0.05 using equation (5.6) to calculate ∆t. The
solution is plotted against the initial profile and exact solution in figure 5.2.
Figure 5.2: The solutions to the one-dimensional heat diffusion equation using the FTCS
scheme with N = 10 at t = 0.05.
The temperature profile along the bar remains centred as the scheme is marched forward in time.
This is because the initial profile was symmetrical and the boundary conditions are the same at either
end of the domain. Dirichlet boundary conditions ensure the temperature at the boundary nodes
remains zero and this has a cooling effect on the nodes in the domain. The FTCS scheme gives very
good agreement with the exact solution where the absolute L2 error between the exact and computed
solutions is 0.0095.
The process of solving equation (5.7) is similar to the one-dimensional case. If a FTCS scheme is
employed then using a forward difference in time and symmetric differences in both spatial directions
then
n
un+1 − uni ui−1,j − 2uni,j + uni+1,j uni,j−1 − 2uni,j + uni,j+1
i
−α + =0
∆t ∆x2 ∆y 2
which gives
α∆t n α∆t n
un+1 n
i,j = ui,j + (u − 2uni,j + uni+1,j ) + (u − 2uni,j + uni,j+1 ). (5.8)
∆x2 i−1,j ∆y 2 i,j−1
2α∆t 2α∆t
G=1− 2
[1 − cos(km ∆x)] − [1 − cos(ℓm ∆y)]
∆x ∆y 2
where ℓm is the wave number in the y direction. For stability we require |G| ≤ 1 and G is at a
maximum when km ∆x = ℓm ∆y = π which gives 1 − cos(π) = 2 so
1 − 4 α∆t + α∆t ≤ 1.
∆x2 ∆y 2
This is satisfied when the term in brackets is less than or equal to 12 , therefore
α∆t α∆t 1
2
+ 2
≤
∆x ∆y 2
∆x2 ∆y 2
∆t ≤ .
2α(∆x2 + ∆y 2 )
Note that if ∆x = ∆y then
∆x2
∆t ≤
.
4α
Comparing this expression to equation (5.6) we see that the two-dimensional scheme requires a time
step half of that of the one-dimensional scheme.
y
Uy (t, x, 1) = 0
1
U (0, x, y) = 1
0.8
0.6
Ux (t, 0, y) = 0 Ux (t, 1, y) = 0
0.4
0.2
U (0, x, y) = 0
0 x
0 0.2 0.4 0.8 1
Uy (t, x, 0) = 0
Figure 5.3: Modelling heat diffusion across a metal sheet: initial conditions.
Zero gradient Neumann boundary conditions using ghost nodes are implemented at all four boundaries,
i.e.,
un−1,j = un1,j , unNX ,j = unNX −2,j , uni,−1 = ui,1 , uni,NY = uni,NY −2 .
This means that the temperature gradient across the boundaries is zero so that the metal sheet is
perfectly insulated (i.e., the temperature outside of the domain has no effect on the temperature within
the domain).
The solutions for this problem at times t = 0.00, 0.25, 0.75, 1.00 and 1.50 have been plotted and
shown in figure 5.4. As the solution is marched forward in time the area surrounding the C-shape
begins to heat up whilst C-shape itself begins to cool. This diffusion continues until the initial shape
is no longer recognisable.
The second term on the right-hand side of equation (5.10) is the fractional error for f1 . Calculating
the percentage error between f1 and fexact gives
fexact − f1 f1 − f2 1 ε21
100 ≈ 100 = p ,
f1 r − 1 f1
p r −1
where ε21 is the percentage relative error between the grids using h1 and h2 , i.e.,
f2 − f1
ε21 = 100 .
f1
The GCI gives an estimate of the discretisation error in the fine grid solution (f1 ) relative to the
extrapolated solution fexact . Roache (1994) defines the GCI index calculated using the fine and
medium grids as
1.25|ε21 |
GCI21 = p . (5.11)
r −1
and similar for the medium and coarse grids GCI32 . A solution is said to be in the asymptotic range
of convergence if
rp GCI21 ≈ GCI32 . (5.12)
If equation (5.12) is satisfied then the solution using the h2 grid has achieved grid convergence and no
further refinement of the grid will provide more accurate results.
To perform a grid refinement study using the GCI we do the following (Roache, 1994):
1. Calculate the solution of the PDE using three different finite-difference grids with grid
spacings h1 (fine grid), h2 (medium grid) and h3 (coarse grid) where h1 < h2 < h3 and
h2 h3
r= = .
h1 h2
2. For a sample of points in the grids calculate the observed order of convergence using
f3 − f2
ln
f2 − f1
p= , (5.13)
ln(r)
where f1 , f2 and f3 are the solutions at the points for grid spacings h1 , h2 and h3
respectively. The use of the moduli in equation (5.13) is so we can deal with oscillatory
convergence.
3. Calculate the grid convergence index for the fine and medium grid solutions and the
medium and coarse grid solutions.
f − f f − f
1.25 100 1.25 100
2 1 3 2
f1 f2
GCI21 = , GCI 32 = .
rp − 1 rp − 1
4. If rp GCI21 ≈ GCI32 then the medium grid solution has reached asymptotic convergence.
If not, refine the grid further and repeat steps 1–3.
The FDS described previously to solve the heat diffusion equation has been applied using square
finite-difference grids with N3 = 10, N2 = 20 and N1 = 40 nodes such that r = 2 and h3 = 0.1,
h2 = 0.05 and h1 = 0.025. The GCI was calculated for a sample of four points from the domain
located at (0.3, 0.3), (0.5, 0.3), (0.7, 0.3) and (0.3, 0.5) and these are shown in the table below. Note
that for the point at (0.7,0.3), GCI32 ≪ rp GCI21 which indicates that the h2 grid has not reached
asymptotic convergence.
The finite-difference grid was refined further so that the fine, medium and coarse grids use N1 = 80,
N2 = 40 and N1 = 20. The GCI for the same sample of points are shown in the table below. Here, all
of the points sampled satisfy equation (5.12) (or close enough) which suggests that the discretisation
using N = 40 nodes is sufficient in this case.
(a) N = 10 (b) N = 20
(c) N = 40 (d) N = 80
Figure 5.5: Solutions to the heat diffusional example at t = 0.5 using four different grid
sizes.
Example 11
A numerical solver was used to calculate the solution to a particular PDE using five different
grid sizes. The values of the spatial step used and solutions at two points in the finite-difference
grid are shown in the table below.
h point 1 point 2
0.1520 0.5549 0.5432
0.0760 0.5533 0.5577
0.0380 0.5462 0.5668
0.0190 0.5513 0.5719
0.0095 0.5490 0.5693
Use the grid convergence index to determine the optimum grid spacing that provides the best
compromise between accuracy and computational time.
Solution: Considering the first three grids we have h1 = 0.038, h2 = 0.076, h3 = 0.152 and
Calculate GCIs:
0.5533 − 0.5462
1.25 100
0.5462
point 1 : GCI21 = = 0.4727,
22.1497 − 1
0.5549 − 0.5533
1.25 100
0.5533
GCI32 = = 0.1052,
22.1497 − 1
0.5577 − 0.5668
1.25 100
0.5668
point 2 : GCI21 = 0.6721 − 1
= 3.3820,
2
0.5432 − 0.5577
1.25 100
0.5577
GCI32 = = 5.4768.
20.6721 − 1
Checking for asymptotic convergence:
so for the solution using h = h2 = 0.076 point 2 has reached asymptotic convergence point 1 has
not. Therefore we need to use a finer grid, considering the next three grids where h1 = 0.019,
h2 = 0.038, h3 = 0.076 and
So for the solution using h = h2 = 0.038 both sample points have reached asymptotic conver-
gence so this is the optimum grid spacing.
For most real world practical applications we require methods that can model systems over two or
three-dimensional spatial domains. To simplify our numerical models we can split the problem up
so that we solve for multiple models each with single spatial dimension. This method is known as
dimensional splitting and can also be used for PDEs including multiple terms.
where v and w are the velocities in the x and y directions respectively. Equation (6.1) models pure
advection so the shape of the initial profile is unchanged as it propagates though the spatial domain.
If the initial conditions are defined by
i,j − ui,j
un+1 uni,j − uni−1,j uni,j − uni,j−1
n
+v +w = 0,
∆t ∆x ∆y
which is rearranged to give
Unsplit FOU scheme for the two-dimensional advection equation
v∆t n w∆t n
i,j = ui,j −
un+1 (u − uni−1,j ) − (u − uni,j−1 ) + O(∆t, ∆x, ∆y).
n
(6.2)
∆x i,j ∆y i,j
85
Chapter 6. Solving Multidimensional Partial Differential Equations
Equation (6.2) is the FOU scheme for the two-dimensional advection equation which is first-order in
time and space. To test this scheme the advection of a two-dimensional Gaussian curve is used. The
domain is defined by the range 0 ≤ x, y ≤ 1 which is discretised using a finite-difference grid with
spatial steps ∆x = ∆y = 0.02. Dirichlet boundary conditions are used at all four boundaries where
the boundary nodes remain a constant zero. The initial profile is defined by
and the velocities are v = w = 1. The initial profile the numerical solution at t = 0.5 is shown in
figure 6.1. The numerical scheme exhibits dissipation perpendicular to the main direction of flow given
by the vector v = vi + wj.
Figure 6.1: Solutions of the two-dimensional advection equation using the FOU scheme.
Performing a von Neumann stability analysis on the FDS in equation (6.2) shows that it is stable
when
v∆t w∆t
+ ≤ 1,
∆x ∆y
∆x∆y
∆t = . (6.3)
v∆y + w∆x
This is a very restrictive constraint on the value of ∆t and the analysis requires difficult algebra.
In general, unsplit schemes are unwieldy, have restrictive stability constraints, are computationally
expensive and are rarely used for parabolic and hyperbolic PDEs.
∆t2
U (t + ∆t) = U + ∆tUt + Utt + O(∆t3 ). (6.4)
2
We want to replace the Ut (t, x) and Utt (t, x) terms with spatial derivatives which will depend on the
PDE begin solved. Let ∂t and ∂tt denote be two differential operators for partial derivatives for the t
variable such that ∂t U = Ut and ∂tt U = Utt , equation (6.4) can then be written as
∆t2
U (t + ∆t) = U + ∆t∂t U + ∂tt U + O(∆t3 ) (6.5)
2
We require expressions for ∂t U and ∂tt U which will depend upon the PDE being solved. For example,
consider the one-dimensional advection equation equation (4.1) written using differential operator
notation
∂t U + v∂x U = 0.
Therefore
∂t U = −v∂x U,
and
v 2 ∆t2
U (t + ∆t) = U − v∆t∂x U + ∂xx U + O(∆t3 ),
2
factorising out the U terms results in
v 2 ∆t2
U (t + ∆t) = 1 − v∆t∂x + ∂xx U + O(∆t3 ).
2
where
Differential marching operator for the one-dimensional advection equation
v 2 ∆t2
LX (∆t) = 1 − v∆t∂x + ∂xx + O(∆t3 ). (6.7)
2
LX (∆t) is the differential marching operator for the advection equation (an uppercase X has been used
in the subscript so as to avoid confusing it with a partial derivative). The different finite-difference
schemes presented in chapter 4 can be derived by replacing ∂x and ∂xx by different finite-difference
approximations from the toolkit, i.e.,
un+1
i = LX (∆x)uni .
∆t2 2
LXY (∆t) = 1 − ∆t(v∂x + w∂y ) + (v ∂xx + 2vw∂xy + w2 ∂yy ), (6.8)
2
Using dimensional splitting we solve a series of one-dimensional problems in one direction before
solving another series of one-dimensional problems in the other direction(s). We can write this as
In this case we have chosen to solve in the x direction first (since LX (∆t) is next to uni,j ) and then we
use those solutions to solve in the y direction. This is shown pictorally in figure 6.2.
y y
x x
(a) x sweep (b) y sweep
In order for the split scheme to be equivalent to an unsplit scheme we need to show that LY (∆t)LX (∆t) =
LXY (∆t)
w2 ∆t v 2 ∆t
LY (∆t)LX (∆t) = 1 − w∆t∂y + ∂yy 1 − v∆t∂x + ∂xx + O(∆t3 )
2 2
∆t2 2
= 1 − ∆t(v∂x + w∂y ) + (v ∂xx + 2vw∂xy + w2 ∂yy ) + O(∆t3 )
2
= LXY (∆t).
So the split scheme is equivalent to the unsplit scheme. Note that if we applied the one-dimensional
scheme in the y direction first we would still get the same result so the order in which the individual
dimensional operators are applied doesn’t matter. The choice of which one-dimensional scheme is used
in the x and y directions will depend on the PDE being solved and the problem that it is applied to.
For example, using the one-dimension FOU for both dimensions gives
v∆t n
u∗i,j = uni,j − (u − uni−1,j ), (6.12a)
∆x i,j
∗ w∆t ∗
un+1
i,j = ui,j − (u − u∗i,j−1 ). (6.12b)
∆y i,j
The application of equation (6.12a) creates new values of the dependent variable at each node denoted
by u∗i,j . equation (6.12b) then uses these new values to update the values of the dependent variable
for one full time step. The choice of the one-dimensional schemes do not have to be the same for each
dimension although accuracy considerations may require that they be of the same order.
For stability, the maximum allowable time step for the split scheme is the smallest of the time steps
for the individual one-dimensional schemes, e.g.,
∆x ∆y
∆t ≤ min CX , CY .
|v| |w|
where CX and CY are the Courant numbers that ensure the stability of the schemes used in the x
and y directions respectively. This stability criterion is much less restrictive than that of the unsplit
scheme equation (6.3).
The split FOU scheme has been applied to the same test problem described in section 6.1.1 and the
solution is shown in figure 6.3. Here the numerical scheme exhibits the dissipation that is characteristic
of first-order schemes, however, the anti-dissipation seen in the direction of flow using the unsplit
scheme is no longer present.
Figure 6.3: Solution of the two-dimensional advection equation using the split FOU scheme.
This PDE combines the advection equation equation (4.1) with the diffusion equation equation (5.1) to
model transport and diffusion at the same time. Equation (6.13) is a parabolic PDE since it contains a
Uxx term. Assuming v > 0 and using first-order differences for Ut and Ux and second-order difference
for Uxx results in
un+1 − uni un − ui−1 un − 2uni + uni+1
i
+v i − α i−1 = 0,
∆t ∆x ∆x2
which gives the following unsplit scheme
v∆t n α∆t n
un+1 = uni − (ui − uni−1 ) + (u − 2uni + uni+1 ) + O(∆t, ∆x). (6.14)
i
∆x ∆x2 i−1
To perform a von Neumann stability analysis on this scheme we can write it in the form
un+1
i = (C + D)uni−1 + (1 − C − 2D)uni + Duni+1 ,
where C = v∆t ∆x and D = ∆x2 . Substituting the Fourier nodes and applying the condition |G| ≤ 1
α∆t
Ut + vUx = 0, (6.17a)
Ut − αUxx = 0. (6.17b)
Applying the FOU scheme for equation (6.17a) and the FTCS scheme for equation (6.17b) results in
the following split scheme
v∆t n
u∗i = uni − (u − uni−1 ), (6.18a)
∆x i
α∆t ∗
un+1 = u∗i + (u − 2u∗i + u∗i+1 ). (6.18b)
i
∆x2 i−1
where u∗i denotes the values of the dependent variable updated using the first scheme as before.
Equations (6.18a) and (6.18b) have been applied model advection-diffusion of the same Gaussian
curve used in section 4.3. All of variables remain the same and the diffusion coefficient of α = 0.01
was used. The solution for times t = 0.0, 0.1, 0.2, 0.3, 0.4 and 0.5 is shown in figure 6.4.
As the Gaussian curve travels down the channel the diffusion term causes it to dissipate. However, it
was seen in figure 4.4 that the FOU scheme exhibits numerical dissipation when solving the advection
equation so how much of this dissipation is due to the diffusion term and how much is because of the
first-order scheme? The second-order Lax-Wendroff scheme has been used to solve the advection term
in the split scheme and the numerical solutions are compared to the FOU scheme at time t = 0.5 in
figure 6.5. This shows that at the peak of the curve, the first-order dissipation causes at 0.05 reduction
in height.
Figure 6.5: Comparison between the FOU and Lax-Wendroff schemes for solving the advec-
tion part of the split advection-diffusion equation.
The maximum time step for the unsplit scheme is ∆t = 0.0033 and the split scheme is ∆t = 0.005
which means that the unsplit scheme requires 50% more time steps than the split scheme.
Example 12
Ut + αUx + βUyy = 0.
(i) Derive the unsplit second-order differential marching LXY (∆t) for this PDE.
(ii) Use dimensional splitting to split this 2D PDE into two 1D PDEs.
(iii) Derive the two second-order differential marching operators LX (∆t) and LY (∆t) for each
of the split PDEs.
(iv) Show that the split operator sequence LX (∆t)LY (∆t) is equivalent to the unsplit operator
LXY (∆t).
Solution:
(i) Using differential operator notation
∂t U + α∂x U + β∂yy U = 0,
∴ ∂t U = −α∂x U − β∂yy U,
∂tt U = −∂t (α∂x U ) − ∂t (β∂yy U )
= −α∂x (∂t U ) − β∂yy (∂t U )
= −α∂x (−α∂x U − β∂yy U ) − β∂yy (−α∂x U − β∂yy U )
= α2 ∂xx U + 2αβ∂xyy U + β 2 ∂yyyy U.
Since the terms involving ∆t3 and ∆t4 are not O(∆t3 ) they can be omitted and we have
∆t2 2
LX (∆t)LY (∆t) = 1 + ∆t(−α∂x − β∂y ) + (α ∂xx + 2αβ∂xyy + β 2 ∂yyyy ) + O(∆t3 )
2
which is the same as LXY (∆t) from part (i).
∆x
∆tX = ,
|v|
∆y
∆tY = .
|w|
If v ≫ w (≫ means ‘much greater than’) and if the spatial steps are roughly equal then it follows that
∆tX ≪ ∆tY . If the sequence LY (∆t)LX (∆t) is used then for stability both schemes will need to use
the same value of ∆t which in this cases will be the much more restrictive ∆tX . So even though the
scheme for solving in the y direction is stable for a much larger value of ∆t, it is constrained by the
stability of the scheme in the x direction. This is an example of a stiff problem.
Fortunately, we can form a sequence where each scheme uses a different value of ∆t whilst the whole
sequence remains stable. Suppose that ∆tY is some multiple of ∆tX , i.e., ∆tY = n∆tX then the
following sequence can be used,
n
∆tY ∆tY ∆tY
LY (∆tY ) LX . . . LX = LY (∆tY ) LX , (6.21)
n n n
| {z }
n times
i.e., a single application of LY (∆tY ) followed by n applications of LX ( ∆tnY ). To show that this is a
valid sequence consider the first-order differential marching operator for the one-dimensional advection
equation
L(∆t) = 1 − v∆t∂x .
Applying this to equation (6.21) and multiplying out using the binomial theorem
n n
∆tY ∆tY
LY (∆tY ) LX = (1 − w∆tY ∂y ) 1 − v ∂x
n n
∆tY
= (1 − w∆tY ∂y ) 1 − nv 2
∂x + O(∆t )
n
= (1 − w∆tY ∂y )[1 − v∆tY ∂x + O(∆t2 )]
= 1 − ∆tY (v∂x + w∂y ) + O(∆t2 )
= LXY (∆tY ),
which is the same as the first-order form of equation (6.8(). Therefore the sequence in equation (6.21)
is valid. If LX (∆tX ) and LY (∆tY ) are second-order and n is even then it can be shown that
n n
∆tY 2 ∆tY 2
LXY (∆tY ) = LX LY (∆tY ) LX . (6.22)
n n
An operator sequence of the form given in equation (6.22) is called a symmetric operator sequence.
One particular example of a symmetric operator uses n = 2, i.e.,
∆t ∆t
LXY (∆t) = LX LY (∆t)LX , (6.23)
2 2
which is known as Strang splitting after American mathematician Gilbert Strang (1934–present) (fig-
ure 6.6).
y y y
x x x
∆t ∆t
(a) x sweep using 2
(b) y sweep using ∆t (c) x sweep using 2
The universe is governed by physical laws where variables are intrinsically linked. For example, the
behaviour of a fluid depends upon the velocity, density, pressure, temperature, viscosity etc. Any
change in one of these variables will have an effect on all of the others. Also in order to model
physical phenomena we need to make sure that the relationships between the variables satisfy certain
requirements. For example, at its most basic level, the modelling of fluid flow needs to ensure the
conservation of mass and momentum in the system, i.e., mass and energy can’t be added or taken
away∗ . This means that instead of using a single PDE to model physical behaviour, a system of PDEs
is required. We can use the numerical schemes and splitting methods developed for single PDEs on
a system of PDEs with some minor changes. This chapter will use the shallow water equations to
demonstrate the solution of a system of PDEs but the concepts are similar for other systems.
The Shallow Water Equations (SWE) are a hyperbolic system of PDEs that model water flow in
shallow regions. The derivation of the SWE is achieved by taking the Navier-Stokes system of PDEs
and depth-averaging by assuming the flow in the vertical direction is negligible. This may seem like an
odd assumption but it is valid due to the physics of water flow in shallow water. Consider figure 7.1
that shows the motion orbits of a neutrally buoyant particle in different water depths. The water
depth d is defined as the distance between the bed surface and the mean water level. The wavelength
λ for a regular wave is defined as the distance between the corresponding points on two successive
waves (i.e., the troughs). The depth-to-wavelength ratio is used to classify water in three regions.
When λd > 12 the water is considered deep and the motion orbits are roughly circular with the radius
decreasing quadratically as the distance under the surface increases. When 20 1
≤ λd < 21 the water is
considered to be in intermediate depth water and the motion orbits become elliptic with the minor
axis decreasing as the depth under the water surface increases. When λd < 20 1
the water is considered
shallow and the motion of the particle follows a horizontal orbit with very little motion in the vertical
direction. Therefore the assumption that vertical velocity is negligible in shallow water is valid.
∗
This only applies to the modelling of the behaviour of the fluid and not necessary the complete system being modelled.
For example, when modelling water waves we may need to add mass or momentum into the system (using sink or source
terms) to generate the waves. The conservation of mass and momentum in the governing equations ensures that the
fluid behaves as required.
97
Chapter 7. Solving Systems of Partial Differential Equations
λ λ λ
d d d
d 1 1 d 1 d 1
(a) Deep: ≥ (b) Intermediate: ≤ < (c) Shallow: <
λ 2 20 λ 2 λ 20
Figure 7.1: The motion of a particle depends on the depth to wavelength ratio.
In one-dimension, the SWE are a system of two PDEs which are written as
ht + (hu)x = 0, (7.1a)
1 2
2
(hu)t + hu + gh = −ghzx . (7.1b)
2 x
where h is the water depth, u is the horizontal velocity, g = 9.81ms−2 is the acceleration due to gravity
and z is the bed surface elevation (figure 7.2). Equation (7.1a) ensures that we have conservation of
mass, h, and that no mass is added or removed from the system. Equation (7.1b) ensures that we
have conservation of momentum, hu, which means that no energy is added or taken away. The term
on the right-hand side of equation (7.1b) is the bed source term and ensures that change in velocity
caused by changes in the bed topography is balanced by the change in momentum.
g = 9.81ms−2
z
water surface
u
h
bed surface
z
x
Ut + Fx = S,
where U is the vector of conserved variables, F is the flux vector that models the changes in the
conserved variables in the x direction and S is the vector of source terms, i.e.,
h hu 0
U= , F = , S= .
hu hu2 + 21 gh2 −ghzx
In addition to modelling shallow water flows, the SWE can be used to model tsunami propagation.
A tsunami is generated by seismic activity on the ocean floor that causes the displacement of a large
volume of water. This results in a solitary wave with a wavelength up to λ = 200km and a height of
just 2m travelling at approximately 800kms−1† . Since the average depths of the oceans are typically in
region of 4km, the depth-to-wavelength ratio for a tsunami is 0.02, well within the range of applicability
of the SWE. As the tsunami propagates into shallower water it begins to steepen and a bore wave
forms which can be up to 5m in height. The damage that a tsunami can cause to coastal regions is
catastrophic, one of the worst examples in recent years was the Indian ocean tsunami on Boxing day
2004 that claimed the lives of more than 280,000 people.
Ut + Fx = 0, (7.2)
where
U1 h F1 hu
U= = , F = = .
U2 hu F2 hu2 + 12 gh2
by writing it in the form
Ut + JUx = 0, (7.3)
where J is the Jacobian matrix defined by
Definition 12: The Jacobian matrix
∂F1 ∂F1
∂F ∂U ∂U2
J= =
∂F
1 . (7.4)
∂U 2 ∂F2
∂U1 ∂U2
so the Jacobian is
0 1
J= ,
−u2 + gh 2u
P Wt + JP Wx = 0,
P −1 P Wt + P −1 JP Wx = 0
Wt + DWx = 0.
√
Since the wave speeds for the SWE are |u| + gh then the maximum allowable time step for the SWE
is
∆x
∆t ≤ √ . (7.8)
|u| + gh
Equation (7.8) calculates a separate value of ∆t for each point in the domain. We require a single
value of ∆t so that a numerical scheme is stable for all points in the domain, therefore we choose the
smallest value of ∆t, i.e.,
∆x
∆t ≤ p . (7.9)
max(|ui | + ghi )
i
Since the values of h and u will change over time, equation (7.9) is calculated at the beginning of each
time step (note that in practice the maximum allowable time step is multiplied by some safety, e.g.,
0.9, factor to ensure stability).
The FDS developed for the advection equation in chapter 4 can be modified to solve the SWE. Recall
that the Lax-Friedrichs scheme for solving the advection equation is
1 ∆t n
un+1 = (uni+1 + uni−1 ) − v (u − uni−1 ) + O(∆t, ∆x).
i
2 2∆x i+1
The velocity in the advection equation, v, is constant in space and time. The velocity in the SWE is
variable in space and time and is governed by the flux vector F that is differentiated with respect to
x. Therefore we can replace the uni±1 terms in the spatial derivative with the corresponding flux terms
and remove v giving the Lax-Friedrichs scheme for solving the SWE
1 n ∆t
Uin+1 = (Ui+1 n
+ Ui−1 )− (F n − Fi−1
n
) + O(∆t, ∆x). (7.10)
2 2∆x i+1
The solution procedure for solving the SWE is similar to that used for the advection equation with a
couple of exceptions. Unlike the advection equation where the velocity, and therefore the maximum
allowable time step ∆t, is constant, the velocities in the SWE are variable so ∆t is calculated at the
beginning of each time step. Also the flux vector is calculated before the finite-difference scheme is
applied to march the solution forward in time.
start
initialise variables
calculate U 0
no
t < tmax end
yes
calculate ∆t
calculate F n
Example 13
The SWE are being used to model a one-dimensional dam break. The domain is 2m in length
with a partition located at x = 1m separating two bodies of water with heights 5m and 2m
for the upstream and downstream bodies respectively. First-order transient flow boundary
conditions are employed at both ends of the domain that is discretised into a reduced grid with
∆x = 0.5m for verification purposes. Acceleration due to gravity is assumed to be g = 9.81ms-2 .
Moving onto the second step we first calculate the maximum allowable time step
0.5
∆t = 0.9 √ = 0.0581,
1.8923 + 3.5g
and using the boundary conditions to calculate the values of the ghost nodes
h−1 = h1 = 5, h5 = h3 = 3.5,
u−1 = u1 = 0, u5 = u3 = 1.8910.
The values of the conserved variables after two steps of the Lax-Friedrichs scheme are
partition
hup
hdown
x
(a) initial conditions
h
rarefaction wave
bore
wave
x
(b) solution at time t
The dam break problem is a tough test for FDS since the initial conditions contain a discontinuity
which is also present in the solutions in the form of a bore wave. The one-dimensional dam break
problem is one of the few problems for the SWE where it is possible to calculate an analytical solution.
The Lax-Friedrichs scheme shown in equation (7.10) has been applied to the one-dimensional dam
break problem. The domain 0 ≤ x ≤ 1 is discretised using N = 101 nodes giving ∆x = 0.01 with a
partition located at x = 0.5 separating the reservoirs to the upstream (left) and downstream (right)
sides of the partition with water depths hup = 1 and hdown = 0.5 respectively. Transmissive boundary
conditions using ghost nodes were employed at both ends of the domain so that waves will pass through
the boundary without reflection, i.e.,
h−1 = h1 , hN = hN −2 ,
u−1 = u1 , uN = uN −2 .
The solutions for the water depth h and velocity u are shown in figure 7.5. Similar to the advection
equation, the Lax-Friedrichs scheme exhibits numerical dissipation which means the numerical solution
does not capture the discontinuity at the bore wave and smooths out the rarefaction wave. In order
to capture these phenomena we require a higher order scheme.
Figure 7.5: Solution to the one-dimensional dam break at time t = 0.1s using the Lax-
Friedrichs scheme
Listing 7.1: MATLAB code for solving the SWE using the Lax-Friedrichs scheme
1 % oned_swe . m by Jon Shiach
2 %
3 % This program solves the SWE using the Lax - Friedrichs scheme
4
5 % Clear workspaces
6 clear
7 clc
8
9 % Define global parameters
10 global gravity
11 gravity = 9.81; % acceleration due to gravity
12
13 % Define variables
14 N = 101; % number of nodes
15 xmin = 0; % lower bound of x
16 xmax = 1; % upper bound of x
17 hL = 1; % water height to the left of the dam
18 hR = 0.5; % water height to the right of the dam
19 t = 0; % time variable
20 tmax = 0.1; % max value of t
21
22 % Define spatial array ( including ghost nodes )
23 dx = ( xmax - xmin ) / ( N - 1) ;
24 x = xmin - dx : dx : xmax + dx ;
25
26 % Define initial conditions
27 U = zeros (2 , length ( x ) ) ;
28 U (1 , x <= 0.5) = hL ;
29 U (1 , x > 0.5) = hR ;
30
67 % -------------------------------------------------------------------------
68 function dt = calculate_dt (U , dx , t , tmax )
69
70 % This function calculates the maximum allowable time step
71 global gravity
72 dt = 0.9 * dx / max ( abs ( U (2 , :) ./ U (1 , :) ) + sqrt ( gravity * U (1 , :) ) ) ;
73 dt = min ( dt , tmax - t ) ;
74
75 end
76
77 % -------------------------------------------------------------------------
78 function U = calculate_BC ( U )
79
80 % This function calculates the values of the ghost nodes using transmissive
81 % boundary conditions
82 U (: , 1) = U (: , 3) ;
83 U (: , end ) = U (: , end -2) ;
84
85 end
86
87 % -------------------------------------------------------------------------
88 function F = calculate_F ( U )
89
90 % This function calculates the flux vector F from U .
91 global gravity
92 F (1 , :) = U (2 , :) ;
93 F (2 , :) = U (2 , :) .^2 ./ U (1 , :) + 0.5 * gravity * U (1 , :) .^2;
94
95 end
96
97 % -------------------------------------------------------------------------
98 function Unp1 = Lax_Friedrichs (U , F , dt , dx )
99
100 % This function calculates a single step of the Lax - Friedrichs scheme
101
102 N = size (U , 2) - 2;
103 Unp1 = U ;
104 C = 0.5 * dt / dx ;
105 for i = 2 : N + 1
106 Unp1 (: , i ) = 0.5 * ( U (: , i +1) + U (: , i -1) ) - C * ( F (: , i +1) - F (: , i -1) )
;
107 end
108
109 end
The Lax-Wendroff scheme given in equation (4.15) can not be applied to solve the SWE in its current
form since it can only handle flow in one direction (i.e., linear PDEs). Instead, we can write it as an
equivalent two-step method which is known as the MacCormack scheme (MacCormack, 1969).
n+ 12 ∆t
Ui = Uin − n
Fi+1 − Fin , (7.11a)
∆x
1 n n+ 1 ∆t n+ 1 n+ 1
Uin+1 = (Ui + Ui 2 ) − (Fi 2 − Fi−1 2 ) + O(∆t2 , ∆x2 ). (7.11b)
2 2∆x
The predictor step equation (7.11a) updates the solution over a partial time step and the corrector
step equation (7.11b) uses the predictor values to update the solution over a full time step. The
two-step MacCormack scheme is equivalent to the Lax-Wendroff scheme and has the same order and
stability properties. Since the predictor step changes the values of the variables used in the corrector
step, the boundary conditions and flux vector need to be calculated using the predictor values prior
to the corrector step.
The MacCormack scheme was applied to the one-dimensional dam break problem and the solutions
can be seen in figure 7.6. Unlike the Lax-Friedrichs scheme, the MacCormack scheme is able to capture
the discontinuity at the bore wave and does not suffer from numerical dissipation. However, there
are noticeable oscillations in the solution caused by the abrupt changes in the gradient of the surface.
This is a common feature of second-order methods. There are methods that can be applied to the
scheme in order to minimise these oscillations but this are outside the scope of this unit (interested
readers are advised to search for ‘artificial dissipation’, ‘TVD schemes’ and ‘MUSCL schemes’).
Figure 7.6: Solution to the one-dimensional dam break at time t = 0.1s using the MacCor-
mack scheme
ht + (hu)x + (hv)y = 0,
1 2
2
(hu)t + hu + gh + (huv)y = −ghzx ,
2
x
1 2
2
(hv)t + (huv)x + hv + gh = −ghzy .
2 y
The variables h, u, g and z have been defined previously with the exception of v which is the horizontal
flow velocity in the y direction. These can be written more succinctly in vector form as
Ut + Fx + Gy = S,
where U is the vector of conserved variables, F and G are flux vectors that model change in the
conserved variables in the x and y directions respectively and S are the source terms that model bed
topography
h hu hv 0
U = hu , F = hu2 + 21 gh2 , G= huv , S = −ghzx .
hv huv 2 1
hv + 2 gh 2 −ghzy
The solution procedure for solving the two-dimensional SWE is summarised in the flow chart in
figure 7.7.
start
initialise variables
calculate U 0
yes
t ≥ tmax end
no
calculate ∆t
calculate bound-
ary conditions
calculate F n or Gn
use 1D Lax-Wendroff in
the x direction with ∆t
2 1
calculate U n+ 2
calculate
1 1
use 1D Lax-Wendroff in the F n+ 2 or Gn+ 2
in the y direction with ∆t
2
calculate U n+1
use 1D Lax-Wendroff in
the x direction with ∆t
2
update U n ← U n+1
and t ← t + ∆t
Figure 7.7: Flow chart for solving the two-dimensional SWE using the MacCormack scheme.
The first test that is being used is a pseudo one-dimensional dam break problem where the one-
dimensional dam break test described in section 7.3.2 is applied to a two-dimensional grid (figure 7.8).
The domain is discretised using 50 nodes in both the x and y directions. The solution to this problem
should be the same as that described for the one-dimensional case along the direction of flow and
horizontal in the other direction. The numerical solutions for the pseudo one-dimensional dam break
for both the x and y directions can be seen in figure 7.9. Taking a cross-section of the solutions shows
that they are comparable to the one-dimensional solutions seen previously (figure 7.10).
partition
y y
1 1
hup = 1
hdown = 0.5
0 0
0 1 x 0 1 x
(a) x direction (b) y direction
Figure 7.8: Plan view of the pseudo one-dimensional dam break problem.
Figure 7.10: Comparisons of the solutions for the water height and velocity for the pseudo
one-dimensional dam break problem in the x and y directions.
1
hdown = 0.5
hup = 1.5
0 x
0 1
Figure 7.11: Initial conditions for the collapse of a cylindrical column problem.
The values of the initial conditions can be calculated using the following
( p
1, (x − 0.5)2 + (y − 0.5)2 < 0.1,
h(x, y) = ,
0.5, elsewhere,
u(x, y) = 0,
v(x, y) = 0.
Solid wall boundary conditions are employed at all four boundaries and the values of the ghost nodes
The collapse of the cylindrical column causes a circular bore wave to form propagating in the radial
direction away from the centre. If there are any problems in resolving the spatial derivatives caused
by the dimensional splitting it will be noticeable in the solution. A contour plot of the values of h at
t = 0.075 is shown in figure 7.12. Here we can see the a circular bore wave is propagating at equal
velocities away from the centre of the cylinder thus indicating that the two-dimensional solver can
resolve the spatial dimensions equally.
Figure 7.12: Contour plot of the collapse of a cylindrical column showing the bore waves
travelling with equal velocities away from the centre of the cylinder.
The solutions at various times in the simulation are shown in figure 7.13. At time t = 0.05 the
cylindrical column has collapsed in on itself and the outward travelling bore wave has started to form.
At time t = 0.10 the bore wave has almost reached the boundaries and a depression has formed where
the centre of the cylinder was. At time t = 0.15 the bore waves have reached the boundaries and are
reflected back towards the interior of the domain. At time t = 0.25 and t = 0.3 the reflected waves
have started to interact with each other causing the water surface to break up into lots of individual
waves.
Figure 7.13: Surface plots of the solution to the collapse of a cylindrical column problem at
t = 0.00, 0.05, 0.10, 0.15, 0.25 and 0.30.
Courant, R., Friedrichs, K., and Lewy, H. (1967). “On the partial difference equations of mathematical
physics”. In: IBM Journal of Research and Development 11.2. (English translation from German
of the original paper published in 1927), pp. 215 –234. url: http://web.stanford.edu/class/
cme324/classics/courant-friedrichs-lewy.pdf.
Crank, J and Nicolson, P. (1947). “A practical method for numerical evaluation of solutions of partial
differential equations of the heat conduction type.” In: Proc. Camb. Phil. Soc. 1, pp. 50–67.
Hirsch, C. (1994). Numerical Computation of Internal and External Flows. Vol. 1: Fundamentals of
Numerical Discretization. John Wiley & Sons. Chap. 8, pp. 283–288.
Hoffman, J.D. (2001). Numerical Methods for Engineers and Scientists. 2nd ed. CRC Press.
Lax, P.D. and Richtmyer, R.D. (1956). “Survey of the stability of linear finite-difference equations”.
In: Comm. Pure Appl. Math. 9, pp. 267–293.
Lax, P.D. and Wendroff, B. (1960). “Systems of conservation laws”. In: Comm. Pure Appl. Math. 13.2,
pp. 217–237.
MacCormack, R.W. (1969). “The effect of viscosity in hypervelocity impact cratering”. In: pp. 69–354.
Roache, P.J. (1994). “Perspective: a method for uniform reporting of grid refinement studies”. In: J.
Fluids Eng. 116, pp. 405–412.
Yang, Shiming and Gobbert, Matthias K (2009). “The optimal relaxation parameter for the SOR
method applied to the Poisson equation in any space dimensions”. In: Applied Mathematics Letters
22.3, pp. 325–331.
115
Appendix A
This linear system is converted into upper triangular form U x = d′ where the main diagonal only
contains 1’s. Consider row 1 of equation (A.1)
b1 x1 + c1 x2 = d1
a2 x1 + b2 x2 + c2 x3 = d2 (A.3)
c2 d2 − a2 d′1
x2 + x 3 = .
b2 − a2 c′1 b2 − a2 c′1
c2 d2 − a2 d′1
Let c′2 = ′ and d′2 = then
b2 − a2 c1 b2 − a2 c′1
117
Appendix A. Derivation of the Thomas algorithm
c3 d3 − a3 d′2
x3 + x 4 = .
b3 − a3 c′2 b3 − a3 c′2
′
Let c′3 =
c3
and d′ = d3 − a3 d2 then
b3 − a3 c′2 3
b3 − a3 c′2
Consider an ith row of equation (A.1). Using a similar method shown for rows 2 and 3 above the ith
row can be written as
where
ci
c′i = (A.7)
bi − ai c′i−1
di − ai d′i−1
d′i = . (A.8)
bi − ai c′i−1
an xn−1 + bn xn = dn .
dn − an d′n−1
xn = .
bn − an c′n−1
0 0 0 0 0 1 xn d′n
Once the system is written in upper triangular form the solution of x can be found by back-substitution.
Starting with row n of equation (A.9) the solution for xn is simply d′n
xn = d′n . (A.10)
therefore