Numerical Solution of Partial Differential Equations
Numerical Solution of Partial Differential Equations
equations
Email: [email protected]
Web: http://researchers.uq.edu.au/researcher/768
@DrOlsenKettle
ISBN: 978-1-74272-149-1
Acknowledgements
Special thanks to Cinnamon Eliot who helped typeset these lecture notes in
LATEX.
Note to reader
This document and code for the examples can be downloaded from
http://espace.library.uq.edu.au/view/UQ:239427.
Please note if any of the links to code are not working please contact my
email address and I can send you the code.
Contents
1 Overview of PDEs 9
1.1 Classification of PDEs . . . . . . . . . . . . . . . . . . . . . . 9
1.1.1 Elliptic . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.2 Hyperbolic . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.3 Parabolic . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2 Implicit Vs Explicit Methods to Solve PDEs . . . . . . . . . . 10
1.3 Well-posed and ill-posed PDEs . . . . . . . . . . . . . . . . . 10
2
4 Iterative methods 28
4.1 Jacobi method . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.1.2 Applying the Jacobi method . . . . . . . . . . . . . . . 30
4.2 Gauss-Seidel Method . . . . . . . . . . . . . . . . . . . . . . . 31
4.2.1 Example: using Gauss-Seidel method to solve a
matrix equation . . . . . . . . . . . . . . . . . . . . . . 31
4.3 Relaxation Methods . . . . . . . . . . . . . . . . . . . . . . . 32
3
7.7.4 Aliasing error . . . . . . . . . . . . . . . . . . . . . . . 59
10 Spectral methods 83
10.1 An introduction to spectral methods . . . . . . . . . . . . . . 83
10.1.1 Example 1: Comparing the accuracy of solutions of a
variable speed wave equation with either the spectral
or finite difference method . . . . . . . . . . . . . . . . 84
10.1.2 Example 2 Comparing spectral and finite difference
methods with constant wave speed conditions and
initial conditions of a non-smooth pulse . . . . . . . . . 87
11 Shock wave 90
11.1 Analytical solution: Method of characteristics . . . . . . . . . 90
11.1.1 Example 1: Using method of characteristics to solve
the linear 1-D advection equation . . . . . . . . . . . . 91
11.1.2 Example 2: Using method of characteristics to solve
the nonlinear inviscid Burgers equation . . . . . . . . 91
11.2 Numerical Solution for nonlinear Burgers Equation . . . . . . 94
11.2.1 Example I: Finite difference solution with Lax Method 95
11.2.2 Example II: Solution using Method of Lines . . . . . . 97
4
11.2.3 Example III: Solution using Spectral Method . . . . . . 97
5
List of Figures
6
8.3 Initial conditions in (a) and matlab solution using explicit
central difference method for 1D wave equation with friction
in (b) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7
Numerical solution of parabolic
and hyperbolic PDEs
This document and code for the examples can be downloaded from
http://espace.library.uq.edu.au/view/UQ:239427.
Please contact me ([email protected]) if you are unable to download
the code and I can send it to you.
References:
Applied Numerical Methods for Engineers using Matlab and C, R. J. Schilling
and S. L. Harris.
8
Chapter 1
Overview of PDEs
1.1.1 Elliptic
AC > B 2
Uxx + Uyy = 0
A = C = 1, B = 0
1.1.2 Hyperbolic
AC < B 2
1
Uxx = Utt
c2
A = 1, C = 1/c2 , B = 0
9
1.1.3 Parabolic
AC = B 2
Ut = Uxx
A = 1, B = C = 0
stable only for certain time step sizes (or possibly never stable!). Sta-
bility can be checked using Fourier or von Neumann analysis. Time
step size governed by Courant condition for wave equation.
Implicit Methods:
there is no explicit formula at each point, only a set of simultaneous
equations which must be solved over the whole grid.
10
2t
an = an n2 an (t) = an (0)en
For the heat equation the transient part of the solution decays and this has
stable numerical solutions.
2t
an = an n2 an (t) = an (0)en
For the backwards heat equation the transient part of the solution blows up
and the numerical solution would fail! In general it is difficult or impossible
to obtain numerical solutions for ill-posed PDEs.
11
Part I
12
Chapter 2
We will focus on the heat or diffusion equation for the next few chapters.
This is an example of a parabolic equation.
First we will derive an analtical solution to the 1-D heat equation. Consider
the temperature U (x, t) in a bar where the temperature is governed by the
heat equation, Ut = Uxx . The ends of the bar are cooled to 0 C and the
initial temperature of the bar is 100 C.
U (0, t) = 0 C - U (L, t) = 0 C
6
U (x, 0) = 100 C
We want to solve Ut = Uxx using separation of variables. We assume
that the solution can be written as the product of a function of x and a
function of t, ie. U (x, t) = X(x)T (t) then:
T 2X
Ut = X = T = Uxx divide by XT
t x2
1 T 0 (t) X 00 (x) 2
= = (2.1)
T (t) X(x) | {z }
| {z } | {z } constant
function of t only function of x only
13
The only way the LHS and RHS of equation 2.1 can be a function of t and
x respectively is if they are both equal to a constant which we define to be
2 for convenience.
2
T 0 + 2 T = 0 T = e t
X 00 + 2 X = 0 X = A sin x + B cos x
2
e t is a transient solution and decays in time to boundary conditions.
Use initial conditions U (0, x) = 100 C to find An :
X
U (0, x) = T0 = An sin (nx/L)
n=1
Am = T0 [L/m(cos(m) 1)]
2L
= T0
m
for m=1,3,5,. . .
14
2.2 Numerical solution of 1-D heat equation
2.2.1 Difference Approximations for Derivative Terms
in PDEs
We consider U (x, t) for 0 x a, 0tT
Discretise time and spatial variable x:
T a
t = , x = ,
m n+1
Ujk t2 2 Ujk
Ujk+1 = Ujk + t + + O(t3 ) (2.2)
t 2 t2
If we only consider O(t) terms in equation 2.2 then we arrive at the forward
difference in time approximation for Ut :
Ujk t2 2 Ujk
Ujk1 = Ujk t + + O(t3 ) (2.3)
t 2 t2
15
We can also perform similar manipulations to arrive at approximations
for the second derivative Utt :
Ut = Uxx
Initial conditions
U (0, x) = f (x)
Ux (t, 0) = g1 (t)
Ux (t, a) = g2 (t)
U (t, 0) = g1 (t)
U (t, a) = g2 (t)
U (t, 0) = g1 (t)
Ux (t, a) = g2 (t)
16
2.2.3 Explicit Forward Euler method
or FTCS (Foward Time Centred Space)
We want to solve the 1-D heat equation:
Ut = Uxx . (2.4)
We solve this PDE for points on a grid using the finite difference method
where we discretise in x and t for 0 x a and 0 t T :
xn+1 = a
xn
..
.
x3
x2
x1
x0
t0 t1 t2 t3 ... tm1 tm = T
We can write out the matrix system of equations we will solve numerically
for the temperature U . Suppose we use 5 grid points x0 , x1 , x2 , x3 , x4 = xn+1 ,
ie. n = 3 in this example:
x0 = 0 x1 x2 x3 x4 = xn+1 = a
We let:
U1k
~k
U
k ~ k at time tk .
= U2 , solution for temperature vector U
k
U3
18
Using boundary conditions: U0k = g1 (tk ) and U4k = xg2 (tk )+U3k equation
(2.6) becomes:
1 2s s 0
U1k+1 U1k
~ k+1 =
s 1 2s s
U U2k+1 = U2k
0 s s}
1| {z
U3k+1 U3k
| {z
Neumann bc }
A
Dirichlet bc
z }| {
sg1 (tk )
+ 0
sxg2 (tk )
| {z }
|
Neumann
{z
bc }
~b
~ k+1
or U = AU + ~b
~k (2.7)
The term (1 s) in the matrix A above and the term (sxg2 (tk )) in
vector ~b above are from the Neumann boundary condition given using the
approximation in equation (2.5).
Figure 2.1 shows the initial conditions in (a) and matlab solution for tem-
perature distribution along rod with time in (b). The numerical solution
matches the analytical solution reasonably well: U (x, t) = 2x + 1 at the final
time. However the Neumann boundary condition at x = 1 introduces error
19
Initial condition for Temperature distribution Variation of Temperature distribution with time
2.8
2.6
3
2.4
2.5
2.2
U
2
U
1.5
1.8
1.6 1
1
0.8 15000
1.4 0.6
10000
0.4
0.2 5000
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 x 0 0
x t
Figure 2.1: Initial conditions in (a) and matlab solution using Forward Euler
method for temperature distribution along rod with time in (b)
Ujk+1 = s(Uj+1
k k
+ Uj1 ) + (1 2s)Ujk
= (1 4s)(1)j
At k = 2:
20
Uj2 = (1 2s)(1 4s)(1)j + s [(1 4s)(1)j+1 + (1 4s)(1)j1 ]
| {z }
2(14s)(1)j
= (1 4s)2 (1)j
Therefore at k = n
2.3.1 Example
The matlab code for this example is MethodOfLines.m and Uprime.m
We are solving the same system again with the method of lines: Ut = Uxx
where the initial conditions are U (x, 0) = sin(2x) + 2x + 1
0 x 1, = 105 , 0 t 12, 000.
boundary conditions are U (0, t) = 1 and Ux (1, t) = 2
Again we get:
~
U ~ + ~b
= AU
t
How? Replace
Uj+1 2Uj + Uj1
Uxx = ,
x2
21
where U (xj , t) = Uj (t), xj = jx, 0 j n + 1
x = a/(n + 1) = 1/(n + 1) (a = 1)
with boundary conditions: U (0, t) = U0 (t) = 1
U Un+1 Un+1 Un
(1, t) = (t) ' = 2 Un+1 = Un + 2x
x x x
x0 = 0 x1 x2 x3 x4 = 1
~ U1 2 1 0 U1 1
U
= U2 =
1 2 1
U2
+ 0
t x2 x2
U3 0 1 1 U3 2x
~ = AU
U ~ + ~b
22
Chapter 3
k+1
Uj+1 2Ujk+1 + Uj1
k+1
Uxx (tk+1 , xj ) =
x2
k+1
Uj Ujk
Ut (tk+1 , xj ) =
t
Ut = Uxx becomes:
t
Ujk = Ujk+1 k+1
[Uj+1 2Ujk+1 + Uj1
k+1
]
x2
= (1 + 2s)Ujk+1 s(Uj+1
k+1 k+1
+ Uj1 ) (3.1)
t
where s = x 2 as before.
We still need to solve for Ujk+1 given Ujk is known This requires solving a
tridiagonal linear system of n equations.
a
Again we let Ujk = U (xj , tk ); xj = jx, j = 0, ..., n + 1, x = n+1 ; tk =
T
kt, k = 0, ..., m, and t = m .
23
3.1.1 Numerical implementation of the Implicit Back-
ward Euler Method
Again we are solving the same problem: Ut = Uxx , U (x, 0) = Uj0 = f (xj )
s 1 + 2s s
U2 = U2 + s 0
~ k + ~b
~ k+1 = U
AU
~ k + ~b]
~ k+1 = A1 [U
U
Ujk k
Uj+1 k
Uj1
= + O(x2 )
x 2x
24
This is more accurate than the forward approximation we used previously
(see section 2.2.1):
Ujk k
Uj+1 Ujk
= + O(x)
x x
U5k = 4x + U3k
k
Because we have Neumann boundary conditions at x = a(= 1), Un+1 = U4k
is unknown, and given by equation 3.1:
|
Neumann
{z
b.c. }
~b
~ k + ~b = ~c
~ k+1 = U
AU
~ k+1 = A1~c
U
25
Using an implicit solver means we have to invert the matrix A to solve for
~ k+1 which is a lot more computationally expensive than the matrix multi-
U
~ k+1 for explicit solvers in section 2.2.3.
ply operation in equation 2.7 to find U
This method is stable for s 0 so larger time steps can be used for implicit
methods than explicit methods.
Initial condition for Temperature distribution Variation of Temperature distribution with time using Backward Euler method
3
2.8
3
2.6
2.4 2.5
2.2 U 2
U
2
1.5
1.8
1
1.6 1
0.8 15000
1.4 0.6
10000
0.4
0.2 5000
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 x 0 0
x t
Figure 3.1: Initial conditions in (a) and matlab solution using Backward
Euler method for temperature distribution along rod with time in (b)
Figure 3.1 shows the initial conditions in (a) and matlab solution for tem-
perature distribution along rod with time in (b). The solution using the
Backward Euler method in figure 3.1 is stable even for large time steps and
this matlab code uses a time step 6 greater than the solution using the For-
ward Euler method in figure 2.1. So even though there is more work in each
time step (inverting a matrix) using the implicit Backward Euler method it
allows larger time steps than the explicit Forward Euler method.
Uses:
Ujk+1 Ujk
Ut =
t
26
k+1
1 Uj+1 2Ujk+1 + Ujk+1 Uj+1
k k
2Ujk + Uj1
Uxx = 2
+ 2
2 |
x
{z } | x
{z }
implicit explicit
27
Chapter 4
Iterative methods
Implicit methods are stable - however they can take much longer to compute
than explicit methods. We saw that the Backward Euler method requires a
system of linear equations to be solved at each time step:
~ k + ~b)
~ k+1 = ~c (where: ~c = U
AU
Iterative methods begin with an initial guess for the solution ~x0 to the
matrix equation we are trying to solve: A~x = ~b. Each iteration updates
the new k th estimate (~xk ) which converge on the exact solution ~x. Different
methods have different convergence times and for big inverse matrix problems
are much faster than direct matrix inverse methods.
28
4.1 Jacobi method
A is decomposed into a sum of lower-triangular (L), diagonal (D) and upper-
triangular terms (U ):
A = L+D+U
@
@ @ U
@ @
@ @
@ @
A= @D @
@ @
@ @
@
@
L @
4.1.1 Example
A for Backward Euler Method with Dirichlet boundary conditions:
1 + 2s s 0
s 1 + 2s s
.. .. ..
A= . . .
s 1 + 2s s
0 s 1 + 2s
1 + 2s 0
0 0
1 + 2s
...
s
D = ,L = ,
..
.. ..
.
. .
0 1 + 2s 0 s 0
0 s 0
... ...
U =
...
s
0 0
We want to solve A~x = b
(D + L + U )~x = ~b
29
or D~x = b (L + U )~x
x1
x2
~x = ..
.
xn
kD1 (L + U )k < 1
X
|Aii | > |Aij | , 1in
j6=i
| {z }
A is strictly diagonally dominant
where kBk is the row-sum norm defined below:
n n
X X |aij |
kBk = max |Bij | = max < 1.
j=1,j6=i |aii |
1in 1in
j=1
X
|Aii | > |Aij |, 1 i n
j6=i
30
holds is a measure of how fast the estimate ~xk converges to actual solution
~x.
i1 n
1
xk+1 Aij xk+1 Aij xkj ,
X X
i = bi j 1 i n.
Aii j=1 j=i+1
This is an improvement over the Jacobi method because it uses the new
estimate xk+1
j when it can. In vector form: ~xk+1 = D1 (bL~xk+1 U~xk )
or (D + L)~xk+1 = b U xk .
0
17 iterations to converge so takes nearly twice as long as the Gauss-Seidel
method.
31
Residual after each iteration using GaussSeidel method
9
6
Residual
0
0 1 2 3 4 5 6 7 8 9
no of iterations
Figure 4.1: Plot of residual using the Gauss-Seidel method after each itera-
tion
Figure 4.1 shows the residual using the Gauss-Seidel method after each iter-
ation, it takes 9 iterations for the residual error to be less than 0.001.
i1 n
xk+1 = xki + Aij xk+1 Aij xkj ,
X X
i bi j 1 i n. (4.2)
Aii j=1 j=i
= 1 Gauss-Seidel method
> 1 over-relaxation
32
We can re-write equation 4.2:
i1 n
xk+1 = (1 )xki + Aij xk+1 Aij xkj ,
X X
i bi j 1in
Aii j=1 j=i+1
33
Chapter 5
..
.
y3
y2
y1
y0
x 0 x1 x2 x3 ... xm xm+1 = a
34
conditions:
Using central difference approximations for Uxx and Uyy then the finite dif-
ference approximations for equation (5.1) are given by:
35
y4 = yn+1 = b
y3 h h h
U13 U23 U33
y2 h h h
U12 U22 U32
y1 h h h
U11 U21 U31
y0
x0 x1 x2 x3 x4 = xm+1 = a
Thus we need to solve for the interior values marked with a circle above
as the boundary values are already given. We let the vector of interior values
we are solving for be defined as:
U11
U12
U13
U21
~ =
U
U22 ,
vector of interior values we are solving for.
U23
U31
U32
U33
36
Uk+1,j + Uk1,j 4Uk,j + Uk,j+1 + Uk,j1 = h2 fk,j becomes:
4
k=1 1 0 1 0 0 0 0 0
U11
j=1
Uk,j Uk,j+1 Uk+1,j
k=1 1 4 1 0 1 0 0 0 0
U12
j=2
Uk,j1 Uk,j Uk,j+1 Uk+1,j
k=1 0 1 4 0 0 1 0 0 0
U13
j=3
Uk,j1 Uk,j Uk+1,j
k=1
1 0 0 4 1 0 1 0 0
U21
j=1
Uk1,j Uk,j Uk,j+1 Uk+1,j
k=1 0 1 0 1 4 1 0 1 0
U22
j=2 Uk1,j Uk,j1 Uk,j Uk,j+1 Uk+1,j
k=2 0 0 1 0 1 4 0 0 1
U23
j=3 Uk1,j Uk,j1 Uk,j Uk+1,j
k=2 0 0 0 1 0 0 4 1 0
U31
j=1
Uk1,j Uk,j Uk,j+1
k=3 0 0 0 0 1 0 1 4 1
U32
j=2
Uk1,j Uk,j1 Uk,j Uk,j+1
k=3 0 0 0 0 0 1 0 1 4
U33
j=3 Uk1,j Uk,j1 Uk,j
U10 + U01
f11
Uk,j1 + Uk1,j
U02
f12
Uk1,j
U14 + U03
f13
Uk,j+1 + Uk1,j
U20
f21
Uk,j1
0
+ = h2 f22
U24
f23
Uk,j+1
U30 + U41
f31
Uk,j1 + Uk+1,j
U42
f32
Uk+1,j
U34 + U43
f33
Uk,j+1 + Uk+1,j
37
ie:
4 1 0 1 0 0 0 0 0 U11
1 4 1 0 1 0 0 0 0
U12
0 1 4 0 0 1 0 0 0 U13
1 0 0 4 1 0 1 0 0
U21
0 1 0 1 4 1 0 1 0
U22
1 4 0
0 0 1 0 0 1
U23
0 0 0 1 0 0 4 1 0
U31
0 0 0 0 1 0 1 4 1 U32
0 0 0 0 0 1 0 1 4 U33
38
If we use explicit forward Euler scheme as we did for the 1-D heat
equation the stability criteria is even stricter than for 1-D:
x2 + y 2
t
8
this is less attractive because the time step is much smaller.
Ut = (Uxx + Uyy )
U (tk , xi , yj ) = Uijk
2 Uijk k
Ui+1,j 2Uijk + Ui1,j
k
= at time tk ,
x2 | x
{z
2
}
EARLY STEP
2 Uijk+1 k+1
Ui,j+1 2Uijk+1 + Ui,j1
k+1
= at time tk+1 .
y 2 y 2
39
This introduces an early bias which evaluates Uxx (tk ) at an earlier time
than Uyy (tk+1 ). This bias is compensated by also evaluating Uxx (tk+2 ) at
tk+2 ; Uyy (tk+1 ) again at tk+1 and Ut (tk+3/2 ) at mid-point between tk+1 and
tk+2 :
k+3/2
Uij Uijk+2 Uijk+1
= ,
t t
2 Uijk+2 k+2
Ui+1,j 2Uijk+2 + Ui1,j
k+2
=
x2 | x{z
2
}
LATE STEP
2 Uijk+1
and as before.
y 2
k k
The boundary conditions specify Uoj , Um+1,j , Uiok , Ui,p+1
k
, and initial condi-
0
tions specify Uij . So we are solving for 1 k m, 1 i m, 1 j p.
ie. for each interior point at time tk .
We solve the problem for both time steps tk+1 and tk+2 at the same time
using early and late definitions. If we let sx = t/x2 , sy = t/y 2
then Ut = (Uxx + Uyy ) becomes:
Early step:
Late step:
40
z=z
r = x2 + y 2
= arctan( xy )
41
r = x2 + y 2
= arctan( xy ).
r sin
= + = cos
x x r x r r
r cos
= + = sin +
y y r y r r
1 T
(r, t) 52 T (r, t) = S(r, t)
t | {z }
source term
| {z }
2 -D heat equation
where the source term due to the radioactive decay of rod is given by:
(
Trod et/0 /a2 for r a
S(r, t) =
0 elsewhere.
42
2-D problem to 1-D!
How do we evaluate 52 T in polar co-ordinates?
2T sin T sin T
Txx = 2
= (cos )(cos )
x r r r r
2 sin cos sin2 2 cos sin sin2
= cos2 Trr Tr + Tr + T + T
r r r r
cos T cos T
Tyy = (sin + )(sin + )
r r r r
2 cos sin 2 cos sin cos2 cos2
= sin2 Trr + Tr T + Tr + T
r r2 r r2
1 12
and Txx + Tyy = Trr + Tr + T
r r
1 T 2T 1 T
2 = S(r, t)
K t r r r
We know that in the steady state solution eventually the nuclear rod is no
longer radioactive and stops releasing heat: S(r, t) 0 as t , and
further enough away from the rod the temperature equals the environment
temperature, T (r = rc , t) = 300K. So the solution should approach the
environmental temperature T (r, t) = 300K once rod has finished radioactive
decaying.
We use finite differences to solve:
1 T 2T 1 T
2 = S(r, t) (5.4)
K t r r r
We observe that there is a singularity at r = 0 in the above equation where
special care needs to be taken so that the numerical solution is stable.
Initial conditions T (r, 0) = 300K.
Neumann boundary conditions at r = 0 (temperature cannot flow into r = 0
region)
T
(r = 0, t) = 0
r
43
Dirichlet boundary conditions at r = rc
T (r = rc , t) = 300K
Tjk (r = rc , t) = Tn+1
k
= 300
We will use the backward Euler method (implicit) to solve the PDE. This
means evaluating the spatial derivatives in r at the future time step tk+1 :
Tjk+1 Tjk
Tt (tk+1 , rj ) =
t
Tj+1 2Tjk+1 + Tj1
k+1 k+1
Trr (tk+1 , rj ) = (centred difference at tk+1 )
r2
k+1 k+1
Tj+1 Tj1
Tr (tk+1 , rj ) = (leap-frog in space)
2r
1 1
Tt Trr Tr = S(r, t)
r
k+1 k+1 k+1 k+1 k+1
1 T 2Tj + Tj1 1 Tj+1 Tj1
[Tjk+1 Tjk ] [ j+1 2
] [ ] = Sjk
t
| {z } | r
{z } jr 2r
| {z }
Tt / Trr Tr /r
k+1 s k+1 s
Tj+1 [s ] + Tj1 [s + ] + Tjk+1 [1 + 2s] = Tjk + Sjk t (5.5)
2j 2j
44
Numerical solution of the 1-D heat equation in polar co-ordinates
using the Backward Euler method
For n = 3:
-
r0 = 0 r1 r r2 r3 rn+1 = rc = r4
(s + s ) (s 2js ) k+1
2j
1 + 2s T2 + 0
s k+1 s k+1
0 (s + 2j ) 1 + 2s T3 (s 2j )T4
T1k S1k
k k
= T2 + t S2
T3k S3k
(1 + s + 2js ) (s 2js ) 0 T k+1
s s 1k+1
(1 + 2s) (s 2j )
(s + )
2j T2
0 (s + 2js ) (1 + 2s) T3k+1
T1k S1k
0
k k
= T2 + t S2 0
k k s k+1
T3 S3 (s 2j )T4
(1 + s + 2s ) (s 2s ) T1k+1
0
(s + 4s ) (1 + 2s) (s 4s ) T2k+1
0 (s + 6s ) (1 + 2s) T3k+1
T1k S1k
0
k k
= T2 + t S2 0
k k s
T3 S3 (s 6 )300
Or to simplify we are solving the following matrix equation for the vector of
unknown temperatures T~ k+1 :
~ k + ~b
AT~ k+1 = T~ k + tS
45
The matlab code NuclearWaste.m can be used to check that solution
for T 300K as t (steady state approaches environment temperature,
300K).
300.4 300.7
300.2 300.6
Temperature
Temperature
300 300.5
299.8 300.4
299.6 300.3
299.4 300.2
299.2 300.1
299 300
0 10 20 30 40 50 60 70 80 90 100 0 10 20 30 40 50 60 70 80 90 100
r r
Figure 5.3: Initial conditions in (a) and matlab solution using Backward
Euler method for temperature distribution near nuclear rod at different time
intervals in (b)
Figure 5.3 shows the initial conditions and temperature distribution near the
nuclear rod at different time intervals.
46
Part II
Numerical solution of
hyperbolic equations
47
Chapter 6
Utt c2 Uxx = 0
or ( c )( + c )U = 0 (6.1)
t x t x
This is a hyperbolic equation since A = 1, C = c2 , B = 0 so that AC < B 2
48
g(x + ct) defines waves that travel in left direction with speed c
The pulses move without dispersion and the initial pulse breaks into a
left and right pulse.
Again we assume U (x, t) = X(x)T (t) then substitute into equation 6.2:
X = A sin(kx) + B cos(kx)
49
where an , bn are given by initial conditions:
U
U (x, 0) = U0 (x), (x, 0) = V0 (x)
X
t X
U0 = bn sin(kn x) V0 = an n sin(kn x)
n n
RL
using orthogonality of sine functions: 0 sin(km x) sin(kn x)dx = nm
2ZL
bm = U0 (x) sin(km x)dx
L 0
2 ZL
am = V0 (x) sin(km x)dx
wm L 0
where kn = n/L and km = m/L.
50
Chapter 7
~
U f ~ ~ ~
= (U , Ux , Uxx , ...)
t x
Ut = cUx , x0 x x1 , t0 t T
1 x0
Again we discretise problem x = xn+1 , t = T t m
0
and let xj = x0 +
jx, j = 0, . . . , n + 1, tk = t0 + kt, k = 0, . . . , m, and Ujk = U (xj , tk ).
Ujk k
Uj+1 k
Uj1
= + O(x2 )
x 2x
Using FTCS method: Ut = cUx gives:
ct k
Ujk+1 = Ujk k
[U Uj1 ] (7.2)
2x j+1
52
only through successive powers of ( k ). Difference equations are unstable
if |(p)| > 1 for some p. is called the amplification factor.
To find (p) for FTCS method substitute Ujk = k eipjx into equation 7.2:
ct ipx
k+1 eipjx = k eipjx eipx )
1 (e
2x | {z }
2i sin(px)
ict
(p) = 1 sin(px)
x
and |(p)| 1 p FTCS scheme is unconditionally unstable for solving
Ut = cUx .
The Lax method is conditionally stable. To see substitute Ujk = k eipjx into
equation 7.4:
1 ct ipx
[eipx + eipx ]
k+1 eipjx = k eipjx (e eipx )
2
} 2x
| {z }
| {z
2i sin(px)
cos(px)
ct
= cos(px) i sin(px)
x
Lax method stable when ||2 1 :
2 c2 t2
| cos (px) + 2
sin2 (px)| 1
x
53
c2 t2
or |1 (1 ) sin2 (px)| 1
x2
c2 t2
1 0
x2
c2 t2
or 1
x2
x
or t (c > 0)
| {z c }
COURANT CONDITION
t 6
s s s s s
6
Ujk+1 x t
s s s s
-s?
J
J
s s s s s
J
J
k k k
Uj1
Uj J U
J j+1
J -
J
wave speed c
J
- x
54
Unstable schemes arise when x t
c ie. when the time step t be-
k+1
comes too large because Uj requires information from points outside
k k
[Uj1 , Uj+1 ] as shown in the plot below. (see Press et al, Numerical
Recipes, p. 825-830)
t 6
Ujk+1
s s s s
-s
J
J
x 6
J
J
J
J t
J
J
J
s
s s s J s?
J
Uk Ujk k
Uj+1 J
j1 J
J -
J
x
wave speed c
J-
J
Utt = c2 Uxx
! !
r cUx
let w
~ = =
s Ut
1 k ct k
rjk+1 = [rj1 k
+ rj+1 ]+ (sj+1 skj1 ) (7.5)
2 2x
55
st = crx becomes:
1 ct k
sk+1
j = [skj1 + skj+1 ] + k
(r rj1 ) (7.6)
2 2x j+1
x
This is stable if ||2 1 which gives same Courant condition t c
.
ie. this mode is completely undamped and the amplitude is constant (no
dispersion) for the numerical solution using a time step which satisifes
this dispersion relation.
56
Dispersion relation for the Lax Method
The dispersion relation is only satisfied if: t = x c
.
k k ipjx
Why? Consider Uj = e
In section 7.4.1 we found:
ct
= cos(px) i sin(px)
x
ct
= eipx + i(1 ) sin(px)
x
If we let t = xc
= eipx and Ujk = k eipjx = eip(kx+jx)
When we substitute xj = jx, tk = kt and the dipsersion relation x =
ct then: Ujk = eip(ckt+jx) = eip(xj ctk ) = f (xj ctk ) .
| {z }
exact solution
Thus the Lax method has no dispersion present when the time step sat-
isfies the dispersion relation exactly: t = x
c
. We will show this in the next
section. x
Exact solution
initial pulse final pulse
t=0 t=1
-
0.2 0.4 1.2 1.4
U (x, t) = U 0 (x ct)
57
= U 0 (x t) (c = 1)
In the next section we compare the above exact solution with the numerical
solution using the Lax method with different time steps:
x
t = c
no dispersion matches analytic solution.
x
t = 2c
dispersion present but pulse matches speed of wave.
1.001x
t = c
courant condition not met unstable!
x0 x1 x2 x3 x4
U1k+1 1
U1k 1
(1 + s)U0k
0 2
(1 s) 0 2
k+1 1 1
U2 = 2 (1 + s) 0 U2k +
(1 s) 0
2
k+1 1 k 1 k
U3 0 2
(1 + s) 0 U3 2
(1 s)U4
or U ~ k + ~b
~ k+1 = AU
Dispersion means the initial pulse changes shape (unlike analytical solution)
because wave components with different frequencies travel at different speeds.
The matlab code is Lax Flux.m.
The numerical solution changes for different time steps depending on
whether or not the scheme is stable or dispersion is present. Figure 2.1 shows
how the solution changes for different time steps depending on whether or
not the scheme is stable or dispersion is present.
58
Final solution at t=1 Final solution at t=1 Final solution at t=1
1 1 1
U
0.4 0.4 0.4
0 0 0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x x x
Figure 7.1: Solution at t = 1 using the Lax method with different time
steps, (a) t = x/2c where dispersion is present but the pulse matches
the analytical solution for the speed of the wave, (b) t = x/c where
no dispersion is present and numerical solution matches analytical solution
exactly, and (c) t = 1.001x/c where the Courant condition is not met
and solution is becoming unstable.
Ut + U U
| {z x}
=0
nonlinear term
nonlinear term causes wave profile to steepen resulting in a shock.
59
1
0.5
-0.5
-1 x
1
2
-1.5
0 1 2 3 4 5 6
x
Figure 7.2: Aliasing error occurs when the mesh spacing x is too large to
represent the smallest wavelength 1 and misinterprets it as a longer wave-
length oscillation 2
60
Chapter 8
Utt = c2 Uxx , 0 t T, 0 x a
T a
Discretise: t = m , x = n+1 ,
tk = kt, 0 k m, xj = jx and 0 j n + 1.
Q
Q
Q
Q
Q
Q
Q
Q
0 0.8a a
61
Plucked string is released from rest:
U
(x, 0) = g(x) = 0
t
x0 x1 x2 x3 x4
U11 U10
2(1 s) s 0
~1
U = U21 =
s 2(1 s) s 0
U2
1
U3 0 s 2(1 s) U30
| {z }
A
62
U00 U11
1
+ s 0 U2
U40 U31
| {z }
b
U11
2(1 s) s 0 f1
~1 1 1
U = U2 = s 2(1 s) s f2
2
U31 0 s 2(1 s) f3
U0
g1
s 0
+ 0 + t g2
2
U40 g3
~ 1 = 1 AU
U ~ 0 + 1~b + d~
2 2
0 0
For this example, U0 = 0, U4 = 0 and:
Uj0
(x, t = 0) = g(xj ) = 0 d~ = ~0
t
~ 2, . . . , U
for U ~ m we have:
U ~ k + ~b U
~ k+1 = AU ~ k1
63
Initial condition for plucked string
1
0.9
0.8
0.7
0.6
0.5
U
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30 35 40 45 50
x
Figure 8.1: Initial conditions in (a) and matlab solution using explicit central
difference method for 1D wave equation in (b)
1
U (x, t) = [f (x ct) + f (x + ct)] since Ut (x, 0) = 0
2
where U (x, 0) = f (x) (initial conditions) for < x <
What if we want to solve the wave equation for 0 x a, with fixed bound-
ary condition U (t, 0) = 0 = U (t, a)? We can extend DAlemberts general so-
lution for Utt = c2 Uxx with initial conditions:U (x, 0) = f (x) Ut (x, 0) = g(x):
f (x + ct) + f (x ct) 1 Z x+ct
U (x, t) = + g(z)dz
2 2c xct
for x
In our example we have initial conditions:
(
1.25x
a
, 0 x 0.8a
Ut (x, 0) = 0, U (x, 0) = f (x) = x
5(1 a ), for x 0.8a
0xa
with fixed boundary conditions:
The boundary condition U (0, t) = 0 is equivalent to f and g being odd
functions:
U (0, t) = 0 f (x) = f (x)
g(x) = g(x)
(f and g are odd functions)
64
The boundary condition U (a, t) = 0 is equivalent to f and g being peri-
odic with period 2a:
f (x + ct) + f (x ct)
U (t, x) =
2
and we can compare the analytical solution with the numerical solution in
figure 8.2.
Figure 8.2: DAlemberts solution in (a) and error using numerical matlab
solution using explicit central difference method for 1D wave equation in (b)
U + 2 U = c2 Uxx , 0 x a = 50, 0 t T = 20
The friction term opposes motion of string and means that eventually
vibrations decay with time.
65
Suppose string is initially plucked in 2 places:
@ @
@ @
@ @
0 0.1a 0.3a 0.7a 0.9a a
Ut (x, 0) = 0
and boundary conditions: U (x, 0) = 0, U (x, a) = 0.
Again we use central difference for Uxx and Utt as in section 8.1.1.
We use a leap-frog step for Ut
66
We evaluate Uj1 :
1
Uj1 = 2(1 s)Uj0 (1 t) Uj1 +s(Uj+1
0 0
Uj1 )
1 + t
| {z }
=Uj1
2 1 n o
Uj1 = 2(1 s)Uj0 + s(Uj+1
0 0
Uj1 )
1 + t 1 + t
1 n o
Uj1 = 2(1 s)Uj0 + s(Uj+1
0 0
Uj1 )
2
Example n = 3
x0 = 0 x1 x2 x3 x4 = a
U1k+1 U1k
2(1 s) s 0
~ k+1 1
= U2k+1 =
k
U
s 2(1 s) s U2
1 + t
k+1
U3 0 s 2(1 s) U3k
| {z }
A
k1
U0k
U1
s 1 t
+ 0 U2k1
1 + t 1 + t
U4k | {z } U3k1
| {z } e
b
~ k + ~b eU
= AU ~ k1
The numerical solution is plotted in figure 8.3 below.
67
Initial condition for plucked string
250
200
150
U
100
50
0
0 5 10 15 20 25 30 35 40 45 50
x
Figure 8.3: Initial conditions in (a) and matlab solution using explicit central
difference method for 1D wave equation with friction in (b)
..
.
y3
y2
y1
y0
x 0 x1 x2 x3 ... xn xn+1 = a
T a b
We discretise: t = m , x = n+1 , y = p+1 , tk = kt, xi = ix, yj =
jy0 k m, 0 i n + 1, 0 j p + 1, and let Uijk = U (tk , xi , yj )
68
Suppose we solve for n = 3 and p = 3 and have Dirichlet boundary
conditions:
k k k
U (0, y, t) = 0 = Uoj , U (a, y, t) = 0 = Un+1,j = U4j , U (x, 0, t) = 0 =
k k k
Ui0 , U (x, b, t) = 0 = Ui,p+1 = Ui4
and initial conditions:
U (x, y, 0) = f (x, y) = fij Ut (x, y, 0) = g(x, y) = gij .
Since we have Dirichlet boundary conditions: the outer boundaries of the
k k k k
region we are solving for are known: U0,j , Un+1,j , Ui,0 , Ui,p+1 , and we need to
k
find the interior values: Ui,j for 1 i n and 1 j p.
y4 = yp+1 = b
y3 h h h
U13 U23 U33
y2 h h h
U12 U22 U32
y1 h h h
U11 U21 U31
y0
x0 x1 x2 x3 x4 = xn+1 = a
t2 t2
We let sx = x2
, sy = y 2
and substitute the central difference approx-
69
imations into our PDE, Utt = (Uxx + Uyy ) we solve for Uijk+1 :
U ~ k + ~b U
~ k+1 = AU ~ k1
where:
sy 0 sx 0 0 0 0 0
s
y sy 0 sx 0 0 0 0
0 sy 0 0 sx 0 0 0
s
x 0 0 sy 0 sx 0 0
A =
0 sx 0 sy sy 0 sx 0
0 0 sx 0 sy 0 0 sx
0 0 0 sx 0 0 sy 0
0 0 0 0 sx 0 sy sy
0 0 0 0 0 sx 0 sy
and = 2(1 sx sy )
70
k k
sx U01 + sy U10
k
sx U02
k k
sx U03 + sy U14
k
sy U20
b =
0
k
sy U24
k k
sx U41 + sy U30
k
sx U42
k
sx U43 + sy sk34
where
E
8.1 EUxx = Utt or Utt = Uxx
q
E
elastic waves propagate with speed
c2 2 E = E and c2 2 B = B (8.2)
E
B = 0, B = 0 0 (8.4)
t
~ ~ 2~
8.4 and using ( V ) = ( V ) V
taking curl of 8.3 and
and ( E) = 0 = 0, ( B) = 0 gives Equation 8.2 where
q
1
c= 0 0
= 3 108 m/s.
71
3. Schrodingers Wave Equation
ih = H
t
for a wavefunction of a quantum system defined by Hamiltonian,
H.
2 2
eg. H = KE + P E = h2m + V (r)
R
numerical solutions also need to satisfy |(x)|2dx = 1
72
Chapter 9
73
Figure 9.1: FEM mesh with triangles
Equation (9.3) holds for all functions (x) which are piece-wise continous
and satisfy the bc: (0) = (L) = 0.
To solve equation (9.3) using the FEM we again introduce a mesh (as in
FD) on the interval [0, L] with mesh points xj = jx, j = 0, . . . , n + 1 where
L
x = n+1 . To complete the discretisation we must choose a basis for (x).
The most common basis chosen for (x) are the hat functions, j (x).
We solve (9.3) using these:
n
X
(x) = aj j (x)
j=1
where
74
0 x xj1
0, for
1
(x xj1 ), for xj1 x xj
j (x) = x
1
1 x (x xj ), for xj x xj+1
0, for x xj+1
6
1
A
A
A
j (x)
A
A
A
A
A
A -
xj1 xj xj+1
-
75
This simplifies equation 9.3 and we solve for = i , i = 1, . . . , n:
ie.
Z L Z L
0 0
Uh (x)i (x)dx = q(x)i (x)dx, for i = 1, . . . , n
0 0
0 f
where f (x) = x
.
Z L
0 0
LHS = Uh (x)i (x)dx
0
n
Z LX
0 0
= bj j (x)i (x)dx
0 j=1
n
X
= Ci,j bj
j=1
0 0
where Ci,j = 0L j (x)i (x)dx. Ci,j is known as the stiffness matrix in me-
R
chanics.
To find the coefficients bj which define our solution U (x) we must solve
n linear equations:
n
X Z L
LHS = Ci,j bj = RHS = q(x)i (x)dx = qi (9.4)
j=1 0
define our solution U (x) and FEM solves the following equation for vector
~b = (b1 , . . . , bn ):
n
X Z L Z L
bj j,x (x)i,x (x)dx = q(x)i (x)dx,
j=1 0 0
n
X
or bj Ci,j = qi
j=1
for i = 1, . . . , n.
We will show that the stiffness matrix C is tridiagonal for this example.
We are solving the above system for coefficients bj , thus we are solving C~b = ~q
and can use iterative methods in FEM solutions too.
We can show that the stiffness matrix is tridiagonal:
1
x
, i=j1
Z L 2 ,
i=j
Cij = j,x (x)i,x (x)dx = x
1
0 , i=j+1
x
0, elsewhere
76
We approximate qi using:
Z L Z L
qi = q(x)i (x)dx q(xi ) i (x)dx
0 0
Z xj Z xj+1 !
1 1
= q(xi ) (x xj1 )dx + 1 (x xj )dx
xj1 x xj x
= xq(xi )
We can substitute the above simplifications into equation 9.4 and arrive
at C~b = x~q or x
1
C~b = ~q:
2 1
x2 x2
0 ...
b1
q1
1 2 1 ...
x2 x2 x2
b
2
q2
. = .
.. .. .. . .
0 . . . . .
.. .. 1 2
bn qn
. . x2 x2
Thus the matrix system to solve is the same as FD solution in this example
and the solution involves inverting the stiffness matrix C:
~b = C 1 x~q
Iterative methods are useful in FEM too as it involves inverting large, sparse
matrices.
Once ~b is known, the solution U to the PDE is given by:
n
X
U (x) bj j (x)
j=1
9.2.1 FD solution
L
Discretise using xj = jx, j = 0, 1, . . . , n + 1 where x = n+1
, U0 = 0 =
Un+1 (using boundary conditions).
77
We let U (xj ) = Uj , q(xj ) = qj . The central difference approximation to
the PDE is:
Uj+1 2Uj + Uj1
Uxx =
x2
and Uxx = q becomes
Uj+1 + 2Uj Uj1
= qj
x2
We solve for U1 , . . . , n since U0 and Un+1 given from boundary conditions
and we can rewrite in matrix form:
2 1
0 ...
Ux
0 (=0)
x2 x2 U1 2 q1
1 2 1 ..
.
x2 x2 x2
U2 0
q2
.. + ..
= ..
... ... ...
0 . . .
.. ... 1 2
Un Un+1 =0
qn
. x2 x2 x2
| {z }
same coefficient matrix for FD as FEM
In this example FEM and FD methods solve the same matrix system.
78
We consider a triangular mesh (could also be rectangular) shown in figure
9.2 where G is the domain inside the circle and G is the domains boundary.
We are solving:
2 U = q in G (9.5)
with boundary conditions, U = 0 on G.
The weak solution for U satisfies the variational form for Equation 9.5:
Z Z Z Z
2 U (x, y)(x, y)dxdy = q(x, y)(x, y)dxdy (9.6)
G G
79
Figure 9.3: 2D hat function (j (xj , yj ) = 1, j (xi , yl ) = 0 if i 6= j and j 6=
l)
Z Z n
X
LHS = Uh i dxdy = Ci,j bj
G j=1
If C is symmetric and positive definite then the system has a unique solution.
80
We are using Einstein notation and according to this convention if an index
appears twice in a single term it implies we are summing over all possible
values:
2 fi 2 f1 2 f2
ai f,ii = ai = a 1 + a 2
x2i x21 x2
We will see that the FEM takes care of spatial derivative in the problem
below. However we still need to approximate time derivatives.
We want to solve the 2-D wave equation for a point source:
where p is the wave speed, is the wave-field and FPS is the force due to
the point source.
In eScript this becomes:
Da = Xj,j + Y
where a = tt
D = 1
Xj = Vp2 ,j
Y = FPS
Figure 9.4: Plot of Euclidean normal of the displacement at t > 0 for a point
source using eScript.
81
formula:
2 k k+1 2k + k1
ak =
t2 t2
or
k+1 = 2k k1 t2 ak
The eScript python code is 2Dpointsource.py and the output from this
code is shown in figure 9.4.
82
Chapter 10
Spectral methods
Like the FEM, the spectral method also approximates the solution
U (x).
We will show an example using the spectral method where U (x) is ex-
panded as a Fourier series and this series and its spatial derivatives are then
substituted into the PDE resulting in a system of ODEs in time.
83
10.1.1 Example 1: Comparing the accuracy of solu-
tions of a variable speed wave equation with
either the spectral or finite difference method
Spectral method for variable speed wave equation
In this example we will compare the accuracy of either the spectral or finite
difference method when solving the 1D advection equation with a variable
wave speed, c(x) = 51 + sin2 (x 1). First we will derive the solution using
the spectral method:
xj = jx, j = 0, 1, 2, . . . , 2n 1
tk = kt, k = 0, 1, 2, . . . , m
U (xj , tk ) = Ujk
The spectral method uses the discrete Fourier transform of U (xj , t):
U = F (U ),
2n1
X
= U (xj , t) exp(ixj )
j=0
2n1
X
= U (xj , t) exp(i2j/(2n)), using xj = jx = j2/2n
j=0
2n1
X
= U (xj , t) exp(ij/n)
j=0
for = n + 1, . . . , n.
U (xj , t) is then defined as the inverse discrete Fourier transform of U :
U (xj , t) = Uj = F 1 (U ),
n
1 X
= U exp(ixj )
2n =n+1
n
1 X
= U exp(i2j/(2n)
2n =n+1
84
where j = 0, . . . , 2n 1.
With this definition the spatial derivatives are:
n
U (xj , t) 1 X
= i U exp(ixj )
x 2n =n+1
= F 1 (i U )
= F 1 (iF (U ))
We solve the advective equation with variable wave speeds and compare
the solution with either FD or spectral method:
Ut + c(x)Ux = 0
1
where c(x) = + sin2 (x 1)
5
ic: U (x, 0) = exp(100(x 1)2 )
periodic bc: U (0, t) = U (, t)
1 t
Uj1 = U (x, t) = U (x + c(x)t) U 0 (x + t) = exp(100(x + 1)2 )
5 5
The matlab code is spectral variable wave speed.m.
Ut + c(x)Ux = 0
85
This matlab code is fd variable wave speed.m.
The Lax method is used for Ut and central difference method for Ux :
U Ujk+1 21 (Uj1
k k
+ Uj+1 )
=
t t
k k
U Uj+1 Uj1
=
x 2x
c(xj ) = cj .
Ujk+1 21 (Uj1
k k
+ Uj+1 ) k
U k Uj1
+c(xj ) j+1 = 0
| t
{z } | 2x
{z }
Ut Ux
1 1
or Ujk+1 k
= (1 + scj )Uj1 k
+ (1 scj )Uj+1
2 2
x0 x1 x2 x3 x4
~ k + ~b
~ k+1 = AU
or U
Figure 10.1(b) shows that the solution using finite differences is much worse
than the spectral method because dispersion is introduced in FD method
when a variable wave speed is applied. However figure 10.1(a) shows the
spectral method performs well when smooth initial conditions of a Gaussian
pulse are used and there is very little dispersion present.
86
Variation of U with time Variation of U with time
1.5
1
1
0.8
0.5
U
0.6
0
U
0.5 0.4
10
0.2
8
0
6 10
4
5
2
t
0 t 5 6 7
6 7 0 2 3 4
0 1 2 3 4 5 0 1
x x
Figure 10.1: Numerical solution for 1D advection equation with initial condi-
tions of a smooth Gaussian pulse with variable wave speed using the spectral
method in (a) and finite difference method in (b)
Ut + Ux = 0
(
1, 0.5 x 1.
ic: U (x, 0) =
0, otherwise
periodic bc: U (0, t) = U (, t)
Figure 10.2(a) shows that the solution with an initial condition which is not
smooth like the box pulse we used here using the spectral method is much
worse than the finite difference method. This is because the spectral method
uses Fourier series to approximate the initial conditions and is unable to
87
Variation of U with time Variation of U with time
1.5 1
0.8
1
0.6
0.5
U
U
0.4
0
0.2
0.5
0
5
5
4
4
3 3
2 2
1 1
t 6 7 t 7
4 5 5 6
0 2 3 0 3 4
0 1 1 2
0
x x
Figure 10.2: Numerical solution for 1D advection equation with initial con-
ditions of a box pulse with a constant wave speed using the spectral method
in (a) and finite difference method in (b)
88
Part III
89
Chapter 11
Shock wave
90
11.1.1 Example 1: Using method of characteristics to
solve the linear 1-D advection equation
Ut + cUx = 0
initial conditions: x(s = 0) = x0 , t(s = 0) = 0
U (x, t = 0) = f (x) or U (s = 0) = f (x0 )
dx
= c x = cs + k1 , use x(0) = x0 = k1
ds
x = cs + x0
dt
= 1 t = s + k2 , use t(0) = 0 = k2
ds
t=s
and since t = s x = ct + x0
Solve for U :
dU dt dx
= Ut + Ux = Ut + cUx = 0
ds ds ds
ie dU
ds
= 0 U = k3 (U is constant along characteristic curves).
Use initial conditions U (s = 0, x0 ) = f (x0 ) = k3
ie U = f (x0 ) = f (x ct) since x0 = x ct.
This is the same as DAlemberts solution for a wave moving to the right at
speed c. The characteristic curves are given by: x = x0 + ct or t = 1c (x x0 ).
t 6
-
1
slope (= c
) and U are constant along curves x
91
Now the wave speed is not constant but depends on the amplitude U (x, t).
The characteristic equations are:
dt
= 1 t = s (using t(0) = 0)
ds
dx
= U x = U t + x0 (using x(0) = x0 and t = s)
ds
Again:
dU dt U dx U
= +
ds ds t ds x
= Ut + U Ux = 0
U = k3 = f (x0 ) = f (x U t)
1 1
t= (x x0 ) = (x x0 )
U f (x0 )
The characteristic curves no longer have constant slope - they may cross
(meaning U is multiply defined shock waves) or be discontinuous (regions
with no solution for U expansion waves) as we will see in the next example.
Example
Solving Ut + U Ux = 0 with the following initial conditions:
(
U1 , x > 0
U (x, t = 0) = f (x) =
U2 , x < 0
( 1
U1
(x x0 ), x > 0 or x = U1 t + x0
t= 1
U2
(x x0 ), x < 0 or x = U2 t + x0
2 cases
92
Case 1: Shock wave U1 < U2
6
t
x = U1 t x = U2 t
-
x<0 x>0 x
1 1
t= U2
(x x0 ) t= U1
(x x0 )
x<0 x>0 x
1 1
t= U2
(x x0 ) t= U1
(x x0 )
The solution is single-valued for t > 0 unlike the shock wave case. How-
ever in wedge between x = U2 t and x = U1 t there is no information. We
assume x = Ut in wedge since U2 t x U1 t and speeds vary U2 U U1
93
and add solution to the wedge.
U2 , xt < U2
x
Thus U = t
, U2 < xt < U1
U1 , xt > U1
U t=0 U t>0
6 U1 6'$U1
U2 U2 rarefaction
&%
- -
x centred expansion wave x
Ut + U Ux = 0, 0 x 1, 0t1
94
1
t=0
0.9 t>0
0.8
0.7
0.6
U
0.5
0.4 rarefaction shock
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5
x
Ut + U U x = 0
Ut + U Ux = 0 becomes:
1n k o
Ujk+1 = Uj+1 (1 sUjk ) + Uj1
k
(1 + sUjk ) (11.3)
2
t
Where s = x
Use boundary conditions U (0, t) = U (1, t) = 0 and for 4 elements:
x0 x1 x2 x3 x4
95
So U0k = 0 = U4k given by boundary conditions and we can rewrite Equa-
tion 11.3 as a matrix system of equations:
0.8
0.6
U
0.4
0.2
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Figure 11.2: Initial conditions in (a) and solution for nonlinear Bugers equa-
tion using the Lax method in (b)
96
11.2.2 Example II: Solution using Method of Lines
The matlab code is Shock mol.m and Uprime2.m.
U
= U Ux
t
Using the method of lines solution (as demonstrated in section 2.3) we only
replace spatial derivative Ux with FD approximation.
x0 x1 x2 x3 x4
or
~ = 1 ~ + ~b
U A(U )U
2x
When the shock develops in figure 11.3(b) the numerical solution becomes
unstable using the method of lines.
Ut = U Ux , 0 x 2
x
(we can change variable: = 2
later so that the range for is 0 1)
97
Initial condition for U
1
0.9
0.8
0.7
0.6
0.5
U
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Figure 11.3: Initial conditions in (a) and solution for nonlinear Bugers equa-
tion using the method of lines in (b)
Spectral method
We let U (xj , tk ) = Ujk , xj = jx, j = 0, 1, . . . , 2n 1, tk = kt, k =
T
0, 1, . . . , m, and t = m .
Take the discrete Fourier transform of U :
2n1
X
U = F (U ) = U (xj , t) exp(ixj ) for = n + 1, . . . , n
j=0
j
where xj = jx =
n
then
n
1 X
Uj = F 1 (U ) = U exp(ixj )
2n =n+1
for j = 0, 1, . . . , 2n 1
and
Ujk 1 X n
= i U exp(ixj )
x 2n =n+1
= F 1 (i U )
= F 1 (i F (U ))
Ujk+1 Ujk1
Ut =
2t
98
then Ut = U Ux becomes:
Uj1 = U (x, t) = U 0 (x U t) = f (x U t)
f (x + t) = exp(10(4(x + t) 1)2 )
0.8
1
0.6
U
0.8
0.6
U
0.4 0.1
0.4
0.08
0.2
0.06
0.2
0
0.04
0
0.2
0.4 0.02
0 0.6
0.8 x
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0
1
x
t
Figure 11.4: Initial conditions in (a) and solution for nonlinear Bugers equa-
tion using the spectral method in (b)
99
Chapter 12
12.1 Solitons
solitons or solitary waves result from solution of the KdV equation
solitons pass through each other without change of form except shifted.
Ut + U Ux + Uxxx = 0
100
Let U = f () = f (x V t) where = x V t then:
= = V , = =
t t x x
df
Let f 0 () =
then Ut + U Ux + Uxxx = 0 becomes: (using U = f () =
f (x V t))
V f 0 + f f 0 + f 000 = 0
d f2
We integrate once: (use f f 0 = ( ))
d 2
Z
0
Z
d f2 Z
V f d + ( )d + f 000 d = 0
d 2
f2
V f + + f 00 = C (12.1)
2
Multiply by f 0 and integrate again:
2
0f
Z Z Z Z
0 0 00
V f f d + f d + f f d = Cf 0 d + c0
2
Z Z Z
V 2
Term 1 = V f f 0 d = V f 2 V f f 0 d V f f 0 d = f
2
2
Z
0f f3 Z 2 0 Z
f2 f3
Term 2 = f d = f f d f 0 d =
2 2 2 6
Z
0 00 0 2
Z
00 0
Z
0 00 f 02
Term 3 = f f d = (f ) f f d f f d =
2
So multiplying 12.1 by f 0 and integrating again gives:
V 2 f 3 f 02
f + + = Cf + C0 (12.2)
2 6 2
101
100
90
80
70
60
f(x)
50
40
30
20
10
0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x
q
Figure 12.1: The initial conditions U (x, 0) = f (x) = Asech2 ( A
12
x).
f3
f 02 = V f 2 (12.3)
3
Use
A
f (0) = A, f 0 (0) = 0 0 = A2 (V )
3
A
V =
3
Since f 0 () 0 for 0 we take the negative square root of 12.3:
f q
0
f = Af
3
This can be integrated analytically by making a change of variable if we let
f = Asech2 then df = 2Asech2 tanh d and integrating:
f q
0
f = Af
3
Since we assumed 0 we integrate from = 0 in integration limits:
df
Z
d
d 1 Z
= d
0 f Af 3 0
Z f () Z f
df df
= =
3 f (0) f Af A f Af
102
now substitute f = Asech2
Z
2Asech2 tanh d
= q
0
Asech2 A Asech2
Z
2 tanh d
= q
0
A 1 sech2
now use 1 sech2 = tanh2
Z
2 2
d = =
0 A A 3
s
A
=
12
U (x, t) = f () = Asech2
s
A
= Asech2
12
s
A A A
U (x, t) = Asech2 (x t) (using V = )
12 3 3
| {z }
soliton travelling to right
1
where sechx = coshx
and coshx = 12 (ex + ex ).
Ut + U Ux + Uxxx = 0, 0 x 2
with periodic boundary conditions U (0) = U (2).
and initial conditions:
s
A
U (x, 0) = Asech2 ( (x )), A = 100
12
The analytical solution is:
s
A A
U (x, t) = Asech2 (x t)
12 3
If we apply the spectral method directly we find that the linear term, Uxxx ,
involves high frequencies making the numerical solution unstable as we will
see in section 12.3.1. Section 12.3.2 shows how to modify this term to gain
stability using a modified spectral method.
103
12.3.1 Solving directly with Spectral Method
Ut + U Ux + Uxxx = 0
Ujk 1 X n
Ux = = U (i) exp(ixj )
x 2n =n+1
= F 1 (i U ) = F 1 (iF (U ))
and:
3 Ujk 1 X n
Uxxx = = U (i 3 ) exp(ixj )
x3 2n =n+1
= F 1 (i 3 U ) = F 1 (i 3 F (U ))
At high wavenumbers, , this term causes instabilities in solution.
We use a leap-frog approximation for Ut :
Figure 12.2 shows that the numerical solution for the KdV equation blows
up using a direct spectral method. In the next section we modify the Uxxx
term causing instabilities.
104
Initial condition for U Variation of U with time
100
90
80
70 300
60 200
50 100
U
U
40
30 100
3
20 200
2
5
10 300 x 10
1
0
2
0 4
0 1 2 3 4 5 6 7 6 0 t
8
x
x
Figure 12.2: Initial conditions in (a) and solution for nonlinear KdV equation
using the direct spectral method in (b)
The last term approximating Uxxx makes PDE very stiff at high wavenum-
bers.
To remove this instability for high wavenumbers we replace the last term
with:
105
Variation of U with time
120
Final solution at t=one period
Initial Condition at t=0
100
100
80
60
80
U
40
20
60 0
U
0.2
40 0.15
0.1
20
0.05 8
6 7
4 5
0 t 3
0 1 2 3 4 5 6 7 0 1 2
0
x x
Figure 12.3: Initial conditions and final solution after one period in (a) and
solution for nonlinear KdV equation using a modified spectral method in (b)
Figure 12.3 shows that the numerical solution for the KdV equation is sta-
ble using a spectral method where we have modified the Uxxx term causing
instabilities.
The method of integrating factors can also be used to remove the insta-
bility due to Uxxx term (see Trefethen).
where A = 100.
Figure 12.5 shows the numerical solution for the KdV equation for 2
interacting solitons using the modified spectral method. In figure 12.5(a) we
plot the initial conditions and final solution after one period. We see that
after the interaction the smaller soliton is backward shifted and taller soliton
forward shifted in time.
106
200 V=2A/3
150
f(x)
100 V=A/3
50
0
0 1 2 3 4 5 6
x
Figure 12.4: The initial conditions U (x, 0) = f (x) is plotted to show the 2
solitions and their speeds
200
250
200
150
150
100
U
100
U
50
0
50
50
0.2
0.15 8
0
6
0.1
4
0.05
2
50
0 1 2 3 4 5 6 7 t 0 0 x
x
Figure 12.5: Initial conditions and final solution after one period in (a) and
solution for nonlinear KdV equation for two interacting solitons in (b)
107