Anda di halaman 1dari 77

Computational Physics

MAPH 4161


Dr. Sam Dolan

School of Mathematical Sciences

University College Dublin


Each week:

Lecture: Monday 2pm Seminar Room

Tutorial: Tuesday 10am Computer Lab

Lecture: Wednesday 11am Teaching Room.


Three in-class exams [20%]

Three assignments (problem sheets) [20%]

An end-of-semester exam [60%]

MATLAB: We will use Matlab in tutorials to test numerical
methods, and to visualise our results.

Blackboard: Course notes, problem sheets, assignments

and example Matlab programs will be posted on Black-

Website: Ill also put course material on my website, as a


Email: If you have any questions, email me:

Or visit office 6, Science Hub (2nd floor).
Computational Physics can be taught with the main em-
phasis coming from three different areas; the numerical
analysis, programming and the physics, although ultimately
it is a mix of all three.

On the numerical analysis front we examine techniques

which allow us to find approximate solutions to mathe-
matical problems; e.g. Runge-Kutta methods for solving
ordinary differential equations as we saw in second year.
The analysis of these techniques involves tools such as
the Taylor series which tell us how accurate and stable
our numerical methods are.

Programming or actually using our methods on a com-

puter is essential and in this course we will use the Mat-
lab language as we did in second year. New features of
Matlab will be picked up as we go along.

We will be interested in Physics problems and this will

largely determine our choice of numerical method and the
course layout.
Course Outline

Conservation laws in physics (hyperbolic equations): Mass

conservation in gas dynamics. Linear advection equa-
tion. Characteristics. Basic numerical methods. Conser-
vation laws. Lax Wendroff scheme and Burgers equation.
Shock formation. Traffic flow. Gas dynamics.

Heat flow and diffusion (parabolic equations): Basic equa-

tion of heat flow. Series approximation. Explicit and im-
plicit methods. Crank-Nicholson scheme. Thomas algo-
rithm. 2D and 3D problems. ADI methods.

Quantum mechanics and Schrodingers equation : Bound-

ary and eigenvalue problems in a one-dimensional, steady
potential. Numerical techniques for solving Schrodingers
equation. Scattering and tunnelling problems.

Electrostatics (elliptic equations): Electrostatic problems.

Relaxation methods. Iterative solution of linear algebraic
Monte Carlo methods and simulation: Random walks. Self-
avoiding random walks and polymer chains. Direct simu-
lation Monte Carlo methods in statistical mechanics. The
ideal gas model. The Ising model of ferromagnetism. The
variational technique in quantum mechanics.

Numerical Methods for Physics by A. Garcia (Prentice Hall).

Good mix of numerical methods, physics applications and
Matlab programmes.

Numerical Solution of Partial Differential Equations by K.W.

Morton and D.F. Mayers (Cambridge University Press).
Very good on the numerical analysis of pdes.

Computational Physics by N. Giordano. Physics based

book - not good for the analysis part of the course.

A First Course in the Numerical Analysis of Differential

Equations by A. Iserles (Cambridge University Press). Good
on the numerical analysis of odes and pdes

Numerical Methods for Conservation Laws by R.J. LeV-

eque (Birkhauser). Not for the faint-hearted ! But acces-
sible introductory chapters.
1. Conservation Laws in Physics

Before giving an exact definition of a hyperbolic equation

or a conservation law well have a brief reminder of the
basic principles, and some applications, taken from gas

1.1 Basic Principles

Consider a gas moving in one dimension, along the x

axis, and let (x, t) be the mass density at point x and
time t. If v(x, t) is the bulk gas flow velocity then v is the
mass flux at x and t. If matter is neither created nor de-
stroyed then the mass density at a point can only change
of there is a gradient in the mass flux,

+ (v) = 0
t x

or, in different notation,

t + (v)x = 0 (1)

which is the differential form of the mass conservation law

and the type of equation we are interested in solving.

If v is a known function of either (x, t) or then equation

1 can, in principle, be solved. Otherwise we need the
momentum conservation equation which in the absence
of viscosity is,

(v)t + (v 2 + P )x = 0

where P is the pressure and the energy conservation equa-


Et + (v(E + P ))x = 0

where E is the energy density. An equation of state is

also needed to relate E, P and .
All of these pdes have the same conservative form,

(Density) + (F lux) = 0
t x

and occur in many areas of physics outside gas dynamics;

plasma physics, general relativity etc. We want to study
methods of solving such equations when exact or analytic
methods are not available.

But first lets look at the kind of advanced results that can
be obtained in this area.

1. The equations of gas dynamics are vital to the under-

standing of the aerodynamics of aircraft. This is a compu-
tational problem and a sample result is shown below for
the cross sectional flow past a wing and flap profile. Red
denotes fast flow velocity while below denotes slow.

Taken from

2. Astrophysical fluid flow is an extremely important appli-
cation of computational methods for conservation laws.
As an example ( consider an
enormous cloud of gas which is 50 times as massive as
the sun, 9.5 million kilometres across and only 10 degrees
kelvin above absolute zero.
The cloud begins to collapse under its own weight, heats
up and shock fronts propagate throughout.
Stars start to form in the hot dense core (note this is a
simulation of gas dynamics and nuclear physics !)
1.2 Linear Advection Equation

We need to start with the basics and to this end we first

consider the linear advection equation for the quantity u(x, t)

u u
+ v(x, t) =0 (2)
t x

where v(x, t) is given. This is not in conservative form

as defined in equation 1 but is a simpler case to consider

The Cauchy problem consists of solving this equation for

t 0, < x < subject to the initial condition

u(x, 0) = u0(x)

The solution can be obtained by looking at the character-

istics of equation 2. The total time derivative of u along
any curve x(t) is given by
du u dx u
= +
dt t dt x

Equation 2 therefore implies that along any curve which


= v(x, t)

which is called a characteristic curve, the quantity u is

constant. Therefore, the solution is given in terms of the
initial condition. This is the simplest way to solve such
equations analytically and will be our guide in constructing
numerical techniques.
t Characteristic Curves


Analytic Examples

1. v(x, t) = v = constant
Characteristic curve, along which u(x, t) is constant,
is the solution to

=v x = x + vt

where x is a constant of integration so that the char-

acteristics are straight lines. Therefore the solution to
equation 2 is
u(x, t) = u(x, 0) = u0(x vt)

which describes the the initial shape moving with ve-

locity v. For example if the initial shape were the Gaus-
sian profile u(x, 0) = exp(x2/L2) then at later
times we have

(x vt)2
u(x, t) = exp

the Gaussian at time t is now centered at x = vt.

2. v(x, t) = (1 + x2)/(1 + 2xt + 2x2 + x4)

Characteristic curve is solution to

dx 1 + x2
= t = (x x)(1 + x2)
dt 1 + 2xt + 2x2 + x4

which gives the solution to the pde in terms of the initial

u(x, t) = u(x, 0) = u0 x
1 + x2

Again if the initial shape is u(x, 0) = exp(x2/L2)


" 2 #
1 t

u(x, t) = exp 2 x
L 1 + x2

Note : Consider the conservative equation (which differs

from the linear advection equation 2),

ut + (v(x)u)x = 0
ut + v(x)ux = v (x)u

In this case, along the characteristics defined by dx/dt =

v the evolution of u follows a simple ordinary differential
= v u

which is easier to solve than the original pde.

1.3 Finite difference methods for linear advection

How could we solve the linear advection equation if v(x, t)

were too complicated to use the analytic method of char-
acteristics ?

Consider a uniform grid in the (x, t) plane with a spacing

h in the x direction and along the t axis.

(x j,t )

We denote by un j the numerical approximation to u(x, t)
at the jth spatial point and nth timestep; i.e. un
j u(xj , tn ).

We say that a numerical scheme is convergent if un j

u(xj , tn ) as h 0 and 0; i.e. if we make the grid
progressively more refined we want the numerical solution
to tend to the analytical one. Otherwise the numerical
scheme is useless !!.

Several numerical methods can be constructed for equa-

tion 2 and we discuss these below. Although this equation
has a simple analytic solution the numerical solution is not
trivial. First we define some new notation for finite differ-

Finite Difference Notation

We discussed finite differences in second year and here

we will briefly outline a notation which will be used in parts
of the course.
Consider a function of two variables, u(x, t), where we
have a spacing h along the x-axis and along the t-axis.
There are three kinds of finite differences which we can
define. We define the operators for the analytic function
u(x, t) but they also apply to un

Forward differences :

+tu(x, t) u(x, t + ) u(x, t)

+xu(x, t) u(x + h, t) u(x, t)

Backward differences :

tu(x, t) u(x, t) u(x, t )

xu(x, t) u(x, t) u(x h, t)

Central differences :
tu(x, t) u(x, t + /2) u(x, t /2)
xu(x, t) u(x + h/2, t) u(x h/2, t)

Applying the central difference operator twice gives the

second order central difference

x2u(x, t) = x (u(x + h/2, t) u(x h/2, t))

= u(x + h, t) 2u(x, t) + u(x h, t)

The double interval central difference is also defined as

0xu(x, t) +x + x u(x, t)
= [u(x + h, t) u(x h, t)]
Forward Time Centered Space (FTCS) method

Using forward time and centered space differencing on

equation 2 gives,

j n
x u n
= vj
j un
j u n
j+1 u n
= vjn

n+1 n n n

uj = uj uj+1 uj1

where vjn v(xj , tn ) and = vjn /h. This method

(ftcs.m) is simple to programme and is completely use-
less !!!.

The stability of a numerical method can be studied (before

writing a programme !) by the following method.

Fourier/Von Neumann Stability analysis

Consider how the trial solution eikx evolves from one timestep
to the next. This is one Fourier mode of the full solution
for any given problem.

The Fourier mode will be amplified/dampened by a factor

from one timestep to the next. At the nth timestep we
therefore have the trial solution

un n ikxj
j = e

where n means to the power of n.

If || > 1 then the solution grows in amplitude and is

therefore unstable. If || < 1 the solution is damped. The
wave shape is preserved only if || = 1.

Inserting the trial solution in the FTCS method gives

n+1 ikjh n ikjh  n ik(j+1)h n ik(j1)h

e = e e e

= neikjh 1 eikh eikh

Dividing by neikjh gives

 ikh ikh

= 1 e e = 1 i sin(kh)
q 2
|| = 1 + 2 sin2(kh)

so that for nearly all modes || > 1 and the method is


The Lax Equivalence Theorem, which we will not prove,

further states that the stability of a scheme is necessary
and sufficient for convergence once the differencing scheme
is consistent.

Upwind scheme

The upwind scheme makes use of the concept of char-

acteristics. Consider the simplest case where v(x, t) =
v = constant (assume v > 0) and draw a characteristic
through the point (xj , tn+1).

t n+1

1 1


Forward time and backward space differencing gives (with

= v /h)

j = x u n
j = u n un
j x j
= (1 )un
j + u n
Note that = 1 v = h/ , a characteristic goes
through (xj1, tn) and (xj , tn+1) so that the upwind scheme
gives the correct answer un+1
j = u n . Similarly when
= 0 we get un+1 j = u n . For all cases where 0 <
< 1, the upwind scheme gives a linear interpolation
(see below) approximation to un+1
j .

For a general v(x, t) this idea can be generalised to give

the upwind scheme for the linear advection equation

un +xun if vjn < 0

j j

j =
un x un if vjn > 0

j j

where vjn /h.

Fourier analysis of the schemes stability gives, when vjn >


= 1 (1 eikh)
2 = [(1 ) + cos(kh)]2 + [ sin(kh)]2
= (1 )2 + 2 + 2(1 ) cos(kh)
= 1 2(1 )(1 cos(kh))
2 = 1 4(1 ) sin2(kh/2)

so that |(k)| 1 for all k provided the Courant Friedrich

Lewy (CFL) condition, < 1, is satisfied.

The truncation error of the scheme describes how the

schemes error depends on h and . The actual differ-
ential equation we are trying to solve is

u u
+ v(x, t) =0
t x

whereas the upwind scheme, when v > 0, can be rewrit-

ten as

j n
x u n
+ vj =0
The truncation error, Tjn , is obtained by inserting the ana-
lytic solution, u(x, t), into the numerical scheme and Tay-
lor expanding in the following manner

u(xj , tn+1) u(xj , tn) u(xj , tn) u(xj1, tn)

Tjn + vjn
n h 

1 n
= ut + utt + ... + v ux huxx + ...
2 j 2 j
= ( utt vhuxx)n
j + ...

where second and higher order terms in h and have

been omitted, so that the method is first order accurate.

Note that when v is constant we have utt = v 2uxx and

Tjn = (1 )vhuxx + ...
There are numerous other schemes to solve the linear
advection scheme such as the Lax method

1 n n
j = (uj+1 + un
j1 ) (uj+1 un
2 2

and the Leapfrog scheme

j = un1
j (un n
j+1 uj1)

Before looking at some of the above methods in Matlab

we will look at one more very important technique.

Lax Wendroff scheme

Consider again the characteristic passing through (xj , tn+1)

which is denoted by point P on the diagram below. The
characteristic intersects the grid at tn at point Q. Points
A, B and C are shown.
P t n+1

1 1
1 1
1 1


Since P and Q are on the same characteristic we have

uP = uQ

Noting that BQ = (1 )h and AQ = h, the upwind

scheme used linear interpolation to approximate uQ

uP = uQ uB + (1 )uA
The Lax Wendroff scheme uses quadratic interpolation
between uB , uA and uC at time tn as shown in the di-


B uC

x x x x x

The unique quadratic function, U (x), which passes through

the points (xB , uB ), (xA, uA) and (xC , uC ) can be writ-
ten in Lagrange form

(x xA)(x xC )
U (x) = uB
(xB xA)(xB xC )
(x xB )(x xC )
+ uA
(xA xB )(xA xC )
(x xB )(x xA)
+ uC
(xC xB )(xC xA)

To estimate uQ = uP we then calculate U (xQ). Note

first that

xB + (1 )h

xQ = xA h

xC (1 + )h

to give

1 1
uQ = (1 + )uB + (1 2)uA (1 )uC
2 2

The Lax Wendroff scheme can therefore be written as,

1 1
j = (1 + )un
j1 + (1 2 n
)u j (1 )u n
2 2

which is equivalent to

un+1 = u n un + 1 2 2un (3)

j j 0x j x j

Note that we could have obtained this method by applying

forward time, centered differencing to the partial differen-
tial equation

u u v 2 2 u
= v +
t x 2 x2

which is the linear advection equation plus the addition of

an extra term that plays the role of viscosity. Indeed this
technique is known as artificial viscosity and essentially
acts to dampen out the instabilities.
The above derivation is only valid for constant v. For a
general v(x, t) we can go about the derivation first by Tay-
lor expanding,

u(x, t + ) = u(x, t) + ut(x, t) + 2utt(x, t) + O( 3)

The differential equation allows us to replace time deriva-

tives by spatial derivatives which can then be central dif-

ut = vux utt = vtux vuxt

Noting that utx = (vux)x gives

utt = vtux + v (vux)x

The Taylor expansion can then be written as

u(x, t + ) = u(x, t) v ux(x, t)

+ [vtux(x, t) + v (vux)x]

Applying central differences for the spatial derivatives give

the Lax Wendroff scheme for the linear advection equa-

u n
0x j
j = u n
j vj
n n n

2 n
0x u j v u
n x j x j
+ (vt)j + vj (4)
2 h h2

so that the Lax Wendroff scheme is therefore second or-

der accurate.

The stability of the Lax Wendroff scheme when v is con-

stant can be studied by the usual Fourier analysis which
= 1 i sin(kh) 2 2 sin2(kh/2)
2 = 1 4 2(1 2) sin4(kh/2)

so that the method is stable when || 1.

MATLAB Programme linadvect.m

We now study some of the above methods in practice

with different initial conditions and advection speeds. The
choices allowed are summarised in the following table.
Note that the choice of v determines vt and the exact so-
u(x, 0) H(x0.1)H(0.3x) exp(10(4x 1)2))
shape1.m shape2.m

v(x, t) 1 (1 + x2)/(1 + 2xt +

2x2 + x4)
adspeed1.m adspeed2.m
vt(x, t) 0 2x(1 + x2)/(1 +
2xt + 2x2 + x4)2
speed1 dot.m speed2 dot.m
Exact exact1.m exact2.m
We are also free to choose any one of the methods up-
wind (upwind.m), Lax (adlax.m) and Lax Wendroff (laxwend.m),
can be used.

Results are presented here for the non-uniform advection

speed, with either initial condition and the upwind or Lax
Wendroff schemes; i.e. four different sets of results, and
the analytic solution using characteristics is also plotted.
In each case h = 0.1 and the Courant number
vmax/h = 1. A full set of results should be generated by
the student. The programme terminates before the wave
profile runs off the grid.
The results for the upwind method with an initial gaussian
profile are shown below for t = 0, t = 1/3, t = 2/3 and
t = 1 and compared against the analytic solution (solid
line); the same times apply to the subsequent three exam-
ples. It is clear that, although the peak of the numerical
results moves with the correct speed, the upwind method
reduces the height of the initial shape, i.e. an amplitude
error which is quite large.

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5
The same qualitative features can be seen with the up-
wind method for the top-hat initial condition. Again the
pulse moves at the correct speed but the amplitude is re-
duced and the edges of the pulse are smoothed out.

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5
The Lax Wendroff scheme for the gaussian profile shows
very good agreement between the numerical results and
the analytic solution and represents a considerable im-
provement on the upwind method.

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5
However, Lax Wendroff method for the top-hat condition
shows how difficult it is to reproduce non-smooth solu-
tions; quite advanced schemes are needed ! In the case
of the Lax-Wendroff scheme the coefficient of un j+1 is
(1 )/2 which negative because of the stability re-
quirement. Therefore, un+1
j is a weighted mean of three
values with two positive and one negative weight. The
solution can, and does in the case of non-smooth data,
generate internal local maxima and minima.

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.5 1 1.5 0 0.5 1 1.5
1.3 Lax Wendroff Method for Conservation Laws

Many conservation laws, such as those of gas dynamics,

are of the form

u f (u)
+ =0 (5)
t x

i.e. the flux is a function of u(x, t). This can also be re-
written as

ut + v(u)ux = 0 where v(u)

The Lax Wendroff scheme can be derived in the same

manner as for the linear advection equation. As before,
the Taylor expansion gives,

u(x, t + ) = u(x, t) + ut(x, t) + 2utt(x, t) + O( 3)
Again we need to replace the t-derivatives with x-derivatives
by using the pde and then apply central differencing. Be-
gin by noting that

ut = fx utt = fxt = ftx = (vut)x = (vfx)x

so that the Taylor expansion becomes

1 2
u(x, t + ) = u(x, t) fx + (vfx)x

Using central differencing gives

n+1 n n 1 h
n n
uj = uj 0xf (uj ) + x v(uj )xf (uj )
h 2 h

and the second order term can be expanded out accord-

ing to
n n n n n
h i h  i
x v(uj )xf (uj ) = x v(uj ) f (uj+1/2) f (uj1/2)
n n n
h i
= v(uj+1/2) f (uj+1) f (uj )
n n n
h i
v(uj1/2) f (uj ) f (uj1)

where un
j1/2 (u n + un )/2.
j j1

Adopting the notation that vj1/2 v(un ) and f n
j1/2 j
f (uj ), the Lax Wendroff method for conservation laws
can be written as,

n+1 n n
uj = uj 1 vj+1/2 +xfjn
2h  h
1 + vj1/2 xfjn (6)
2h h

One important example is Burgers equation for inviscid

fluid flow
1 2
ut + uux = 0 ut + u =0 (7)
2 x

Recall that the Navier-Stokes equation for one dimensional

flows is

ut + uux = px + uxx

where is the coefficient of viscosity. Therefore the Burgers

equation describes the flow of a pressureless, inviscid
fluid. Its solution is, nevertheless, non-trivial.

The characteristics for this equation are defined by

= u (x(t), t)

and, since u is constant on each characteristic, the char-

acteristics are straight lines with their slopes determined
by the initial data, i.e. given the initial condition u(x, 0) =
u0(x) the characteristic passing through the point (x, 0)
has the slope

dt 1
dx u0(x)

on the (t, x) plane.

When the general solution is smooth, at (x, t) we can

draw the straight characterstic through that point back to
t = 0 so that the solution can be written in implicit form,

u(x, t) = u0 (x u(x, t)t)

as shown in the diagram below for small t.



Note that since v(u) = u, the greater the amplitude of

the wave the faster it moves. Therefore, parts of the wave
which have an initial negative gradient will steepen until...

Characteristics eventually cross at which point a singu-

larity appears in the solution where it becomes multiple


The problem is that the mathematical model for gas flow
breaks down at this point since viscosity becomes impor-
tant as steep gradients develop and we should be solving
the equation

ut + uux = uxx

This leads to the formation of a shock or discontinuity in

the flow beyond which the flow cant steepen further. This
is shown schematically in the following plot.

Shock formation is a very difficult and specialised part of

computational gas dynamics and we will not study it in
detail. Physically of course a shock is not a mathemat-
ical discontinuity but has a width determined by viscous

The Riemann problem and shock propagation

We can, however, gain insight into the problem of shock

propagation by discussing the Riemann problem. Con-
sider a conserved quantity u(x, t) that obeys ut+f (u)x =
0 where f is convex, i.e. f (u) > 0. Consider the initial

ur x<0

u(x, 0) =

ul x0

where ur > ul . The discontinuity/shock in the flow will

propagate at speed vs as shown in the diagram.



The shock speed can be determined from integrating the

conservation equation from L to +L which gives

d L
u(x, t) dx = f (ul ) f (ur )
dt L

But if L > vst we also have

u(x, t) dx = (L + vst)ul + (L vst)ur
u(x, t) dx = vs(ul ur )
dt L

Combining the above gives

f (ul ) f (ur )
Vs =
ul ur

For Burgers equation f = u2/2 so that vs = (ul +

ur )/2. The characteristics from each region go into the
shock (x = vst) as shown below.

t Shock

For completeness we should look at the case where ul <

u < ur . In this case there is also a shock solution shown


But in this case characteristics come out of the shock.

Therefore, if the data is smeared just a little, through vis-

cosity, this solution becomes unstable. It is known as an
entropy-violating shock and does not occur in gas dynam-

What actually happens when ul < ur is that a rarefaction

wave is produced and there is a linear increase from ul to
ur .



t Rarefaction wave

The full solution is

ul x < ul t

u(x, t) = x/t ul t x ur t

ur x > ur t

Returning to numerical methods there is an interesting

variant of the Lax Wendroff method called the two-step
method. Here provisional values of u are calculated at
half step values (xj+1/2, tn+1/2),

n+1/2 1 n n
  n n

uj+1/2 = uj + uj+1 f (uj+1) f (uj )
2 2h

which are then used to calculate the fluxes,

n f (un+1/2 ) f (un+1/2 )
j = u j j+1/2 j1/2
There is no need to calculate u which is an advantage
when dealing with systems of equations.

The upwind scheme can also be generalised to the con-

servation law to give

uj h +xfjn
n n

if vj+1/2 <0

j = (8)
un x f n n
if vj1/2 >0

j h j
MATLAB Programme burgers.m

This file solve the burgers equation using an initial gaus-

sian profile. The process of shock formation can be seen
qualitatively from the plots. Note that the Lax Wendroff
term contains essentially a second derivative term which
is called artificial viscosity which numerically plays the
same role as viscosity in the Navier Stokes equations.
The upwind scheme, which is not shown here, actually
resolves the shock better than Lax Wendroff.

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.2 0.4 0.6 0 0.2 0.4 0.6

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.2 0.4 0.6 0 0.2 0.4 0.6
The two-step method, for comparison, with Lax Wendroff.

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.2 0.4 0.6 0 0.2 0.4 0.6

1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 0.2 0.4 0.6 0 0.2 0.4 0.6
All of the above methods can easily be generalised to sys-
tems of equations. For example the wave equation de-
scribing the propagation of waves on a string is

2A 2 2A
t x2

where A(x, t) is the wave amplitude and v the wave speed.

Using the definitions

u1 and u2 v
t x

the wave equation can be written as a pair of linear ad-

vection equations,

u1 u2 u2 u1
=v and =v
t x t x

which can be solved simultaneously using the numerical

methods above.
Another example is the Euler equations of gas dynamics,
which were mentioned earlier, and can be written in the

ut + (f (u))x = 0

where the arrays u and f are given by

v u2

E u3


v u2
f = v 2 + p = u2 /u + p(u)

2 1
v(E + p) u2(u3 + p(u))/u1

and p(u) is the equation of state. Note that this is a non-

linear system of equations.
The numerical difference equations for u1, u2 and u3 are
then solved simultaneously. For the Lax-Wendroff method
we would need to calculate the Jacobian matrix f (u). For
the two-step method this is not necessary which is one of
its biggest advantages.
1.4 Traffic Flow

One of the most interesting applications of conservation

laws is to the problem of traffic flow. While this may seem
an odd example to take in computational physics, it pro-
vides important insight into the behaviour of fluid and gas

We model the flow of traffic in one-dimension by taking

(x, t) to denote the density of cars at a point x on the
road and time t; i.e. were modelling the traffic flow as a
continuum. If v(x, t) is the traffic speed at that point then
the conservation law for the traffic flow is

+ =0
t x

However, the flow speed depends on the local traffic den-

sity so that we should write v as an explicit function of ;
i.e. v(x, t) = v().
When = 0 we have a free road and the traffic speed will
tend to the speed limit vm. Likewise in bumper to bumper
traffic at density m the traffic speed ought to be zero.
Using a linear model between these two limiting cases we


v() = vm 1

Other choices are possible but this turns out to be a good

first approximation. Note that we strictly have v vm and
m. Also, in this model a car does not respond to
the global traffic conditions; i.e. what lies some distance

Note that the conservation equation can now be written in

the form

+ a+ =0
t x 2
where a = vm and b = 2vm/m. This equation is
called the generalised inviscid Burgers equation. The
standard Burgers equation we obtained beforehand cor-
responds to a = 0 and b = 1.

We can manipulate the conservation equation into a form

which gives us information on the characteristics,

+ (v()) = 0 + c() =0
t x t x


d 2
c() (v) = vm 1
d m

The traffic density is therefore constant along the straight

lines defined by

= c()
Note the limiting cases for c(),

vm for = 0

c() = 0 for = m/2

vm for = m

Note that v() is the speed of the traffic while c() is the
speed of disturbances or waves in the traffic which can be
seen from the following example.
Traffic at a Stoplight

Consider the initial condition

m x

(x, 0) = x
1 x




which corresponds to a traffic stoplight going from red to

green at t = 0.

In the limit of 0 it is clear from the characteristics in

the diagram below that

m x vmt

(x, t) =

0 x vm t

i.e. the next car to move is at x = vmt and behind this
car it is still bumper-to-bumper while in front of the lead
car at x = vmt we still have a free road.

= =0

+ x

The region between = m and = 0 is called an ex-

pansion fan. The characteristics vary in slope from 1/vm
to 1/vm across the expansion fan in the tx plane as shown
in the diagram.

Since the initial condition between and varies linearly

so too will the solution between vmt and + vmt
since the density is constant along each characteristic.
In the limit of 0 the density therefore drops linearly
from m to 0 as x varies from vmt to vmt. Therefore the
full solution is

m x vmt

(x, t) = x
1 vmt vmt x vmt


0 x vm t

which is in accordance with the intuitive notion of what

happens at a stoplight.

Note that the above plot show the characteristic profiles

and not the vehicle trajectories which are shown in the
plot below.


There are other models for the car flux, f = v, for ex-
ample data for the Lincoln tunnel in New York City was
used by Greenberg (1959) to suggest the model f =
a log(m/).

MATLAB Programme traffic.m

The file traffic.m solves the traffic stoplight problem using

the Lax-Wendroff method which in this case can be writ-
ten out to give,
n+1 n  n n

j = j F Fj1
2h j+1
2 h 
n n
n n
+ c F Fj cj1/2 Fj Fj1
2h2 j+1/2 j+1


1 n
cj1/2 c(n n n

j1/2) and j1/2 =
j + j1



F () = vm 1

Periodic boundary conditions are used which basically turns

the road into a racetrack. The density is plotted out at var-
ious times below up to the point where the last car moves.
Density versus x and t




0.5 5
0.4 10

0.3 15
0.2 20

0.1 25

0 30
0 10 20 30 40 50 60 70 80 90 100

Note that the rear of the traffic profile holds steady up to

this point which is an important test of a numerical method
for this problem. This rear point is a shock front. What
should ultimately happen to the distribution and what does
the programme produce ?
Traffic Jams and Shock Fronts

Consider the traffic jam problem where we have as initial


m /2 for x < 0

(x, 0) =

m for x 0

Physically, the cars with x > 0 cannot move since we

have bumper-to-bumper trafic there. The cars with x < 0
can move forward, at least initially, until the local density
increases to m and they stop; a traffic jam/shock front
moves backwards through the traffic.

Mathematically we can trace the characteristics which have

a velocity of 0 for traffic at density m/2 and a velocity of
vm for the bumper-to-bumper part. Therefore, the char-
acteristics intersect and the line connecting these inter-
section points is the shock front which moves at velocity
f (l ) f (r )
vs =
l r

where f v, l = m/2 and r = m so that, in this

case, vs = vm/2 as shown in the figure below which
plots the characteristics for this problem.

Shock Front

The vehicle trajectories are plotted below.

Traffic jam

Moving traffic

Since cars cannot be created or destroyed (in this model

!) show that in the rest frame of the shock, the flux of
cars into the shock front is equal to flux of cars out of
the shock front. This is just a consistency check - but is
another useful way of determing the shock speed !