MATLAB Tutorial
MATLAB Tutorial
c Dr. G.N. Mercer
Index
Preface
These notes comprise the numerical part of the course ZPEM3311 Mathematical Methods
for Differential Equations. They are not a complete set of notes. Extra material and
examples may also be presented in the lectures and tutorials.
These notes are hyperlinked. All green text is a link to somewhere else within this docu-
ment. For example the contents page links to the appropriate page in the text, the numbers
in the index link to the page reference, the word Index in the header of most pages links to
the index and the page numbers in the header on each page link back to the contents page.
There are also some internal linked words that take you to the relevant text. Links to ex-
ternal web pages are red in colour. Provided your PDF reader (eg Adobe Acrobat Reader)
is set up correctly these links should open the appropriate page in your web browser (eg
Microsoft Internet Explorer, Netscape, Mozilla).
MATLAB code
The MATLAB codes used in these notes are available at the course webCT site.
MATLAB code in the text is usually shown in purple and the resulting output in blue.
If you have any difficulties with any part of the course do not hesitate in contacting me to
sort them out.
Geoff Mercer
Room G12 Science South
School of Physical, Environmental and Mathematical Sciences
Phone: 6268 8734
Email:[email protected]
ii Index
Contents
1 Introduction to MATLAB 1
1.1 Access to MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Using MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 A few simple examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Running MATLAB scripts . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Sending output to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 MATLAB help facility and lookfor command . . . . . . . . . . . . . . . 8
1.7 Functions in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.8 Functions in MATLAB – vector input . . . . . . . . . . . . . . . . . . . 10
1.9 Passing function names . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.10 MATLAB finding zeros . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.11 An Introduction to solving DEs numerically . . . . . . . . . . . . . . . . 14
1.12 Symbolic Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.12.1 Differentiating using MATLAB . . . . . . . . . . . . . . . . . . 17
1.12.2 Integrating using MATLAB . . . . . . . . . . . . . . . . . . . . 19
1.12.3 Solving equations . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.13 Some Elementary MATLAB Commands . . . . . . . . . . . . . . . . . . 20
2 Laplace Transforms 23
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2 Some standard results in Laplace Transforms . . . . . . . . . . . . . . . 23
2.2.1 Heaviside function . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.2 Dirac delta function . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2.3 First shifting theorem . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2.4 The second shifting theorem . . . . . . . . . . . . . . . . . . . . 26
2.2.5 Transforms of derivatives . . . . . . . . . . . . . . . . . . . . . . 26
2.2.6 Multiplication by t . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.7 Integral theorem . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.8 Division by t . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.9 Convolution theorem . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.10 Taking Laplace transforms using MATLAB . . . . . . . . . . . . 28
2.3 Inversion of Laplace transforms . . . . . . . . . . . . . . . . . . . . . . 29
2.3.1 Partial fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3.2 Application of partial fractions to inversion . . . . . . . . . . . . 31
2.3.3 MATLAB functions for partial fractions . . . . . . . . . . . . . . 33
2.3.4 Inversion using MATLAB . . . . . . . . . . . . . . . . . . . . . 36
2.3.5 Systems of Equations . . . . . . . . . . . . . . . . . . . . . . . . 38
iv CONTENTS Index
MATLAB (MATrix LABoratory) is a very powerful package designed for numerical anal-
ysis, matrix calculations and visualisation (as well as many other applications). One of
MATLABs strengths is that it can be used as a simple ‘calculator’ and also is easy to
program more complicated tasks. Many mathematical algorithms are already built in to
MATLAB and many more are easily added.
University College has a limited user licence for the full version of MATLAB, this is
available via any central computing facility. Alternatively (and preferred as it is more
flexible for you) you can purchase MATLAB (from the Co-op Bookshop), this is a student
release. The most recent version is MATLAB 7, R2006b. Older versions of MATLAB
had a student edition which is more than adequate for this course (and is often all that
is ever needed for many applications). You will find that purchasing a copy is the best
option as you will be using MATLAB in a number of your other third year courses during
the year. MATLAB is on the essential textbook list so you will be able to be reimbursed
for its purchase.
MATLAB is an interactive program so you can enter commands at the prompt or read
in files with lines of MATLAB commands. For relatively simple tasks it is acceptable
to enter commands at the prompt but for more complicated tasks it is better to store the
commands in a file and then read in the file. This makes it easy to change the commands
and to store them. In fact all inbuilt MATLAB commands are written as files of com-
mands. For this reason there are vast numbers of MATLAB files (called M-files) written
for various purposes available on the internet. A central repository of contributed M-files
can be found at http://www.mathworks.com/matlabcentral/.
For this course you should do your work using M-files as these will be handed up as part
of your assignments along with the relevant output.
All M-files given in the notes will be available from the course webCT page. Many of the
assignment questions will be a modification of these files.
2 1. INTRODUCTION TO MATLAB Index
1. Matrix Manipulation
Enter a 2× 2 matrix find its inverse, and eigenvalues and eigenvectors. Check that
its inverse times itself is the identity.
MATLAB code
% any thing after a % sign is a comment
A=[1,2;3,4] % enter the 2x2 matrix and call it A
B=inv(A) % calculate the inverse of A and call it B
eig(A) % calculate the eigenvalues of A
[V,D]=eig(A) % calc the eigenvectors (columns of matrix V)
% diagonal matrix D (eigenvalues on diagonal)
check=B*A
If these commands are stored in an M-file called example1.m then to run it type
example1 (without the .m) from within MATLAB. (Note you have to be in the
same directory as the file is saved in, or have made that directory in the path that
MATLAB looks in). The MATLAB output from these commands is the following
A =
1 2
3 4
B =
-2.0000 1.0000
1.5000 -0.5000
ans =
-0.3723
5.3723
V =
-0.8246 -0.4160
0.5658 -0.9094
D =
-0.3723 0
0 5.3723
check =
1.0000 0
0.0000 1.0000
Index A FEW SIMPLE EXAMPLES 3
2. Plotting
MATLAB has very good graphics capability. The easiest method for simple plots
is to define a vector (for example x) and then perform a function on each element
of that vector (eg y(x)) and then plot y(x) versus x.
Defining a vector is straightforward. The MATLAB command x=a:b:c defines
x to be a vector of values starting at a increasing in steps of b to c. For example
x=-4:3:8 results in x being the vector [−4, −1, 2, 5, 8].
Example. Plot y = x2 + sin(xπ ).
MATLAB code
25
20
15
10
−5
−5 0 5
4 1. INTRODUCTION TO MATLAB Index
3. Plotting data
It is very easy to read data in from a file, manipulate it and plot it. The command
load is used to load data from an external file. For example read data from a file
called datafile, calculate the line of best fit to the data and add some titles, legend
and text to the plot.
MATLAB code
% read data from the file datafile. This puts it into
% a variable matrix called datafile by default
load datafile
x=datafile(:,1) % put first column into variable x
y=datafile(:,2) % put second column into variable y
% Plot the data and line and add titles and labels
plot(x,y,’ro’,x,straightline,’b-’)
title(’data and line of best fit’)
xlabel(’x’)
ylabel(’y’)
50
40
y
30
20 This is text
10
0 1 2 3 4 5 6 7 8
x
Index A FEW SIMPLE EXAMPLES 5
4. 3D Plotting
MATLAB has very good 3D graphics capability. The easiest method is do define
the domain using the meshgrid command, then define the function and then plot
it using mesh or surf commands.
Example. Plot z = 4x2 − 2y2 .
MATLAB code
[x,y]=meshgrid(-5:0.5:5,-2:1.0:7);
% define the mesh (x,y) the domain of plotting
% x from -5 to 5 in steps of 0.5
% y from -2 to 7 in steps of 1.
z=4*x.*x-2*y.ˆ2;
% calculate z=4xˆ2-2yˆ2
% note the .* and .ˆ this is because x and y are
% vectors so we need to multiple ’component by component’
mesh(x,y,z)
% draw the surface as a mesh
% or use surf(x,y,z) to draw it as a surface
print -depsc example2a
% send the plot output to a colour postscript file
100
50
−50
−100
10
5 5
0 0
−5 −5
6 1. INTRODUCTION TO MATLAB Index
x =
-1.0000
1.0000
0.0000
A=[1,2,3;4,5,6;7,8,10];
c=A(1,:) % extract all of the first row of A
d=A(:,2) % extract all of the second column of A
c =
1 2 3
d =
2
5
8
diary off
Index RUNNING MATLAB SCRIPTS 7
One of the easiest and most transportable ways to use MATLAB is to enter your MATLAB
commands into a file and then run that file. These types of files are called M-files and have
the extension .m after their name. To run an M-file from MATLAB make sure you are in
the directory where the file is located and then just type the name of the file without the
extension (i.e. without the .m). MATLAB will then process all the commands that are in
that file.
Example
A =
1 2
3 -4
C =
1395967 -2789886
-4184829 8370682
ans =
1024
9765625
Often you want to keep the output of a MATLAB command. The diary commands does
that for you. By typing
diary example1.out
all subsequent output will sent to the file example1.out as well as to the screen.
To turn the diary command off use
diary off
8 1. INTRODUCTION TO MATLAB Index
Note that the help facility uses capitals for the command name, capitals should NOT be
used when actually using the commands.
lookfor differentiate
POLYDER Differentiate polynomial.
FNDER Differentiate a function.
DIFF Differentiate.
Index FUNCTIONS IN MATLAB 9
Sometimes you will want to write your own function in MATLAB. For example an M-file
that takes some kind of input and returns some calculations on that input.
For example write a function that calculates
f (x) = x3 + 4x2 + 3
function f=calcf(x)
% this file is named calcf.m
% this function calculates f(x) and
% returns the answers in the variables f
%
f = xˆ3 + 4*xˆ2 + 3;
return
calcf(3)
ans =
66
10 1. INTRODUCTION TO MATLAB Index
Since MATLAB deals with vectors of numbers it is more useful to write functions so that
they can handle vectors rather than an individual number. The only change needed to
achieve this is to ensure that the calculations within the function are done on an element-
by-element basis by using the .* notation instead of the * notation (and equivalently .ˆ,
./ etc).
For example write a function that calculates
f (x) = x3 + 4x2 + 3
for an input vector x. That is, it should calculate f (x) for each value of a vector x and
return the answer in a vector.
MATLAB code
function f=calcfvec(x)
% this file is named calcfvec.m
% this function calculates f(x) for a vector x and
% returns the answers in the variable f
%
f = x.ˆ3 + 4.*x.ˆ2 + 3;
return
y=0:0.2:1.0
y =
0 0.2000 0.4000 0.6000 0.8000 1.0000
calcfvec(y)
ans =
3.0000 3.1680 3.7040 4.6560 6.0720 8.0000
Index PASSING FUNCTION NAMES 11
Often in MATLAB you need to pass the name of a function to another function. For
example if you want to numerically calculate the integral of a function MATLAB has an
inbuilt function that does this but somehow you need to tell it what function to integrate.
Hence you need to pass the functions name.
MATLAB has two inbuilt functions for calculating integrals
[A,count]=quad(’funcname’,a,b,tol,trace)
[A,count]=quad8(’funcname’,a,b,tol,trace)
quad uses Simpson’s rule, quad8 uses a degree 8 polynomial. tol is the tolerance for
the convergence using a comparison between successive approximations. trace is an
additional feature for plotting a trace of the integrand used. The answer is returned in A
and count is a count of how many function evaluations have been performed.
funcname is the name of the function to be passed. For example if integrating the function
defined in the M-file quadexamplefunc.m then use quadexamplefunc where ever you see
funcname.
Important Note: The function referenced by ’funcname’ must be written to take a vector
input and give a vector output.
Example
Calculate Z x
2 2
f (x) = √ e−t dt
π 0
For x = 1. This function is known as the error function (erf(x) in MATLAB)
MATLAB code
% quadexample.m
format long % use lots of decimal places
% the ’exact’ answer using MATLABs erf function
errorfunc=erf(1)
% numerically integrate using Simpsons rule (quad)
integral=quad(’quadexamplefunc’,0,1,1e-6)
% now do it again using an 8th order polynomial (quadl)
integral=quadl(’quadexamplefunc’,0,1)
% count the number of function calls using the trace switch
trace=1;
[integral,count]=quad(’quadexamplefunc’,0,1,1e-6,trace)
MATLAB code
function f=quadexamplefunc(x)
% quadexamplefunc.m
% the function to be integrated
f=(2/sqrt(pi))*exp(-x.ˆ2);
return
12 1. INTRODUCTION TO MATLAB Index
>> quadexample
errorfunc =
0.84270079294971
integral =
0.84270079342046
integral =
0.84270079427671
integral =
0.84270079342046
count =
17
diary off
Index MATLAB FINDING ZEROS 13
MATLAB has an inbuilt function for finding zeros of a function called fzero . Its use
is
r=fzero(’funcname’,x0)
searches for a region with a zero of the function defined in funcname.m then uses bisec-
tion method with linear or quadratic interpolation to find the zero.
>> help fzero
Example
Find a zero of x3 − 2x2 − 9 = 0
>> fzero(’findzerof’,4)
Zero found in the interval: [2.72, 4.9051].
ans =
3.0000
MATLAB code
function f=findzerof(x)
% findzerof.m
f=x.ˆ3-2*x.ˆ2-9;
return
14 1. INTRODUCTION TO MATLAB Index
MATLAB has many ways of numerically solving Differential Equations. Here we will
present one method, the details of the method and the mathematics behind it will be
explained in later lectures. This will give you enough now to be able to numerically solve
many of the DEs you will encounter early in this course. Consider a first order ODE
dy
= f (t, y)
dt
subject to some initial condition (at time t = 0)
y(0) = y0
The MATLAB function ode45 will solve these kind of problems. Here is an excerpt
from the help on ode45
Example
[t,y]=ode45(@vdp1,[0 20],[2 0]);
plot(t,y(:,1));
solves the system y’ = vdp1(t,y), using the default relative error
tolerance 1e-3 and the default absolute tolerance of 1e-6 for each
component, and plots the first component of the solution.
There are 3 things you need to define to use MATLAB to solve this type of DE.
Notes
1. t is returned as a vector of all the times where the solution was determined.
Example
Solve
dy
= y − 2π cost subject to y(0) = 3
dt
over the range t = [0, 5].
Note that you could solve this particular DE using the Integrating Factor method. The
numerical solution works for DEs that can not be solved analytically. This is the point of
developing numerical methods for solving DEs, they let you get solutions that can not be
obtained analytically.
MATLAB code
% odeex1.m
% solves y’= y-2*pi*cos(t) subject to y(0)=3
% over the range t=[0,5]
%
tspan=[0,5] % sets up the time span
y0=3 % the initial condition
% solve the DE and return solution in t and y
[t,y]=ode45(’odeex1fun’,tspan,y0)
% now plot the solution, note that t is a vector of all the
% points in time where the solution was calculated and y is a
% matrix whose first dimension corresponds to t and second
% dimension is the number of equations (here just 1)
plot(t,y(:,1))
% now send the plot to a file called odeex1.eps to be saved
print -deps odeex1
MATLAB code
function f=odeex1fun(t,y)
% odeex1fun.m
% the RHS of the system of DEs
% note using f(1) and y(1) as these can be vectors
% of longer than 1 if you have more than 1 equation
f(1)=y(1) - 2*pi*cos(t);
f=f(:); % forces f to be a column vector
return
16 1. INTRODUCTION TO MATLAB Index
−5
−10
−15
−20
0 1 2 3 4 5
% odeex1.m
tspan=[0,5]
y0=3
[t,y]=ode45(’odeex1fun’,tspan,y0)
plot(t,y(:,1))
print -deps odeex1
MATLAB code
function f=odeex1fun(t,y)
% odeex1fun.m
f(1)=y(1) - 2*pi*cos(t);
f=f(:);
return
To solve other single equation DEs simply alter the equation (f(1)=. . . ), the time range
(tspan) or the initial condition (y0).
Index SYMBOLIC MANIPULATION 17
MATLAB can also do some (limited) symbolic manipulation of equations such as finding
roots of equations, simplifying, differentiating and integrating. Firstly one much declare
the variable(s) in question to be symbolic in nature (so that MATLAB knows they are not
just a number) and then it is possible to manipulate them.
An example of differentiation
MATLAB code
% diffexample.m
% an example of how to use MATLAB to differentiate functions
%
% define a function of x
f=xˆ3-3*y-cos(xˆ2)-atan(x)
diffexample
f = xˆ3-3*y-cos(xˆ2)-atan(x)
dfdx = 3*xˆ2+2*sin(xˆ2)*x-1/(1+xˆ2)
d2fdx2 = 6*x+4*cos(xˆ2)*xˆ2+2*sin(xˆ2)+2/(1+xˆ2)ˆ2*x
d2fdx2 = 6*x+4*cos(xˆ2)*xˆ2+2*sin(xˆ2)+2/(1+xˆ2)ˆ2*x
g = x*y+sin(-x+y)+yˆ3*cos(1/y)
dgdx = y-cos(-x+y)
dgdy = x+cos(-x+y)+3*yˆ2*cos(1/y)+y*sin(1/y)
d2gdx2 = -sin(-x+y)
d2gdx2 = -sin(-x+y)
d2gdy2 = -sin(-x+y)+6*y*cos(1/y)+4*sin(1/y)-1/y*cos(1/y)
d2gdy2 = -sin(-x+y)+6*y*cos(1/y)+4*sin(1/y)-1/y*cos(1/y)
d2gdxdy = 1+sin(-x+y)
Index SYMBOLIC MANIPULATION 19
An example of integration
MATLAB code
% integralexample.m
% examples of integrating in MATLAB
% define a function f
f=xˆ2-sin(2*x)+cosh(3*x)-exp(-x)
integralexample
f =
xˆ2-sin(2*x)+cosh(3*x)-exp(-x)
intf =
1/3*xˆ3+1/2*cos(2*x)+1/3*sinh(3*x)+exp(-x)
intfab =
1/3*exp(-3*pi)*piˆ3*exp(3*pi)+1/2*exp(-3*pi)*exp(3*pi)
-1/6*exp(-3*pi)+1/6*exp(-3*pi)*exp(6*pi)+exp(-3*pi)*exp(2*pi)-3/2
diary off
MATLAB has other symbolic routines for solving a variety of equations. For example
to solve systems of equations you can use solve and to solve differential equations
symbolically you can use dsolve. These will be discussed at a later time in the course.
20 1. INTRODUCTION TO MATLAB Index
40
35
30
25
20
15
10
0
−4 −2 0 2 4 6 8
45
line
hold on 40
35
hold the plot in place
plot(x,y,’bo’) 30 add blue circles to the plot
25
20
15
10
0
−4 −2 0 2 4 6 8
0.4
−0.6
−0.8
−1
−1 −0.5 0 0.5 1
c=[-3:1.0:5]; 3D plotting
d=[-2:0.5:7]; Set up the domain in c and d
[x,y]=meshgrid(c,d) 100 Make a mesh of values in x
50
and y
The function z = 4x2 − 2y2
0
z=4*x.ˆ2-2*y.ˆ2; −50
mesh(x,y,z) −100
10
Plot the function in 3D
5 5
0 0
−5 −5
2.1 Introduction
The majority of the theory of Laplace Transforms is given in the theoretical section of
this course. Here we will investigate how MATLAB can be used to help solve Laplace
Transform problems.
The notation we will adopt is
Z ∞
F(s) = L { f (t)} = e−st f (t) dt.
0
L −1 {F(s)} = f (t).
For completeness sake what follows are some standard results often used in Laplace
Transforms.
1 H(t-a)
t
a
The Heaviside Step function is like a switch. At time t = a it ’turns on’ with value 1.
The Heviside function is defined in MATLAB both as a numerical function and as a
symbolic function. Its usage is heaviside(x).
24 2. LAPLACE TRANSFORMS Index
Example
Plot
f (x) = H(x − 1)(x2 + 1)
MATLAB code
% heavisideexample.m
% plot H(x-1)*(xˆ2+1)
x=-2:0.05:3;
y=heaviside(x-1).*(x.ˆ2+1)
plot(x,y)
axis([-2 3 -1 10])
print -depsc heavisideexample
10
9
8
4
3
−1
−2 −1 0 1 2 3
Index SOME STANDARD RESULTS IN LAPLACE TRANSFORMS 25
Suppose that a constant force, f (t), of amount A acts on a body for a short time (of
duration ε ).
f(t)
force
A
t - ε/2 t + ε/2
time
force
time
26 2. LAPLACE TRANSFORMS Index
Z ∞ and
f (t) dt = 1.
−∞
1. δ (t − t0 ) = 0, t 6= t0,
Z b
2. δ (t − t0 ) dt = 1 a < t0 < b.
a
Note that the Dirac delta function is not a proper function. It can be thought of as the limit
of a distribution of step functions (as in the previous diagram). It can also be thought of
as the derivative of the Heaviside function.
L eat f1 (t) = F1 (s − a)
If the function f (t) has well defined derivatives and transform then:
2.2.6 Multiplication by t
L {t f (t)} = −F ′ (s)
In general
d n F(s)
L {t n f (t)} = (−1)n
dsn
2.2.8 Division by t
Often when we cannot get an inverse in terms of a known function we have to use the
convolution theorem
Z t
L −1
{F(s)G(s)} = f (τ )g(t − τ )d τ
0
28 2. LAPLACE TRANSFORMS Index
MATLAB can do Laplace Transforms and their inversions symbolically. Firstly you must
declare the variables that are used (eg t and s) as symbolic variables using the syms
command. The laplace function is then used to find the Laplace Transform.
Example
For example find the Laplace Transform of f (t) = e4t sin(3t)
MATLAB code
results in
Fs = 3/((s-4)ˆ2+9)
Example
−3t 2
Find
dthe −3t of the derivative of f (t) = e sinh(5t) + t , that is find
Laplace Transform
2
L dt e sinh(5t) + t
MATLAB code
results in
Fs = -15/((s+3)ˆ2-25)+5*(s+3)/((s+3)ˆ2-25)+2/sˆ2
Index INVERSION OF LAPLACE TRANSFORMS 29
The inversion of Laplace transforms can be done directly using an infinite integral in the
complex plane and finding residues around the singularities. Generally this is difficult to
do and beyond the scope of this course. It is much easier to modify our function F(s) to
look like something we already know the inverse transform of. For example we already
know that
s
L {cos at} = 2
s + a2
so that the inverse of
−1 2s
L = 2 cos 3t.
s2 + 9
All that is necessary is to get our solution in the same form as the tables and then do the
same process as the Laplace transform but in reverse.
Partial Fractions are useful to convert complicated inversions into a form that can be used
from the tables. Partial fraction are used to convert a complicated rational expression
such as y = G(s)/F(s) into a sum of simpler terms each of which can be inverted using
the tables. F(s) must be written in a form so that it is a product of polynomials of as a low
order as possible.
If the denominator has distinct linear factors then it is a simple matter of writing the
expression as a sum of terms with those linear factors as denominators and unknown
numerators. For example
3s + 13 A B
= +
(s + 3)(s + 1) s + 3 s + 1
where A and B are the unknown numerators that have to be found. To find these rewrite
the right hand side over a common denominator so that it resembles the left hand side and
then equate the coefficients of the different powers of s in the numerators.
3s + 13 A B
= +
(s + 3)(s + 1) s+3 s+1
A(s + 1) + B(s + 3)
=
(s + 3)(s + 1)
(A + B)s + A + 3B
=
(s + 3)(s + 1)
So then the coefficient of s gives
A+B = 3
and the constant term (s to the zero power) gives
A + 3B = 13
30 2. LAPLACE TRANSFORMS Index
A = −2 B = 5.
Hence
3s + 13 −2 5
= +
(s + 3)(s + 1) s + 3 s + 1
Method 1
If the denominator has a repeated linear factor then the unknown numerator must be linear.
The procedure then follows the same as before. For example
2s + 1 A B +Cs
2
= +
s(s − 2) s (s − 2)2
A(s − 2)2 + (B +Cs)s
=
s(s − 2)2
where we thus have
2s + 1 = A(s − 2)2 + Bs +Cs2
so equating coefficients we get:
O(s2 ) : 0 = A +C
O(s1 ) : 2 = −4A + B
O(s0 ) : 1 = 4A
Method 2
Alternatively (this method often leads to a better form for doing the inversion) the un-
known right hand side can be written as
2s + 1 A D E
2
= + +
s(s − 2) s s − 2 (s − 2)2
where we thus have
2s + 1 = A(s − 2)2 + Ds(s − 2) + Es
Index INVERSION OF LAPLACE TRANSFORMS 31
O(s2 ) : 0 = A+D
O(s1 ) : 2 = −4A − 2D + E
O(s0 ) : 1 = 4A
hence giving:
1 −1 5
A= , D= , E=
4 4 2
2s + 1 1/4 −1/4 5/2
2
= + +
s(s − 2) s (s − 2) (s − 2)2
Using this method you must make sure there are as many terms involving the factor as it
is repeated. For example
2s + 1 A B C D E F
= + 2+ + + +
s2 (s − 5)4 s s s − 5 (s − 5) 2 (s − 5) 3 (s − 5)4
If the factors are not linear then use method 1 as for repeated linear factors. That is use a
numerator that has degree one less than the term in the denominator. For example if the
denominator has a quadratic that can not be reduced any further then the corresponding
numerator is linear
1 A Bs +C
2
= + 2
s(s + 4) s s +4
Example
−1 2s + 1 −1 1/4 −1/4 5/2
L = L + + (using partial fractions)
s(s − 2)2 s (s − 2) (s − 2)2
1 1 2t 5 2t
= − e + e t (using shifting theorem)
4 4 2
32 2. LAPLACE TRANSFORMS Index
Example
1 A Bs +C
= + 2
s(s2 + 4) s s +4
We thus get
1 = A(s2 + 4) + (Bs +C)s
and by equating coefficients we get:
O(s2 ) : 0 = A+B
O(s1 ) : 0 = C
O(s0 ) : 1 = 4A
This following example uses the derivative formulae to transform a differential equation
into an algebraic equation.
Example
Let us solve the initial value problem:
s2Y − 3s − 1 + 4 (sY − 3) + 3Y = 0
which simplifies to
(s2 + 4s + 3)Y = 3s + 13
giving
3s + 13
Y =
(s + 3)(s + 1)
−2 5
= + (using partial fractions, see before)
s+3 s+1
As
L eat = 1/(s − a)
Residue
The residue function takes a ratio of polynomials B(s) A(s) and returns the corresponding
partial fraction representation of it. It takes as input the coefficients of the polynomials
B(s) and A(s) (in descending powers of s) and returns the coefficients and the pole values
(the zeros of the denominator).
help residue
Example
For example find the partial fraction representation of
3s − 1
F(s) =
s2 + 2s − 15
MATLAB code
B =
3 -1
A =
1 2 -15
R =
2
1
P =
-5
3
K =
[]
Example
MATLAB can also deal with repeated roots but care must be taken when interpretting the
output. For example find the partial fraction representation of
s2 + 4s − 2
F(s) =
s3 − 3s − 2
MATLAB code
B =
1 4 -2
A =
1 0 -3 -2
R =
1.1111
-0.1111
1.6667
P =
2.0000
-1.0000
-1.0000
K =
[]
Often it is not necessary to do the partial fraction steps yourself as MATLAB can do some
inversions symbolically using the MATLAB command ilaplace.
Example
Find the inversion of
s
F(s) =
s2 − 2s + 1
MATLAB code
results in
Fs = s/(sˆ2-2*s+1)
ft = t*exp(t)+exp(t)
Index INVERSION OF LAPLACE TRANSFORMS 37
More complicated expressions are also straightforward including ones that result in Heav-
iside functions.
Example
Find the inverse Laplace Transform of
e−5s
F(s) =
s3 − 3s − 2
MATLAB code
results in
Fs = exp(-5*s)/(sˆ3-3*s-2)
ft = 1/9*Heaviside(t-5)*exp(2*t-10)
-1/3*Heaviside(t-5)*exp(-t+5)*t
+14/9*Heaviside(t-5)*exp(-t+5)
38 2. LAPLACE TRANSFORMS Index
Laplace Transforms can be used to solve systems of differential equations. By taking the
Laplace Transform of a system of differential equations it is converted into an algebraic
system in the transformed variable. These can be solved to get the transformed functions
and then inverted to find the solutions to the original system.
Example
Solve the system
dx1
= −x1 + x2
dt
dx2
= 2x1
dt
subject to the initial conditions x1 (0) = 0 and x2 (0) = 1.
First take the Laplace Transform of both equations
(s + 1)X1(s) = X2
sX2(s) − 1 = 2X1
Solve to get X1 (s) and X2 (s). Substitute the first equation into the second for X2 to get
(s2 + s − 2)X1 = 1
1
X1 =
(s + 2)(s − 1)
Substitute in to get X2
X2 = (s + 1)X1
s+1
=
(s + 2)(s − 1)
Note that MATLAB’s solve function could be used to solve the system for X1 and X2 .
Now use partial fractions then invert to get the solution to the original system as
1 1 1 1 1 t 1 −2t
X1 = − ⇒ x1 (t) = e − e
3 s−1 3 s+2 3 3
2 1 1 1 2 t 1 −2t
X2 = + ⇒ x2 (t) = e + e
3 s−1 3 s+2 3 3
Index INVERSION OF LAPLACE TRANSFORMS 39
Example
Solve the system
dx1 dx2
2 + − 2x1 = 1
dt dt
dx1 dx2
+ − 3x1 − 3x2 = 2
dt dt
subject to the initial conditions x1 (0) = 0 and x2 (0) = 0.
First take the Laplace Transform of both equations
1
2(sX1 − x1 (0)) + (sX2 − x2 (0)) − 2X1 =
s
2
(sX1 − x1 (0)) + (sX2 − x2 (0)) − 3X1 − 3X2 =
s
Now apply the initial conditions and rearrange to get
1
(2s − 2)X1(s) + sX2(s) =
s
2
(s − 3)X1(s) + (s − 3)X2(s) =
s
Now solve this to get X1 (s) and X2 (s). Rearrange the first equation to get
1
s − (2s − 2)X1
X2 =
s
s2 (s − 3) − (s − 3)s(2s − 2) X1 (s) = 2s − s + 3
(collect X1 terms)
−(s + 3)
X1 =
(s − 3)(s − 2)s
Substitute in to get X2
1 −(s+3)
s − (2s − 2) (s−3)(s−2)s
X2 =
s
(s − 3)(s − 2) + (2s − 2)(s + 3)
=
s2 (s − 3)(s − 2)
3s − 1
=
s(s − 3)(s − 2)
40 2. LAPLACE TRANSFORMS Index
3.1 Introduction
An Initial Value Problem (IVP) is a differential equation of nth order where the function
value and the first n − 1 derivatives are all specified at one given point known as the initial
point.
For examples
y′ = 2y2 with y(1) = 3
Also it can be a system of first order differential equations with each function given a
value at the same point.
For example
can be written as
42 3. INITIAL VALUE PROBLEMS Index
% system1.m
tspan=[2,6] % note starts at t=2 now
y0=[0.5,-1,2] % note its a vector now
[t,y]=ode45(’system1fun’,tspan,y0)
% plot all y, y’ and y’’ on one graph and add a legend
plot(t,y(:,1),t,y(:,2),’r-.’,t,y(:,3),’g:’)
legend(’y’,’dy/dt’,’d2y/dt2’)
print -depsc system1
MATLAB code
function f=system1fun(t,y)
% system1fun.m
f(1)=y(2);
f(2)=y(3);
f(3)=-3*y(3)-7*y(2)+3*y(1);
f=f(:);
return
2.5
y
dy/dt
2 d2y/dt2
1.5
0.5
−0.5
−1
2 2.5 3 3.5 4 4.5 5 5.5 6
Index NUMERICAL APPROACH 43
For an IVP because all the derivatives are known at the one given initial point a Taylor’s
Series can be used to expand about that point to get an approximation to the solution at
neighbouring points. So for instance if you know the about the function at the point a
then information at a point x is given by
1 ′′
f (x) = f (a) + f ′ (a)(x − a) + f (a)(x − a)2 + . . .
2!
If you know something about f ′ (a), f ′′ (a),. . . then you can approximate the solution at
the point x.
The process for solving IVPs numerically is to start at the initial point and step forward
using information provided by the governing differential equation. This can be done in a
variety of ways. The simplest is Euler’s method.
Euler’s method is equivalent to approximating the solution by using the first two terms in
the Taylor Series (the function value and the first derivative) and ignoring the higher order
terms. The first ignored term is
1 ′′ 1
f (a)(x − a)2 = f ′′ (a)(∆x)2
2! 2!
so each step in the process has an error of the order of (∆x)2 .
44 3. INITIAL VALUE PROBLEMS Index
3.3.1 Graphically
Hence the total error is proportional to (∆x)1 so this is known as a first order method.
A pth order method is one whose error is proportional to (∆x) p .
The higher the order the method the more accurate it is. This usually comes at a price of
being more difficult to program.
First order methods are usually very slow if you want any reasonable amount of accuracy.
Why is this?
Index EULER’S METHOD 45
Example
Solve
y′ = y with y(0) = 1
over the range x = [0, 1]. This has exact solution y = ex .
MATLAB code
% eulerexample.m
%
% solve y’=y subject to y(0)=1
% find the result at x=1 and calculate the error.
%
clear all;
N=100; % number of steps used
a=0; b=1; % endpoints of solution
dx=(b-a)/N; % step size
% save x and y values in vectors so we can plot solution
x(1)=a; y(1)=1; % initial x and y values
for i=1:N
f=y(i); % calculate the function value at (xi,yi)
y(i+1)=y(i)+dx*f;
x(i+1)=x(i)+dx;
end
plot(x,y,’b-’)
axis([0 1 1 2.8])
hold on
exact=exp(x);
plot(x,exact,’r--’);
legend(’Euler’,’Exact’,2)
hold off
print -depsc eulerexample
error=abs(exact(N+1)-y(N+1))
Results of running the above code for different number of points (N = 10, 100, 1000, 10000)
and hence different step sizes
N ∆x yN error
10 0.1 2.59374246010000 0.12453936835904
100 0.01 2.70481382942153 0.01346799903752
1000 0.001 2.71692393223590 0.00135789622315
10000 0.0001 2.71814592682523 0.00013590163356
What do you notice about how the error decreases as the step size decreases? How is this
related to the order of the method?
46 3. INITIAL VALUE PROBLEMS Index
y′ = y with y(0) = 1
2.8 2.8
Euler Euler
2.6 Exact 2.6 Exact
2.4 2.4
2.2 2.2
2 2
1.8 1.8
1.6 1.6
1.4 1.4
1.2 1.2
1 1
0 0.2 0.4 0.6 0.8 1 0 0.2 0.4 0.6 0.8 1
How do you know how accurate the solution is? When should you use a smaller step size
to get a more accurate solution at the cost of increased computing time?
All you know about Euler’s method is that the error is proportional to the step size (∆x)
since it is a first order method, but you have no idea of what that constant of proportion-
ality is. It can change for each different problem and even within the one problem.
What would be ideal if we had some way of changing the step size so that in areas where
the solution changes rapidly we choose a small step size and in areas where it changes
slowly we can take a larger step size so that the error is about the same at each step. This
is something we would want to be able to do for any method not just Euler’s method.
All well written numerical ODE solvers will use some kind of adjustable step size that
takes account of how fast the solution is varying and the size of the errors involved.
Index EULER’S METHOD 47
1. For each step compare the result of using one step of size ∆x with the result of using
two steps of size ∆x
2 . Call the difference between the solutions ∆y. If the method
you are using is a pth order method then the error from a single step is (∆x) p+1 so
∆x
∆y = y(xi + ∆x) − y(xi + 2 )
2
p+1 !
∆x ∆x
p+1
≈ k(∆x) p+1 − k +k
2 2
k(∆x) p+1
= k(∆x) p+1 −
2p
1
= k(∆x) p+1 1 − p
2
= k′ (∆x) p+1
2. If you wish to make your solution accurate to some predetermined accuracy (call it
ε ) then |∆y/y| < ε . This corresponds to some ideal step size which we will call δ x.
Then ε 1/(p+1)
|∆y/y| = ε = k(δ x) p+1 so δ x =
k
in reality we used a step size ∆x which had
1/(p+1)
1
|∆y/y| = k(∆x) p+1
so ∆x = |∆y/y|
k
The ratio of the step sizes (R) is then a measure of how good our step size is com-
pared to the ideal step size
δx εy
1/(p+1)
R= =
∆x ∆y
3. If R > 1 then step size is too small so it can be increased at the next step.
If R < 1 then step size is too big so it must be decreased at the current step and the
process repeated until R > 1.
Use the size of R as a guide to how much to increase or decrease the step size.
For example let
∆xnew = mR∆xold
where m is a tuning parameter usually taken to be a little less than one (eg m = 0.95
or thereabouts).
48 3. INITIAL VALUE PROBLEMS Index
MATLAB code
% euleradaptive.m
% Euler method with adaptive time stepping
%
% dy/dx + y = exp(-x) + 1 with y(0)=1
% which has exact solution y = xexp(-x) + 1
%
clear all;
p=1; m=0.95; eps=5.e-4;
a=0; b=1.0; % endpoints of solution
dx(1)=0.1; % initial step size
% save x,y and dx values in vectors so we can plot solution
x(1)=a; y(1)=a*exp(-a)+1; % initial x and y values
i=1;
while x(i) < b % keep going until you reach the end point
R=0;
while R < 1
% calculate solution in one step
yone=y(i)+dx(i)*feval(’eulerf’,x(i),y(i));
% calculate solution in two steps
yhalf=y(i)+dx(i)/2*feval(’eulerf’,x(i),y(i));
ytwo=yhalf+dx(i)/2*feval(’eulerf’,x(i)+dx(i)/2,yhalf);
R=(abs(eps*ytwo/(yone-ytwo)))ˆ(1/(p+1));
dx(i)=m*R*dx(i);
end
y(i+1)=ytwo; x(i+1)=x(i)+dx(i);
dx(i+1)=dx(i); i=i+1;
end
for j=1:i
exact(j)=x(j)*exp(-x(j))+1;
end
plot(x,y,’ro’)
hold on
plot(x,exact,’b-’);
legend(’Adaptive’,’Exact’,2)
hold off
print -depsc euleradaptive
plot(x,dx,’ro’)
print -depsc euleradaptivedx
MATLAB code
function fxy=eulerf(xx,yy)
fxy=exp(-xx)-yy+1;
return
Index EULER’S METHOD 49
Below are plots of the Euler method and adaptive Euler method and also a plot showing
the step size used. Notice that the step size increases as the solution flattens as larger steps
can be taken for the same error.
1.4
Adaptive
Exact
1.35
1.3
1.25
1.2
1.15
1.1
1.05
1
0 0.2 0.4 0.6 0.8 1 1.2 1.4
0.08
0.075
0.07
0.065
0.06
0.055
0.05
0.045
0.04
0.035
0.03
0 0.2 0.4 0.6 0.8 1 1.2 1.4
Instead of using the slope at the starting point use the slope at the midpoint. This in
general will be a better approximation to the function.
Graphically
The slope at the midpoint is then approximated by f (xi+1/2 , yi+1/2 ) so use this in an Euler
like formulation to get the formula
Example
Solve
y′ = xy with y(0) = 1
2 /2
on [0, 1]. This has exact solution y = ex .
1.8
Euler
Exact
1.7
Midpoint
1.6
1.5
1.4
1.3
1.2
1.1
1
0 0.2 0.4 0.6 0.8 1
Below are the results of calculation with the Euler and Midpoint methods for various step
sizes. Compare the size of the errors for the same number of flops∗ .
The Midpoint method is a second order method. That is the total error is proportional to
(∆x)2 . Hence for a 10 fold decrease in the step size (∆x) you get an approximate 100 fold
decrease in the error.
For the same amount of computing effort (flops) the Midpoint method is far superior to
Euler’s method. Just compare the errors for the Euler 120 flops calculation (N = 20,
error≈ 0.05278) with the Midpoint 120 flops calculation (N = 10, error≈ 0.00257).
52 3. INITIAL VALUE PROBLEMS Index
MATLAB code
% midpoint.m
%
% solve y’=xy subject to y(0)=1
% find the result at x=1
% for Euler and Midpoint methods and count flops.
%
clear all;
N=10; % number of points,
a=0; b=1; % initial and end points
dx=(b-a)/N; % step size
x(1)=a; y(1)=1; % initial x, y(Euler) values
z(1)=y(1); % initial z(Midpoint) value
flops(0)
for i=1:N % Euler method
y(i+1)=y(i)+dx*feval(’midf’,x(i),y(i));
x(i+1)=x(i)+dx;
end
eulerflops=flops
flops(0);
for i=1:N % Midpoint method
zhalf=z(i)+dx/2*feval(’midf’,x(i),z(i));
z(i+1)=z(i)+dx*feval(’midf’,x(i)+dx/2,zhalf);
x(i+1)=x(i)+dx;
end
midptflops=flops
plot(x,y,’b-’)
hold on
exact=exp(0.5.*x.ˆ2);
plot(x,exact,’go’);
plot(x,z,’r-’)
legend(’Euler’,’Exact’,’Midpoint’,2)
hold off
print -depsc midptexample
erroreuler=abs(exact(N+1)-y(N+1))
errormidpt=abs(exact(N+1)-z(N+1))
MATLAB code
function f=midf(xx,yy)
% midf.m
f=xx*yy;
return
Index SECOND ORDER RUNGE-KUTTA METHOD 53
Named after 2 German Mathematicians, Carl David Tolme Runge (1856–1927) and Wil-
helm Kutta (1867–1944)
The previous methods (Euler, Midpoint) have all used some approximation to the slope.
Euler: the slope at the left point
Midpoint: the slope at the midpoint.
The Runge-Kutta method uses a weighted average of the the slope of the left point and
some as yet unknown intermediate point. So the general formula is
yi+1 = yi + ∆x favg
favg = a fi + b fi′
where fi = f (xi , yi ) and fi′ = f (xi′ , yi′ ) is the function evaluated at some undetermined
point given by
xi′ = xi + α ∆x
yi′ = yi + β fi ∆x
The method consists of finding the values of the weights a, b and the position of the
intermediate point given by α and β .
Recall the Taylor Series expansion for yi+1 = y(xi + ∆x) is
dy (∆x)2 d 2 y
yi+1 = yi + ∆x |i + |i + . . .
dx 2 dx2
(∆x)2 d f
= yi + ∆x fi + |i + . . .
2 dx
(∆x)2 ∂ f ∂ f dy
= yi + ∆x fi + |i + |i + . . .
2 ∂x ∂ y dx
(∆x)2 ∂ f ∂f
= yi + ∆x fi + |i + f |i + . . .
2 ∂x ∂y
yi+1 = yi + ∆x favg
= yi + ∆x (a fi + b fi′ )
54 3. INITIAL VALUE PROBLEMS Index
∂f ∂f
≈ yi + ∆x a fi + b f (xi , yi ) + α ∆x |i + β fi ∆x |i
∂x ∂y
∂f ∂f
= yi + ∆x a fi + b fi + bα ∆x |i + bβ fi ∆x |i
∂x ∂y
Comparing this with the Taylor Series expansion for yi+1 gives
1 1
a+b = 1 bα = bβ =
2 2
Which is 3 equations in the 4 unknowns a, b, α and β so there is still some degree of
freedom of choice.
For example setting
1
a=0 b=1 α =β =
2
gives the midpoint method.
These methods are second order. Because there is still a degree of freedom in the equa-
tions for specific problems it is possible to make a choice of a, b, α and β that will make
the method third order. This method has been precoded in MATLAB and is used with a
call to ode23
Use help ode23 to find out more about the MATLAB command and how to use it. In
particular how to choose the accuracy.
ode23 also uses an adaptive step size as well if it needs to find the solution more accu-
rately in a given region of the interval.
Index SECOND ORDER RUNGE-KUTTA METHOD 55
Example
Solve
dy
= ty with y(0) = 1
dt
2 /2
on [0, 1]. This has exact solution y = et .
MATLAB code
% o23example.m
% solve dy/dt=odefunc(t,y) on [a,b] given y(a)=c
a=0; b=1; c=1; % interval and initial value
y0=[c]; % initial condition
tspan=[a b]; % interval of integration
[t,y]=ode23(’odefunc’,tspan,y0);% soln in vectors t and y
plot(t,y)
hold on
exact=exp(0.5.*t.ˆ2);
plot(t,exact,’ro’)
legend(’ode23’,’Exact’,2)
print -depsc o23example
MATLAB code
function f=odefunc(t,y)
% odefunc.m
% the rhs of the IVP dy/dt=f(t,y)
f=t*y;
return
1.8
ode23
Exact
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1
0 0.2 0.4 0.6 0.8 1
56 3. INITIAL VALUE PROBLEMS Index
In exactly the same way as the second order Runge-Kutta method was developed higher
order methods can be developed. By including more sampling points in the interval
greater accuracy can be obtained. The second order method used the left-hand point
and one other point to determine an approximation to the solution. Hence two function
evaluations were required for each step. The Runge-Kutta fourth order method uses the
left-hand point and 3 other points to hence four function evaluations are required for each
step. The method is fourth order accurate that is the total error is proportional to (∆x)4 .
1
yi+1 = yi + (z1 + 2z2 + 2z3 + z4 )
6
where
z1 = f (xi , yi )∆x
1 1
z2 = f (xi + ∆x, yi + z1 )∆x
2 2
1 1
z3 = f (xi + ∆x, yi + z2 )∆x
2 2
z4 = f (xi+1 , yi + z3 )∆x
The MATLAB command for using this method is ode45 and is used exactly the same
way as ode23. The fourth order Runge-Kutta method is by far the most commonly used
method for solving IVPs.
Index ODE45 57
3.7 ode45
>> help ode45
Example
Solve
y′′ = t 2 cos(t)y s.t. y(0) = 2 and y′ (0) = 3
MATLAB code
% o45ex1.m
% solve y’’=tˆ2*y*cos(t)on [0,5] given y(0)=2, y’(0)=3
% this can be written as a system of first order DEs
% y1’=y2 y1(0)=2
% y2’=tˆ2*y1*cos(t) y2(0)=3
a=0; b=5; % interval
y0=[2 3]; % initial condition
tspan=[a:.1:b]; % interval of integration
[t,y]=ode23(’o45ex1func’,tspan,y0);% soln in vectors t and y
plot(t,y(:,1),t,y(:,2),’r--’) % plot y1 and y2 versus t
legend(’y’,’dy/dt’,2)
xlabel(’t’); ylabel(’y and dy/dt’)
print -depsc o45ex1 % output to postscript file
MATLAB code
function f=o45ex1func(t,y)
% o45ex1func.m
% the RHS of the system of DEs
f(1)=y(2);
f(2)=tˆ2.*y(1).*cos(t);
f=f(:); % forces f to be a column vector
return
30
y
dy/dt
20
10
y and dy/dt
−10
−20
−30
0 1 2 3 4 5
t
Index ODE45 59
The MATLAB function ode45 has a vast array of options that can be used.
Error tolerances
There is an extra parameter that can be added to the parameter list called ’options’. The
call to ode45 is then
ode45(’func’,tspan,y0,options)
The options parameter is set using the command odeset. For example
options = odeset(’RelTol’,1e-3,’AbsTol’,[1e-4 1e-4 1e-5]);
Would set the relative error tolerance to 1 × 10−3 and the absolute error tolerance to
1 × 10−4 for the first two components and 1 × 10−5 for the third. This is useful if you
know (or expect) that some of the variables will be substantially different is magnitude
than others.
Consider the problem of a projectile fired from a fixed position. If you know the initial
velocity and angle of the projectile it is possible to determine the trajectory of the projec-
tile. Other factors such as drag on the projectile and lift can also be taken into account. A
system of differential that governs the motion of the projectile is given by
dv 1
m = −mg sin φ − ρ ACD v2
dt 2
dφ 1
mv = −mg cos φ + ρ ACL v2
dt 2
where φ = angle from horizontal, v = velocity, m = mass. ρ = density, A = area, CD drag
coefficient, CL lift coefficient, g gravity.
The trajectory of the projectile is governed by
dx
= v cos φ
dt
dy
= v sin φ
dt
If the initial position (x, y coordinates), initial velocity (v) and angle (φ ) are known then
the trajectory of the projectile can be easily determined.
This is a system of 4 nonlinear differential equations in the 4 unknowns x, y, v and φ .
Solving these equations analytically is not possible but solving them numerically is very
easy using MATLAB. Rearrange the equations into a standard form where the left hand
side is just the derivative of each variable.
dv ρ ACD v2
= −g sin φ −
dt 2m
dφ −g cos φ ρ ACL v
= +
dt v 2m
dx
= v cos φ
dt
dy
= v sin φ
dt
Then just numerically integrate these equations forward in time form the known initial
position, velocity and angle.
Index PROJECTILE EXAMPLE 61
MATLAB code
MATLAB code
function f=projectiledes(t,y)
% projectiledes.m the governing DEs for the projectile
% y(1) is velocity, y(2) is angle, y(3) is x, y(4) is y.
% values for a spinning soccer ball
g=9.8; rho=1.23; A=0.038; m=0.42; D=0.22; Cd=0.20; Cl=0.05;
v=y(1); phi=y(2);
f(1)=(-g.*sin(phi)-rho.*A.*Cd./(2..*m).*v.ˆ2); % velocity
f(2)=(-g.*cos(phi)./v + rho.*A.*Cl./(2.*m).*v);% angle
f(3)=v.*cos(phi); % x coordinate
f(4)=v.*sin(phi); % y coordinate
f=f(:); % force f to be a column vector
return
62 3. INITIAL VALUE PROBLEMS Index
>> projectile
enter theta, use < 0 to stop...30
theta =
30
range =
8.2272
enter theta, use < 0 to stop...45
theta =
45
range =
6.7203
enter theta, use < 0 to stop...60
theta =
60
range =
4.7443
enter theta, use < 0 to stop...-1
theta =
-1
>> diary off
3.5
2.5
1.5
0.5
0
0 2 4 6 8 10
MATLAB can be set up to integrate the DE until a specific event occurs, for example
here that might be when y = 0 (ie when the projectile hits the ground). You can then use
fzero to find the initial angle that gives a specific range (ie hits the target).
4 Phase Plane Analysis
4.1 Introduction
Phase plane analysis is useful in determining the behaviour of solutions to systems of dif-
ferential equations without necessarily solving the system analytically. More importantly
this technique is not limited to studying linear equations.
For systems of 2 autonomous first order differential equations it is possible to plot solu-
tions of one dependent variable against the other dependent variable with the time depen-
dence varying along the curves. This is known as a phase plane. The goal is to sketch
enough solution curves so that the general behaviour of the system can be summarised by
looking at the graph.
The system
dx1
= x2
dt
dx2
= −2x1 − x2
dt
has phase plane consisting of spirals converging to the origin.
2
x2
−1
−2
−1 −0.5 0 0.5 1 1.5 2 2.5
x
1
64 4. PHASE PLANE ANALYSIS Index
MATLAB code
% ppexample.m
% drawing phase plane for
% x1’=x2
% x2’=-2x1-x2
%
tend=10;
tspan=[0 tend];
y0=[0 5];
[t,y]=ode45(’ppexamplef’,tspan,y0);
plot(y(:,1),y(:,2))
xlabel(’x_1’); ylabel(’x_2’);
print -depsc ppexample
MATLAB code
function f=ppexamplef(t,y)
% shootingexamplef.m
f(1)=y(2);
f(2)=-2*y(1)-y(2);
f=f(:);
return
Index INTRODUCTION 65
Or with multiple initial points put the whole thing into a for loop and update the initial
points as you go through the loop.
MATLAB code
% ppexample2.m
% drawing phase plane for
% x1’=x2
% x2’=-2x1-x2
%
tend=10;
tspan=[0 tend];
for s=-2:1:3
y0=[s 5];
[t,y]=ode45(’ppexamplef’,tspan,y0);
plot(y(:,1),y(:,2))
xlabel(’y_1’); ylabel(’y_2’);
hold on
end
hold off
print -depsc ppexample2
1
2
y
−1
−2
−3
−4
−2 −1 0 1 2 3 4 5
y1
66 4. PHASE PLANE ANALYSIS Index
A very simple way in MATLAB to quickly get a feel for the solutions of a system of DEs
is to draw the velocity field.
The MATLAB command quiver is designed to do precisely this.
help quiver
Example:
[x,y] = meshgrid(-2:.2:2,-1:.15:1);
z = x.*exp(-x.ˆ2 - y.ˆ2);
[px,py] = gradient(z,.2,.15);
contour(x,y,z), hold on
quiver(x,y,px,py), hold off, axis image
diary off
Index INTRODUCTION 67
MATLAB code
% vfield.m
%
% plots velocity vectors for the following system
% dx/dt = y
% dy/dt = -2x-y
%
a=-5; b=5; % size of the grid
[x,y]=meshgrid(a:1:b,a:1:b);% a square grid (a to b)x(a to b)
dx=y; % the DEs
dy=-2*x-y; %
quiver(x,y,dx,dy,’r’) % plot the vector field
axis square % make the plot square
axis tight % make axis tight on data
hold on
w=[a-1 b+1];
plot(w,0*w,’k-’,0*w,w,’k-’) % adds x and y axes to plot
xlabel(’x’); ylabel(’y’);
hold off
% make an eps version of the plot called vfield.eps
print -depsc vfield
% if you dont want the arrow heads then just use
% quiver(x,y,dx,dy,’.’);
% the arrows are automatically scaled to fit the plot space
% if you want bigger or smaller arrows use a scaling
% quiver(x,y,dx,dy,3) % makes the arrow 3 times longer
% for more help type help quiver
0
y
−1
−2
−3
−4
−5
−5 0 5
x
68 4. PHASE PLANE ANALYSIS Index
Predator-Prey models are used to model the interaction between a predator and its prey.
The simplest is known as the Lotka-Volterra Model. For example the number of rabbits
(prey) and foxes (predator). Let y1 be the number of predators and y2 be the number of
prey then the governing differential equations are
dy1
= −ay1 + by1 y2
dt
dy2
= −cy1 y2 + dy2
dt
Critical points
Set derivatives equal to zero and solve for y1 and y2 .
This gives the critical points as (y1 , y2 ) = (0, 0) and (y1 , y2 ) = (d/c, a/b)
Calculate the Jacobian and classify each of the critical points. The Jacobian is
" ∂ ∂
#
∂ y1 (−ay1 + by1 y2 ) ∂ y2 (−ay1 + by1 y2 )
J(y1 , y2 ) = ∂ ∂
∂ y1 (−cy1 y2 + dy2 ) ∂ y2 (−cy1 y2 + dy2 )
−a + by2
by1
=
−cy2 −cy1 + d
So substituting each critical point gives
−a 0
J(0, 0) =
0 d
Which is a diagonal matrix so the eigenvalues are λ1 = −a and λ2 = d hence (0, 0) is a
saddle.
0 bd/c
J(d/c, a/b) =
−ac/b 0
√
Calculating the eigenvalues gives λ = ±i ad and so (d/c, a/b) is (probably) a centre.
How does the population of both the predator and prey species vary with time?
Index PREDATOR-PREY MODEL 69
MATLAB
MATLAB code
% predprey.m
% Predator-Prey system
%
clear all
global a b c d % variables global to all functions
a=0.1; b=0.002; c=0.0025; d=0.2;
y0=[10 50]; % initial point
tspan=[0 120]; % range to integrate over
[t,y]=ode45(’predpreyf’,tspan,y0);
plot(t,y(:,1),’b-’) % plot predator popoluation vs time
hold on % hold the plot
plot(t,y(:,2),’r--’) % plot prey popoluation vs time
legend(’predator’,’prey’)
hold off % turn off plot hold
print -depsc predprey % send plot to postscript file
plot(y(:,1),y(:,2),’g-’) % plot trajectories in (y1,y2)space
print -depsc predpreytraj% send plot to postscript file
MATLAB code
function f=predpreyf(t,y)
% predpreyf.m
global a b c d
f(1)=-a*y(1) + b*y(1)*y(2);
f(2)=-c*y(1)*y(2) + d*y(2);
f=f(:);
return
300 300
predator
prey
250 250
200 200
150 150
100 100
50 50
0 0
0 20 40 60 80 100 120 0 50 100 150 200 250 300
Competition models are used to model the competition between two (or more) species
(companies, people) who are competing for the same resource.
dy1 r1
= y1 (K1 − y1 − ay2 )
dt K1
dy2 r2
= y2 (K2 − y2 − by1 )
dt K2
If there is no second species (y2 = 0) then the first population grows until it approaches
the steady population K1 . Similarly for the other species.
Critical points
Set derivatives equal to zero and solve for y1 and y2 .
(y1 , y2 ) = (0, 0)
(y1 , y2 ) = (K1 , 0)
(y1 , y2 ) = (0, K2)
and a fourth point is located at the intersection of the two lines K1 − y1 − ay2 = 0 and
K2 − y2 − by1 = 0.
Look at the Jacobian to classify each critical point.
MATLAB code
% compjacobian.m
%
format compact
clear all
a=0.75; b=3.0; r1=0.2; r2=0.1; K1=50; K2=100;
>> compjacobian
How does the population of both the species vary with time? Does one species dominate
the other?
MATLAB code
% competition.m
%
clear all
global a b r1 r2 K1 K2 % variables global to all functions
a=0.75; b=3.0; r1=0.2; r2=0.1; K1=50; K2=100;
for i=0:10 % loop through various initial points
for j=0:1
y0=[j*50+5 i*10+5]; % initial points
tspan=[0 200]; % range to integrate over
[t,y]=ode45(’competitionf’,tspan,y0);
plot(y(:,1),y(:,2)) % plot trajectories in (y1,y2)space
hold on
end
end
print -depsc competitiontraj % send plot to postscript file
hold off % turn off plot hold
MATLAB code
function f=competitionf(t,y)
% competitionf.m
global a b r1 r2 K1 K2 % variables global to all functions
f(1)=r1/K1*y(1)*(K1 - y(1) - a*y(2));
f(2)=r2/K2*y(2)*(K2 - y(2) - b*y(1));
f=f(:);
return
120
100
80
60
40
20
0
0 10 20 30 40 50 60
Index SIR DISEASE MODEL 73
Disease models can give great insight into how diseases spread through a population.
They can be used to determine suitable strategies for inoculation, such as what percentage
of the population need to be inoculated to prevent the spread of a disease. Or simply as a
guide to how widespread a disease will become if left alone.
Consider a fixed population of size K. A fixed population size is a reasonable assumption
if the birth rate is approximately the same as the death rate and the disease does not
actually kill the person. For instance measles in the developed world is rarely a fatal
disease and has no impact on the overall birth or death rate.
Consider three distinct classes of people:
Susceptibles (S): those people who are susceptible to catching the disease
Infectives (I): those people who have the disease and can pass it on
Recovered (R): those people who have had the disease and recovered from it.
They are no longer susceptible as they have immunity now.
This is (for obvious reasons) known as an SIR model. The total population is fixed so that
S+R+I = K
The general birth rate of people (who are all born susceptible) is µ . Because the pop-
ulation is assumed to be constant the death rate must also be µ but any of susceptibles,
infectives or recovereds can die. The disease is assumed not to alter the death rate.
Consider how the population of susceptibles can change.
Susceptibles catching the disease and becoming infective, births of susceptibles, or dying.
To catch the disease a susceptible must come in contact with an infective hence the chance
of catching the disease is proportional (with constant β ) to the product of the number of
susceptibles and infectives. The DE that models the susceptible population is therefore
dS
= −β SI + µ K − µ S
dt
recovered death
Note that the recovered equation does not effect the susceptible or infective equations
and in fact since the population is assumed constant the number of recovereds can always
be determined from the number of susceptibles and the number of infectives. This last
equation is not needed in the model.
So we have two first oder nonlinear differential equations in two unknowns (S and I).
dS
= −β SI + µ K − µ S
dt
dI
= β SI − γ I − µ I
dt
These are nonlinear equations hence we need to calculate the critical points and analyse
the behaviour near those critical points.
Critical points
Set derivatives equal to zero and solve for S and I.
0 = −β SI + µ K − µ S
0 = β SI − γ I − µ I
Index SIR DISEASE MODEL 75
−β I − µ −β S
" #
=
βI βS−γ −µ
−µ −β K
J(K, 0) =
0 βK −γ −µ
If β K − γ − µ < 0 then the critical point (S, I) = (K, 0) is a stable node and hence the
S → K, I → 0, that is the disease dies out.
If β K − γ − µ > 0 then the critical point (S, I) = (K, 0) is a saddle and hence (K, 0) is
not the final state. So what is the final state in his case? Presumably it must be the other
critical point since the model is bounded. Why is the model bounded?
Example
Consider the case with K = 100, β = 0.001, µ = 0.02, γ = 0.03
76 4. PHASE PLANE ANALYSIS Index
MATLAB code
% sir.m
% this M-file plots velocity vectors for the SIR model, plots
% some trajectories, calculates e’values and e’vectors of Jacobian
%
global beta K mu gamma % makes these global variables
format compact
beta=0.001; K=100; mu=0.02; gamma=0.03;
%
% use quiver to plot the vector field
a=0; b=100; % size of the grid
[S,I]=meshgrid(a:10:b,a:10:b);% a square grid (a to b)x(a to b)
dS=-beta*S.*I+mu*K-mu*S; % the DEs
dI=beta*S.*I-gamma*I-mu*I; %
quiver(S,I,dS,dI,2.5,’r’) % plot vector field scaled by 2.5
axis square % make the plot square
axis([a b a b]) % restrict the axes to be a to b
hold on
w=[a b];
plot(w,0*w,’k-’,0*w,w,’k-’) % adds x and y axes to plot
xlabel(’Susceptibles’); ylabel(’Infectives’); title(’SIR model’)
%
% now add some trajectories by numerically solving the DE
tspan=[0 250];
for icS=0:100:100
for icI=5:10:95
initcond=[icS icI];
[t,y]=ode45(’sirfunc’,tspan,initcond);
plot(y(:,1),y(:,2),’b-’)
end
end
hold off
print -depsc sirtraj
%
% now set up the critical points and calculate the Jacobian
% and the eigenvalues and eigenvectors so you can classify the
% critical points
cpS=[K (gamma+mu)/beta];
cpI=[0 mu*K/(gamma+mu)-mu/beta];
for i=1:1:2
fprintf(’\nCritical point number %d is (%5.2f,%5.2f) \n’,...
i,cpS(i),cpI(i))
Jacobian=[-beta*cpI(i)-mu -beta*cpS(i);...
beta*cpI(i) beta*cpS(i)-gamma-mu]
[Jeigvec, Jeigval]=eig(Jacobian)
end;
MATLAB code
function f=sirfunc(t,y)
% sirfunc.m
% the DEs for the SIR model
global beta K mu gamma
S=y(1); I=y(2);
f(1)=-beta*S.*I+mu*K-mu*S;
f(2)=beta*S.*I-gamma*I-mu*I;
f=f(:);
return
Index SIR DISEASE MODEL 77
sir
SIR model
100
90
80
70
60
Infectives
50
40
30
20
10
0
0 20 40 60 80 100
Susceptibles
78 4. PHASE PLANE ANALYSIS Index
5 Boundary Value Problems
5.1 Introduction
In contrast to Initial Value Problems where all function values and derivatives are given at
one point Boundary Value Problems (BVP) have function values and derivatives at two
points. Hence it is not possible to construct a Taylor Series about one point and use this
to build up the solution as was done with IVPs.
Examples
There are two general methods for solving BVPs numerically, shooting method and re-
laxation method.
80 5. BOUNDARY VALUE PROBLEMS Index
Example
How might you go about numerically solving
2. Integrate forward to x = 2 using any of the previous methods (Euler, midpoint, R-K,
ode45, . . . )
MATLAB code
% shootingexample.m
% trying to solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3
clear all
c=1; % the first guess for y’(0)
tspan=[0 2];
while c > -999 % loop through different c values
c=input(’enter c, -1000 to stop ’);
if c==-1000 break; end % if c=-1000 jump out of loop
y0=[1 c]; % initial condition [y(0) y’(0)]
[t,y]=ode45(’shootingexamplef’,tspan,y0);
plot(t,y(:,1)) % plot current solution
hold on % keep the plot
lgthy=length(y); % find the length of the vector y
disp([y(lgthy,1)]) % show y(x=2) value
cc=num2str(c); % convert value of c to a string variable
text(2.1,y(lgthy,1),cc) % write value of c on rhs of graph
end
text(2.1,5,’c guess’) % write c guess on the plot at (2.1,5)
hold off
print -deps shootingexample
MATLAB code
function f=shootingexamplef(t,y)
% shootingexamplef.m
% the DE y’’ + 3y’ + 2y = 2x written as a system
f(1)=y(2);
f(2)=-3*y(2)-2*y(1)+2*t;
f=f(:);
return
Index SHOOTING METHOD 81
5.5
5 c guess
4.5
3.5 20
18
3 16
2.5
10
2
1.5
1
0 0.5 1 1.5 2
Hence there is a solution that has y(2) = 3 somewhere between y′ (0) = 16 and y′ (0) = 18.
How do you go about finding this value of y′ (0) that gives the correct value of y(2) ?
Treat the problem as a zero finding problem. That is find the value of y′ (0) = c that results
in f (c) = y(2) − 3 = 0. So use one of MATLABs methods for finding zeros of functions.
82 5. BOUNDARY VALUE PROBLEMS Index
Set it up so that there is a MATLAB function that takes as input the guess for the initial
slope and returns the error in the right hand boundary condition. Then call this function
from fzero to find the correct slope that integrates to the correct right hand boundary
condition.
MATLAB code
% shootingexamplemain.m
% the main M-file for using the shooting method to solve the
% DE defined in shootingexamplef.m
% Uses fzero to solve the equation y’(0)=3 where y’(0) is
% determined in shootingfunc.m by shooting forward using ode45
%
myguess=4; % a guess at the value of y’(0)
actualvalue=fzero(’shootingfunc’,myguess)
% use ode45 to get correct solution using actualvalue and plot
tspan=[0 2]; y0=[1 actualvalue];
[t,y]=ode45(’shootingexamplef’,tspan,y0);
plot(t,y(:,1))
print -deps shootingexampleplot
MATLAB code
function f=shootingfunc(c)
% set up as a function, the input (c) is the initial slope
% y’(0) and the output (f) is the difference between the value
% of y at x=2 and the number 3 (since y(2)=3 for the correct
% solution. This function is called by MATLABs fzero
%
% shootingfunc.m
% trying to solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3
% using y(0)=1 and y’(0)=c
tspan=[0 2]; y0=[1 c];
[t,y]=ode45(’shootingexamplef’,tspan,y0);
f=y(length(y),1)-3; % set function value to y(2)-3
return
MATLAB code
function f=shootingexamplef(t,y)
% shootingexamplef.m
% the DE y’’ + 3y’ + 2y = 2x written as a system
f(1)=y(2);
f(2)=-3*y(2)-2*y(1)+2*t;
f=f(:);
return
Index SHOOTING METHOD 83
>> shootingexamplemain
actualvalue = 16.9727
5.5
4.5
3.5
2.5
1.5
1
0 0.5 1 1.5 2
84 5. BOUNDARY VALUE PROBLEMS Index
Relaxation methods differ substantially from the previous shooting methods. The func-
tion is not integrated but rather an approximate solution is made that fits the governing
differential equation. This results in a system of equations where the unknowns are the
values of the function at the interior points. To do this we need to determine some ap-
proximations to the derivatives in any given differential equation.
Finite differences is the procedure where we replace any derivatives by a finite difference
approximation. For example recall the definition of the derivative
Both the forward and backward approximations are first order accurate, that is the first
neglected term is order ∆x.
There is in fact a second order accurate approximation known as the central approxima-
tion given by
dy yn+1 − yn−1
≈
dx n 2h
d 2 y
yn+1 − 2yn + yn−1
2
≈
dx n h2
This is the central approximation to the second derivative and is second order accurate.
Index RELAXATION METHODS 85
Finite differences can be used to solve a BVP. What results is simply solving a system of
linear equations which is simple in MATLAB.
Algorithm
1. Divide up the region into N equal subintervals. Note that the differential equation
must be true at each grid point in the region.
2. Replace the derivatives in the differential equation by thier finite difference approx-
imations at each grid point in the region.
3. If possible use the boundary conditions to replace some of the values. This is usu-
ally only possible for the equations at each end of the interval.
4. Rearrange the equations so that they are written as a matrix system of equations.
The unknowns are the value of the solution at each grid point.
5. Solve the matrix system of equations. This then gives the solution at each grid
point.
Example
Numerically solve
4. Mutiply by h2 to simplify, collect all terms with the same unknown value and writ-
ten out in full we have
.. .. ..
. . .
Now y0 and yN are known (since they are the boundary conditions) so put them on
the right hand side with all the other known parts gives
2h2 − 2
1 − 3h/2 0 0 0 ... 0
1 − 3h/2
2h2 − 2 1 + 3h/2 0 0 ... 0
0 1 − 3h/2 2h2 − 2 1 + 3h/2 0 ... 0
1 − 3h/2 2h2 − 2 1 + 3h/2 ...
A= 0 0 0
.. .. .. .. .. .. ..
. . . . . . .
2h2 − 2
0 ... 0 0 1 − 3h/2 1 + 3h/2
0 ... 0 0 0 1 − 3h/2 2h2 − 2
2h2 x1 − (1 − 3h/2)y0
2h2 x2
2h2 x3
2h2 x4
b=
..
.
2h2 xN−2
2h2 xN−1 − (1 + 3h/2)yN
What do you notice about the matrix A? You will see that it is ’tri-diagonal’ (only
3 diagonals are non-zero) and each diagonal has the same value. This makes it
particularly easy to program in MATLAB using the diag command.
Once you have coded A, and b it is a simple matter to solve the system AY = b to
get the solution Y . This is then the approximation to the solution of the original
ODE.
Coding the diagonal matrix A is relatively easy in MATLAB as there is a command
diag that is used to enter values into a diagonal matrix. The ones command
is also useful for making vectors of a certain length with the same value in each
position.
Solving the system AY = b is easy using the ’backslash divide’ command y=A\b .
88 5. BOUNDARY VALUE PROBLEMS Index
Example
m = 5;
diag(-m:m) + diag(ones(2*m,1),1) + diag(ones(2*m,1),-1)
produces a tridiagonal matrix of order 2*m+1.
Overloaded methods
help sym/diag.m
MATLAB code
% fdexample.m
%
% solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3
% using finite differences
a=0; b=2; % endpoints
y0=1; yN=3; % y values at endpoints
N=100; % number of points
h=(b-a)/N; % x step size
x=a+h:h:b-h; % set up vector of x points (interior points)
% set up the matrix and solve Ay=RHS for y
A=(2*hˆ2-2)*diag(ones(1,N-1)); % main diagonal elements
A=A+(1-3*h/2)*diag(ones(1,N-2),-1); % one below main diagonal
A=A+(1+3*h/2)*diag(ones(1,N-2),1); % one above main diagonal
RHS=2*hˆ2*x;
RHS(1)=RHS(1)-(1-3*h/2)*y0;
RHS(N-1)=RHS(N-1)-(1+3*h/2)*yN;
y=A\RHS’;
plot(x,y)
5.5
4.5
3.5
2.5
1.5
1
0 0.5 1 1.5 2
90 5. BOUNDARY VALUE PROBLEMS Index
Example
Solve
y′′ + 5y′ + 6y = cos x where y(0) = 2 y(3) = 6
using finite differences.
Index RELAXATION METHODS 91
MATLAB code
% fdexample2.m
%
% solve y’’ + 5y’ + 6y = cos x y(0)=2 y(3)=6
% using finite differences
a=0; b=3; % endpoints
ya=2; yb=6; % y values at endpoints
N=100; % number of points
h=(b-a)/N; % x step size
x=a+h:h:b-h; % set up vector of x points (interior points)
% set up the matrix and solve Ay=RHS for y
A=(6*hˆ2-2)*diag(ones(1,N-1)); % main diagonal elements
A=A+(1-5*h/2)*diag(ones(1,N-2),-1); % one below main diagonal
A=A+(1+5*h/2)*diag(ones(1,N-2),1); % one above main diagonal
RHS=hˆ2*cos(x);
RHS(1)=RHS(1)-(1-5*h/2)*ya;
RHS(N-1)=RHS(N-1)-(1+5*h/2)*yb;
y=A\RHS’;
plot(x,y)
N=5 N = 10
160 400
140 350
120
300
100
250
80
200
60
150
40
20 100
0 50
−20 0
0.5 1 1.5 2 2.5 0 0.5 1 1.5 2 2.5 3
N = 20 N = 100
400 400
350 350
300 300
250 250
200 200
150 150
100 100
50 50
0 0
0 0.5 1 1.5 2 2.5 3 0 0.5 1 1.5 2 2.5 3
92 5. BOUNDARY VALUE PROBLEMS Index
There are many numerical problems with solving the matrix equation Ax = b
The matrix equations that result from the finite difference method are tridiagonal. That is
they have entries down the 3 central diagonals and zeros elsewhere. These are relatively
efficient to solve Ax = b for.
2. Iteration
Make a guess and use the equations to refine that guess until some convergence
criteria is met.
Usually the best method for large matrices (bigger than 25 × 25).
Most common method is known as the Gauss-Seidel Iteration method
Finite differences are only one of the many method of solving BVPs. Another very pop-
ular method is known as the finite element method. The concept underlying finite dif-
ference methods is that approximations are made to the derivatives in the differential
equation. In contrast to this for finite element methods the solution is approximated by a
sequence of model functions and the error in using these functions is minimized.
For finite element methods it is more convenient to work on a BVP that has homogeneous
boundary conditions. That is the value of the function at the end points is zero. Fortunately
we can make a change of variable to ensure this. For example consider the problem
we can transform this to a problem with homogeneous boundary conditions by the trans-
formation
y(x) = Y (x) + ax + b
Which can be shown to give
1 1
a= (yL − yR ) b= (−xR yL + xL yR )
xL − xR xL − xR
Example
What does
y′′ + 3y′ + 2y = 5x2 y(0) = 2 y(2) = 3
become when it is transformed to have homogeneous boundary conditions
94 5. BOUNDARY VALUE PROBLEMS Index
where L is a sysmbol that represents all the differential operators in the differential equa-
tion.
Example
The ODE
y′′ + 3x2 y′′ + 2ex y = 5x2
can be represented as
2 d2 d
L y = 5x where L ≡ 2 + 3x2 + 2ex
dx dx
Algorithm
The aim is to find the coefficients ci to get the best solution possible.
4. It is this last condition 3(b) that gives conditions that enable the coefficients ci to be
found. By substituting the definition of ∆(x) above into this expression we get for
each j = 1, . . ., n
Z xR
w j (x)∆(x)dx = 0
xL
" #
Z xR n
xL
w j (x) ∑ ciL φi(x) − r(x) dx = 0
i=1
Z xR n Z xR
w j (x) ∑ ci L φi (x)dx − w j (x)r(x)dx = 0
xL i=1 xL
n Z xR Z xR
∑ ci xL
w j (x)L φi (x)dx =
xL
w j (x)r(x)dx
i=1
If the weight functions (w j (x)) and the expansion functions (φ j (x)) are specified
then the right hand side of the last expression is known.
Z xR
bj = w j (x)r(x)dx
xL
These functions are triangles that span 3 grid points and are zero everwhere else.
For example φ3 is a triangle that begins at zero at x2 increases to a maximum at x3
and then decreases to zero at x4 .
φ3
x0 x1 x2 x3 x4 x5 x6
The benefit of these functions is that they are local. That it is only nonzero in the
immediate neighbourhood of the central point. This guarantees that the resulting
matrix eqaution will be sparse (have many zero entries) and hence can be solved
effeciently using iterative methods.
These triangular hat functions are useful as they have some special properties that
make calculating the M matrix easier.
Z xR
F(x) φi F(x) dx
xL
φi 2h/3
φi±1 h/6 (zero for all other indices)
φi±1
′ ±1/2 (zero for all other indices)
φi′′ −2/h
φi±1
′′ 1/h (zero for all other indices)
1 h
x ih2
x2 (i2 + 1/6)h3
x3 i(i2 + 1/2)h4
Index RELAXATION METHODS 97
Solve
y′′ + y = 1 y(0) = 0 y(π /2) = 0
d2
Now L ≡ dx2
+ 1 so to calculate M ji break it down into each term such that
Z π /2
M ji = φ j (x)L φi (x)dx
0
Z π /2 2
d
= φ j (x) + 1 φi (x)dx
0 dx2
Z π /2 Z π /2
d 2 φi (x)
= φ j (x) dx + φ j (x)φi (x)dx
0 dx2 0
MATLAB code
% feexample.m
%
% solving L{y} = y’’ + y = 1 with y(0)=0 y(pi/2)=0
% using finite element method
% Exact solution is y(x) = -sin(x) - cos(x) + 1
%
clear all
global a b N
a=0; b=pi/2; N=10; % N interior points
h=(b-a)/(N+1);
xk=a+h:h:b-h; % interior points
% set up and solve Mc=RHS
M=(-2+2/3*hˆ2)*diag(ones(1,N)); % main diagonal elements
M=M+(1+hˆ2/6)*diag(ones(1,N-1),-1); % one below main diagonal
M=M+(1+hˆ2/6)*diag(ones(1,N-1),1); % one above main diagonal
M=M/h; % multiplicative factor
RHS=h*ones(N,1); % right hand side vector
c=M\RHS % solve for c the coefficients
% plot numerical solution and exact solution and calculate error
npts=26;
x=a:(b-a)/(npts-1):b; % vector to calculate solution at
% call feexamplef.m which is a function that calculates the solution
% at a given point i.e. it works out y(x)=sum(c(i)*phi_i(x))
% where the phi_i are the ’triangular hat’ functions
for k=1:npts
y(k)=feval(’feexamplef’,c,x(k));
end
plot(x,y)
hold on
exact=-sin(x)-cos(x)+1;
plot(x,exact,’ro’)
ylabel(’exact and numerical’)
error=abs(exact-y);
maxerror=max(error)
hold off
Index RELAXATION METHODS 99
MATLAB code
function f=feexamplef(c,x)
% feexamplef.m
% function that calculates y(x)=sum(c(i)*phi_i(x))
% where the phi_i are the ’triangular hat’ functions centered around x
%
global a b N
h=(b-a)/(N+1);
xk=a+h:h:b-h;
if (x <= xk(1)) % left most function
f=c(1)*(x-a)/h;
end
for i=1:N-1 % all interior functions that have left and right parts
if (x >= xk(i)) & (x <= xk(i+1))
right=c(i)*(xk(i+1)-x)/h;
left =c(i+1)*(x-xk(i))/h;
f=left+right;
end
end
if (x >= xk(N)) % right most function
f=c(N)*(b-x)/h;
end
return
−0.05
−0.1
−0.15
−0.2
−0.25
−0.3
−0.35
−0.4
−0.45
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6
100 5. BOUNDARY VALUE PROBLEMS Index
3. Let 1
h (x − xi−1 ) for xi−1 < x < xi
wi (x) = φi (x) = 1
h (xi+1 − x) for xi < x < xi+1
0 otherwise
n
4. Numerical solution Y (x) = ∑ ci φi (x)
i=1
yn = An sin ωn x
How would you solve this numerically given you don’t know the eigenvalues ω ?
Treat the eigenvalues as one of the unknowns of the problem. But how ?
We know that the eigenvalue is a constant so we can write a differential equation for it as
dω
=0
dx
but then we also need an extra boundary condition.
ω (0) = ω0
To use a shooting method you need 3 boundary conditions at the same point so introduce
a new boundary condition
y2 (0) = c
where c is a value that has to be determined to satisfy the boundary conditions at the other
end and ω0 is a value that also has to be determined.
But there is still not enough information to find the solution as there are only 2 known
boundary conditions (y1 (0) = 0 y1 (π ) = 0) so we must introduce another boundary con-
dition that is considered to be known. Looking at the analytic solutions that were found
earlier
yn = An sin ωn x
note that they are only defined to within some arbitrary constant (An ). To specify a specific
solution another condition would also have to be used. This condition is known as the
normalising condition and consists of choosing some given value for one of the unknown
values. So in fact we are free to choose the value of y2 (0) = c to be any (non zero) value
as this just fixes the unknown constant.
The system of equations is now
dy1
= y2
dx
dy2
= −(y3 )2 y1
dx
dy3
= 0
dx
subject to
So there are the required 3 known boundary conditions but 1 unknown one at x = 0 that
has to be determined to satisfy the boundary conditions at x = π .
So choose a value for ω0 integrate forward to x = π and check the value of y1 (π ) if it is
equal to zero it is the correct ω0 if it is not zero then change the ω0 guess. This can be
done using the MATLAB command fzero.
Index DIFFERENTIAL EIGENVALUE EQUATIONS 103
MATLAB code
function f=deig(omega0)
% deig.m
%
% solving y’’ + omegaˆ2 y = 0 y(0)=0 y(pi)=0
% This function takes as input the value of omega0 and returns
% the value at y(pi). If this equals zero then that value of
% omega0 gave a valid solution to the ODE.
% Note the system of equations becomes 3rd order!
% This function is called by fzero to find the value of omega0
% that gives the correct solution.
%
format compact
tspan=[0 pi];
y0=[0 1 omega0]; % y_1(0)=0, y_2(0)=1, y_3(0)=omega0
[t,y]=ode45(’deigf’,tspan,y0);
plot(t,y(:,1));
i=length(y); % determine length of solution vector
f=y(i,1); % set function value = y_1(pi)
return
MATLAB code
function f=deigf(t,y)
% deigf.m
f(1)=y(2);
f(2)=-y(3).ˆ2.*y(1);
f(3)=0;
f=f(:); % forces f to be a column vector
return
104 5. BOUNDARY VALUE PROBLEMS Index
>> omega=fzero(’deig’,1.4,0.0001)
omega = 1.0000
1.2
0.8
0.6
0.4
0.2
0
0 0.5 1 1.5 2 2.5 3 3.5
>> omega=fzero(’deig’,1.6,0.0001)
omega = 2.0000
0.5
0.4
0.3
0.2
0.1
−0.1
−0.2
−0.3
−0.4
−0.5
0 0.5 1 1.5 2 2.5 3 3.5
Index DIFFERENTIAL EIGENVALUE EQUATIONS 105
Example
Consider the differential equation
dz √
= 3 z − αt2 with z(0) = 2 z(1) = 4
dt
MATLAB code
% alpha.m
% get the correct value of alpha by calling fzero
% fzero in turn calls findalpha.m which is the function that
% integrates the DEs with a guess for alpha.
% The actual DEs are in alphaf.m
%
format compact
format long
initialguess=11
correctalpha=fzero(’findalpha’,initialguess)
% now use that value of alpha to integrate the
% equations and plot the solution
y0=[2 correctalpha]; % values at t=0
tspan=[0 1]; % integrate equation over t=(0,1)
[t,y]=ode45(’alphaf’,tspan,y0);
plot(t,y(:,1)) % plot y vs t
print -depsc alpha
MATLAB code
function f=findalpha(alphaguess)
% findalpha.f
% this function takes as input a guess for the constant
% alpha and returns y(1)-4
% if this is close to zero then its a good guess
y0=[2 alphaguess]; % values at t=0
tspan=[0 1]; % integrate equation over t=(0,1)
[t,y]=ode45(’alphaf’,tspan,y0);
lgthy=length(y); % determine how long the vector is
% f is a measure of how good the solution matches the desired
% boundary condition which is y(1)=4
% if f is close to zero then this is a good solution.
currentguess=alphaguess ;
f=y(lgthy,1)-4;
sprintf(’Current guess for alpha =%10.8f y(1)-4 = %10.8f’...
,currentguess,f)
return ;
MATLAB code
function f=alphaf(t,y)
% alphaf.m the governing DEs the alpha
% eigenvalue problem
%
% y(1) is z, y(2) is alpha
%
z=y(1); alpha=y(2);
% set up the functions
f(1)=3*sqrt(z)-alpha*t.ˆ2; % the DE
f(2)=0; % since alpha is a constant
f=f(:); % force f to be a column vector
return ;
Index DIFFERENTIAL EIGENVALUE EQUATIONS 107
alpha
initialguess =
11
ans =
Current guess for alpha =11.00000000 y(1)-4 = 0.15451914
ans =
Current guess for alpha =10.68887302 y(1)-4 = 0.27958057
ans =
Current guess for alpha =11.31112698 y(1)-4 = 0.02927189
ans =
Current guess for alpha =10.56000000 y(1)-4 = 0.33132966
ans =
Current guess for alpha =11.44000000 y(1)-4 = -0.02266307
ans =
Current guess for alpha =11.38366126 y(1)-4 = 0.00004514
ans =
Current guess for alpha =11.38377326 y(1)-4 = 0.00000001
ans =
Current guess for alpha =11.38377328 y(1)-4 = 0.00000000
ans =
Current guess for alpha =11.38377328 y(1)-4 = 0.00000000
correctalpha =
11.38377327554915
diary off
4.5
3.5
2.5
2
0 0.2 0.4 0.6 0.8 1
108 5. BOUNDARY VALUE PROBLEMS Index
6 Partial Differential Equations (PDEs)
6.1 Overview
Partial differential equations come from problems where there is variation in more than
one variable. For example, the temperature in a room will normally be a function of both
time and the position in the room — four different variables t, x, y and z. To describe rates
of change with respect to each of these variables we need partial derivatives.
∂ u ∂ 2u ∂f ∂f ∂c ∂c ∂c
2
= 2, = , + + = c. (6.1)
∂t ∂x ∂x ∂y ∂x ∂y ∂z
In the first example the dependent variable u is a function of the two independent variables
x and t. In the second example the dependent variable f is a function of x and y and in the
third example the dependent variable c is a function of x, y, and z.
110 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
The order of a PDE is the order of the highest derivative in the PDE, no matter which
variable. In the above examples, the order of the 1st PDE in equation (6.1) is 2, the order
of the 2nd and 3rd PDEs in equation (6.1) are both 1.
A PDE is said to be linear if it is a linear combination of the dependent variable and its
various partial derivatives. For example,
∂ u ∂ 2u ∂f ∂f ∂f ∂ 4c ∂ 2c
= 2, +x =z , + +c = 0 (6.2)
∂t ∂x ∂x ∂y ∂z ∂ x4 ∂ x2
are linear, whereas
∂u ∂u ∂f ∂ 4c
2
= , = ef , + c2 = 0 (6.3)
∂t ∂x ∂x ∂ x4
are not.
For analytic methods we will be concentrating on linear PDEs. In general nonlinear PDEs
are too difficult to solve analytically with notable exceptions. One reason for this is that
linear PDEs allow superposition. This means that if we have any two solutions of a PDE
then their sum is also a solution. This is a useful way of building up the entire solution of
a linear PDE analytically. The method of superposition (adding solutions) does not work
for nonlinear PDEs.
Numerical methods aren’t restricted to solving just linear problems. This is the entire
point of studying numerical solutions to these types of problems in that they can not be
solved by any other means. The analytic methods used for linear PDEs often provide a
useful check on the numerical methods in particular circumstances. This is important if
you want to have any confidence in your numerical solutions.
Broadly speaking there are three types of PDEs. The type of numerical solution method
is different for each type. What we will do is study an example of each type.
Index THREE COMMON PDES IN APPLICATIONS 111
Three of the most common PDEs in applications are the wave equation, the diffusion
equation and Laplace’s equation. These equations are defined as follows:
∂F ∂ 2F
1-D Diffusion equation: =D 2
∂t ∂x
∂ 2F ∂ 2F
2-D Laplace’s equation: + =0
∂ x2 ∂ y2
∂ 2F 2∂ F
2
1-D Wave equation: =c
∂ t2 ∂ x2
112 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
∂F ∂ 2F
=D 2 (6.4)
∂t ∂x
where D is a positive constant and is called the diffusion coefficient. This partial differ-
ential equation occurs in studies of heat conduction and diffusion of mass (eg. pollutants
or chemicals).
In heat conduction the dependent variable F(x,t) is the temperature inside a material,
where heat is conducted in only one direction. Solving this PDE gives the temperature at
any point and at any time.
The derivation of the diffusion equation when related to heat uses the principle of con-
servation of heat energy. The RHS of (6.4) physically comes from the net conduction
of heat through the material, whereas the LHS comes from the heat being used to raise
the temperature of the material. The diffusion equation when related to heat is sometmes
known as the heat equation.
The diffusion equation also arises in the study of mass transport by molecular diffusion.
For example, a drop of dye spreads out in a beaker of liquid or a pollution spreading out
in a lake. Here the dependent variable is the concentration.
It is also possible to derive the diffusion equation in two or three dimensions. For heat
conduction, we again use conservation of heat energy. In two and three dimensions, the
diffusion equations are
∂F ∂ F ∂ 2F ∂F ∂ F ∂ 2F ∂ 2F
2 2
=D + 2 , =D + 2 + 2 . (6.5)
∂t ∂ x2 ∂y ∂t ∂ x2 ∂y ∂z
respectively. We can also write these as
∂F
= D∇2 F.
∂t
Index LAPLACE’S EQUATION 113
∂ 2F ∂ 2F
+ 2 =0 (6.6)
∂ x2 ∂y
This equation arises in many different applications. One common application is in 2D or
3D heat conduction problems, where we assume the temperature is in thermal equilib-
rium. In thermal equilibrium, there is no time dependence on the temperature. Setting the
time derivative in (6.5) to zero, we thus obtain (6.6).
Laplace’s equation also arises in electrostatics (where the electrical potential, measured
in Volts, satisfies Laplace’s equation) and in fluid mechanics (where the velocity potential
satisfies Laplace’s equation). Laplace’s equation also arises in ground-water flow exam-
ples.
More generally, we write Lapalce’s equation as
∇2 F = 0.
114 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
∂ 2F 2∂ F
2
= c (6.7)
∂ t2 ∂ x2
where c is a constant. This partial differential equation describes small amplitude wave
propagation for a number of applications. Here the constant c is the speed of a wave in a
given medium.
The wave equation arises in sonar, electromagnetic waves, radar, and seismic prospecting
for example.
∂ 2F 2 ∂ F ∂ 2F ∂ 2F 2 ∂ F ∂ 2F ∂ 2F
2 2
=c + 2 , =c + 2 + 2 (6.8)
∂ t2 ∂ x2 ∂y ∂ t2 ∂ x2 ∂y ∂z
respectively. More generally, we write
∂ 2F
= c2 ∇2 F.
∂ t2
Index POISSON’S EQUATION 115
∇2 F = g(x, y, z).
So far all the examples given have been in Cartesian co-ordinates (x, y, z). These equations
can also be considered in other co-ordinate systems such as cylindrical (r, θ , z) or spherical
(ρ , φ , θ ).
For example Laplace’s equation in cylindrical co-ordinates is
∂ 2F 1 ∂ F 1 ∂ 2F ∂ 2F
∇ F= 2 +
2
+ +
∂r r ∂ r r 2 ∂ θ 2 ∂ z2
∂ 2F 2 ∂ F 1 ∂ 2F 1 ∂ 2 F cot θ ∂ F
∇ F=
2
+ + + + 2
∂ ρ 2 ρ ∂ ρ ρ 2 sin2 θ ∂ φ 2 ρ 2 ∂ θ 2 ρ ∂θ
Sometimes this can make the solution process (both analytic and numerical) easier. Can
you think of an example?
116 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
The number of initial and boundary conditions needed is determined by the order of the
respective derivatives in the PDE. The diffusion equation (6.4) requires one initial condi-
tion since its time derivative is of order one. It requires two boundary conditions, since the
x-derivative is second-order. For the wave equation (6.7) we need to specify two initial
conditions (since there is a second-order derivative with respect to t) and two boundary
conditions. Laplace’s equation (6.6) requires four boundary conditions, two for the x-
variable and two for the y-variable.
∂F
F(0,t) = 30, (10,t) = 100.
∂x
The first condition specifies the value of F, at the point x = 0, and the second specifies the
value of the derivative of f at the point x = 10, for all time.
Index TYPES OF BOUNDARY CONDITIONS 117
There are 3 types of Boundary Conditions. The type of boundary condition has an impact
on which numerical solution method you can use for a problem hence it is important to
be able to classify the boundary conditions.
Dirichlet
The values of the function are given at the boundary.
For example F(0,t) = 30
Neumann
The value of the normal derivative is given at the boundary.
For example ∂∂Fx (10,t) = 100 which can also be written ∂∂Fx = 100 at x = 10
118 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
Cauchy
Values of both the function and its normal derivative are given at the boundary.
For example ∂∂Fx = F at x = 10
Classify (with reason) each boundary condition of the following PDEs as Dirichlet, Neu-
mann or Cauchy.
d2T 2
(a) dy2
+ ddxT2 = 4 with T (0, y) = 0 T (x, 0) = 2 T (L, y) = sin y dT
dy (x, M) = 0
dT d2 T dT
(b) dt = dx2
with T (0,t) = 0 dx (L,t) = 0 T (x, 0) = f (x)
dT d2T dT
(c) dt = dx2
with T (0,t) = sint dx (L,t) = T − Ta T (x, 0) = f (x)
Index DIFFERENT SOLUTION APPROACHES 119
Analytic methods mainly involve reducing a PDE to one or more ordinary differential
equations. The approach we will explore in this course is separation of variables. This
involves assuming the dependent variable as a product of functions of each independent
variable.
We only have time in this course to learn the above method for solving PDEs. But you
should at least be aware that other techniques exist. One technique is to use Laplace trans-
forms. The Laplace transform of an ordinary differential equation reduces the differential
equation to an algebraic equation. Applying Laplace transforms to a PDE (which is a
function of two variables) reduces the PDE to an ODE which is a function of one vari-
able. One then solves the ODE and then inverts the Laplace transform. Another method
for solving PDEs involves finding transformations of variables. These are called similarity
solutions.
Most practicing engineers or mathematicians would use numerical techniques for solving
PDEs. These are often customised to a particular class of applications. The two most
common numerical techniques for solving PDEs are finite differences and finite elements.
In finite differences, the partial derivatives are approximated with derivative quotients,
evaluated on a rectangular grid. In finite elements, the region is divided up into small
panels, or elements, and the quantity you are solving for is expressed as a basis of simple
functions defines over just the finite element. Both finite difference and finite element
methods lead to matrix equations where the matrices can be large sparse matrices.
While numerical techniques are often used for practical problems, knowledge of the basic
analytical solutions, such as those obtained in this course, can provide valuable incite
into the problems and may also provide a simple check on the accuracy of the numerical
program in particular circumstances. It is always a wise idea to check numerical methods
on a solution with a known analytic solution to try and guarantee that the method and the
coding is correct.
120 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
6.13 Examples
Heat in a rod
Consider an insulated rod of steel of length L were one end is held at 100◦ C and the
other can lose heat according to Newton’s Law of Cooling (heat loss is proportional to the
difference in the temperature of the rod and the surrounding ambient air). The ambient
air is at 25◦ C.
Write down the governing equation, the initial and boundary conditions.
Index EXAMPLES 121
Plucked String
Consider a string of length L that is held fixed at both ends. The string is pulled out to
a triangular shape and then released. Write down the governing equation, boundary and
initial conditions.
Index EXAMPLES 123
Drum
Consider a drum of radius 10cm. If the drum is beaten so that the velocity of the surface
is initially f (r) (r is the radius) Write down the governing equation, boundary conditions
and initial conditions.
124 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index
The numerical solution of the 3 standard types of linear PDEs (Diffusion, Laplaces and
Wave equations) differs for each type of equation. In fact these PDEs are examples of
more general classes of PDEs known as Parabolic, Elliptic and Hyperbolic PDEs. The
numerical methods for solving PDEs differ depending on which type of PDE you are
solving.
Consider a general PDE that depends on 2 variables (a and b).
∂ 2u ∂ 2u ∂ 2u ∂u ∂u
A + 2B +C = F(a, b, u, , )
∂a 2 ∂ a∂ b ∂b 2 ∂a ∂b
where A, B and C are constants and F(a, b, u, ∂∂ ua , ∂∂ ub ) is an arbitrary function of those
variables.
The classification of a PDE (and hence the type of numerical method used to solve it)
depends on the the quantity B2 − AC.
B2 − AC = 0 Parabolic eg Diffusion equation
B2 − AC < 0 Elliptic eg Laplaces equation
B2 − AC > 0 Hyperbolic eg Wave equation
Finite differences can be used to solve PDEs. Each derivative is approximated by a finite
difference and then the equation is rearranged to enable a solution to be determined.
Both the forward and backward approximations are first order accurate, that is the first
neglected term is order ∆x.
First derivative Central Approximation given by
dy yn+1 − yn−1
≈
dx n 2∆x
d 2 y
yn+1 − 2yn + yn−1
2
≈
dx n (∆x)2
126 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
∂F ∂ 2F
=D 2 (7.1)
∂t ∂x
with boundary conditions
We require the solution in the region a ≤ x ≤ b and over the time span 0 ≤ t ≤ T
To use finite differences we need to divide the region up into discrete points where we
calculate the solution. This is known as discretising the region.
Take Nx points in the x direction so that the spacing between points is then given by
b−a
∆x =
Nx
and let the variable i be the index that refers to the discrete x points. That is
xi = a + i∆x i = 0, 1, . . ., Nx
Do a similar process for the t direction. Take Nt points in the t direction so that the spacing
between time steps is then given by
T −0
∆t =
Nt
and let the variable j be the index that refers to the discrete time steps. That is
t j = j∆t j = 0, 1, . . ., Nt
This has now divided the (x,t) plane into a grid. The aim is to calculate the solution at
each of the grid points.
Index FTCS METHOD 127
t
j = 10 t
u bc bc bc bc bc bc bc t
u
j=9 t
u bc bc bc bc bc bc bc t
u
j=8 t
u bc bc bc bc bc bc bc t
u
j=7 t
u bc bc bc bc bc bc bc t
u
j=6 t
u bc bc bc bc bc bc bc t
u
j=5 t
u bc bc bc bc bc bc bc t
u
j=4 t
u bc bc bc bc bc bc bc t
u
∆x ∆t
j=3 t
u bc bc bc bc bc bc bc t
u
j=2 t
u bc bc bc bc bc bc bc t
u
j=1 t
u bc bc bc bc bc bc bc t
u
t =0 rs rs rs rs rs rs rs rs rs
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 x
x=a x=b
j
Let the solution at the point (xi ,t j ) be denoted by Fi , that is
∂ F ∂ 2F
= 2
∂t ∂x
around the point (xi ,t j ) by using a forward finite difference approximation for the time
derivative
j+1 j
∂ F j Fi − Fi
≈
∂ t i ∆t
and a centred finite difference approximation for the spatial second derivative
j j j j
∂ 2 F Fi+1 − 2Fi + Fi−1
≈
∂ x2 i (∆x)2
j+1
now make Fi the subject
j j j
!
j+1 F − 2Fi + Fi−1 j
Fi = ∆tD i+1 + Fi
(∆x)2
= γ Fi+1 + (1 − 2γ )Fi + Fi−1
j+1 j j j
Fi (7.2)
where
D∆t
γ=
(∆x)2
Equation (7.2) is known as the Forward Time Centered Space (FTCS) approximation to
the diffusion equation.
The FTCS equation enables solutions at time level j +1 to be calculated from information
at time level j.
Given that we know the initial condition (level j = 0) then we are able to use this to
calculate the solution at time level j = 1. This is then used to get the solution at level
j = 2 and so on.
At a given time level you use the FTCS equation to sweep across in the i direction calcu-
j+1
lating the value of Fi .
The FTCS method is an explicit method as at each time step there is an explicit equation
for the values at the next time step.
Index FTCS METHOD 129
t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
j+1 u
t bc bc bc bc bc bc bc t
u
j t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
t
u bc bc bc bc bc bc bc t
u
t =0 rs rs rs rs rs rs rs rs rs
i−1 i i+1
x=a x=b
Solve the heat diffusion problem of a steel bar of length 2m with one end held at 0◦ C the
other end at 0◦ C and initially the bar is at at 100◦ C.
∂T ∂ 2T
=D 2
∂t ∂x
where for steel D = 5 × 10−6 m2 /s
The boundary and initial conditions can be written
Find the temperature distribution at times t = 60, 120, 180, . . .600 minutes
The exact solution can be found analytically using the method of separation of variables
to be
∞
−Dm2 π 2t mπ x
T (x,t) = ∑ am exp sin
m=1 4 2
where
−200 ((−1)m − 1)
am =
mπ
80
70
Temperature
60
50
40
30
20
10
0
0 0.5 1 1.5 2
x
Index FTCS METHOD 131
MATLAB code
% ftcsdiffusion.m
% Solve heat diffusion in a steel bar of length 2
% with ends at 0oC and initially the bar is 100oC.
% Use FTCS finite difference method
% dT/dt = D d2T/dx2
% subject to boundary conditions T(0,t)=0 T(2,t)=0
% and initial condition T(x,0)=100
%
clear all;
a=0; b=2; % x boundaries
Nx=10; % number of x points to use
dx=(b-a)/Nx; % x step
x=[a:dx:b]; % x points
fa=0; fb=0; % boundary values
D=5e-6; % steel bar diffusivity = 5.0x10ˆ-6 mˆ2/s
dt=3600 % time step in seconds (=60 minutes)
tfinal=36000; % final time in seconds (=600 minutes)
gamma=D*dt/(dxˆ2)
T(1)=fa; T(Nx+1)=fb;% boundary conditions, note that index for
% i starts at 1 not 0 as matlab doesnt
% allow zero indexing
T=100*ones(1,Nx+1); % initial temperature in bar is 100
hold off
132 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
7.2.4 Stability
The stability of a time stepping numerical method is all to do with whether errors grow or
decay as you step in time.
Consider the forward time approximation to the time derivative at the point (xi ,t j )
j+1 j
∂ F j Fi − Ti
≈
∂ t i ∆t
This has an error of order (∆t)2 so at each time step there is an error introduced.
The question is do these errors build up and swamp the solution or do they get damped
out?
It turns out that for an FTCS (Forward Time Centred Space) approximation to the diffu-
sion equation as given by
where
D∆t
γ=
(∆x)2
that if
1
γ< then the method is stable and errors do not grow
2
1
γ> then the method is unstable and errors grow
2
If γ > 12 then the method is totally useless! So it is essential that you keep track of the
size of γ and ensure that it stays below 12 at all times. This is particularly important if the
diffusion coefficient varies in the problem.
How does this effect the usefulness of the FTCS method?
If you have a metal bar with diffusion constant of D = 5 × 10−6 m2 /s of length 2m and
you want 100 points along the bar to get an accurate representation of the temperature in
the bar what value of the time step do you need to use?
Now if you double the number of space points how is the time step effected?
Index FTCS METHOD 133
150
100
Temperature
50
−50
−100
−150
0 0.5 1 1.5 2
x
Disaster !
In contrast to the FTCS method the Crank-Nicolson method uses an average of the old
and new time step for the spatial derivative.
j+1 j!
∂ F j 1 ∂ 2 F ∂ 2 F
=D +
∂ t i 2 ∂ x2 i ∂ x2 i
Which rearranges to
−γ j+1 γ j
Fi+1 + Fi−1 + (1 + γ )Fi Fi+1 + Fi−1 + (1 − γ )Fi
j+1 j+1 j j
=
2 2
where
D∆t
γ=
(∆x)2
The left hand side has the unknowns at time j + 1 the right hand side the known values
at time j. These are for each value of i as you step across the region. Hence it is really n
equations in n unknowns (recall the finite-difference work for ODEs).
This is known as an implicit method as there is now no longer an explicit equation for the
solution at time j + 1 in terms of time level j values but rather a system of equations to
solve.
7.3.1 Template
bc bc bc bc bc
j+1 bc bc bc bc bc
j bc bc bc bc bc
bc bc bc bc bc
i−1 i i+1
Index CRANK-NICOLSON METHOD 135
The boundary conditions at i = 0 and i = Nx are known so the equations are slightly
different for i = 1 and i = Nx − 1 to account for this.
The system of equations can be written in matrix form (recall the finite difference work
on ODEs) as
AF j+1 = BF j + d j
where
1 + γ −γ /2
0 ... 0 0
−γ /2 1 + γ −γ /2 ... 0 0
A=
.. .. .. .. .. ..
. . . . . .
. . . 1 + γ −γ /2
0 0 0
0 0 0 . . . −γ /2 1 + γ
1 − γ γ /2
0 ... 0 0
γ /2 1 − γ γ /2 ... 0 0
B=
.. .. .. .. .. ..
. . . . . .
. . . 1 − γ γ /2
0 0 0
0 0 0 . . . γ /2 1 − γ
j
F1j+1
j j+1 F1
Fa + Fa j
F2j+1
0
F2
0 0
γ
0 j j+1
F = =
F
dj =
..
.. ..
2 .
.
.
0 0
0
j j+1
j
Fb + Fb
j+1 FNx −1 FNx −1
This system of equations can be easily solved in MATLAB using the ’backslash divide’
command. For example something like
Fnext=A\(B*F+d)
One of the advantages of the implicit scheme such as Crank-Nicolson is that it is stable
irespective of the value of γ . That is errors do not build up to catastrophic proportions even
for large time steps. Hence large time steps can be taken only limited by the accuracy at
each step.
This is the major advantage of the implicit Crank-Nicolson scheme over the explicit FTCS
scheme. This stability comes at a price, namely that a matrix equation must be solved at
each time step. This is not too bad in this case as the matrix A is tri-diagonal and hence
there are fast and efficient methods available to invert it. Also since in this case A does
not change from one tme step to the next then it is even possible to determine the inverse
of A once and store it for future use thereby speeding up the calculations even more.
As a general rule explicit schemes suffer from stability problems and implicit schemes
are usually stable.
136 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
7.3.3 Example
Solve the heat diffusion problem of a steel bar of length 2m with one end held at 100◦C
the other end at 0◦ C and initially the bar is at at 0◦ C.
∂T ∂ 2T
=D 2
∂t ∂x
where for steel D = 5 × 10−6 m2 /s
The boundary and initial conditions can be written
90
80
70
Temperature
60
50
40
30
20
10
0
0 0.5 1 1.5 2
x
Index CRANK-NICOLSON METHOD 137
MATLAB code
% cranknic.m
% Solve the heat diffusion equation on a steel bar of length 2
% with one end held at 100oC the other at 0oC and initially
% the bar is 0oC. Use Crank-Nicolson finite difference method
%
% dT/dt = D d2T/dx2 boundary conditions T(0,t)=100 T(L,t)=0
% and initial condition T(x,0)=0
%
clear all;
n=40; L=2; dx=L/n;
x=[0:dx:L]; % x points
fa=100; fb=0; % boundary values
D=5e-6; % diffusivity = 5x10ˆ-6 mˆ2/s
dt=144 % time step in seconds (plot every 100th)
tfinal=144000; % final time in seconds (=4 hours)
gamma=D*dt/(dxˆ2)
%
T=zeros(n-1,1); % initial condition
Tplot=[fa;T;fb]; % add in the boundary condition for plot
plot(x,Tplot); xlabel(’x’); ylabel(’Temperature’);
title(’ Temperature in bar at steps of 4 hours’)
hold on
%
% set up left and right hand tri-diagonal matrices
Aleft=diag((1+gamma)*ones(n-1,1))...
+diag((-gamma/2)*ones(n-2,1),1)...
+diag((-gamma/2)*ones(n-2,1),-1);
Aright=diag((1-gamma)*ones(n-1,1))...
+diag((gamma/2)*ones(n-2,1),1)...
+diag((gamma/2)*ones(n-2,1),-1);
%
% loop through time calculating T at each new step and plot
% each 100th calculation (see the mod function for how to do this)
for t=dt:dt:tfinal % take time steps of dt
d=gamma*[fa;zeros(n-3,1);fb];% adjust for known boundary values
T=Aleft\(Aright*T+d); % solve for T using backslash divide
if (mod(t,dt*100)==0) % plot each 100th step
Tplot=[fa;T;fb]; % add boundary values for plotting
plot(x,Tplot);
end
end
print -depsc cranknic
hold off
138 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
The method of lines is a way of numerically solving PDEs using the efficient techniques
(eg Runge-Kutta) developed to solve ODEs.
Consider the 1D diffusion equation
∂T ∂ 2T
=D 2
∂t ∂x
with Dirichlet boundary conditions at the boundaries x = a and x = b and initial condition
given by
T (a,t) = fa (t) T (b,t) = fb (t) T (x, 0) = f0 (x)
discretise this in the x-direction using the variable i to indicate which x point you are
at. That is, divide the x-direction into N subintervals with i = 0, 1, 2, . . ., N. Use centred
space approximation to the second derivative at each of those points but leave the time
derivative as it is. So we get
∂ Ti Ti−1 − 2Ti + Ti+1
≈D
∂t (∆x)2
T0 = fa (t) TN = fb (t)
... = ...
dTN−2
= β (TN−3 − 2TN−2 + TN−1 )
dt
dTN−1
= β (TN−2 − 2TN−1 + TN ) = β (TN−2 − 2TN−1 + fb )
dt
where β = D/(∆x)2 .
With the initial condition giving us the initial values for each of T1 , T2 , T3 , . . ., TN−1 .
This system of ODEs is easily solved numerically using any of the ODE solvers men-
tioned previously (eg Euler’s, midpoint, Runge-Kutta, ode45,. . . ). The more points used
across the domain (larger the value of N) the more accurate the solution will be and the
longer it will take to run.
Index METHOD OF LINES 139
MATLAB code
% mol.m
%
% method of lines on diffusion equation
%
% bar length 2m, with T(0,t)=100, T(2,t)=50
% initially quadratic temperature profile T(x,0)=100-25x(x-1)
% diffusion coefficient of D=1x10ˆ(-6) m2/s
% integrated from t=0 to 1,000,000sec in steps of 100,000sec
global n Ta Tb beta
MATLAB code
function f=molf(t,y);
%
% molf.m
% the system of equations
global n Ta Tb beta
f(1)=beta*(Ta-2*y(1)+y(2));
for i=2:n-2
f(i)=beta*(y(i-1)-2*y(i)+y(i+1));
end;
f(n-1)=beta*(y(n-2)-2*y(n-1)+Tb);
f=f(:);
return
∂T ∂ 2T
=D 2
∂t ∂x
with
T (0,t) = 100 T (2,t) = 50 T (x, 0) = 100 − 25x(x − 1)
100
90
Temperature
80
70
60
50
0 0.5 1 1.5 2
x
Index OTHER BOUNDARY CONDITIONS 141
So far all the examples have used Dirichlet Boundary Conditions (the value of the function
given on the boundary). What changes if instead the boundary conditions are Neumann or
Cauchy type? Now the value of the function is not known and hence the boundary should
also be considered an unknown point.
Example
Left edge has a Dirichlet (u = 0) boundary condition and hence the value is known. Right
edge has a Cauchy ( ∂∂ ur = h(25 − u)) boundary condition and hence the value is unknown.
t
u bc bc bc bc bc bc bc bc
∂u
u=0 u t bc bc bc bc bc bc bc bc ∂ r = h(25 − u)
boundary boundary
value t
u bc bc bc bc bc bc bc bcvalue
known unknown
t
u bc bc bc bc bc bc bc bc
rs rs rs rs rs rs rs rs rs
Example
The different types of boundary conditions can all be derived from Newton’s Law of
cooling. For example consider a steel bar where the end at x = 0 loses heat according to
Newton’s Law of cooling with a heat transfer coefficient of h, and an ambient temperature
of Ta , that is at x = 0
∂T
= h(T − Ta )
∂x
If h = 0 then the boundary is fully insulated as no heat can flow through that boundary
∂T
= 0 insulated
∂x
If h is very large then divide each side by h to give approximately
T = Ta Dirichlet condition
The function value is not known at the boundary so the boundary is an unknown value that
must also be determined. After discretizing in the x-direction use centred differencing on
the derivative term, for example centred differencing about i = 0 gives
∂ T
T1 − T−1
≈
∂ x i=0
2∆x
where T−1 is a fictitious point. That is it is an artificial point that is not in the domain of
interest. So how does this help?
∂T
Consider the case of an insulated boundary ∂x = 0.
∂T
Using the above expression and the fact that the boundary is insulated so that ∂x = 0 we
get a relationship between T−1 and T1 in this case this is simply that
T−1 = T1
∂ 2 T
T−1 − 2T0 + T1
≈
∂ x i=0
2 (∆x)2
the fictitious point T−1 can then be replaced by T1 to give
∂ 2 T
2T1 − 2T0
≈
∂ x2 i=0 (∆x)2
Hence it is possible to incorporate the boundary condition into the scheme that is used
with the value of the temperature at the boundary being one of the unknowns.
For example in the Method of Lines the system of equations is modified to include an
extra equation for the T0 point and the T1 equation is also altered to still include the T0
which is unknown, the other equations remain unchanged.
dT0
= β (T−1 − 2T0 + T1 ) = β (2T1 − 2T0 )
dt
dT1
= β (T0 − 2T1 + T2 )
dt
dT2
= β (T1 − 2T2 + T3 )
dt
... = ...
dTN−2
= β (TN−3 − 2TN−2 + TN−1 )
dt
dTN−1
= β (TN−2 − 2TN−1 + TN ) = β (TN−2 − 2TN−1 + fb )
dt
Index OTHER BOUNDARY CONDITIONS 143
Example
Heat diffusion in a steel bar with D = 1 × 10−6 m2 /s
∂T ∂ 2T
=D 2
∂t ∂x
with
∂T
(0,t) = 0 T (2,t) = 50 T (x, 0) = 100 − 25x(x − 1)
∂x
MATLAB code
% molinsulated.m
%
% method of lines on diffusion equation
%
% bar length 2m, with dT/dx(0,t)=0, T(2,t)=50
% initially a quadratic temperature profile T(x,0)=100-25x(x-1)
% diffusion coefficient of D=1x10ˆ(-6) m2/s
% integrated from t=0 to 1,000,000 sec in steps of 100,000 sec
global n Tb beta
MATLAB code
function f=molinsulatedf(t,y);
%
% molinsulatedf.m
% the system of equations
global n Tb beta
100
90
Temperature
80
70
60
50
0 0.5 1 1.5 2
x
Index OTHER BOUNDARY CONDITIONS 145
∂T
= h(T − Ta )
∂x
the derivative of the function is given in terms of the unknown value at the boundary that
must also be determined. After discretizing in the x-direction use centered differencing
on the derivative term, for example centred differencing about i = 0 gives
∂ T
T1 − T−1
≈ = h(T0 − Ta )
∂ x i=0
2∆x
T−1 = T1 − 2∆xh(T0 − Ta )
Now use this to expression to incorporate the Cauchy boundary condition into the expres-
sion for the second derivative at the point i = 0.
146 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
Not that this equation can not be solved analytically! Use FTCS finite differences to get
Ti, j+1 = γ (Ti+1, j + Ti−1, j ) + (1 − 2γ )Ti, j + ∆t(Ti, j − Ta )2
80
70
Temperature
60
50
40
30
20
0 0.5 1 1.5 2
x
Index NONLINEAR EXAMPLE 147
MATLAB code
% reactdiff.m
% Solve the heat reaction-diffusion equation on a length 2 with
% both ends at ambient=25oC and initially the bar is ambient.
% A hot spot of T=50oC at the centre (x=1)
% Use FTCS finite difference method
%
% dT/dt = D d2T/dx2 + (T-Ta)ˆ2
% subject to boundary conditions T(0,t)=Ta T(L,t)=Ta
% and initial condition T(x,0)=Ta (for x=/=1) and T(1,0)=50
%
clear all;
n=100; L=2; dx=L/n;
x=[0:dx:L]; % x points
D=0.08; % diffusivity of wood = 0.08 mˆ2/s
dt=.001; % time step in seconds
tfinal=1.48; % final time in seconds
gamma=D*dt/(dxˆ2)
Ta=25; % ambient Temperature
T=Ta*ones(1,n+1); % ambient Temperature everywhere except hot spot
T(n/2+1)=Ta+25; % hot spot at centre
% plot initial temperature distribution
plot(x,T); xlabel(’x’); ylabel(’Temperature’);
title(’ Temperature at steps of 0.1 sec: Final Temperature 1.5 sec’)
hold on
for t=dt:dt:tfinal % take time steps of dt
%step across the bar calculating the Temperature at next time level
for i=2:n
Tnext(i)=gamma*(T(i-1)+T(i+1))+(1-2*gamma)*T(i)+dt*(T(i)-Ta)ˆ2;
end
Tnext(1)=Ta; Tnext(n+1)=Ta;
T=Tnext; % update the temperature
if (mod(t,dt*100)==0) % plot each 100th step
plot(x,T);
end
end
plot(x,T);
print -depsc reactdiff
hold off
148 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
So far all the examples given have been in Cartesian co-ordinates (x, y, z). These equations
can also be considered in other co-ordinate systems such as cylindrical (r, θ , z) or spherical
(ρ , φ , θ ).
For example the Laplacian in cylindrical co-ordinates is
∂ 2F 1 ∂ F 1 ∂ 2F ∂ 2F
∇2 F = + + +
∂ r 2 r ∂ r r 2 ∂ θ 2 ∂ z2
The benefit of using a coordinate system that matches the object is that often it is possible
to reduce the dimension of the problem.
Consider the case of a circular disc of metal with a radius of 50cm. The base and top are
insulated which means the only directions of interest are in the plane of the disc ((x, y)
plane) and hence the problem is two dimensional. The disc is initially at 100◦C and
loses heat through the edge according to Newton’s law of cooling. The ambient air is at
25◦C. The diffusivity of the metal is D = 5 × 10−6m2 /s and the Newtonian heat transfer
coeeficient is h = 10m−1 .
∂u
= −h(u − ua ) at r = 0.5
∂r
∂u
= D∇2 u
∂t
which can be written in circular coordinates as
∂u ∂ F 1 ∂ F 1 ∂ 2F
2
=D + +
∂t ∂ r2 r ∂ r r2 ∂ θ 2
Now since the boundary condition is applied uniformly over the boundary and the initial
condition is also uniform then the temperature in the disc will not depend on the angle
Index OTHER CO-ORDINATE SYSTEMS 149
around the disc. Hence there will not be any θ dependence and so any derivatives with
respect to θ are zero. So the governing PDE becomes
∂u ∂ u 1 ∂u
2
=D +
∂t ∂ r2 r ∂ r
This has reduced the problem form a 2-D one down to a 1-D one.
u(r, 0) = 100
and there should be two boundary conditions since the PDE is second order in the space
variable r. One boundary condition is obvious, namely
∂u
= −h(u − ua ) at r = 0.5
∂r
The other boundary condition is derived from the fact that the temperature is independent
of θ and hence at the origin the derivative with respect to r must be zero. That is the
temperature distribution must be centred on the origin. So the other boundary condition
is
∂u
= 0 at r = 0
∂r
L’Hopitals rule is a consequence of Taylor series and states in this particular situation that
f (r) f ′ (r)
if lim f (r) = 0 and lim g(r) = 0 then lim = lim ′
r→0 r→0 r→0 g(r) r→0 g (r)
∂u
hence in this case f (r) = ∂r and g(r) = r so that at r = 0 the governing PDE is modified
by
∂ 2 u ∂∂ ur
!
∂u
= D +
∂t ∂ r2 r
∂ 2u
!
∂ 2 u ∂ r2
= D +
∂ r2 1
∂ 2u
= 2D
∂ r2
So in any numerical method this alternative PDE must be used if at the point r = 0.
150 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
Now derive the FTCS system for solving this problem. The only space dimension of
interest is the radius r = 0 to 0.5. Set up a grid in space and time. Discretise the region so
j
that ui = u(i∆r, j∆t) with i = 0, 1, . . ., Nr and j = 0, 1, . . ., Nt with ∆r = 0.5/Nr
t
j = 10 bc bc bc bc bc bc bc bc bc
j=9 bc bc bc bc bc bc bc bc bc
j=8 bc bc bc bc bc bc bc bc bc
j=7 bc bc bc bc bc bc bc bc bc
j=6 bc bc bc bc bc bc bc bc bc
∂u ∂u
∂r =0 ∂r = h(25 − u)
j=5 bc bc bc bc bc bc bc bc bc
j=4 bc bc bc bc bc bc bc bc bc
∆r ∆t
j=3 bc bc bc bc bc bc bc bc bc
j=2 bc bc bc bc bc bc bc bc bc
j=1 bc bc bc bc bc bc bc bc bc
t =0 rs rs rs rs rs rs rs rs rs
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 r
r=0 r = 0.5
FTCS formulation
Provided r 6= 0 the governing PDE is
∂u ∂ u 1 ∂u
2
=D +
∂t ∂ r2 r ∂ r
so use Forward Time and Centred Space finite difference around the point (i, j) to approx-
imate this by
j j j j j
!
j+1 j
ui − ui ui+1 − 2ui + ui−1 1 ui+1 − ui−1
=D +
∆t (∆r)2 i∆r 2∆r
Rearranging this to get the new time level as the subject results in
γ
ui = γ ui+1 − 2ui + ui−1 +
j+1 j j j j j j
ui+1 − ui−1 + ui
2i
where γ = (∆r)
D∆t
2 . This is the FTCS formulation for the internal points. Points at the two
j j
∂ u j uNr +1 − uNr −1
j
≈ = −h(uNr − ua )
∂ r Nr 2∆r
j j j
uNr +1 = −2∆rh(uNr − ua ) + uNr −1
j
This value is then used in the FTCS scheme above whenever uNr +1 is used.
152 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index
Internal edge r = 0
Do a similar process for the internal edge but recall that the PDE is altered there. The
boundary condition is ∂∂ ur = 0 which on using central finite differences is
j j
∂ u j u1 − u−1
≈ =0
∂ r 0 2∆r
j
which rearranges so that the fictitious point u−1 can be written as
j j
u−1 = u1
∂u ∂ 2u
= 2D 2
∂t ∂r
so use Forward Time and Centred Space finite difference around the point (0, j) to ap-
proximate this by
j j j
j+1 j
!
u0 − u0 u1 − 2u0 + u−1
= 2D
∆t (∆r)2
j
Using the value for the fictitious point (u−1 ) above and rearranging gives the governing
FTCS equation for the r = 0 point as
u0 = 4γ u1 − u0 + u0
j+1 j j j
MATLAB code
% ftcs_cyl.m
% Solve the heat diffusion equation on a steel disc radius 0.5m
% with Newton’s law of cooling dT/dr=-h(T-Ta) Ta=25oC at r=0.5m
% initially the disc is 100oC. Use FTCS finite difference method
% dT/dt = D d2T/dr2 + D 1/r dT/dr
% initial condition T(x,0)=100
% boundary conditions dT/dr=0 at r=0
% dT/dr=-h(T-25) at r=0.5m
%
clear all;
n=20; % number of points
L=0.5; % radius of disc
dr=L/n; % space step
Ta=25; % ambient temperature
r=[0:dr:L]; % r points
D=5e-6; % steel diffusivity = 5x10ˆ6 mˆ2/s
h=10.0; % heat loss coefficient
dt=30 % time step in seconds
tfinal=30000; % final time in seconds
gamma=D*dt/(drˆ2)
% calculate and plot initial temperature distribution
T=ones(1,n+1)*100;
plot(r,T,’r-’); xlabel(’radius’); ylabel(’Temperature’);
title(’ Temperature in disc at 900 second intervals’)
hold on
% internal r points
for i=2:n
Tnext(i)=gamma*(T(i-1)-2*T(i)+T(i+1))...
+gamma/(2*(i-1))*(T(i+1)-T(i-1))+T(i);
end
90
80
70
Temperature
60
50
40
30
20
0 0.1 0.2 0.3 0.4 0.5
radius
8 Elliptic Equations: Laplace’s Equation
Elliptic equations (such as Laplace’s Equation) do not allow ’time-stepping’ since there is
no ’time-like’ variable to step in. Instead the solution must somehow be found in the entire
demand simultaneously. The best method for this is to use some kind of iterative scheme
where you use the equation to update an initial guess of the solution until it converges to
a steady solution. This is a sort of pseudo ’time-stepping’ but it is not necessary to keep
the intermediate ’times’ hence some short cuts and speed ups can be done.
Consider a steady state heat problem on a rectangular domain with Dirichlet Boundary
conditions. For example a slab of wood of size 10cm (wide) by 5cm (long) that has
reached thermal equilibrium. The left and right edges are held at 20◦ C the bottom at
20◦ C and the top is subjected to to a quadratic temperature such that the maximum in the
middle is 70◦ C. What is the steady state temperature distribution in the slab?
y
(2x(10 − x) + 20)◦C
5
20◦C 20◦C
20◦C 10 x
The governing PDE is Laplace’s Equation in 2D
∂ 2T ∂ 2T
+ 2 =0
∂ x2 ∂y
subject to the boundary conditions
Divide the region up into a grid so that there Nx sections in the x-direction and Ny sections
in the y-direction. Let i be the index for the x-direction and j the index for the y-direction.
So in this case
(10 − 0) (5 − 0)
∆x = ∆y =
Nx Ny
with i = 0, 1, . . ., Nx and j = 0, 1, . . ., Ny . Let Ti, j be shorthand notation for the temperature
at the position (xi , y j ) where xi = 0 + i∆x and y j = 0 + j∆y.
Turning to the governing PDE discretise the second derivative terms using centred differ-
encing in the appropriate direction to get
Ti+1, j − 2Ti, j + Ti−1, j Ti, j+1 − 2Ti, j + Ti, j−1
+ ≈0
(∆x)2 (∆y)2
That is the template used is a cross pattern
bc bc bc bc bc
j+1 bc bc cb bc bc
j bc bc cb cb bc
j−1 bc bc cb bc bc
bc bc bc bc bc
i−1 i i+1
and so we get an equation for the central point in terms of its 4 neighbours as
1/2 2
2
Ti, j = (∆y) Ti+1, j + Ti−1, j + (∆x) Ti, j+1 + Ti, j−1
(∆y)2 + (∆x)2
MATLAB code
% laplace_direct.m
% Solve Laplaces equation d2u/dx2 + d2u/dy2 = 0
% subject to u(0,y)=20 u(x,0)=20 u(10,y)=20 u(x,5)=2x(10-x)+20
%
L=10; M=5; Nx=30; Ny=30; dx=L/Nx; dy=M/Ny; eps=1.0; delta=100;
dx2=dxˆ2; dy2=dyˆ2; x=0:dx:L; y=0:dy:M;
V=20*ones(Nx+1,Ny+1); % initialise solution everywhere to 20
V(:,Ny+1)=2*x’.*(10-x’)+20; % boundary condition on y=5
Vnext=V;
number_iter=0;
denom=2*(dx2+dy2); % this is used often so calculate it outside loop
% loop through until solution converges
while delta > eps
number_iter=number_iter+1;
maxdiff=0;
for i=2:Nx
for j=2:Ny
Vnext(i,j)=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(V(i,j-1)+V(i,j+1)))/den
dV=V(i,j)-Vnext(i,j); % the difference between successive solut
% keep track of the maximum difference
if abs(dV) > maxdiff;
maxdiff = abs(dV);
end
end
end
V=Vnext;
delta=maxdiff;
end
number_iter % show how many iterations were used
% plot the solution in 3D in 3 ways, surface plot, mesh plot, contour
figure(1)
surf(x,y,V’) % surf plots the surface
colorbar % put a color bar next to it showing what each color
shading interp % use interpolation for the coloring (smooths the col
xlabel(’x’); ylabel(’y’); zlabel(’Temperature’);
title(’Steady State Temperature’);
print -depsc laplace_direct_3D
figure(2)
mesh(x,y,V’) % mesh plots the surface with the mesh stretched over
colorbar % put a color bar next to it showing what each color
xlabel(’x’); ylabel(’y’); zlabel(’Temperature’);
title(’Mesh plot of temperature’)
print -depsc laplace_direct_mesh
figure(3)
contour(x,y,V’) % cont plots a contour plot
axis image
xlabel(’x’); ylabel(’y’); title(’Contour plot of temperature’)
print -depsc laplace_direct_contour
158 Steady
8. ELLIPTIC EQUATIONS: State EQUATION
LAPLACE’S Temperature Index
70
65
70 60
60 55
Temperature
50 50
40 45
40
30
35
20
6 30
4 10
25
2 5
20
y 0 0 x
65
70 60
60 55
Temperature
50 50
40 45
40
30
35
20
6 30
4 10
25
2 5
20
y 0 0 x
4.5
1 2 3 4 5 6 7 8 9
x
Index IMPROVING THE CONVERGENCE 159
8.3 Gauss-Seidel
The previous method where the new values are calculated over the entire domain using
the existing values is known as the Jacobi method. This requires storing 2 matrices of
values (the old and the new) and is relatively inefficient.
The speed of the iterative scheme convergence can be substantially improved if instead of
sweeping across using the old values we make sure we use the most recently calculated
values. This has the added advantage of not requiring the storing of two matrices.
for i=2:Nx
for j=2:Ny
Vnext=(dyˆ2*(V(i-1,j)+V(i+1,j))+dxˆ2*(V(i,j-1)+V(i,j+1)))/denom;
dV=V(i,j)-Vnext;
V(i,j)=V(i,j)+dV;
if abs(dV) > maxdiff; maxdiff = abs(dV); end
end
end
This is known as the Gauss-Seidel method. Because the most recent value of the solu-
tion is used it greatly improves the efficiency of the method. Typically the Gauss-Seidel
method uses approximately 25% to 50% less iterations for the same level of accuracy.
It turns out that the Gauss-Seidel method always converges monotonically to the solution.
For example
for i=2:Nx
for j=2:Ny
Vnext=(dyˆ2*(V(i-1,j)+V(i+1,j))+dxˆ2*(V(i,j-1)+V(i,j+1)))/denom;
dV=V(i,j)-Vnext;
>>> V(i,j)=V(i,j)+omega*dV; <<<
if abs(dV) > maxdiff; maxdiff = abs(dV); end
end
end
The optimum value of ω (the one resulting in the fastest convergence) is not a specific
value but is different for each problem. So how do you know what value to use? The
best method is to find the approximate value by considering the problem with fewer grid
points and then using that optimal value for the full problem with many more grid points.
A typical behaviour of the number of iterations versus the relaxation parameter is for
the number of iterations to slowly decrease as ω increases to the optimal value and then
to rapidly rise (see figure below). Hence it is usually best to take a value of ω that is
slightly less than the optimal value to ensure that you stay in the region where the best
convergence is obtained. Note that for this example (solving Laplaces equation as before)
the number of iterations decreased from about 115 for ω = 1 (no relaxation) to about 34
for ω ≈ 1.83. This results in the program taking approximately a third of the time to run
for no more effort.
Successive Over-relaxation is particularly important for very large complicated problems
in many dimensions where run time and convergence issues become critical. Most com-
mercial software will involve some form of Successive Over-relaxation when solving
steady state problems such as Laplace’s equation.
110
100
number of iterations
90
80
70
60
50
Optimal value of omega
40
30
1 1.2 1.4 1.6 1.8 2
omega
162 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index
8.3.2 Example
Consider the problem of a long house wall 2m high and 25cm thick. The wall is at thermal
equilibrium. The outside temperature is −5◦C the inside temperature 22◦C. The wall
foundations are in the ground at 15◦C. The top of the wall is insulated by insulation in the
ceiling of the house.
The governing PDE is Laplace’s Equation in 2D
∂ 2T ∂ 2T
+ 2 =0
∂ x2 ∂y
subject to the boundary conditions
∂T
= h(T − 22) on x=0
∂x
∂T
= −h(T + 5) on x = 0.25
∂x
T (x, 0) = 15
∂T
= 0 on y=2
∂y
1.8 18
1.6 16
1.4 14
1.2 12
10
1
8
0.8
6
0.6
4
0.4
2
0.2
0
0
0 0.05 0.1 0.15 0.2 0.25
Index GAUSS-SEIDEL 163
MATLAB code
% wall.m Solve Laplaces equation d2u/dx2 + d2u/dy2 = 0
% in a 2m x 0.25m wall subject to du/dx(0,y)=h(u-Tin) u(x,0)=Tground
% u(.25,y)=h(u-Tout) du/dy(x,2)=0
delta=maxdiff;
fprintf(’number of iterations = %3.0f max(dV) = %6.4f\n’,number_iter,maxdiff)
end % while loop finishes when convergence is obtained to accuracy eps
contlevels=[outsidetemp:1:max(max(V))];
contour(x,y,V’,contlevels) % plot contours of the solution at contlevels
title(’Contours of Steady State Temperature’);
colorbar
print -depsc wall_contour
164 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index
8.3.3 Example
A cylinder of height 2m and radius 1m has the bottom and sides held at 0◦C and the top
satisfies a quadratic temperature distribution with maximum 1◦C at the centre and 0◦C at
the edge.
Write down the governing equation, boundary conditions and find the steady state tem-
perature distribution using MATLAB.
Index GAUSS-SEIDEL 165
MATLAB code
% laplace_cylinder.m
% Solve Laplaces equation d2u/dz2 + 1/r du/dr + d2u/dr2 = 0
% subject to du/dr(0,z)=0 u(r,0)=0 u(r,2)=1-rˆ2 u(1,z)=0
%
L=1; M=2; Nz=30; Nr=20; dr=L/Nr; dz=M/Nz; eps=0.001; delta=1;
r=0:dr:L; z=0:dz:M;
V=zeros(Nr+1,Nz+1); % initialise solution everywhere to zero
V(:,Nz+1)=(1-r.ˆ2)’; % boundary condition on z=2 (j=Nz+1)
number_iter=0;
denom=2*(drˆ2+dzˆ2); % used often so calculate outside loop
figure(2)
contour(r,z,V’) % plot contours of the solution
title(’Contours of Steady State Temperature’);
print -depsc laplace_cylinder_contours
166 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index
0.8
0.6
0.4
0.2
0
2
1.5 1
0.8
1
0.6
0.5 0.4
0.2
0 0
1.8
1.6
1.4
1.2
0.8
0.6
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
9 Hyperbolic Equations: Wave Equation
bc bc bc bc bc
j+1 bc bc rsbc bc bc
j bc bc bc bc bc
j−1 bc bc bc bc bc
bc bc bc bc bc
i−1 i i+1
The significant difference between this scheme and the FTCS scheme for the diffusion
equation is that here the new value depends on the previous two time steps. The problem
168 9. HYPERBOLIC EQUATIONS: WAVE EQUATION Index
with this is at the beginning of the process as initially only values at one time level (the
initial time) are known. To overcome this the easiest method is to make a forward time
approximation of the initial derivative boundary condition to get values at the next time
level. If the initial derivative boundary condition at the ith space point is
∂ F
= V0 i
∂ t i
∂ F Fi1 − Fi0
= = V0 i
∂ t i ∆t
Now the first two time levels are known and the method can proceed.
9.1.1 Stability
As with the FTCS method for the diffusion equation this explicit method also suffers from
stability problems.
If γ 2 < 1 the method is stable.
If γ 2 > 1 the method is unstable and not useable.
9.2 Example
Consider a string 1 metre long that is fixed at the ends. Initial the string is pulled to a
parabolic shape between x = 25 and 33cm and then released. The governing differential
equation is the one dimensional wave equation
∂ 2F 2∂ F
2
= c
∂ t2 ∂ x2
subject to the boundary conditions
F(0,t) = 0 F(100,t) = 0
MATLAB code
y y y
−1 −1 −1
y y y
−1 −1 −1
y y y
−1 −1 −1
y y y
−1 −1 −1
y y y
−1 −1 −1
170 9. HYPERBOLIC EQUATIONS: WAVE EQUATION Index
You will notice that the initial pulse broke into two waves each moving in opposite direc-
tions. This is typical behaviour for solutions of the wave equation. You will also notice
form the time snapshots that the wave reflects from the rigid (fixed) ends.
A closer look at a future time plot reveals small wiggles developing in the solution. This
is numerical error beginning to show up. This sort of error is typical of what is found with
numerical solutions of hyperbolic equations.
−1
10 Concluding Remarks
All the examples used for PDEs have involved regular boundaries that are easily described
within the coordinate system in use (cartesian, cylindrical etc). These kinds of problems
involving regular boundaries are ideally suited to finite difference methods as it is rela-
tively simple to set up a grid that accurately represents the shape of interest.
Shapes with irregular boundaries are better dealt with using Finite Element Methods
rather than Finite Difference Methods. This is because for the finite element method mesh
points can be arbitrarily placed on the boundary and the mesh is ’triangularated’ thus al-
lowing for complicated shapes to be easily covered. Contrast this to the finite difference
method where typically you use a regular grid which fitting complicated boundaries to
is often difficult. One difficulty with the finite element method is then choosing how to
distribute the mesh points. This needs some knowledge of how you expect the solution to
behave.
There are many commercially available software packages for solving ODEs and PDEs.
Some are designed for specific types of problem (fluid flow, heat transfer etc) while others
are more multipurpose.
MATLAB PDE Toolbox:
solves fairly general Parabolic, Hyperbolic, and Elliptic equations using finite element
methods. Graphical interface for inputting equations and boundary conditions.
FASTFLO:
Solves almost any PDE using finite element methods. Relatively difficult to use but very
powerful.
FLUENT:
Solves many general PDEs using finite element methods. Primarily used for heat and
mass flow problems.
FlexPDE:
Solves many general PDEs using finite element methods. Best at 2D, the demo version is
available free (for Windows and Linux) at http://www.pdesolutions.com. It only takes a
few minutes to download and has numerous very good examples. Easy to use.
There are numerous others as well. Most commercial software uses finite elements rather
than finite differences specifically so that more complicated regions can be analysed.
172 10. CONCLUDING REMARKS Index
10.3 Finally
This has only been a very brief look at numerical solutions of ODEs and PDEs. The
complexities and subtleties go way beyond what we have presented here. Hopefully you
have now an appreciation of at least some of the ideas of numerical solutions and why it
is important to be able to calculate at least some analytic solutions.
Index
laplace, 28 plotting, 3
Laplace transform, 33 3D, 5
partial fractions, 33 data, 4
residue, 33 Poisson’s equation, 115
MATLAB commands, 20 predator-prey, 68
diag, 88 projectile, 60
diary, 7
eig, 7 quiver, 66
fzero, 13 radar, 114
help, 8 relaxation method, 84
lookfor, 8 example, 85
ode23, 54 Runge-Kutta, 53, 138
ode45, 14, 56 fourth order, 56
odeset, 59 second order, 53
ones, 88 running scripts, 7
quad, 11
quiver, 66 saddle, 68, 75
MATLAB PDE toolbox, 171 seismic, 114
matrix equations, 92 sending output to a file, 7
method of lines, 138, 142 shooting, 102
midpoint method, 50, 54 example, 82
shooting method, 80, 82
Neumann boundary condition, 117, 141, 142 Simpson’s rule, 11
Newtonian heat loss, 141, 145 SIR disease model, 73
node, 75 software, 171
ode23, 54 sonar, 114
ode45 spherical coordinates, 115, 148
example, 14 stability, 132, 135, 168
options, 59 step function, 23
Runge-Kutta, 56 step size, 46
ones, 88 algorithm, 47
order, 44 successive over relaxation, 160
susceptible, 73
parabolic PDE, 124, 125 system of equations, 41
partial fractions, 29
Taylor series, 43, 53, 54, 79, 149
distinct factors, 29
inversion, 31 vector field, 66
MATLAB, 33 velocity potential, 113
non linear factors, 31
repeated factors, 30 wave equation, 111, 114, 167
PDE, 109 web site, i, 1
classification, 124
definitions, 110
elliptic, 124, 155
examples, 109
hyperbolic, 124, 167
linear, 110
parabolic, 124, 125
separation of variables, 119
phase plane, 63