MATLAB Tutorial

182
ZPEM3311 Mathematical Methods for Differential Equations Numerical Methods and Techniques using MATLAB c Dr. G.N. Mercer

Transcript of MATLAB Tutorial

Page 1: MATLAB Tutorial

ZPEM3311

Mathematical Methods forDifferential Equations

Numerical Methods and Techniques using MATLAB

c© Dr. G.N. Mercer

Page 2: MATLAB Tutorial

Index

Page 3: MATLAB Tutorial

Preface

These notes comprise the numerical part of the course ZPEM3311 Mathematical Methodsfor Differential Equations. They are not a complete set of notes. Extra material andexamples may also be presented in the lectures and tutorials.

Using the electronic version of these notes

These notes are hyperlinked. Allgreen textis a link to somewhere else within this docu-ment. For example the contents page links to the appropriatepage in the text, the numbersin the index link to the page reference, the wordIndexin the header of most pages links tothe 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 therelevant text. Links to ex-ternal web pages areredin colour. Provided your PDF reader (egAdobe Acrobat Reader)is set up correctly these links should open the appropriate page in your web browser (egMicrosoft 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 inpurpleand the resulting output inblue.

If you have any difficulties with any part of the course do not hesitate in contacting me tosort them out.

Geoff MercerRoom G12 Science SouthSchool of Physical, Environmental and Mathematical SciencesPhone: 6268 8734Email:[email protected]

Page 4: MATLAB Tutorial

ii Index

Page 5: MATLAB Tutorial

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 byt . . . . . . . . . . . . . . . . . . . . . . . . . 27

2.2.7 Integral theorem. . . . . . . . . . . . . . . . . . . . . . . . . . 27

2.2.8 Division byt . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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

Page 6: MATLAB Tutorial

iv CONTENTS Index

3 Initial Value Problems 41

3.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3.2 Numerical approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3.3 Euler’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3.3.1 Graphically. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.3.2 Errors and order of the method. . . . . . . . . . . . . . . . . . . 44

3.3.3 Adjusting the step size. . . . . . . . . . . . . . . . . . . . . . . 46

3.4 Improvements to Euler’s method - midpoint method. . . . . . . . . . . . 50

3.5 Second order Runge-Kutta method. . . . . . . . . . . . . . . . . . . . . 53

3.6 Fourth order Runge-Kutta method. . . . . . . . . . . . . . . . . . . . . 56

3.7 ode45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

3.8 Projectile example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

4 Phase Plane Analysis 63

4.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

4.1.1 Quiver plots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

4.2 Predator-Prey model. . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

4.3 Competition model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

4.4 SIR disease model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

5 Boundary Value Problems 79

5.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

5.2 Shooting method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

5.2.1 Shooting method using guesses. . . . . . . . . . . . . . . . . . 80

5.2.2 Shooting method using fzero. . . . . . . . . . . . . . . . . . . . 82

5.3 Relaxation methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

5.3.1 Finite differences. . . . . . . . . . . . . . . . . . . . . . . . . . 84

5.3.2 Using finite differenes to solve a BVP. . . . . . . . . . . . . . . 85

5.3.3 Comments on solving matrix equations. . . . . . . . . . . . . . 92

5.3.4 Finite elements. . . . . . . . . . . . . . . . . . . . . . . . . . . 93

5.3.5 Symbolic form of a BVP. . . . . . . . . . . . . . . . . . . . . . 94

5.3.6 Finite element theory. . . . . . . . . . . . . . . . . . . . . . . . 94

5.3.7 Finite element example. . . . . . . . . . . . . . . . . . . . . . . 97

5.3.8 Finite element summary. . . . . . . . . . . . . . . . . . . . . . 100

5.4 Differential eigenvalue equations. . . . . . . . . . . . . . . . . . . . . . 101

6 Partial Differential Equations (PDEs) 109

6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

6.2 Functions of more than one variable. . . . . . . . . . . . . . . . . . . . 109

6.2.1 Types of problems. . . . . . . . . . . . . . . . . . . . . . . . . 109

6.2.2 Examples of PDEs. . . . . . . . . . . . . . . . . . . . . . . . . 109

6.3 Basic definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

Page 7: MATLAB Tutorial

Index CONTENTS v

6.3.1 Order of a PDE. . . . . . . . . . . . . . . . . . . . . . . . . . . 110

6.3.2 Linear PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

6.3.3 Why use numerical methods. . . . . . . . . . . . . . . . . . . . 110

6.4 Three common PDEs in applications. . . . . . . . . . . . . . . . . . . . 111

6.5 The diffusion equation. . . . . . . . . . . . . . . . . . . . . . . . . . . 112

6.5.1 Heat conduction. . . . . . . . . . . . . . . . . . . . . . . . . . 112

6.5.2 Mass diffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

6.5.3 Diffusion equation in 2D and 3D. . . . . . . . . . . . . . . . . . 112

6.6 Laplace’s equation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

6.6.1 Equilibrium solutions. . . . . . . . . . . . . . . . . . . . . . . . 113

6.6.2 Other applications. . . . . . . . . . . . . . . . . . . . . . . . . 113

6.7 The wave equation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

6.7.1 Sonar, radar, seismic waves. . . . . . . . . . . . . . . . . . . . 114

6.7.2 2D and 3D waves. . . . . . . . . . . . . . . . . . . . . . . . . . 114

6.8 Poisson’s equation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

6.9 Other co-ordinate systems. . . . . . . . . . . . . . . . . . . . . . . . . 115

6.10 Boundary and initial conditions. . . . . . . . . . . . . . . . . . . . . . . 116

6.10.1 ICs and BCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

6.10.2 Number of initial and boundary conditions. . . . . . . . . . . . 116

6.10.3 A typical initial condition . . . . . . . . . . . . . . . . . . . . . 116

6.10.4 Typical boundary conditions. . . . . . . . . . . . . . . . . . . . 116

6.11 Types of boundary conditions. . . . . . . . . . . . . . . . . . . . . . . . 117

6.12 Different solution approaches. . . . . . . . . . . . . . . . . . . . . . . . 119

6.12.1 Separation of variables. . . . . . . . . . . . . . . . . . . . . . . 119

6.12.2 Other approaches. . . . . . . . . . . . . . . . . . . . . . . . . . 119

6.12.3 Numerical techniques. . . . . . . . . . . . . . . . . . . . . . . . 119

6.12.4 Value of analytic solutions. . . . . . . . . . . . . . . . . . . . . 119

6.13 Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

6.14 Classification of PDEs. . . . . . . . . . . . . . . . . . . . . . . . . . . 124

7 Parabolic Equations: Diffusion Equation 125

7.1 Finite differences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

7.1.1 First Derivative. . . . . . . . . . . . . . . . . . . . . . . . . . . 125

7.1.2 Second Derivative. . . . . . . . . . . . . . . . . . . . . . . . . 125

7.2 FTCS method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

7.2.1 Discretise the region. . . . . . . . . . . . . . . . . . . . . . . . 126

7.2.2 Template for the FTCS Method. . . . . . . . . . . . . . . . . . 129

7.2.3 FTCS example. . . . . . . . . . . . . . . . . . . . . . . . . . . 130

7.2.4 Stability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

7.2.5 Unstable Example. . . . . . . . . . . . . . . . . . . . . . . . . 133

7.3 Crank-Nicolson method. . . . . . . . . . . . . . . . . . . . . . . . . . . 134

7.3.1 Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

Page 8: MATLAB Tutorial

vi CONTENTS Index

7.3.2 Matrix Representation. . . . . . . . . . . . . . . . . . . . . . . 135

7.3.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

7.4 Method of lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

7.5 Other boundary conditions. . . . . . . . . . . . . . . . . . . . . . . . . 141

7.5.1 Neumann Boundary Condition. . . . . . . . . . . . . . . . . . . 142

7.5.2 Cauchy Boundary Condition. . . . . . . . . . . . . . . . . . . . 145

7.6 Nonlinear Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

7.7 Other co-ordinate systems. . . . . . . . . . . . . . . . . . . . . . . . . 148

7.7.1 Disc example. . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

7.7.2 L’Hopital’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . 149

7.7.3 FTCS Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

8 Elliptic Equations: Laplace’s Equation 155

8.1 Example with Dirichlet boundary conditions. . . . . . . . . . . . . . . . 155

8.2 Improving the convergence. . . . . . . . . . . . . . . . . . . . . . . . . 159

8.3 Gauss-Seidel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

8.3.1 Successive Over Relaxation. . . . . . . . . . . . . . . . . . . . 160

8.3.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

8.3.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

9 Hyperbolic Equations: Wave Equation 167

9.1 Finite Differences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

9.1.1 Stability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

9.2 Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

10 Concluding Remarks 171

10.1 Irregular Boundaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

10.2 Commercial Software. . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

10.3 Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

Page 9: MATLAB Tutorial

1 Introduction to MATLAB

MATLAB (MATrix LABoratory) is a very powerful package designed for numerical anal-ysis, matrix calculations and visualisation (as well as many other applications). One ofMATLABs strengths is that it can be used as a simple ‘calculator’ and also is easy toprogram more complicated tasks. Many mathematical algorithms are already built in toMATLAB and many more are easily added.

1.1 Access to MATLAB

University College has a limited user licence for the full version of MATLAB, this isavailable via any central computing facility. Alternatively (and preferred as it is moreflexible for you) you can purchase MATLAB (from the Co-op Bookshop), this is a studentrelease. The most recent version is MATLAB 7, R2006b. Older versions of MATLABhad a student edition which is more than adequate for this course (and is often all thatis ever needed for many applications). You will find that purchasing a copy is the bestoption as you will be using MATLAB in a number of your other third year courses duringthe year. MATLAB is on the essential textbook list so you willbe able to be reimbursedfor its purchase.

1.2 Using MATLAB

MATLAB is an interactive program so you can enter commands atthe prompt or readin files with lines of MATLAB commands. For relatively simpletasks it is acceptableto enter commands at the prompt but for more complicated tasks it is better to store thecommands in a file and then read in the file. This makes it easy tochange the commandsand 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) writtenfor various purposes available on the internet. A central repository of contributed M-filescan be found athttp://www.mathworks.com/matlabcentral/.

For this course you should do your work using M-files as these will be handed up as partof your assignments along with the relevant output.

All M-files given in the notes will be available from the course webCT page. Many of theassignment questions will be a modification of these files.

Page 10: MATLAB Tutorial

2 1. INTRODUCTION TO MATLAB Index

1.3 A few simple examples

1. Matrix Manipulation

Enter a 2× 2 matrix find its inverse, and eigenvalues and eigenvectors.Check thatits inverse times itself is the identity.

MATLAB code

% any thing after a % sign is a commentA=[1,2;3,4] % enter the 2x2 matrix and call it AB=inv(A) % calculate the inverse of A and call it Beig(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 calledexample1.m then to run it typeexample1 (without the.m) from within MATLAB. (Note you have to be in thesame directory as the file is saved in, or have made that directory in the path thatMATLAB looks in). The MATLAB output from these commands is the following

A =

1 23 4

B =

-2.0000 1.00001.5000 -0.5000

ans =

-0.37235.3723

V =

-0.8246 -0.41600.5658 -0.9094

D =

-0.3723 00 5.3723

check =

1.0000 00.0000 1.0000

Page 11: MATLAB Tutorial

Index A FEW SIMPLE EXAMPLES 3

2. Plotting

MATLAB has very good graphics capability. The easiest method for simple plotsis to define a vector (for examplex) and then perform a function on each elementof that vector (egy(x)) and then ploty(x) versusx.

Defining a vector is straightforward. The MATLAB commandx=a:b:c definesx to be a vector of values starting ata increasing in steps ofb to c. For examplex=-4:3:8 results inx being the vector[−4,−1,2,5,8].

Example. Ploty = x2 +sin(xπ).

MATLAB code

x=-5:0.2:5; % define a vector x with values% from -5 to 5 in steps of 0.2% the semicolon suppresses the output

y=x. * x+sin(x * pi); % calculate y=x * x+sin(x * pi), note the . *% this is because x is a vector so we need% to multiple ’component by component’

plot(x,y)print -deps example2 % send the plot output to a

% postscript file called example2.eps

−5 0 5−5

0

5

10

15

20

25

Page 12: MATLAB Tutorial

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 commandload is used to load data from an external file. For example read data from a filecalled datafile, calculate the line of best fit to the data and add some titles, legendand text to the plot.

MATLAB code

% read data from the file datafile. This puts it into% a variable matrix called datafile by defaultload datafilex=datafile(:,1) % put first column into variable xy=datafile(:,2) % put second column into variable y

% Fit a linear regression line to the datat=[ones(size(x)) x];coeffofregn=t\ystraightline=[ones(size(x)) x] * coeffofregn;

% Plot the data and line and add titles and labelsplot(x,y,’ro’,x,straightline,’b-’)title(’data and line of best fit’)xlabel(’x’)ylabel(’y’)

% add some text to the plot and a legend (the number is% where to put the legend,2 is the top left corner)text(3,20,’This is text’)legend(’data’,’line of best fit’,2)

0 1 2 3 4 5 6 7 810

20

30

40

50

60

70data and line of best fit

x

y

This is text

dataline of best fit

Page 13: MATLAB Tutorial

Index A FEW SIMPLE EXAMPLES 5

4. 3D Plotting

MATLAB has very good 3D graphics capability. The easiest method is do definethe domain using themeshgrid command, then define the function and then plotit usingmesh or surf commands.

Example. Plotz = 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

−5

0

5

−5

0

5

10−100

−50

0

50

100

Page 14: MATLAB Tutorial

6 1. INTRODUCTION TO MATLAB Index

5. Solving Linear Equations

The original reason for developing MATLAB was to perform matrix calculations.Due to this it has many convenient ways of dealing with matrices. For exampleto solve a linear equation systemAx = b there is a command known as ’backslashdivide’.

MATLAB code

A=[1,2,3;4,5,6;7,8,10]; % define the matrix Ab=[1,1,1]’; % note the ’ to take transpose

% or could do b=[1;1;1]x=A\b % solve for x

x =

-1.00001.00000.0000

6. Extracting Segments of Matrices

Often you may want to extract sections of a matrix. For example all of the first rowor the second column.

MATLAB code

A=[1,2,3;4,5,6;7,8,10];c=A(1,:) % extract all of the first row of Ad=A(:,2) % extract all of the second column of A

c =1 2 3

d =258

diary off

Page 15: MATLAB Tutorial

Index RUNNING MATLAB SCRIPTS 7

1.4 Running MATLAB scripts

One of the easiest and most transportable ways to use MATLAB is to enter your MATLABcommands into a file and then run that file. These types of files are calledM-files and havethe extension .m after their name. To run an M-file from MATLABmake sure you are inthe directory where the file is located and then just type the name of the file without theextension (i.e. without the .m). MATLAB will then process all the commands that are inthat file.

Example

1. Create a file called example1.m

2. In that file put the following lines

format compact % this outputs in a compact formA=[1,2;3,-4] % this enters the 2x2 matrix AC=Aˆ10 % this finds the tenth power of Aeig(C) % this finds the eigenvalues of C

3. From within MATLAB type

example1

that will run the commands in example1.m

4. The following will appear on the screen

A =1 23 -4

C =1395967 -2789886

-4184829 8370682ans =

10249765625

1.5 Sending output to a file

Often you want to keep the output of a MATLAB command. Thediary commands doesthat 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

Page 16: MATLAB Tutorial

8 1. INTRODUCTION TO MATLAB Index

1.6 MATLAB help facility and lookfor command

MATLAB has a comprehensive help facility. It is accessed by typing help followed bythe name of the command you require help on. For example

help diary

will return the following

DIARY Save text of MATLAB session.DIARY filename causes a copy of all subsequent command windo w inputand most of the resulting command window output to be appende d to thenamed file. If no file is specified, the file ’diary’ is used.

DIARY OFF suspends it.DIARY ON turns it back on.DIARY, by itself, toggles the diary state.

Use the functional form of DIARY, such as DIARY(’file’),when the file name is stored in a string.

Note that the help facility uses capitals for the command name, capitals should NOT beused when actually using the commands.

If you don’t know the name of the function then use

lookfor keyword

This will find all occurrences of functions or commands with the specified keyword.For example

lookfor differentiate

returns all functions that mention differentiate

lookfor differentiatePOLYDER Differentiate polynomial.FNDER Differentiate a function.DIFF Differentiate.

Page 17: MATLAB Tutorial

Index FUNCTIONS IN MATLAB 9

1.7 Functions in MATLAB

Sometimes you will want to write your own function in MATLAB.For example an M-filethat takes some kind of input and returns some calculations on that input.

For example write a function that calculates

f (x) = x3 +4x2+3

Create a file called calcf.m that looks like the following

MATLAB code

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

From within MATLAB type

calcf(3)

to calculatef (3) this results in the following output

calcf(3)

ans =

66

Page 18: MATLAB Tutorial

10 1. INTRODUCTION TO MATLAB Index

1.8 Functions in MATLAB – vector input

Since MATLAB deals with vectors of numbers it is more useful to write functions so thatthey can handle vectors rather than an individual number. The only change needed toachieve 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 vectorx. That is, it should calculatef (x) for each value of a vectorx andreturn 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

From within MATLAB type

y=0:0.2:1.0calcfvec(y)

to calculatef (y) this results in the following output

y=0:0.2:1.0y =

0 0.2000 0.4000 0.6000 0.8000 1.0000calcfvec(y)ans =

3.0000 3.1680 3.7040 4.6560 6.0720 8.0000

Page 19: MATLAB Tutorial

Index PASSING FUNCTION NAMES 11

1.9 Passing function names

Often in MATLAB you need to pass the name of a function to another function. Forexample if you want to numerically calculate the integral ofa function MATLAB has aninbuilt 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 forthe convergence using a comparison between successive approximations. trace is anadditional feature for plotting a trace of the integrand used. The answer is returned inAandcount is a count of how many function evaluations have been performed.

funcname is the name of the function to be passed. For exampleif integrating the functiondefined in the M-file quadexamplefunc.m then use quadexamplefunc where ever you seefuncname.

Important Note:The function referenced by ’funcname’must be written to take a vectorinput and give a vector output.

ExampleCalculate

f (x) =2√π

∫ x

0e−t2

dt

Forx = 1. This function is known as the error function (erf(x) in MATLAB)

MATLAB code

% quadexample.mformat long % use lots of decimal places% the ’exact’ answer using MATLABs erf functionerrorfunc=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 switchtrace=1;[integral,count]=quad(’quadexamplefunc’,0,1,1e-6,tr ace)

MATLAB code

function f=quadexamplefunc(x)% quadexamplefunc.m% the function to be integratedf=(2/sqrt(pi)) * exp(-x.ˆ2);return

Page 20: MATLAB Tutorial

12 1. INTRODUCTION TO MATLAB Index

>> quadexample

errorfunc =

0.84270079294971

integral =

0.84270079342046

integral =

0.84270079427671

5 0.0000000000 1.00000000e+00 0.84271159957 0.0000000000 5.00000000e-01 0.52049955739 0.0000000000 2.50000000e-01 0.2763263864

11 0.2500000000 2.50000000e-01 0.244173486813 0.5000000000 5.00000000e-01 0.322201277515 0.5000000000 2.50000000e-01 0.190655758017 0.7500000000 2.50000000e-01 0.1315451622

integral =

0.84270079342046

count =

17

diary off

Page 21: MATLAB Tutorial

Index MATLAB FINDING ZEROS 13

1.10 MATLAB finding zeros

MATLAB has an inbuilt function for finding zeros of a functioncalled fzero . Its useisr=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

FZERO Scalar nonlinear zero finding.X = FZERO(FUN,X0) tries to find a zero of FUN near X0. FUN(usually an M-file): FUN.M should take a scalar real valueand return a real scalar value when called with feval:F=feval(FUN,X). The value X returned by FZERO is near a pointwhere FUN changes sign, or NaN if the search fails.

X = FZERO(FUN,X0), where X is a vector of length 2, assumesX0 is an interval where the sign of FUN(X0(1)) differs fromthe sign of FUN(X0(2)). An error occurs if this is not true.Calling FZERO with an interval guarantees FZERO will returna value near a point where FUN changes sign.

X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as astarting guess. FZERO looks for an interval containing a sig nchange for FUN and containing X0. If no such interval is found ,NaN is returned. In this case, the search terminates when thesearch interval is expanded until an Inf, NaN, or complex val ueis found.

See also ROOTS.

>> diary off

Example

Find a zero of x3−2x2−9 = 0

>> fzero(’findzerof’,4)Zero found in the interval: [2.72, 4.9051].

ans =

3.0000

>> diary off

MATLAB code

function f=findzerof(x)% findzerof.mf=x.ˆ3-2 * x.ˆ2-9;return

Page 22: MATLAB Tutorial

14 1. INTRODUCTION TO MATLAB Index

1.11 An Introduction to solving DEs numerically

MATLAB has many ways of numerically solving Differential Equations. Here we willpresent one method, the details of the method and the mathematics behind it will beexplained in later lectures. This will give you enough now tobe able to numerically solvemany of the DEs you will encounter early in this course. Consider a first order ODE

dydt

= f (t,y)

subject to some initial condition (at timet = 0)

y(0) = y0

The MATLAB function ode45 will solve these kind of problems. Here is an excerptfrom the help onode45

ODE45 Solve non-stiff differential equations, medium orde r method.[T,Y] = ODE45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL]integrates the system of differential equations y’ = f(t,y) fromtime T0 to TFINAL with initial conditions Y0. Function ODEFU N(T,Y)must return a column vector corresponding to f(t,y). Each ro w inthe solution array Y corresponds to a time returned in the col umnvector T. To obtain solutions at specific times T0,T1,...,T FINAL(all increasing or all decreasing), use TSPAN = [T0 T1 ... TFI NAL].

Example[t,y]=ode45(@vdp1,[0 20],[2 0]);plot(t,y(:,1));

solves the system y’ = vdp1(t,y), using the default relative errortolerance 1e-3 and the default absolute tolerance of 1e-6 fo r eachcomponent, and plots the first component of the solution.

There are 3 things you need to define to use MATLAB to solve thistype of DE.

1. The range oft you wish to calculate the solution over (TSPAN=[T0,TFINAL] )

2. The initial condition ony (Y0)

3. An M-file with the functionf (t,y) (ODEFUN)

Notes

1. t is returned as a vector of all the times where the solutionwas determined.

2. Becauseode45 is set up to solve systems of equations y is a matrix. The firstdimension corresponds to t, that is solutions at the times int, the second dimensionis the number of equations which in this case is just 1. This has an impact on howtheODEFUNfunction is written. See the next example to see how this works.

Page 23: MATLAB Tutorial

Index AN INTRODUCTION TO SOLVING DES NUMERICALLY 15

ExampleSolve

dydt

= y−2π cost subject to y(0) = 3

over the ranget = [0,5].

Note that you could solve this particular DE using the Integrating Factor method. Thenumerical solution works for DEs that can not be solved analytically. This is the point ofdeveloping numerical methods for solving DEs, they let you get solutions that can not beobtained 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 spany0=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 savedprint -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 equationf(1)=y(1) - 2 * pi * cos(t);f=f(:); % forces f to be a column vectorreturn

Page 24: MATLAB Tutorial

16 1. INTRODUCTION TO MATLAB Index

0 1 2 3 4 5−20

−15

−10

−5

0

5

The same files without all the extraneous comments

MATLAB code

% odeex1.mtspan=[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.mf(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).

Page 25: MATLAB Tutorial

Index SYMBOLIC MANIPULATION 17

1.12 Symbolic Manipulation

MATLAB can also do some (limited) symbolic manipulation of equations such as findingroots of equations, simplifying, differentiating and integrating. Firstly one much declarethe variable(s) in question to be symbolic in nature (so thatMATLAB knows they are notjust a number) and then it is possible to manipulate them.

1.12.1 Differentiating using MATLAB

An example of differentiation

MATLAB code

% diffexample.m% an example of how to use MATLAB to differentiate functions%

% make the variables x and y symbolic so that MATLAB% knows to treat them as symbols rather than numberssyms x y

% define a function of xf=xˆ3-3 * y-cos(xˆ2)-atan(x)

% differentiate f with respect to xdfdx=diff(f,x)

% and the second derivative in 2 waysd2fdx2=diff(dfdx,x)d2fdx2=diff(f,x,2)

% function of 2 variables so need to do partial derivatives

% define a function of x and yg=x * y-sin(x-y)+yˆ3 * cos(1/y)

% find the partial derivativesdgdx=diff(g,x)dgdy=diff(g,y)

% find the second derivatives% either take derivatives of above or use diff with extra para meterd2gdx2=diff(dgdx,x)d2gdx2=diff(g,x,2)d2gdy2=diff(dgdy,y)d2gdy2=diff(g,y,2)% and the cross derivative isd2gdxdy=diff(dgdx,y)

Page 26: MATLAB Tutorial

18 1. INTRODUCTION TO MATLAB Index

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)

Page 27: MATLAB Tutorial

Index SYMBOLIC MANIPULATION 19

1.12.2 Integrating using MATLAB

An example of integration

MATLAB code

% integralexample.m% examples of integrating in MATLAB

% make x a symbolic variablesyms x

% define a function ff=xˆ2-sin(2 * x)+cosh(3 * x)-exp(-x)

% integrate f with repsect to xintf=int(f,x)

% integrate f wrt x over the range (0,pi)intfab=int(f,x,0,pi)

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

1.12.3 Solving equations

MATLAB has other symbolic routines for solving a variety of equations. For exampleto solve systems of equations you can usesolve and to solve differential equationssymbolically you can usedsolve . These will be discussed at a later time in the course.

Page 28: MATLAB Tutorial

20 1. INTRODUCTION TO MATLAB Index

1.13 Some Elementary MATLAB Commands

Input Output Commentx = [-3:2:7] x = -3 -1 1 3 5 7 x is a vector that starts at –3

and goes up in steps of 2 to 7x(2) -1 the second element of the

vector xy = x.ˆ2 y = 9 1 1 9 25 49 using ’dot’ notation square

the vector x ’component bycomponent’

plot(x,y)

−4 −2 0 2 4 6 80

5

10

15

20

25

30

35

40

45

50 plot y versus x

plot(x,y,’r--’) plot y vs x with a red dashline

hold on hold the plot in placeplot(x,y,’bo’)

−4 −2 0 2 4 6 80

5

10

15

20

25

30

35

40

45

50

add blue circles to the plot

t = 0:pi/50:2 * pi; t is a vector 0 to 2π in stepsof π/50

a=cos(t); Semicolon supressess theoutput.

b=sin(t);plot(a,b,’g.’)

−1 −0.5 0 0.5 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

The plot is a parametric plotof a circle with green dots

c=[-3:1.0:5]; 3D plottingd=[-2:0.5:7]; Set up the domain in c and d[x,y]=meshgrid(c,d) Make a mesh of values in x

and yz=4 * x.ˆ2-2 * y.ˆ2; The functionz = 4x2−2y2

mesh(x,y,z)

−5

0

5

−5

0

5

10−100

−50

0

50

100

Plot the function in 3D

title(’text here’) add a title to a plotxlabel(’text here’) add x axis label to a plotaxis([0 5 -3 8]) set the x axis to be from 0 to

5 and the y axis from -3 to 8text(1,20,’blah’) place some text on the plot at

the point (1,20)

Page 29: MATLAB Tutorial

Index SOME ELEMENTARY MATLAB COMMANDS 21

Input Output Commentx=[1 2 3] x = 1 2 3 define a vectorlength(x) 3 calculate the size of the vectory=x’ y = 1 y is the transpose of x

23

z=x * x error: dimensions dont agreez=x * y z = 14 vector multiplicationdot(x,y) 14 dot product of vectors x and ycross(x,y) 0 0 0 cross product of vectors x and yA=[1,2;3,4] A = 1 2 enter the matrix A

3 4eig(A) -0.3723 find the eigenvalues of A

5.3723inv(A) -2.0000 1.0000 find the inverse of A

1.5000 -0.5000det(A) -2.0000 the determinant of AAˆ3 37 54 A cubed (A*A*A)

81 118A.ˆ3 1 8 each component of A cubed

27 64A(1,:) 1 2 all of the first row of matrix AA(:,2) 2 all of the second column of ma-

trix A4

b=[1;4] b = 1 b is a column vector4

c=A\b c = 2.0000 Solve Ac=b using backslash di-vide

-0.5000zeros(2) 0 0 a 2x2 matrix of zeros

0 0ones(2) 1 1 a 2x2 matrix of ones

1 1help plot help on the plot commandlookfor sinh find all references to the word

sinhdiary file.txt send all output to the file file.txthold on hold the plot on so that more can

be added to ithold off turn the hold off so that a new

plot is madeload xdata read the contents of the file xdata

and store the results in a variablecalled xdata

Page 30: MATLAB Tutorial

22 1. INTRODUCTION TO MATLAB Index

Page 31: MATLAB Tutorial

2 Laplace Transforms

2.1 Introduction

The majority of the theory of Laplace Transforms is given in the theoretical section ofthis course. Here we will investigate how MATLAB can be used to help solve LaplaceTransform problems.

The notation we will adopt is

F(s) = L { f (t)}=∫ ∞

0e−st f (t)dt.

The inverse transform is denoted:

L−1{F(s)} = f (t).

2.2 Some standard results in Laplace Transforms

For completeness sake what follows are some standard results often used in LaplaceTransforms.

2.2.1 Heaviside function

The Heaviside step function is defined as (see figure)

H(t −a) =

{

0 t < a1 t ≥ a

H(t-a)1

ta

The Heaviside Step function is like a switch. At timet = a it ’turns on’ with value 1.

The Heviside function is defined in MATLAB both as a numericalfunction and as asymbolic function. Its usage isheaviside(x) .

Page 32: MATLAB Tutorial

24 2. LAPLACE TRANSFORMS Index

ExamplePlot

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

−2 −1 0 1 2 3−1

0

1

2

3

4

5

6

7

8

9

10

Page 33: MATLAB Tutorial

Index SOME STANDARD RESULTS IN LAPLACE TRANSFORMS 25

2.2.2 Dirac delta function

Suppose that a constant force,f (t), of amountA acts on a body for a short time (ofdurationε).

forcef(t)

time

A

ε/2ε/2 t +t -

The impulse delivered byf (t) is

I =∫ t0+ε/2

t0−ε/2f (t)dt = εA

Note that the impulse,I, is a constant. SupposeI = 1, thenA = 1/ε. As ε → 0, we musthaveA → ∞. The width of the rectangle gets smaller and the height gets larger so that thearea(I) stays equal to 1.

force

time

Page 34: MATLAB Tutorial

26 2. LAPLACE TRANSFORMS Index

We can see that in the limit asε → 0

f (t) =

{

0, t 6= t0∞, t = t0,

and∫ ∞

−∞f (t)dt = 1.

TheDirac delta function1, δ (t), is defined by the following properties

1. δ (t − t0) = 0, t 6= t0,

2.∫ b

aδ (t − t0) dt = 1 a < t0 < b.

Note that the Dirac delta function is not a proper function. It can be thought of as the limitof a distribution of step functions (as in the previous diagram). It can also be thought ofas the derivative of the Heaviside function.

2.2.3 First shifting theorem

L{

eat f1(t)}

= F1(s−a)

2.2.4 The second shifting theorem

L { f1(t −a)H(t −a)} = e−asF1(s)

whereH(t) is the unit step function (Heaviside function).

2.2.5 Transforms of derivatives

If the function f (t) has well defined derivatives and transform then:

L{

f ′(t)}

= sF(s)− f (0).

If the function f (t) hasn well defined derivatives then:

L

{

f (n)(t)}

= snF(s)− sn−1 f (0)− sn−2 f ′(0)−

·· · − f (n−1)(0).

1The delta function was introduced by the British physicistP.A.M Dirac (1902-1984) in the paper “ThePhysical Interpretation of the Quantum Mechanics”, Proceedings of the Royal Society of London, SeriesA113, 1926-1927, pp. 621-641.

Page 35: MATLAB Tutorial

Index SOME STANDARD RESULTS IN LAPLACE TRANSFORMS 27

2.2.6 Multiplication by t

L {t f (t)}= −F ′(s)

In general

L {tn f (t)}= (−1)n dnF(s)dsn

2.2.7 Integral theorem

The Laplace transform of an integral is:

L

{

∫ t

0f (τ)dτ

}

=1s

F(s)

2.2.8 Division byt

A corresponding theorem for integration is

L

{

1t

f (t)

}

=

∫ ∞

sF(s)ds

2.2.9 Convolution theorem

Often when we cannot get an inverse in terms of a known function we have to use theconvolution theorem

L−1{F(s)G(s)}=

∫ t

0f (τ)g(t − τ)dτ

Page 36: MATLAB Tutorial

28 2. LAPLACE TRANSFORMS Index

2.2.10 Taking Laplace transforms using MATLAB

MATLAB can do Laplace Transforms and their inversions symbolically. Firstly you mustdeclare the variables that are used (egt and s) as symbolic variables using thesymscommand. Thelaplace function is then used to find the Laplace Transform.

Example

For example find the Laplace Transform off (t) = e4tsin(3t)

MATLAB code

% find the Laplace Transform of% f(t)=exp(4 * t) * sin(3 * t)

syms s t % make s and t symbolic variablesFs=laplace(exp(4 * t) * sin(3 * t))

results in

Fs = 3/((s-4)ˆ2+9)

Example

Find the Laplace Transform of the derivative off (t) = e−3tsinh(5t)+ t2, that is findL{d

dt

(

e−3tsinh(5t)+ t2)}

MATLAB code

% find the Laplace Transform of the derivative of a function% f(t)=d/dt (exp(-3 * t) * sinh(5 * t) +tˆ2 )% using the diff command that does symbolic differentiation

syms s t % make s and t symbolic variablesFs=laplace(diff(exp(-3 * t) * sinh(5 * t)+tˆ2))

results in

Fs = -15/((s+3)ˆ2-25)+5 * (s+3)/((s+3)ˆ2-25)+2/sˆ2

Page 37: MATLAB Tutorial

Index INVERSION OF LAPLACE TRANSFORMS 29

2.3 Inversion of Laplace transforms

The inversion of Laplace transforms can be done directly using an infinite integral in thecomplex plane and finding residues around the singularities. Generally this is difficult todo and beyond the scope of this course. It is much easier to modify our functionF(s) tolook like something we already know the inverse transform of. For example we alreadyknow that

L {cosat}=s

s2+a2

so that the inverse of

L−1(

2ss2+9

)

= 2cos3t.

All that is necessary is to get our solution in the same form asthe tables and then do thesame process as the Laplace transform but in reverse.

2.3.1 Partial fractions

Partial Fractions are useful to convert complicated inversions into a form that can be usedfrom the tables. Partial fraction are used to convert a complicated rational expressionsuch asy = G(s)/F(s) into a sum of simpler terms each of which can be inverted usingthe tables. F(s) must be written in a form so that it is a product of polynomials of as a loworder as possible.

Distinct Linear Factors

If the denominator has distinct linear factors then it is a simple matter of writing theexpression as a sum of terms with those linear factors as denominators and unknownnumerators. For example

3s+13(s+3)(s+1)

=A

s+3+

Bs+1

whereA andB are the unknown numerators that have to be found. To find theserewritethe right hand side over a common denominator so that it resembles the left hand side andthen equate the coefficients of the different powers of s in the numerators.

3s+13(s+3)(s+1)

=A

s+3+

Bs+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 givesA+B = 3

and the constant term (s to the zero power) gives

A+3B = 13

Page 38: MATLAB Tutorial

30 2. LAPLACE TRANSFORMS Index

This is 2 equation in 2 unknowns which can be solved to give

A = −2 B = 5.

Hence3s+13

(s+3)(s+1)=

−2s+3

+5

s+1

Repeated Linear Factors

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+1s(s−2)2 =

As

+B+Cs(s−2)2

=A(s−2)2+(B+Cs)s

s(s−2)2

where we thus have2s+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

Or alternatively substitute values for s. Either way we get

A =14, B = 3, C =

−14

and so2s+1

s(s−2)2 =1/4

s+

3− s/4(s−2)2

If the factor is repeatedn times then the unknown numerator must be a polynomial ofdegreen−1. The numerator must always have a degree one less than the denominator.For example

2s+1s2(s−5)4 =

A+Bss2 +

C +Ds+Es2 +Fs3

(s−5)4

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+1s(s−2)2 =

As

+D

s−2+

E(s−2)2

where we thus have2s+1 = A(s−2)2+Ds(s−2)+Es

Page 39: MATLAB Tutorial

Index INVERSION OF LAPLACE TRANSFORMS 31

so equating coefficients we get:

O(s2) : 0 = A+D

O(s1) : 2 = −4A−2D+E

O(s0) : 1 = 4A

hence giving:

A =14, D =

−14

, E =52

2s+1s(s−2)2 =

1/4s

+−1/4(s−2)

+5/2

(s−2)2

Using this method you must make sure there are as many terms involving the factor as itis repeated. For example

2s+1s2(s−5)4 =

As

+Bs2 +

Cs−5

+D

(s−5)2 +E

(s−5)3 +F

(s−5)4

Non Linear Factors

If the factors are not linear then use method 1 as for repeatedlinear factors. That is use anumerator that has degree one less than the term in the denominator. For example if thedenominator has a quadratic that can not be reduced any further then the correspondingnumerator is linear

1s(s2+4)

=As

+Bs+Cs2+4

2.3.2 Application of partial fractions to inversion

The best way to illustrate this is by a series of examples.

Example

L−1{

3s+13(s+3)(s+1)

}

= L−1{ −2

s+3+

5s+1

}

(by partial fractions)

hence enabling the solution:−2e−3t +5e−t .

Example

L−1{

2s+1s(s−2)2

}

= L−1{

1/4s

+−1/4(s−2)

+5/2

(s−2)2

}

(using partial fractions)

=14− 1

4e2t +

52

e2tt (using shifting theorem)

Page 40: MATLAB Tutorial

32 2. LAPLACE TRANSFORMS Index

Example1

s(s2+4)=

As

+Bs+Cs2+4

We thus get1 = 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

easily giving the solutionA = 14,B = −1

4,C = 0 and hence the inverse

L−1{

1s(s2+4)

}

= L−1{

1/4s

− 1/4s2+4

}

=14− 1

4sin2t

2

This following example uses the derivative formulae to transform a differential equationinto an algebraic equation.

Example

Let us solve the initial value problem:

y′′ +4y′ +3y = 0, y(0) = 3, y′(0) = 1.

Taking the Laplace transform of both sides of the equation yields:

(

s2Y − y(0)s− y′(0))

+4(sY − y(0))+3Y = 0

whereY (s) = L {y(t)}. Using the initial conditions gives

(

s2Y −3s−1)

+4(sY −3)+3Y = 0

which simplifies to(s2+4s+3)Y = 3s+13

giving

Y =3s+13

(s+3)(s+1)

=−2

s+3+

5s+1

(using partial fractions, see before)

AsL{

eat}= 1/(s−a)

we can invert to obtainy(t) = −2e−3t +5e−t .

This illustrates how a transformation simplifies the solution to differential equations.

Page 41: MATLAB Tutorial

Index INVERSION OF LAPLACE TRANSFORMS 33

2.3.3 MATLAB functions for partial fractions

MATLAB has commands to do partial fractions (residue andpoly ).

Residue

The residue function takes a ratio of polynomialsB(s)A(s) and returns the corresponding

partial fraction representation of it. It takes as input thecoefficients of the polynomialsB(s) andA(s) (in descending powers ofs) and returns the coefficients and the pole values(the zeros of the denominator).

help residue

RESIDUE Partial-fraction expansion (residues).[R,P,K] = RESIDUE(B,A) finds the residues, poles and direct term ofa partial fraction expansion of the ratio of two polynomials B(s)/A(s).If there are no multiple roots,

B(s) R(1) R(2) R(n)---- = -------- + -------- + ... + -------- + K(s)A(s) s - P(1) s - P(2) s - P(n)

Vectors B and A specify the coefficients of the numerator anddenominator polynomials in descending powers of s. The resi duesare returned in the column vector R, the pole locations in col umnvector P, and the direct terms in row vector K. The number ofpoles is n = length(A)-1 = length(R) = length(P). The direct t ermcoefficient vector is empty if length(B) < length(A), other wiselength(K) = length(B)-length(A)+1.

If P(j) = ... = P(j+m-1) is a pole of multplicity m, then theexpansion includes terms of the form

R(j) R(j+1) R(j+m-1)-------- + ------------ + ... + ------------s - P(j) (s - P(j))ˆ2 (s - P(j))ˆm

[B,A] = RESIDUE(R,P,K), with 3 input arguments and 2 output a rguments,converts the partial fraction expansion back to the polynom ials withcoefficients in B and A.

Warning: Numerically, the partial fraction expansion of a r atio ofpolynomials represents an ill-posed problem. If the denomi natorpolynomial, A(s), is near a polynomial with multiple roots, thensmall changes in the data, including roundoff errors, can ma kearbitrarily large changes in the resulting poles and residu es.Problem formulations making use of state-space or zero-pol erepresentations are preferable.

See also POLY, ROOTS, DECONV.

Page 42: MATLAB Tutorial

34 2. LAPLACE TRANSFORMS Index

Example

For example find the partial fraction representation of

F(s) =3s−1

s2 +2s−15

MATLAB code

% find the partial fraction representation of% B(s) 3s-1% ---- = -------------% A(s) sˆ2 +2s -15

B=[3 -1] % coefficeints of B(s), in descending sA=[1 2 -15] % coefficients of A(s)[R,P,K]=residue(B,A) % find the residues and poles

and the output is

B =3 -1

A =1 2 -15

R =21

P =-5

3K =

[]

The interpretation of this is as follows

K is the remainder, K empty means the partial fraction representation has no remainderP gives the poles. So in this case the denominator factors into (s+5)(s−3).R gives the coefficient of each of the poles, in the same order as they are listed in P sohere

F(s) =2

s+5+

1s−3

Page 43: MATLAB Tutorial

Index INVERSION OF LAPLACE TRANSFORMS 35

Example

MATLAB can also deal with repeated roots but care must be taken when interpretting theoutput. For example find the partial fraction representation of

F(s) =s2+4s−2s3−3s−2

MATLAB code

% find the partial fraction representation of% B(s) sˆ2 + 4s -2% ---- = --------------% A(s) sˆ3 -3s -2

B=[1 4 -2] % coefficeints of B(s), in descending sA=[1 0 -3 -2] % coefficients of A(s)[R,P,K]=residue(B,A) % find the residues and poles

and the output is

B =1 4 -2

A =1 0 -3 -2

R =1.1111

-0.11111.6667

P =2.0000

-1.0000-1.0000

K =[]

The interpretation of this is as follows

K is the remainder, K empty means the partial fraction representation has no remainderP gives the poles. So in this case the denominator factors into (s−2)(s+1)(s+1).R gives the coefficient of each of the poles, in the same order as they are listed in P.Repeated poles increase in their power. So here

F(s) =10/9s−2

+−1/9s+1

+5/3

(s+1)2

Page 44: MATLAB Tutorial

36 2. LAPLACE TRANSFORMS Index

2.3.4 Inversion using MATLAB

Often it is not necessary to do the partial fraction steps yourself as MATLAB can do someinversions symbolically using the MATLAB commandilaplace .

Example

Find the inversion ofF(s) =

ss2−2s+1

MATLAB code

% find the inverse Laplace Transform of% s% F(s) = ----------% sˆ2-2s+1

syms s t % make s and t symbolic variablesFs=s/(sˆ2-2 * s+1)ft=ilaplace(Fs)

results in

Fs = s/(sˆ2-2 * s+1)

ft = t * exp(t)+exp(t)

Page 45: MATLAB Tutorial

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

F(s) =e−5s

s3−3s−2

MATLAB code

% find the inverse Laplace Transform of% exp(-5 * s)% F(s) = ----------% sˆ3-3s+-2

syms s t % make s and t symbolic variablesFs=exp(-5 * s)/(sˆ3-3 * s-2)ft=ilaplace(Fs)

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)

Page 46: MATLAB Tutorial

38 2. LAPLACE TRANSFORMS Index

2.3.5 Systems of Equations

Laplace Transforms can be used to solve systems of differential equations. By taking theLaplace Transform of a system of differential equations it is converted into an algebraicsystem in the transformed variable. These can be solved to get the transformed functionsand then inverted to find the solutions to the original system.

Example

Solve the system

dx1

dt= −x1 + x2

dx2

dt= 2x1

subject to the initial conditionsx1(0) = 0 andx2(0) = 1.

First take the Laplace Transform of both equations

(sX1− x1(0)) = −X1+X2

(sX2− x2(0)) = 2X1

Now apply the initial conditions and rearrange to get

(s+1)X1(s) = X2

sX2(s)−1 = 2X1

Solve to getX1(s) andX2(s). Substitute the first equation into the second forX2 to get

s(s+1)X1−1 = 2X1

Rearrange and factorise to give

(s2+ s−2)X1 = 1

X1 =1

(s+2)(s−1)

Substitute in to getX2

X2 = (s+1)X1

=s+1

(s+2)(s−1)

Note that MATLAB’s solve function could be used to solve the system forX1 andX2.Now use partial fractions then invert to get the solution to the original system as

X1 =13

1s−1

− 13

1s+2

⇒ x1(t) =13

et − 13

e−2t

X2 =23

1s−1

+13

1s+2

⇒ x2(t) =23

et +13

e−2t

Page 47: MATLAB Tutorial

Index INVERSION OF LAPLACE TRANSFORMS 39

Example

Solve the system

2dx1

dt+

dx2

dt−2x1 = 1

dx1

dt+

dx2

dt−3x1−3x2 = 2

subject to the initial conditionsx1(0) = 0 andx2(0) = 0.

First take the Laplace Transform of both equations

2(sX1− x1(0))+(sX2− x2(0))−2X1 =1s

(sX1− x1(0))+(sX2− x2(0))−3X1−3X2 =2s

Now apply the initial conditions and rearrange to get

(2s−2)X1(s)+ sX2(s) =1s

(s−3)X1(s)+(s−3)X2(s) =2s

Now solve this to getX1(s) andX2(s). Rearrange the first equation to get

X2 =1s − (2s−2)X1

s

Substitute into the second equation and simplify to findX1

(s−3)X1(s)+(s−3)1s − (2s−2)X1

s=

2s

s2(s−3)X1(s)+(s−3)(1− s(2s−2)X1) = 2s (multiply by s2)(

s2(s−3)− (s−3)s(2s−2))

X1(s) = 2s− s+3 (collectX1 terms)

(s−3)(−s+2)sX1 = s+3 (simplify)

X1 =−(s+3)

(s−3)(s−2)s

Substitute in to getX2

X2 =

1s − (2s−2)

(

−(s+3)(s−3)(s−2)s

)

s

=(s−3)(s−2)+(2s−2)(s+3)

s2(s−3)(s−2)

=3s−1

s(s−3)(s−2)

Page 48: MATLAB Tutorial

40 2. LAPLACE TRANSFORMS Index

Now use partial fractions to get

X1 =−12

1s

+52

1s−2

+−2

s−3

X2 =−16

1s

+−52

1s−2

+83

1s−3

Take the inverse Laplace Transform to get the solution to theoriginal system as

x1(t) =−12

+52

e2t −2e3t

x2(t) =−16

+−52

e2t +83

e3t

Page 49: MATLAB Tutorial

3 Initial Value Problems

3.1 Introduction

An Initial Value Problem (IVP ) is a differential equation ofnth order where the functionvalue and the firstn−1 derivatives are all specified atonegiven point known as the initialpoint.

For examplesy′ = 2y2 with y(1) = 3

y′′′+3y′′ +7y′−3y = 0 with y′′(2) = 2 y′(2) = −1 y(2) = 0.5

Also it can be asystemof first order differential equations with each function given avalue at thesamepoint.

For example

y′′′+3y′′ +7y′−3y = 0 with y′′(2) = 2 y′(2) = −1 y(2) = 0.5

can be written as

Page 50: MATLAB Tutorial

42 3. INITIAL VALUE PROBLEMS Index

A numerical solution to this system of equations overt = [2,6] using MATLAB is

MATLAB code

% system1.mtspan=[2,6] % note starts at t=2 nowy0=[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 legendplot(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.mf(1)=y(2);f(2)=y(3);f(3)=-3 * y(3)-7 * y(2)+3 * y(1);f=f(:);return

2 2.5 3 3.5 4 4.5 5 5.5 6−1

−0.5

0

0.5

1

1.5

2

2.5ydy/dtd2y/dt2

Page 51: MATLAB Tutorial

Index NUMERICAL APPROACH 43

3.2 Numerical approach

For an IVP because all the derivatives are known at the one given initial point a Taylor’sSeries can be used to expand about that point to get an approximation to the solution atneighbouring points. So for instance if you know the about the function at the pointathen information at a pointx is given by

f (x) = f (a)+ f ′(a)(x−a)+12!

f ′′(a)(x−a)2+ . . .

If you know something aboutf ′(a), f ′′(a),. . . then you can approximate the solution atthe pointx.

The process for solving IVPs numerically is to start at the initial point and step forwardusing information provided by the governing differential equation. This can be done in avariety of ways. The simplest is Euler’s method.

3.3 Euler’s method

(Revision from first year)

Euler’s method is the simplest (and least accurate) method for solving IVPs numerically.It consists of approximating the function by a straight lineat each point that you stepforward at. Recall the definition of the derivative as

dydx

≈ y(x+∆x)− y(x)∆x

If we are solving the IVP

dydx

= f (x,y) with y(x0) = y0

we start at the positionx0 and hencey(x0) = y0 is known then

dydx

(x0,y0) = f (x0,y0) ≈y(x0 +∆x)− y(x0)

∆x

which rearranging gives the solution at the next space step(x0+∆x) to be

y(x0 +∆x) = y(x0)+ f (x0,y0)∆x

This is then repeated treating the new point as known. This gives

yi+1 = yi + fi∆x

whereyi is the approximation at the pointxi = x0 + i∆x and fi = f (xi,yi).

Euler’s method is equivalent to approximating the solutionby using the first two terms inthe Taylor Series (the function value and the first derivative) and ignoring the higher orderterms. The first ignored term is

12!

f ′′(a)(x−a)2 =12!

f ′′(a)(∆x)2

so each step in the process has an error of the order of(∆x)2.

Page 52: MATLAB Tutorial

44 3. INITIAL VALUE PROBLEMS Index

3.3.1 Graphically

3.3.2 Errors and order of the method

Assuming we are calculating the solution fromx = a to x = b usingN steps then∆x =(b−a)/N. The error in each step is proportional to(∆x)2 as this is the first term neglectedin the Taylor Series so thentotal error over the entire range of solution is proportional to

N(∆x)2 = N∆x∆x = (b−a)∆x

Hence the total error is proportional to(∆x)1 so this is known as afirst 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 ofbeing more difficult to program.

First order methods are usually very slow if you want any reasonable amount of accuracy.Why is this?

Page 53: MATLAB Tutorial

Index EULER’S METHOD 45

Example

Solvey′ = y with y(0) = 1

over the rangex = [0,1]. This has exact solutiony = 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 useda=0; b=1; % endpoints of solutiondx=(b-a)/N; % step size% save x and y values in vectors so we can plot solutionx(1)=a; y(1)=1; % initial x and y valuesfor 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;

endplot(x,y,’b-’)axis([0 1 1 2.8])hold onexact=exp(x);plot(x,exact,’r--’);legend(’Euler’,’Exact’,2)hold offprint -depsc eulerexampleerror=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 error10 0.1 2.593742460100000.12453936835904100 0.01 2.704813829421530.013467999037521000 0.001 2.716923932235900.0013578962231510000 0.0001 2.718145926825230.00013590163356

What do you notice about how the error decreases as the step size decreases? How is thisrelated to the order of the method?

Page 54: MATLAB Tutorial

46 3. INITIAL VALUE PROBLEMS Index

Exact solution and Euler’s method withN = 10 andN = 100 applied to

y′ = y with y(0) = 1

0 0.2 0.4 0.6 0.8 11

1.2

1.4

1.6

1.8

2

2.2

2.4

2.6

2.8EulerExact

N = 10,∆x = 0.1

0 0.2 0.4 0.6 0.8 11

1.2

1.4

1.6

1.8

2

2.2

2.4

2.6

2.8EulerExact

N = 100,∆x = 0.01

3.3.3 Adjusting the step size

How do you know how accurate the solution is? When should you use a smaller step sizeto 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 sizeso that in areas wherethe solution changes rapidly we choose a small step size and in areas where it changesslowly we can take a larger step size so that the error is aboutthe same at each step. Thisis 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 thattakes account of how fast the solution is varying and the sizeof the errors involved.

Page 55: MATLAB Tutorial

Index EULER’S METHOD 47

Algorithm for adjusting the step size

1. Foreach step compare the result of using one step of size∆x with the result of usingtwo steps of size∆x

2 . Call the difference between the solutions∆y. If the methodyou are using is apth order method then the error from asinglestep is(∆x)p+1 so

∆y = y(xi +∆x)− y(xi +2∆x2

)

≈ k(∆x)p+1−(

k

(

∆x2

)p+1

+ k

(

∆x2

)p+1)

= k(∆x)p+1− k(∆x)p+1

2p

= k(∆x)p+1(

1− 12p

)

= 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

|∆y/y| = ε = k(δx)p+1 so δx =(ε

k

)1/(p+1)

in reality we used a step size∆x which had

|∆y/y| = k(∆x)p+1 so ∆x =

(

1k|∆y/y|

)1/(p+1)

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

R =δx∆x

=

(

εy∆y

)1/(p+1)

3. If R > 1 then step size is too small so it can be increased at thenext step.If R < 1 then step size is too big so it must be decreased at thecurrent step and theprocess repeated untilR > 1.Use the size of R as aguide to how much to increase or decrease the step size.For example let

∆xnew = mR∆xold

wherem is a tuning parameter usually taken to be a little less than one (egm = 0.95or thereabouts).

Page 56: MATLAB Tutorial

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 solutiondx(1)=0.1; % initial step size% save x,y and dx values in vectors so we can plot solutionx(1)=a; y(1)=a * exp(-a)+1; % initial x and y valuesi=1;while x(i) < b % keep going until you reach the end point

R=0;while R < 1

% calculate solution in one stepyone=y(i)+dx(i) * feval(’eulerf’,x(i),y(i));% calculate solution in two stepsyhalf=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);

endy(i+1)=ytwo; x(i+1)=x(i)+dx(i);dx(i+1)=dx(i); i=i+1;

endfor j=1:i

exact(j)=x(j) * exp(-x(j))+1;endplot(x,y,’ro’)hold onplot(x,exact,’b-’);legend(’Adaptive’,’Exact’,2)hold offprint -depsc euleradaptiveplot(x,dx,’ro’)print -depsc euleradaptivedx

MATLAB code

function fxy=eulerf(xx,yy)fxy=exp(-xx)-yy+1;return

Page 57: MATLAB Tutorial

Index EULER’S METHOD 49

Below are plots of theEuler methodand adaptive Euler method and also a plot showingthe step size used. Notice that the step size increases as thesolution flattens as larger stepscan be taken for the same error.

0 0.2 0.4 0.6 0.8 1 1.2 1.41

1.05

1.1

1.15

1.2

1.25

1.3

1.35

1.4AdaptiveExact

Exact, Euler and Adaptive Euler solution

0 0.2 0.4 0.6 0.8 1 1.2 1.40.03

0.035

0.04

0.045

0.05

0.055

0.06

0.065

0.07

0.075

0.08

Showing how the step size changes

Page 58: MATLAB Tutorial

50 3. INITIAL VALUE PROBLEMS Index

3.4 Improvements to Euler’s method - midpoint method

Instead of using the slope at the starting point use the slopeat the midpoint. This ingeneral will be a better approximation to the function.

Graphically

First estimate the value ofy at themidpoint usingEuler’s method

yi+1/2 = yi + f (xi,yi)∆x/2

The slope at the midpoint is then approximated byf (xi+1/2,yi+1/2) so use this in an Eulerlike formulation to get the formula

yi+1 = yi + f (xi+1/2,yi+1/2)∆x

This is known as themidpoint method.

It can be shown that the midpoint method is asecondorder method (as opposed to Euler’smethod that is first order) but this comes at the price of having to calculate twice thenumber of function evaluations (since we calculate the function at the midpoints as well).

How will this effect the methods efficiency? That is, how willthe extra function evalua-tions be offset by the improvement in the calculation as it isa higher order method?

Page 59: MATLAB Tutorial

Index IMPROVEMENTS TO EULER’S METHOD - MIDPOINT METHOD 51

Example

Solvey′ = xy with y(0) = 1

on [0,1]. This has exact solutiony = ex2/2.

0 0.2 0.4 0.6 0.8 11

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8EulerExactMidpoint

Midpoint method and Euler method withN = 10,∆x = 0.1

Below are the results of calculation with the Euler and Midpoint methods for various stepsizes. Compare the size of the errors for the same number of flops∗.

N ∆x error Euler flops∗ error Midpoint flops∗

Euler Midpoint10 0.1 0.1016108726901160 0.00257111404563120100 0.01 0.01090081247050600 0.0000273001679812001000 0.001 0.001098232499016000 0.000000274608271200020 0.05 0.05277960435958120 0.00066472539344240200 0.005 0.005472955870461200 0.000006847350892400

∗ A flop is a measure of how many operations (addition, multiplication) where necessary for thecalculation.

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 folddecrease in the error.

For the same amount of computing effort (flops) the Midpoint method is far superior toEuler’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).

Page 60: MATLAB Tutorial

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 pointsdx=(b-a)/N; % step sizex(1)=a; y(1)=1; % initial x, y(Euler) valuesz(1)=y(1); % initial z(Midpoint) valueflops(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;

endeulerflops=flopsflops(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;

endmidptflops=flopsplot(x,y,’b-’)hold onexact=exp(0.5. * x.ˆ2);plot(x,exact,’go’);plot(x,z,’r-’)legend(’Euler’,’Exact’,’Midpoint’,2)hold offprint -depsc midptexampleerroreuler=abs(exact(N+1)-y(N+1))errormidpt=abs(exact(N+1)-z(N+1))

MATLAB code

function f=midf(xx,yy)% midf.mf=xx * yy;return

Page 61: MATLAB Tutorial

Index SECOND ORDER RUNGE-KUTTA METHOD 53

3.5 Second order Runge-Kutta method

Named after 2 German Mathematicians, Carl David TolmeRunge(1856–1927) and Wil-helmKutta (1867–1944)

The previous methods (Euler, Midpoint) have all used some approximation to the slope.Euler: the slope at the left pointMidpoint: the slope at the midpoint.

The Runge-Kutta method uses aweighted average of the the slope of the left point andsome as yet unknown intermediate point. So the general formula is

yi+1 = yi +∆x favg

where favg is a weighted average given by

favg = a fi +b fi′

where fi = f (xi,yi) and fi′ = f (xi′,yi′) is the function evaluated at some undeterminedpoint given by

xi′ = xi +α∆x

yi′ = yi +β fi∆x

The method consists of finding the values of the weightsa, b and the position of theintermediate point given byα andβ .

Recall the Taylor Series expansion foryi+1 = y(xi +∆x) is

yi+1 = yi +∆xdydx

|i +(∆x)2

2d2ydx2 |i + . . .

= yi +∆x fi +(∆x)2

2d fdx

|i + . . .

= yi +∆x fi +(∆x)2

2

(

∂ f∂x

|i +∂ f∂y

dydx

|i)

+ . . .

= yi +∆x fi +(∆x)2

2

(

∂ f∂x

|i +∂ f∂y

f |i)

+ . . .

But expandingfi′ in a 2D Taylor Series gives

fi′ = f (xi′ ,yi′)

= f (xi +α∆x,yi +β fi∆x)

= f (xi,yi)+α∆x∂ f∂x

|i +β fi∆x∂ f∂y

|i + . . .

So the Runge-Kutta formulation gives

yi+1 = yi +∆x favg

= yi +∆x(a fi +b fi′)

Page 62: MATLAB Tutorial

54 3. INITIAL VALUE PROBLEMS Index

≈ yi +∆x

(

a fi +b

[

f (xi,yi)+α∆x∂ f∂x

|i +β fi∆x∂ f∂y

|i])

= yi +∆x

(

a fi +b fi +bα∆x∂ f∂x

|i +bβ fi∆x∂ f∂y

|i)

Comparing this with the Taylor Series expansion foryi+1 gives

a+b = 1 bα =12

bβ =12

Which is 3 equations in the 4 unknownsa, b, α andβ so there is still some degree offreedom of choice.

For example setting

a = 0 b = 1 α = β =12

gives themidpoint 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 ofa, b, α andβ that will makethe method third order. This method has been precoded in MATLAB and is used with acall to ode23

To solve the IVPdydt

= ode f unc(t,y) with y(a) = c

over the interval[a,b] a call toode23 would be likey0=[c];tspan=[a b];[t,y] = ode23(’odefunc’,tspan,y0);

Usehelp ode23 to find out more about the MATLAB command and how to use it. Inparticular 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.

Page 63: MATLAB Tutorial

Index SECOND ORDER RUNGE-KUTTA METHOD 55

Example

Solvedydt

= ty with y(0) = 1

on [0,1]. This has exact solutiony = et2/2.

MATLAB code

% o23example.m% solve dy/dt=odefunc(t,y) on [a,b] given y(a)=ca=0; b=1; c=1; % interval and initial valuey0=[c]; % initial conditiontspan=[a b]; % interval of integration[t,y]=ode23(’odefunc’,tspan,y0);% soln in vectors t and yplot(t,y)hold onexact=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

0 0.2 0.4 0.6 0.8 11

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8ode23Exact

Page 64: MATLAB Tutorial

56 3. INITIAL VALUE PROBLEMS Index

3.6 Fourth order Runge-Kutta method

In exactly the same way as the second order Runge-Kutta method was developed higherorder methods can be developed. By including more sampling points in the intervalgreater accuracy can be obtained. The second order method used the left-hand pointand one other point to determine an approximation to the solution. Hence two functionevaluations were required for each step. The Runge-Kutta fourth order method uses theleft-hand point and 3 other points to hence four function evaluations are required for eachstep. The method is fourth order accurate that is the total error is proportional to(∆x)4.

yi+1 = yi +16

(z1+2z2+2z3+ z4)

where

z1 = f (xi,yi)∆x

z2 = f (xi +12

∆x,yi +12

z1)∆x

z3 = f (xi +12

∆x,yi +12

z2)∆x

z4 = f (xi+1,yi + z3)∆x

The MATLAB command for using this method isode45 and is used exactly the sameway asode23 . The fourth order Runge-Kutta method is by far the most commonly usedmethod for solving IVPs.

Page 65: MATLAB Tutorial

Index ODE45 57

3.7 ode45

>> help ode45

ODE45 Solve non-stiff differential equations, medium orde r method.[T,Y] = ODE45(’F’,TSPAN,Y0) with TSPAN = [T0 TFINAL] integr atesthe system of differential equations y’ = F(t,y) from time T0to TFINAL with initial conditions Y0. ’F’ is a stringcontaining the name of an ODE file. Function F(T,Y) mustreturn a column vector. Each row in solution array Ycorresponds to a time returned in column vector T. To obtainsolutions at specific times T0, T1, ..., TFINAL (all increas ingor all decreasing), use TSPAN = [T0 T1 ... TFINAL].

[T,Y] = ODE45(’F’,TSPAN,Y0,OPTIONS) solves as above withdefault integration parameters replaced by values in OPTIO NS,an argument created with the ODESET function. See ODESET fordetails. Commonly used options are scalar relative errortolerance ’RelTol’ (1e-3 by default) and vector of absoluteerror tolerances ’AbsTol’ (all components 1e-6 by default) .

[T,Y] = ODE45(’F’,TSPAN,Y0,OPTIONS,P1,P2,...) passes th eadditional parameters P1,P2,... to the ODE file asF(T,Y,FLAG,P1,P2,...) (see ODEFILE). Use OPTIONS = [] asa place holder if no options are set.

It is possible to specify TSPAN, Y0 and OPTIONS in the ODEfile (see ODEFILE). If TSPAN or Y0 is empty, then ODE45 callsthe ODE file [TSPAN,Y0,OPTIONS] = F([],[],’init’) to obtai nany values not supplied in the ODE45 argument list. Emptyarguments at the end of the call list may be omitted, e.g.ODE45(’F’).

As an example, the commands

options = odeset(’RelTol’,1e-4,’AbsTol’,[1e-4 1e-4 1e-5 ]);ode45(’rigidode’,[0 12],[0 1 1],options);

solve the system y’ = rigidode(t,y) with relative errortolerance 1e-4 and absolute tolerances of 1e-4 for the firsttwo components and 1e-5 for the third. When called with nooutput arguments, as in this example, ODE45 calls the defaul toutput function ODEPLOT to plot the solution as it is compute d.

See also ODEFILE andother ODE solvers: ODE23, ODE113, ODE15S, ODE23S, ODE23Toptions handling: ODESET, ODEGEToutput functions: ODEPLOT, ODEPHAS2, ODEPHAS3, ODEPRINTodefile examples: ORBITODE, ORBT2ODE, RIGIDODE, VDPODE

Page 66: MATLAB Tutorial

58 3. INITIAL VALUE PROBLEMS Index

ExampleSolve

y′′ = t2cos(t)y s.t. y(0) = 2 andy′(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)=3a=0; b=5; % intervaly0=[2 3]; % initial conditiontspan=[a:.1:b]; % interval of integration[t,y]=ode23(’o45ex1func’,tspan,y0);% soln in vectors t a nd yplot(t,y(:,1),t,y(:,2),’r--’) % plot y1 and y2 versus tlegend(’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 DEsf(1)=y(2);f(2)=tˆ2. * y(1). * cos(t);f=f(:); % forces f to be a column vectorreturn

0 1 2 3 4 5−30

−20

−10

0

10

20

30

t

y an

d dy

/dt

ydy/dt

Page 67: MATLAB Tutorial

Index ODE45 59

The MATLAB functionode45 has a vast array of options that can be used.

Finding solutions at given points.

use tspan = [T0 T1 ... TFINAL]

egtspan=[0.1 0.2 0.3 0.4 0.5] will return solutions att = 0.1, 0.2 etc.

Error tolerances

There is an extra parameter that can be added to the parameterlist called ’options’. Thecall to ode45 is then

ode45(’func’,tspan,y0,options)

The options parameter is set using the commandodeset . 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 to1×10−4 for the first two components and 1×10−5 for the third. This is useful if youknow (or expect) that some of the variables will be substantially different is magnitudethan others.

Stopping the integration

It is possible to stop the integration when certain events occur. For example when any ofthe variables pass through a given value.

options = odeset(’Events’,’on’);

[t,y] = ode45(’func’,tspan,y0,options)

For this to work the file func.m must return appropriate information. func.m is coded sothat if ode45 passes it a flag with a value of ’events’ it determines the .m file that tells itinformation about the stopping criteria. If the flag is emptyit determines the .m file thatis actually used to calculate the integration (the DEs).

Page 68: MATLAB Tutorial

60 3. INITIAL VALUE PROBLEMS Index

3.8 Projectile example

Consider the problem of a projectile fired from a fixed position. If you know the initialvelocity 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. Asystem of differential that governs the motion of the projectile is given by

mdvdt

= −mgsinφ − 12

ρACDv2

mvdφdt

= −mgcosφ +12

ρACLv2

whereφ = angle from horizontal,v = velocity,m = mass.ρ = density,A = area,CD dragcoefficient,CL lift coefficient,g gravity.

The trajectory of the projectile is governed by

dxdt

= vcosφ

dydt

= vsinφ

If the initial position (x,y coordinates), initial velocity (v) and angle (φ ) are known thenthe trajectory of the projectile can be easily determined.

This is a system of 4nonlinear differential equations in the 4 unknownsx, y, v andφ .Solving these equations analytically is not possible but solving them numerically is veryeasy using MATLAB. Rearrange the equations into a standard form where the left handside is just the derivative of each variable.

dvdt

= −gsinφ − ρACDv2

2mdφdt

=−gcosφ

v+

ρACLv2m

dxdt

= vcosφ

dydt

= vsinφ

Then just numerically integrate these equations forward intime form the known initialposition, velocity and angle.

Page 69: MATLAB Tutorial

Index PROJECTILE EXAMPLE 61

MATLAB code

% This file is projectile.m% It calculates the trajectory of a projectile% with a predefined initial velocity and an% inputted angle (theta, in degrees).%% This file calls the other file% projectiledes.m the governing differential equations fo r% the velocity, angle, x and y%format compactv0=10; % initial velocitytheta=0;while theta >= 0

theta=input(’enter theta, use < 0 to stop...’)if theta<0

breakelse

% initial point [velocity angle x y]y0=[v0 theta * pi/180 0 0];% make the time span large enough that it gets to the targettspan=[0 1];[t,y]=ode45(’projectiledes’,tspan,y0);plot(y(:,3),y(:,4)) % plot the trajectoryhold on% determine how long the solution vector islgthy=length(y);range=y(lgthy,3) % the range is the last x value

endendhold offprint -deps projectile

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 ballg=9.8; rho=1.23; A=0.038; m=0.42; D=0.22; Cd=0.20; Cl=0.0 5;v=y(1); phi=y(2);f(1)=(-g. * sin(phi)-rho. * A. * Cd./(2.. * m). * v.ˆ2); % velocityf(2)=(-g. * cos(phi)./v + rho. * A. * Cl./(2. * m). * v);% anglef(3)=v. * cos(phi); % x coordinatef(4)=v. * sin(phi); % y coordinatef=f(:); % force f to be a column vectorreturn

Page 70: MATLAB Tutorial

62 3. INITIAL VALUE PROBLEMS Index

>> projectileenter theta, use < 0 to stop...30theta =

30range =

8.2272enter theta, use < 0 to stop...45theta =

45range =

6.7203enter theta, use < 0 to stop...60theta =

60range =

4.7443enter theta, use < 0 to stop...-1theta =

-1>> diary off

0 2 4 6 8 100

0.5

1

1.5

2

2.5

3

3.5

4

MATLAB can be set up to integrate the DE until a specific event occurs, for examplehere that might be wheny = 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).

Page 71: MATLAB Tutorial

4 Phase Plane Analysis

4.1 Introduction

Phase plane analysis is useful in determining the behaviourof solutions to systems of dif-ferential equations without necessarily solving the system analytically. More importantlythis 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 dependentvariable with the time depen-dence varying along the curves. This is known as aphase plane. The goal is to sketchenough solution curves so that the general behaviour of the system can be summarised bylooking at the graph.

The system

dx1

dt= x2

dx2

dt= −2x1− x2

has phase plane consisting of spirals converging to the origin.

−1 −0.5 0 0.5 1 1.5 2 2.5−2

−1

0

1

2

3

4

5

x1

x 2

Page 72: MATLAB Tutorial

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.mf(1)=y(2);f(2)=-2 * y(1)-y(2);f=f(:);return

Page 73: MATLAB Tutorial

Index INTRODUCTION 65

Or with multiple initial points put the whole thing into afor loop and update the initialpoints 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

endhold offprint -depsc ppexample2

−2 −1 0 1 2 3 4 5−4

−3

−2

−1

0

1

2

3

4

5

y1

y 2

Page 74: MATLAB Tutorial

66 4. PHASE PLANE ANALYSIS Index

4.1.1 Quiver plots

A very simple way in MATLAB to quickly get a feel for the solutions of a system of DEsis to draw the velocity field.

The MATLAB commandquiver is designed to do precisely this.

help quiver

QUIVER Quiver plot.QUIVER(X,Y,U,V) plots velocity vectors as arrows withcomponents (u,v) at the points (x,y). The matrices X,Y,U,Vmust all be the same size and contain corresponding positionand velocity components (X and Y can also be vectors tospecify a uniform grid). QUIVER automatically scales thearrows to fit within the grid.

QUIVER(U,V) plots velocity vectors at equally spaced point sin the x-y plane.

QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales thearrows to fit within the grid and then stretches them by S.Use S=0 to plot the arrows without the automatic scaling.

QUIVER(...,LINESPEC) uses the plot linestyle specified fo rthe velocity vectors. Any marker in LINESPEC is drawn atthe base instead of an arrow on the tip. Use a marker of’.’ to specify no marker at all. See PLOT for otherpossibilities.

QUIVER(...,’filled’) fills any markers specified.

H = QUIVER(...) returns a vector of line handles.

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 onquiver(x,y,px,py), hold off, axis image

See also FEATHER, QUIVER3, PLOT.

diary off

Page 75: MATLAB Tutorial

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 DEsdy=-2 * x-y; %quiver(x,y,dx,dy,’r’) % plot the vector fieldaxis square % make the plot squareaxis tight % make axis tight on datahold onw=[a-1 b+1];plot(w,0 * w,’k-’,0 * w,w,’k-’) % adds x and y axes to plotxlabel(’x’); ylabel(’y’);hold off% make an eps version of the plot called vfield.epsprint -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

−5 0 5

−5

−4

−3

−2

−1

0

1

2

3

4

5

x

y

Page 76: MATLAB Tutorial

68 4. PHASE PLANE ANALYSIS Index

4.2 Predator-Prey model

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). Lety1 be the number of predators andy2 be the number ofprey then the governing differential equations are

dy1

dt= −ay1+by1y2

dy2

dt= −cy1y2 +dy2

Here the terms are explained as

−ay1 If there is no prey (y2 = 0) the number of predators must decline.dy2 Number of prey increases if there are no predatorsby1y2 Number of predators increases if it meets (eats) a prey.−cy1y2 Number of prey decreases if it meets (and is eaten) by a predator.

Critical points

Set derivatives equal to zero and solve fory1 andy2.

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

J(y1,y2) =

[ ∂∂y1

(−ay1 +by1y2)∂

∂y2(−ay1 +by1y2)

∂∂y1

(−cy1y2+dy2)∂

∂y2(−cy1y2 +dy2)

]

=

[ −a+by2 by1

−cy2 −cy1 +d

]

So substituting each critical point gives

J(0,0) =

[

−a 00 d

]

Which is a diagonal matrix so the eigenvalues areλ1 = −a andλ2 = d hence(0,0) is asaddle.

J(d/c,a/b) =

[

0 bd/c

−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 speciesvary with time?

Page 77: MATLAB Tutorial

Index PREDATOR-PREY MODEL 69

MATLAB

MATLAB code

% predprey.m% Predator-Prey system%clear allglobal a b c d % variables global to all functionsa=0.1; b=0.002; c=0.0025; d=0.2;y0=[10 50]; % initial pointtspan=[0 120]; % range to integrate over[t,y]=ode45(’predpreyf’,tspan,y0);plot(t,y(:,1),’b-’) % plot predator popoluation vs timehold on % hold the plotplot(t,y(:,2),’r--’) % plot prey popoluation vs timelegend(’predator’,’prey’)hold off % turn off plot holdprint -depsc predprey % send plot to postscript fileplot(y(:,1),y(:,2),’g-’) % plot trajectories in (y1,y2)s paceprint -depsc predpreytraj% send plot to postscript file

MATLAB code

function f=predpreyf(t,y)% predpreyf.mglobal a b c df(1)=-a * y(1) + b * y(1) * y(2);f(2)=-c * y(1) * y(2) + d * y(2);f=f(:);return

0 20 40 60 80 100 1200

50

100

150

200

250

300predatorprey

Time plot

0 50 100 150 200 250 3000

50

100

150

200

250

300

A trajectory in(y1,y2) space

Page 78: MATLAB Tutorial

70 4. PHASE PLANE ANALYSIS Index

4.3 Competition model

Competition models are used to model the competition between two (or more) species(companies, people) who are competing for the same resource.

dy1

dt=

r1

K1y1(K1− y1−ay2)

dy2

dt=

r2

K2y2(K2− y2−by1)

If there is no second species (y2 = 0) then the first population grows until it approachesthe steady populationK1. Similarly for the other species.

Critical points

Set derivatives equal to zero and solve fory1 andy2.

(y1,y2) = (0,0)(y1,y2) = (K1,0)(y1,y2) = (0,K2)and a fourth point is located at the intersection of the two linesK1− y1− ay2 = 0 andK2− y2−by1 = 0.

Look at the Jacobian to classify each critical point.

MATLAB code

% compjacobian.m%format compactclear alla=0.75; b=3.0; r1=0.2; r2=0.1; K1=50; K2=100;

% store critical points in a vectory1=[0 K1 0 20];y2=[0 0 K2 40];for i=1:4

fprintf(’\nFor critical point (%5.2f %5.2f)\n’,y1(i),y2 (i))Jac=[r1-2 * r1 * y1(i)/K1-a * r1/K1 * y2(i) -a * r1 * y1(i)/K1;...

-b * r2/K2 * y2(i) r2-r2 * 2* y2(i)/K2-r2 * b* y1(i)/K2][V,D]=eig(Jac)

end

Page 79: MATLAB Tutorial

Index COMPETITION MODEL 71

>> compjacobian

For critical point ( 0.00 0.00)Jac =

0.2000 00 0.1000

V =1 00 1

D =0.2000 0

0 0.1000

For critical point (50.00 0.00)Jac =

-0.2000 -0.15000 -0.0500

V =1.0000 -0.7071

0 0.7071D =

-0.2000 00 -0.0500

For critical point ( 0.00 100.00)Jac =

-0.1000 0-0.3000 -0.1000

V =0 0.0000

1.0000 1.0000D =

-0.1000 00 -0.1000

For critical point (20.00 40.00)Jac =

-0.0800 -0.0600-0.1200 -0.0400

V =-0.6661 0.4885-0.7458 -0.8726

D =-0.1472 0

0 0.0272>> diary off

Page 80: MATLAB Tutorial

72 4. PHASE PLANE ANALYSIS Index

How does the population of both the species vary with time? Does one species dominatethe other?

MATLAB code

% competition.m%clear allglobal a b r1 r2 K1 K2 % variables global to all functionsa=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:1y0=[j * 50+5 i * 10+5]; % initial pointstspan=[0 200]; % range to integrate over[t,y]=ode45(’competitionf’,tspan,y0);plot(y(:,1),y(:,2)) % plot trajectories in (y1,y2)spacehold on

endendprint -depsc competitiontraj % send plot to postscript filehold off % turn off plot hold

MATLAB code

function f=competitionf(t,y)% competitionf.mglobal a b r1 r2 K1 K2 % variables global to all functionsf(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

0 10 20 30 40 50 600

20

40

60

80

100

120

Page 81: MATLAB Tutorial

Index SIR DISEASE MODEL 73

4.4 SIR disease model

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 percentageof the population need to be inoculated to prevent the spreadof a disease. Or simply as aguide to how widespread a disease will become if left alone.

Consider a fixed population of size K. A fixed population size is a reasonable assumptionif the birth rate is approximately the same as the death rate and the disease does notactually kill the person. For instance measles in the developed world is rarely a fataldisease 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 diseaseInfectives (I): those people who have the disease and can pass it onRecovered (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 totalpopulation 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 notto 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 chanceof catching the disease is proportional (with constantβ ) to the product of the number ofsusceptibles and infectives. The DE that models the susceptible population is therefore

dSdt

= −βSI + µK −µS

contact birth death

Consider how the population of infectives can change.They can be a susceptibles who becomes infectives (as described above), they can recover(with rateγ) or they can die (with rateµ as described above). The DE that models theinfective population is therefore

dIdt

= βSI− γI −µI

contact recover death

Page 82: MATLAB Tutorial

74 4. PHASE PLANE ANALYSIS Index

Consider how the population of recovereds can change.They are infectives who have recovered, or they can die. The DE that models the recov-ered population is therefore

dRdt

= γI −µR

recovered death

Note that the recovered equation does not effect the susceptible or infective equationsand in fact since the population is assumed constant the number of recovereds can alwaysbe determined from the number of susceptibles and the numberof infectives. This lastequation is not needed in the model.

So we have two first oder nonlinear differential equations intwo unknowns (S andI).

dSdt

= −βSI + µK −µS

dIdt

= βSI− γI −µI

These are nonlinear equations hence we need to calculate thecritical points and analysethe behaviour near those critical points.

Critical points

Set derivatives equal to zero and solve forS andI.

0 = −βSI + µK −µS

0 = βSI− γI −µI

Page 83: MATLAB Tutorial

Index SIR DISEASE MODEL 75

Look at the Jacobian to classify each critical point.

J(S, I) =

[ ∂∂S (−βSI + µK −µS) ∂

∂ I (−βSI + µK −µS)

∂∂S (βSI− γI −µI) ∂

∂ I (βSI − γI −µI)

]

=

[

−β I −µ −βS

β I βS− γ −µ

]

So for critical point(S, I) = (K,0)

J(K,0) =

[

−µ −βK0 βK − γ −µ

]

Which is a triangular matrix so the eigenvalues areλ = −µ < 0 and λ = βK − γ −µ

If βK − γ − µ < 0 then the critical point(S, I) = (K,0) is a stable node and hence theS → 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) isnot the final state. So what is the final state in his case? Presumably it must be the othercritical point since the model is bounded. Why is the model bounded?

ExampleConsider the case withK = 100,β = 0.001,µ = 0.02,γ = 0.03

Page 84: MATLAB Tutorial

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 J acobian%global beta K mu gamma % makes these global variablesformat compactbeta=0.001; K=100; mu=0.02; gamma=0.03;%% use quiver to plot the vector fielda=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 DEsdI=beta * S. * I-gamma * I-mu * I; %quiver(S,I,dS,dI,2.5,’r’) % plot vector field scaled by 2. 5axis square % make the plot squareaxis([a b a b]) % restrict the axes to be a to bhold onw=[a b];plot(w,0 * w,’k-’,0 * w,w,’k-’) % adds x and y axes to plotxlabel(’Susceptibles’); ylabel(’Infectives’); title(’ SIR model’)%% now add some trajectories by numerically solving the DEtspan=[0 250];for icS=0:100:100

for icI=5:10:95initcond=[icS icI];[t,y]=ode45(’sirfunc’,tspan,initcond);plot(y(:,1),y(:,2),’b-’)

endendhold offprint -depsc sirtraj%% now set up the critical points and calculate the Jacobian% and the eigenvalues and eigenvectors so you can classify th e% critical pointscpS=[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 modelglobal beta K mu gammaS=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

Page 85: MATLAB Tutorial

Index SIR DISEASE MODEL 77

sir

Critical point number 1 is (100.00, 0.00)Jacobian =

-0.0200 -0.10000 0.0500

Jeigvec =1.0000 -0.8192

0 0.5735Jeigval =

-0.0200 00 0.0500

Critical point number 2 is (50.00,20.00)Jacobian =

-0.0400 -0.05000.0200 0.0000

Jeigvec =0.8452 0.8452

-0.3381 - 0.4140i -0.3381 + 0.4140iJeigval =

-0.0200 + 0.0245i 00 -0.0200 - 0.0245i

diary off

0 20 40 60 80 1000

10

20

30

40

50

60

70

80

90

100

Susceptibles

Infe

ctiv

es

SIR model

Page 86: MATLAB Tutorial

78 4. PHASE PLANE ANALYSIS Index

Page 87: MATLAB Tutorial

5 Boundary Value Problems

5.1 Introduction

In contrast to Initial Value Problems where all function values and derivatives are given atone pointBoundary Value Problems (BVP)have function values and derivatives at twopoints. Hence it is not possible to construct a Taylor Seriesabout one point and use thisto build up the solution as was done with IVPs.

Examples

y′′+3y′ +2y = x where y(a) = 1 y(b) = 3

y′′′′+ y′′ +2y = sinx where y(a) = 1 y′(a) = 3 y′′(a) = 6 y(b) = 3

BVPs do not necessarily have unique solutions . For example

y′′+ω2y = 0 where y(0) = 0 y(π) = 0

There are two general methods for solving BVPs numerically,shooting methodandre-laxation method.

Page 88: MATLAB Tutorial

80 5. BOUNDARY VALUE PROBLEMS Index

5.2 Shooting method

5.2.1 Shooting method using guesses

Example

How might you go about numerically solving

y′′+3y′ +2y = 2x where y(0) = 1 y(2) = 3

1. Guess a value fory′(0). Now the problem is an IVP.

2. Integrate forward tox = 2 using any of the previous methods (Euler, midpoint, R-K,ode45, . . . )

3. If |y(2)−3|> ε make a new guess and repeat from 2.

MATLAB code

% shootingexample.m% trying to solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3clear allc=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 loopy0=[1 c]; % initial condition [y(0) y’(0)][t,y]=ode45(’shootingexamplef’,tspan,y0);plot(t,y(:,1)) % plot current solutionhold on % keep the plotlgthy=length(y); % find the length of the vector ydisp([y(lgthy,1)]) % show y(x=2) valuecc=num2str(c); % convert value of c to a string variabletext(2.1,y(lgthy,1),cc) % write value of c on rhs of graph

endtext(2.1,5,’c guess’) % write c guess on the plot at (2.1,5)hold offprint -deps shootingexample

MATLAB code

function f=shootingexamplef(t,y)% shootingexamplef.m% the DE y’’ + 3y’ + 2y = 2x written as a systemf(1)=y(2);f(2)=-3 * y(2)-2 * y(1)+2 * t;f=f(:);return

Page 89: MATLAB Tutorial

Index SHOOTING METHOD 81

0 0.5 1 1.5 21

1.5

2

2.5

3

3.5

4

4.5

5

5.5

6

10

20

18

16

c guess

Hence there is a solution that hasy(2) = 3 somewhere betweeny′(0) = 16 andy′(0) = 18.

How do you go about finding this value ofy′(0) that gives the correct value ofy(2) ?

Treat the problem as a zero finding problem. That is find the value ofy′(0) = c that resultsin f (c) = y(2)−3 = 0. So use one of MATLABs methods for finding zeros of functions.

Page 90: MATLAB Tutorial

82 5. BOUNDARY VALUE PROBLEMS Index

5.2.2 Shooting method using fzero

So back to the problem of solving

y′′+3y′ +2y = 2x where y(0) = 1 y(2) = 3

Set it up so that there is a MATLAB function that takes as inputthe guess for the initialslope and returns the error in the right hand boundary condition. Then call this functionfrom fzero to find the correct slope that integrates to the correct righthand boundarycondition.

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 od e45%myguess=4; % a guess at the value of y’(0)actualvalue=fzero(’shootingfunc’,myguess)% use ode45 to get correct solution using actualvalue and plo ttspan=[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)=ctspan=[0 2]; y0=[1 c];[t,y]=ode45(’shootingexamplef’,tspan,y0);f=y(length(y),1)-3; % set function value to y(2)-3return

MATLAB code

function f=shootingexamplef(t,y)% shootingexamplef.m% the DE y’’ + 3y’ + 2y = 2x written as a systemf(1)=y(2);f(2)=-3 * y(2)-2 * y(1)+2 * t;f=f(:);return

Page 91: MATLAB Tutorial

Index SHOOTING METHOD 83

>> shootingexamplemain

actualvalue = 16.9727

0 0.5 1 1.5 21

1.5

2

2.5

3

3.5

4

4.5

5

5.5

Page 92: MATLAB Tutorial

84 5. BOUNDARY VALUE PROBLEMS Index

5.3 Relaxation methods

Relaxation methods differ substantially from the previousshooting methods. The func-tion is not integrated but rather an approximate solution ismade that fits the governingdifferential equation. This results in a system of equations where the unknowns are thevalues 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.

5.3.1 Finite differences

Finite differences is the procedure where we replace any derivatives by afinite differenceapproximation. For example recall the definition of the derivative

dydx

≈ y(x+∆x)− y(x)∆x

alternatively this could be written

dydx

n≈ yn+1− yn

xn+1− xn

=yn+1− yn

h

whereyn = y(xn) andh is the difference inx steps or just the space betweenx points. Thisis known as aforward approximation to the first derivative as it uses the current point(n) and the next point (n + 1). There is also abackward approximation that uses thecurrent point (n) and the previous point (n−1). This is given by

dydx

n≈ yn − yn−1

h

Both the forward and backward approximations arefirst order accurate, that is the firstneglected term is order∆x.

There is in fact a second order accurate approximation knownas thecentral approxima-tion given by

dydx

n≈ yn+1− yn−1

2h

These can be used to find approximations to the higher derivatives.

d2ydx2

n≈ yn+1−2yn + yn−1

h2

This is the central approximation to the second derivative and is second order accurate.

Page 93: MATLAB Tutorial

Index RELAXATION METHODS 85

5.3.2 Using finite differenes to solve a BVP

Finite differences can be used to solve a BVP. What results issimply solving a system oflinear equations which is simple in MATLAB.

Algorithm

1. Divide up the region into N equal subintervals. Note that the differential equationmust 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 ofthe 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 gridpoint.

Example

Numerically solve

y′′+3y′ +2y = 2x where y(0) = 1 y(2) = 3

1. First divide up the region intoN subintervals of widthh where

h =b−a

Nso

xn = a+hn for n = 0. . .N

2. Replace each derivative by its approximate value at eachinterior point.

yn+1−2yn + yn−1

h2 + 3yn+1− yn−1

2h+ 2yn ≈ 2xn for n = 1. . .N −1

This results inN −1 equations in theN −1 unknownsy1, y2, . . .yN−1.

3. The values at the edge points are known (y0 = y(a) = 1, yN = y(b) = 3 in thiscase) The equations forn = 1 andn = N − 1 are slightly different to the others(n = 2, . . .N −2) as they involvey0 andYN respectively which are known as theyare just the boundary values.

y2−2y1+ y0

h2 + 3y2− y0

2h+ 2y1 ≈ 2x1 for n = 1

yn+1−2yn + yn−1

h2 + 3yn+1− yn−1

2h+ 2yn ≈ 2xn for n = 2. . .N −2

yN −2yN−1 + yN−2

h2 + 3yN − yN−2

2h+ 2yN−1 ≈ 2xN−1 for n = N −1

Page 94: MATLAB Tutorial

86 5. BOUNDARY VALUE PROBLEMS Index

4. Mutiply by h2 to simplify, collect all terms with the same unknown value and writ-ten out in full we have

(1−3h/2)y0+(2h2−2)y1+(1+3h/2)y2 = 2h2x1

(1−3h/2)y1+(2h2−2)y2+(1+3h/2)y3 = 2h2x2

(1−3h/2)y2+(2h2−2)y3+(1+3h/2)y4 = 2h2x3

......

...

(1−3h/2)yN−3+(2h2−2)yN−2+(1+3h/2)yN−1 = 2h2xN−2

(1−3h/2)yN−2+(2h2−2)yN−1 +(1+3h/2)yN = 2h2xN−1

Now y0 andyN are known (since they are the boundary conditions) so put them onthe right hand side with all the other known parts gives

(2h2−2)y1 +(1+3h/2)y2 = 2h2x1− (1−3h/2)y0

(1−3h/2)yn−1+(2h2−2)yn +(1+3h/2)yn+1 = 2h2xn for n = 2. . .N −2

(1−3h/2)yN−2+(2h2−2)yN−1 = 2h2xN−1− (1+3h/2)yN

5. This is a system ofN −1 equations in theN −1 unknownsy1, y2, . . .yN−1. Sub-stituting the known end points (y0 = 1 andyN = 3) and writing in matrix form thiscan be easily solved in MATLAB using any number of solution methods dependingon the size of the matrix (direct inversion, iterations etc.). In matrix form it can bewritten

AY = b

where the matrixA has coefficients as above,Y is the vectors of unknownsY = (y1,y2, . . . ,yN−1)

T andb is the known right hand side vector.

Page 95: MATLAB Tutorial

Index RELAXATION METHODS 87

A =

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

0 0 1−3h/2 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

b =

2h2x1− (1−3h/2)y0

2h2x2

2h2x3

2h2x4

...

2h2xN−2

2h2xN−1− (1+3h/2)yN

What do you notice about the matrixA? You will see that it is ’tri-diagonal’ (only3 diagonals are non-zero) and each diagonal has the same value. This makes itparticularly easy to program in MATLAB using thediag command.

Once you have codedA, andb it is a simple matter to solve the systemAY = b toget the solutionY . This is then the approximation to the solution of the originalODE.

Coding the diagonal matrix A is relatively easy in MATLAB as there is a commanddiag that is used to enter values into a diagonal matrix. Theones command

is also useful for making vectors of a certain length with thesame value in eachposition.

Solving the systemAY = b is easy using the ’backslash divide’ commandy=A\b .

Page 96: MATLAB Tutorial

88 5. BOUNDARY VALUE PROBLEMS Index

>> help diag

DIAG Diagonal matrices and diagonals of a matrix.DIAG(V,K) when V is a vector with N components is a squarematrix of order N+ABS(K) with the elements of V on the K-thdiagonal. K = 0 is the main diagonal, K > 0 is above the maindiagonal and K < 0 is below the main diagonal.

DIAG(V) is the same as DIAG(V,0) and puts V on the maindiagonal.

DIAG(X,K) when X is a matrix is a column vector formed fromthe elements of the K-th diagonal of X.

DIAG(X) is the main diagonal of X. DIAG(DIAG(X)) is a diagona lmatrix.

Examplem = 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.

See also SPDIAGS, TRIU, TRIL.

Overloaded methodshelp sym/diag.m

>> diary off

>> help ones

ONES Ones array.ONES(N) is an N-by-N matrix of ones.ONES(M,N) or ONES([M,N]) is an M-by-N matrix of ones.ONES(M,N,P,...) or ONES([M N P ...]) is anM-by-N-by-P-by-... array of ones.ONES(SIZE(A)) is the same size as A and all ones.

See also ZEROS.

>> diary off

Page 97: MATLAB Tutorial

Index RELAXATION METHODS 89

MATLAB code

% fdexample.m%% solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3% using finite differencesa=0; b=2; % endpointsy0=1; yN=3; % y values at endpointsN=100; % number of pointsh=(b-a)/N; % x step sizex=a+h:h:b-h; % set up vector of x points (interior points)% set up the matrix and solve Ay=RHS for yA=(2 * hˆ2-2) * diag(ones(1,N-1)); % main diagonal elementsA=A+(1-3 * h/2) * diag(ones(1,N-2),-1); % one below main diagonalA=A+(1+3 * h/2) * diag(ones(1,N-2),1); % one above main diagonalRHS=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)

0 0.5 1 1.5 21

1.5

2

2.5

3

3.5

4

4.5

5

5.5

Page 98: MATLAB Tutorial

90 5. BOUNDARY VALUE PROBLEMS Index

Example

Solvey′′+5y′ +6y = cosx where y(0) = 2 y(3) = 6

using finite differences.

Page 99: MATLAB Tutorial

Index RELAXATION METHODS 91

MATLAB code

% fdexample2.m%% solve y’’ + 5y’ + 6y = cos x y(0)=2 y(3)=6% using finite differencesa=0; b=3; % endpointsya=2; yb=6; % y values at endpointsN=100; % number of pointsh=(b-a)/N; % x step sizex=a+h:h:b-h; % set up vector of x points (interior points)% set up the matrix and solve Ay=RHS for yA=(6 * hˆ2-2) * diag(ones(1,N-1)); % main diagonal elementsA=A+(1-5 * h/2) * diag(ones(1,N-2),-1); % one below main diagonalA=A+(1+5 * h/2) * diag(ones(1,N-2),1); % one above main diagonalRHS=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

0.5 1 1.5 2 2.5−20

0

20

40

60

80

100

120

140

160

N = 20

0 0.5 1 1.5 2 2.5 30

50

100

150

200

250

300

350

400

N = 10

0 0.5 1 1.5 2 2.5 30

50

100

150

200

250

300

350

400

N = 100

0 0.5 1 1.5 2 2.5 30

50

100

150

200

250

300

350

400

Page 100: MATLAB Tutorial

92 5. BOUNDARY VALUE PROBLEMS Index

5.3.3 Comments on solving matrix equations

There are many numerical problems with solving the matrix equationAx = b

The matrix equations that result from the finite difference method aretridiagonal . That isthey have entries down the 3 central diagonals and zeros elsewhere. These are relativelyefficient to solveAx = b for.

Suggested Solution Methods

1. InverseThis can be very slow for large matrices.What if inverse doesn’t exist?

2. IterationMake a guess and use the equations to refine that guess until some convergencecriteria is met.Usually the best method for large matrices (bigger than 25×25).Most common method is known as the Gauss-Seidel Iteration method

3. Row reduction then back-substitution

Page 101: MATLAB Tutorial

Index RELAXATION METHODS 93

5.3.4 Finite elements

Finite differences are only one of the many method of solvingBVPs. Another very pop-ular method is known as thefinite element method. The concept underlying finite dif-ference methods is that approximations are made to the derivatives in the differentialequation. In contrast to this for finite element methods the solution is approximated by asequence 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 hashomogeneousboundary conditions. That is the value of the function at theend points is zero. Fortunatelywe can make a change of variable to ensure this. For example consider the problem

y′′ + p(x)y′ +q(x) = r(x) y(xL) = yL y(xR) = yR

we can transform this to a problem with homogeneous boundaryconditions by the trans-formation

y(x) = Y (x)+ax+b

Which can be shown to give

a =1

xL − xR(yL − yR) b =

1xL − xR

(−xRyL + xLyR)

Example

What doesy′′+3y′ +2y = 5x2 y(0) = 2 y(2) = 3

become when it is transformed to have homogeneous boundary conditions

Page 102: MATLAB Tutorial

94 5. BOUNDARY VALUE PROBLEMS Index

5.3.5 Symbolic form of a BVP

Consider an ordinary differential equation with homogeneous boundary conditions thiscan be written in sysmbolic form as

L y = r(x) y(xL) = 0 y(xR) = 0

whereL is a sysmbol that represents all the differential operatorsin the differential equa-tion.

Example

The ODEy′′+3x2y′′+2exy = 5x2

can be represented as

L y = 5x2 where L ≡ d2

dx2 +3x2 ddx

+2ex

5.3.6 Finite element theory

For the BVP given by

L y = r(x) y(xL) = 0 y(xR) = 0

Algorithm

1. Select a set ofn independent expansion functions. Call theseφi(x) for i = 1, . . . ,n.The exact form of these functions will be specified later. Thecrux of the finiteelement method is that the approximate solution (denotedY (x)) is written as somelinear combination of these expansion functions, namely

y(x) ≈Y (x) =n

∑i=1

ciφi(x)

The aim is to find the coefficientsci to get the best solution possible.

2. Construct what is known as a residual function (denoted∆(x)) which is a measureof the error of the approximate solution

∆(x) = LY (x)− r(x)

= L

n

∑i=1

ciφi(x)− r(x)

=n

∑i=1

ciL φi(x)− r(x)

Page 103: MATLAB Tutorial

Index RELAXATION METHODS 95

3. Establish a set of conditions that allow us to find the coefficientsci. To do thischoose a set of weight functionswi(x) for i = 1, . . . ,n. Again these are as yet un-specified. These weight functions have the following properties

(a) They are normalised so that∫ xR

xL

w j(x)dx = 1 for j = 1, . . . ,n

(b) The weighted residual for each fucntion is zero∫ xR

xL

w j(x)∆(x)dx = 0 for j = 1, . . . ,n

4. It is this last condition 3(b) that gives conditions that enable the coefficientsci to befound. By substituting the definition of∆(x) above into this expression we get foreachj = 1, . . . ,n

∫ xR

xL

w j(x)∆(x)dx = 0

∫ xR

xL

w j(x)

[

n

∑i=1

ciL φi(x)− r(x)

]

dx = 0

∫ xR

xL

w j(x)n

∑i=1

ciL φi(x)dx−∫ xR

xL

w j(x)r(x)dx = 0

n

∑i=1

ci

∫ xR

xL

w j(x)L φi(x)dx =∫ xR

xL

w j(x)r(x)dx

If the weight functions (w j(x)) and the expansion functions (φ j(x)) are specifiedthen the right hand side of the last expression is known.

b j =

∫ xR

xL

w j(x)r(x)dx

and the part∫ xR

xL

w j(x)L φi(x)dx = M ji

is also known. Hence the equation reduces to

n

∑i=1

ciM ji = b j for j = 1, . . . ,n

This is just the matrix equationMc = b

So providedw j(x) andφ j(x) are specified then the coefficients to use as the linearcombination of the expansion functions to give the approximate solution are givenby solvingMc = b. This gives theci in

y(x) ≈Y (x) =n

∑i=1

ciφi(x)

and so the approximate solution has been found.

Page 104: MATLAB Tutorial

96 5. BOUNDARY VALUE PROBLEMS Index

Choice of weight and expansion functions

There are many choices that can be made for the weight functions (w j(x)) and theexpansion functions (φ j(x)). Different choices give different types of methods. Byfar the most common and what has come to be known as the collocation methodis to choose them equal, namelyw j(x) = φ j(x). Thefinite element method uses“triangular hat” functions for both the weight functions (wi(x)) and the expansionfunctions (φi(x)).

φi(x) =

1h (x− xi−1) for xi−1 < x < xi

1h (xi+1− x) for xi < x < xi+1

0 otherwise

These functions are triangles that span 3 grid points and arezero everwhere else.For exampleφ3 is a triangle that begins at zero atx2 increases to a maximum atx3and then decreases to zero atx4.

x0 x1 x2 x3 x4 x5 x6

φ3

The benefit of these functions is that they arelocal. That it is only nonzero in theimmediate neighbourhood of the central point. This guarantees that the resultingmatrix eqaution will be sparse (have many zero entries) and hence can be solvedeffeciently using iterative methods.

These triangular hat functions are useful as they have some special properties thatmake calculating theM matrix easier.

F(x)∫ xR

xL

φiF(x) dx

φ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 hx ih2

x2 (i2+1/6)h3

x3 i(i2+1/2)h4

Page 105: MATLAB Tutorial

Index RELAXATION METHODS 97

5.3.7 Finite element example

Solvey′′+ y = 1 y(0) = 0 y(π/2) = 0

Now L ≡ d2

dx2 +1 so to calculateM ji break it down into each term such that

M ji =∫ π/2

0φ j(x)L φi(x)dx

=∫ π/2

0φ j(x)

[

d2

dx2 +1

]

φi(x)dx

=

∫ π/2

0φ j(x)

d2φi(x)dx2 dx+

∫ π/2

0φ j(x)φi(x)dx

and so from the table of special properties of the triangularfunctions

M =

−2/h 1/h 0 . . . 0 01/h −2/h 1/h . . . 0 0

......

.... . .

......

0 0 0 . . . −2/h 1/h0 0 0 . . . 1/h −2/h

+

2h/3 h/6 0 . . . 0 0h/6 2h/3 h/6 . . . 0 0

......

.... . .

......

0 0 0 . . . 2h/3 h/60 0 0 . . . h/6 2h/3

Calculating the right hand side gives

b j =∫ π/2

0w j(x)r(x)dx

=∫ π/2

0φ j(x) 1 dx

and so from the tables

b =

hhh...hh

Now find the coefficientsci in

y(x) ≈Y (x) =n

∑i=1

ciφi(x)

by solving the matrix equationMc = b

with M andb above.

Page 106: MATLAB Tutorial

98 5. BOUNDARY VALUE PROBLEMS Index

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 allglobal a b Na=0; b=pi/2; N=10; % N interior pointsh=(b-a)/(N+1);xk=a+h:h:b-h; % interior points% set up and solve Mc=RHSM=(-2+2/3 * hˆ2) * diag(ones(1,N)); % main diagonal elementsM=M+(1+hˆ2/6) * diag(ones(1,N-1),-1); % one below main diagonalM=M+(1+hˆ2/6) * diag(ones(1,N-1),1); % one above main diagonalM=M/h; % multiplicative factorRHS=h* ones(N,1); % right hand side vectorc=M\RHS % solve for c the coefficients% plot numerical solution and exact solution and calculate e rrornpts=26;x=a:(b-a)/(npts-1):b; % vector to calculate solution at% call feexamplef.m which is a function that calculates the s olution% 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’ functionsfor k=1:npts

y(k)=feval(’feexamplef’,c,x(k));endplot(x,y)hold onexact=-sin(x)-cos(x)+1;plot(x,exact,’ro’)ylabel(’exact and numerical’)error=abs(exact-y);maxerror=max(error)hold off

Page 107: MATLAB Tutorial

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 centere d around x_i%global a b Nh=(b-a)/(N+1);xk=a+h:h:b-h;if (x <= xk(1)) % left most function

f=c(1) * (x-a)/h;endfor 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;

endendif (x >= xk(N)) % right most function

f=c(N) * (b-x)/h;endreturn

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6−0.45

−0.4

−0.35

−0.3

−0.25

−0.2

−0.15

−0.1

−0.05

0

Page 108: MATLAB Tutorial

100 5. BOUNDARY VALUE PROBLEMS Index

5.3.8 Finite element summary

For the BVPL y = r(x) y(xL) = 0 y(xR) = 0

1. Choosen expansion functionsφi(x) for i = 1,2, . . . ,n.

2. Choosen weight functionswi(x) for i = 1,2, . . . ,n.

3. Let

wi(x) = φi(x) =

1h (x− xi−1) for xi−1 < x < xi

1h (xi+1− x) for xi < x < xi+1

0 otherwise

4. Numerical solutionY (x) =n

∑i=1

ciφi(x)

5. SolveMc = b for theci’s where

M ji =

∫ x f

x0

w j(x)L φi(x) dx

b j =∫ x f

x0

w j(x)r(x) dx

6. Properties of triangular functions

F(x)∫ x f

x0

φiF(x) dx

φ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 hx ih2

x2 (i2+1/6)h3

x3 i(i2+1/2)h4

Page 109: MATLAB Tutorial

Index DIFFERENTIAL EIGENVALUE EQUATIONS 101

5.4 Differential eigenvalue equations

Recall the example

y′′+ω2y = 0 where y(0) = 0 y(π) = 0

which has solution many solutions each one of which is

yn = An sinωnx

whereAn is an unknown constant andωn = n = 0,±1,±2, . . .

This equation is known as aDifferential Eigenvalue Equation as there are only particularvalues (eigenvalues) for which a solution exists.

How would you solve this numerically given you don’t know theeigenvaluesω ?

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ωdx

= 0

but then we also need an extra boundary condition.

ω(0) = ω0

whereω0 is the eigenvalue we need to find. So puttingy = y1, dy/dx = y2 andω = y3 thesystem to date is

dy1

dx= y2

dy2

dx= −(y3)

2y1

dy3

dx= 0

subject toy1(0) = 0 y1(π) = 0 y3(0) = ω0

Note that now that the system is nonlinear (the second equation).

How do you solve this numerically we have 3 boundary conditions but one of them isunknown (ω0) and one of them is atx = π notx = 0. To be able to integrate forward fromx = 0 we need three initial conditions atx = 0 and we also need three known conditionsin total for the problem to have a unique solution.

Page 110: MATLAB Tutorial

102 5. BOUNDARY VALUE PROBLEMS Index

To use a shooting method you need 3 boundary conditions at thesame point so introducea new boundary condition

y2(0) = c

wherec is a value that has to be determined to satisfy the boundary conditions at the otherend andω0 is a value that also has to be determined.

But there is still not enough information to find the solutionas there are only 2knownboundary 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 foundearlier

yn = An sinωnx

note that they are only defined to within some arbitrary constant (An). To specify a specificsolution another condition would also have to be used. This condition is known as thenormalising condition and consists of choosing some given value for one of the unknownvalues. So in fact we are free to choose the value ofy2(0) = c to be any (non zero) valueas this just fixes the unknown constant.

The system of equations is now

dy1

dx= y2

dy2

dx= −(y3)

2y1

dy3

dx= 0

subject to

y1(0) = 0 y2(0) = c (known) y3(0) = ω0 (unknown) y1(π) = 0

So there are the required 3 known boundary conditions but 1 unknown one atx = 0 thathas to be determined to satisfy the boundary conditions atx = π .

So choose a value forω0 integrate forward tox = π and check the value ofy1(π) if it isequal to zero it is the correctω0 if it is not zero then change theω0 guess. This can bedone using the MATLAB commandfzero .

Page 111: MATLAB Tutorial

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 compacttspan=[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 vectorf=y(i,1); % set function value = y_1(pi)return

MATLAB code

function f=deigf(t,y)% deigf.mf(1)=y(2);f(2)=-y(3).ˆ2. * y(1);f(3)=0;f=f(:); % forces f to be a column vectorreturn

Page 112: MATLAB Tutorial

104 5. BOUNDARY VALUE PROBLEMS Index

>> omega=fzero(’deig’,1.4,0.0001)

omega = 1.0000

0 0.5 1 1.5 2 2.5 3 3.50

0.2

0.4

0.6

0.8

1

1.2

>> omega=fzero(’deig’,1.6,0.0001)

omega = 2.0000

0 0.5 1 1.5 2 2.5 3 3.5−0.5

−0.4

−0.3

−0.2

−0.1

0

0.1

0.2

0.3

0.4

0.5

Page 113: MATLAB Tutorial

Index DIFFERENTIAL EIGENVALUE EQUATIONS 105

Example

Consider the differential equation

dzdt

= 3√

z−αt2 with z(0) = 2 z(1) = 4

There is one DE but two boundary conditions. Is the system overdetermined?

No since there is a parameterα. This is like an eigenvalue in that there might be aparticular value (or values) that make the equation satisfied.

How do you find that particular value?

Page 114: MATLAB Tutorial

106 5. BOUNDARY VALUE PROBLEMS Index

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 compactformat longinitialguess=11correctalpha=fzero(’findalpha’,initialguess)% now use that value of alpha to integrate the% equations and plot the solutiony0=[2 correctalpha]; % values at t=0tspan=[0 1]; % integrate equation over t=(0,1)[t,y]=ode45(’alphaf’,tspan,y0);plot(t,y(:,1)) % plot y vs tprint -depsc alpha

MATLAB codefunction 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 guessy0=[2 alphaguess]; % values at t=0tspan=[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 codefunction 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 functionsf(1)=3 * sqrt(z)-alpha * t.ˆ2; % the DEf(2)=0; % since alpha is a constantf=f(:); % force f to be a column vectorreturn ;

Page 115: MATLAB Tutorial

Index DIFFERENTIAL EIGENVALUE EQUATIONS 107

alphainitialguess =

11ans =Current guess for alpha =11.00000000 y(1)-4 = 0.15451914ans =Current guess for alpha =10.68887302 y(1)-4 = 0.27958057ans =Current guess for alpha =11.31112698 y(1)-4 = 0.02927189ans =Current guess for alpha =10.56000000 y(1)-4 = 0.33132966ans =Current guess for alpha =11.44000000 y(1)-4 = -0.02266307ans =Current guess for alpha =11.38366126 y(1)-4 = 0.00004514ans =Current guess for alpha =11.38377326 y(1)-4 = 0.00000001ans =Current guess for alpha =11.38377328 y(1)-4 = 0.00000000ans =Current guess for alpha =11.38377328 y(1)-4 = 0.00000000correctalpha =

11.38377327554915diary off

0 0.2 0.4 0.6 0.8 12

2.5

3

3.5

4

4.5

5

Page 116: MATLAB Tutorial

108 5. BOUNDARY VALUE PROBLEMS Index

Page 117: MATLAB Tutorial

6 Partial Differential Equations (PDEs)

6.1 Overview

Partial differential equations arise in a lot of applications. In this introductory section, Iwill try to give you a bit of a feeling for the diversity of someof these applications. First,however, I will define exactly what a partial differential equation (PDE) is and define whatis meant by a boundary condition and an initial condition.

6.2 Functions of more than one variable

6.2.1 Types of problems

Partial differential equations come from problems where there is variation in more thanone variable. For example, the temperature in a room will normally be a function of bothtime and the position in the room — four different variablest, x, y andz. To describe ratesof change with respect to each of these variables we need partial derivatives.

6.2.2 Examples of PDEs

A partial differential equation is a differential equationwith partial derivatives. Someexamples are

∂u∂ t

=∂ 2u∂x2 ,

∂ f∂x

=

(

∂ f∂y

)2

,∂c∂x

+∂c∂y

+∂c∂ z

= c. (6.1)

In the first example the dependent variableu is a function of the two independent variablesx andt. In the second example the dependent variablef is a function ofx andy and in thethird example the dependent variablec is a function ofx,y, andz.

Page 118: MATLAB Tutorial

110 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

6.3 Basic definitions

6.3.1 Order of a PDE

The order of a PDE is the order of the highest derivative in the PDE, no matter whichvariable. In the above examples, the order of the 1st PDE in equation (6.1) is 2, the orderof the 2nd and 3rd PDEs in equation (6.1) are both 1.

6.3.2 Linear PDEs

A PDE is said to belinear if it is a linear combination of the dependent variable and itsvarious partial derivatives. For example,

∂u∂ t

=∂ 2u∂x2 ,

∂ f∂x

+ x∂ f∂y

= z∂ f∂ z

,∂ 4c∂x4 +

∂ 2c∂x2 + c = 0 (6.2)

are linear, whereas

∂u∂ t

=

(

∂u∂x

)2

,∂ f∂x

= e f ,∂ 4c∂x4 + c2 = 0 (6.3)

are not.

6.3.3 Why use numerical methods

For analytic methods we will be concentrating on linear PDEs. In general nonlinear PDEsare too difficult to solve analytically with notable exceptions. One reason for this is thatlinear PDEs allow superposition. This means that if we have any two solutions of a PDEthen their sum is also a solution. This is a useful way of building up the entire solution ofa linear PDE analytically. The method of superposition (adding solutions) does not workfor nonlinear PDEs.

Numerical methods aren’t restricted to solving just linearproblems. This is the entirepoint of studying numerical solutions to these types of problems in that they can not besolved by any other means. The analytic methods used for linear PDEs often provide auseful check on the numerical methods in particular circumstances. This is important ifyou want to have any confidence in your numerical solutions.

Broadly speaking there are three types of PDEs. The type of numerical solution methodis different for each type. What we will do is study an exampleof each type.

Page 119: MATLAB Tutorial

Index THREE COMMON PDES IN APPLICATIONS 111

6.4 Three common PDEs in applications

Three of the most common PDEs in applications are thewave equation, the diffusionequation andLaplace’s equation. These equations are defined as follows:

1-D Diffusion equation:∂ F∂ t

= D∂ 2F∂ x2

2-D Laplace’s equation:∂ 2F∂ x2

+∂ 2F∂ y2

= 0

1-D Wave equation:∂ 2F∂ t2

= c2∂ 2F∂ x2

Page 120: MATLAB Tutorial

112 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

6.5 The diffusion equation

The diffusion equation in one dimension is

∂F∂ t

= D∂ 2F∂x2 (6.4)

whereD 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. pollutantsor chemicals).

6.5.1 Heat conduction

In heat conduction the dependent variableF(x, t) is the temperature inside a material,where heat is conducted in onlyone direction. Solving this PDE gives the temperature atany 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 conductionof heat through the material, whereas the LHS comes from the heat being used to raisethe temperature of the material. The diffusion equation when related to heat is sometmesknown as the heat equation.

6.5.2 Mass diffusion

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 apollution spreading outin a lake. Here the dependent variable is the concentration.

6.5.3 Diffusion equation in 2D and 3D

It is also possible to derive the diffusion equation in two orthree dimensions. For heatconduction, we again use conservation of heat energy. In twoand three dimensions, thediffusion equations are

∂F∂ t

= D

(

∂ 2F∂x2 +

∂ 2F∂y2

)

,∂F∂ t

= D

(

∂ 2F∂x2 +

∂ 2F∂y2 +

∂ 2F∂ z2

)

. (6.5)

respectively. We can also write these as

∂F∂ t

= D∇2F.

Page 121: MATLAB Tutorial

Index LAPLACE’S EQUATION 113

6.6 Laplace’s equation

6.6.1 Equilibrium solutions

Laplace’s equation, in two dimensions, is

∂ 2F∂x2 +

∂ 2F∂y2 = 0 (6.6)

This equation arises in many different applications. One common application is in 2D or3D 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 thetime derivative in (6.5) to zero, we thus obtain (6.6).

6.6.2 Other applications

Laplace’s equation also arises in electrostatics (where the electrical potential, measuredin Volts, satisfies Laplace’s equation) and in fluid mechanics (where the velocity potentialsatisfies Laplace’s equation). Laplace’s equation also arises in ground-water flow exam-ples.

More generally, we write Lapalce’s equation as

∇2F = 0.

Page 122: MATLAB Tutorial

114 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

6.7 The wave equation

The wave equation in one dimension is

∂ 2F∂ t2 = c2∂ 2F

∂x2 (6.7)

wherec is a constant. This partial differential equation describes small amplitude wavepropagation for a number of applications. Here the constantc is the speed of a wave in agiven medium.

6.7.1 Sonar, radar, seismic waves

The wave equation arises in sonar, electromagnetic waves, radar, and seismic prospectingfor example.

6.7.2 2D and 3D waves

In two and three dimensions, the wave equations are

∂ 2F∂ t2 = c2

(

∂ 2F∂x2 +

∂ 2F∂y2

)

,∂ 2F∂ t2 = c2

(

∂ 2F∂x2 +

∂ 2F∂y2 +

∂ 2F∂ z2

)

(6.8)

respectively. More generally, we write

∂ 2F∂ t2 = c2∇2F.

Page 123: MATLAB Tutorial

Index POISSON’S EQUATION 115

6.8 Poisson’s equation

A forced Laplace’s equation is known as a Poisson Equation.

∇2F = g(x,y,z).

6.9 Other co-ordinate systems

So far all the examples given have been in Cartesian co-ordinates(x,y,z). These equationscan 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 =∂ 2F∂ r2 +

1r

∂F∂ r

+1r2

∂ 2F∂θ2 +

∂ 2F∂ z2

and in spherical co-ordinates is

∇2F =∂ 2F∂ρ2 +

∂F∂ρ

+1

ρ2sin2θ∂ 2F∂φ2 +

1ρ2

∂ 2F∂θ2 +

cotθρ2

∂F∂θ

Sometimes this can make the solution process (both analyticand numerical) easier. Canyou think of an example?

Page 124: MATLAB Tutorial

116 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

6.10 Boundary and initial conditions

6.10.1 ICs and BCs

Partial differential equations require additional conditions to obtain unique solutions.Conditions ontime are calledinitial conditions, usually att = 0. Conditions onspa-tial variables are calledboundary conditions. Boundary conditions refer to the endpointsof the region we are interested in.

6.10.2 Number of initial and boundary conditions

The number of initial and boundary conditions needed is determined by the order of therespective derivatives in the PDE. Thediffusion equation (6.4) requiresone initial condi-tion since its time derivative is of order one. It requirestwo boundary conditions, since thex-derivative is second-order. For thewave equation (6.7) we need to specifytwo initialconditions (since there is a second-order derivative with respect tot) and two boundaryconditions. Laplace’s equation (6.6) requiresfour boundary conditions, two for thex-variable and two for they-variable.

6.10.3 A typical initial condition

A typical initial condition isf (x,0) = 100.

This states the initial value off is a constant value 100.

6.10.4 Typical boundary conditions

Typical boundary conditions include

F(0, t) = 30,∂F∂x

(10, t) = 100.

The first condition specifies the value ofF, at the pointx = 0, and the second specifies thevalue of the derivative off at the pointx = 10, for all time.

Page 125: MATLAB Tutorial

Index TYPES OF BOUNDARY CONDITIONS 117

6.11 Types of boundary conditions

There are 3 types of Boundary Conditions. The type of boundary condition has an impacton which numerical solution method you can use for a problem hence it is important tobe able to classify the boundary conditions.

DirichletThe values of the function are given at the boundary.For exampleF(0, t) = 30

NeumannThe value of the normal derivative is given at the boundary.For example∂F

∂x (10, t) = 100 which can also be written∂F∂x = 100 atx = 10

Page 126: MATLAB Tutorial

118 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

CauchyValues of both the function and its normal derivative are given at the boundary.For example∂F

∂x = F at x = 10

Classify (with reason) eachboundary condition of the following PDEs as Dirichlet, Neu-mann or Cauchy.

(a) d2Tdy2 + d2T

dx2 = 4 with T (0,y) = 0 T (x,0) = 2 T (L,y) = siny dTdy (x,M) = 0

(b) dTdt = d2T

dx2 with T (0, t) = 0 dTdx (L, t) = 0 T (x,0) = f (x)

(c) dTdt = d2T

dx2 with T (0, t) = sint dTdx (L, t) = T −Ta T (x,0) = f (x)

Page 127: MATLAB Tutorial

Index DIFFERENT SOLUTION APPROACHES 119

6.12 Different solution approaches

Partial differential equations (PDEs) can be solved analytically or numerically. It is im-portant to realize that not all PDEs have solutions. Numerical techniques can be used ona wider range of PDEs but analytical techniques sometimes give more information or canbe used to check numerical solutions of PDEs in special cases.

6.12.1 Separation of variables

Analytic methods mainly involve reducing a PDE to one or moreordinary differentialequations. The approach we will explore in this course isseparation of variables. Thisinvolves assuming the dependent variable as a product of functions of each independentvariable.

6.12.2 Other approaches

We only have time in this course to learn the above method for solving PDEs. But youshould 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 differentialequation to an algebraic equation. Applying Laplace transforms to a PDE (which is afunction of two variables) reduces the PDE to an ODE which is afunction of one vari-able. One then solves the ODE and then inverts the Laplace transform. Another methodfor solving PDEs involves finding transformations of variables. These are called similaritysolutions.

6.12.3 Numerical techniques

Most practicing engineers or mathematicians would use numerical techniques for solvingPDEs. These are often customised to a particular class of applications. The two mostcommon 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 smallpanels, or elements, and the quantity you are solving for is expressed as a basis of simplefunctions defines over just the finite element. Both finite difference and finite elementmethods lead to matrix equations where the matrices can be large sparse matrices.

6.12.4 Value of analytic solutions

While numerical techniques are often used for practical problems, knowledge of the basicanalytical solutions, such as those obtained in this course, can provide valuable inciteinto the problems and may also provide a simple check on the accuracy of the numericalprogram in particular circumstances. It is always a wise idea to check numerical methodson a solution with a known analytic solution to try and guarantee that the method and thecoding is correct.

Page 128: MATLAB Tutorial

120 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

6.13 Examples

Heat in a rod

Consider an insulated rod of steel of lengthL were one end is held at 100◦C and theother can lose heat according to Newton’s Law of Cooling (heat loss is proportional to thedifference in the temperature of the rod and the surroundingambient air). The ambientair is at 25◦C.

Write down the governing equation, the initial and boundaryconditions.

Page 129: MATLAB Tutorial

Index EXAMPLES 121

Steady state Temperature in a slab

Consider a slab of wood of size 10cm (wide) by 5cm (long) by 2cm(high) that has reachedthermal equilibrium. If the bottom of the slab is insulated,the top loses heat by Newton’sLaw of Cooling, the sides are held at 20◦C, write down the governing equation and bound-ary conditions.

Page 130: MATLAB Tutorial

122 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

Plucked String

Consider a string of lengthL that is held fixed at both ends. The string is pulled out toa triangular shape and then released. Write down the governing equation, boundary andinitial conditions.

Page 131: MATLAB Tutorial

Index EXAMPLES 123

Drum

Consider a drum of radius 10cm. If the drum is beaten so that the velocity of the surfaceis initially f (r) (r is the radius) Write down the governing equation, boundary conditionsand initial conditions.

Page 132: MATLAB Tutorial

124 6. PARTIAL DIFFERENTIAL EQUATIONS (PDES) Index

6.14 Classification of PDEs

The numerical solution of the 3 standard types of linear PDEs(Diffusion, Laplaces andWave equations) differs for each type of equation. In fact these PDEs are examples ofmore general classes of PDEs known as Parabolic, Elliptic and Hyperbolic PDEs. Thenumerical methods for solving PDEs differ depending on which type of PDE you aresolving.

Consider a general PDE that depends on 2 variables (a andb).

A∂ 2u∂a2 +2B

∂ 2u∂a∂b

+C∂ 2u∂b2 = F(a,b,u,

∂u∂a

,∂u∂b

)

whereA, B andC are constants andF(a,b,u, ∂u∂a , ∂u

∂b) is an arbitrary function of thosevariables.

The classification of a PDE (and hence the type of numerical method used to solve it)depends on the the quantityB2−AC.

B2−AC = 0 Parabolic eg Diffusion equationB2−AC < 0 Elliptic eg Laplaces equationB2−AC > 0 Hyperbolic eg Wave equation

One Dimensional Diffusion equationHasA = D, B = 0, C = 0, a = x (the space variable),b = t (the time variable) and thefunctionF = ∂u

∂ t .

Two Dimensional Laplaces equationHasA = 1, B = 0,C = 1, a = x, b = y (the space variables),F = 0.

One Dimensional Wave equationHasA = 1, B = 0,C = −1

c2 , a = x (the space variable),b = t (the time variable) andF = 0.

Page 133: MATLAB Tutorial

7 Parabolic Equations: Diffusion Equation

7.1 Finite differences

Finite differences can be used to solve PDEs. Each derivative is approximated by a finitedifference and then the equation is rearranged to enable a solution to be determined.

7.1.1 First Derivative

Recall the finite difference approximations

First derivativeForward Approximation

dydx

n≈ yn+1− yn

xn+1− xn

=yn+1− yn

∆x

whereyn = y(xn) and∆x is the difference inx steps or just the space betweenx points.

First derivativeBackward Approximation

dydx

n≈ yn − yn−1

∆x

Both the forward and backward approximations arefirst order accurate, that is the firstneglected term is order∆x.

First derivativeCentral Approximation given by

dydx

n≈ yn+1− yn−1

2∆x

7.1.2 Second Derivative

Second derivativeCentral Approximation given by

d2ydx2

n≈ yn+1−2yn + yn−1

(∆x)2

Page 134: MATLAB Tutorial

126 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.2 FTCS method

Consider the diffusion equation in one space dimension

∂F∂ t

= D∂ 2F∂x2 (7.1)

with boundary conditions

F(a, t) = Fa(t) F(b, t) = Fb(t)

and initial conditionF(x,0) = F0(x)

We require the solution in the regiona ≤ x ≤ b and over the time span 0≤ t ≤ T

7.2.1 Discretise the region

To use finite differences we need to divide the region up into discrete points where wecalculate the solution. This is known as discretising the region.

TakeNx points in thex direction so that the spacing between points is then given by

∆x =b−a

Nx

and let the variablei be the index that refers to the discretex points. That is

xi = a+ i∆x i = 0,1, . . . ,Nx

Do a similar process for thet direction. TakeNt points in thet direction so that the spacingbetween time steps is then given by

∆t =T −0

Nt

and let the variablej 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 ateach of the grid points.

Page 135: MATLAB Tutorial

Index FTCS METHOD 127

t = 0

x = a x = b

i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8

j = 1

j = 2

j = 3

j = 4

j = 5

j = 6

j = 7

j = 8

j = 9

j = 10

utut

utut

utut

utut

utut

utut

utut

utut

utut

utut

∆x ∆t

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rsrs rs

t

x

rs Initial condition – known F(x,0) = F0(x)ut Boundary conditions – knownF(a, t) = Fa(t) andF(b, t) = Fb(t)bc Interior points – unknown

Page 136: MATLAB Tutorial

128 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

Let the solution at the point(xi, t j) be denoted byF ji , that is

F ji = F(xi, t j) = F(a+ i∆x, j∆t)

Now approximate the diffusion equation

∂F∂ t

=∂ 2F∂x2

around the point(xi, t j) by using a forward finite difference approximation for the timederivative

∂F∂ t

j

i≈ F j+1

i −F ji

∆t

and a centred finite difference approximation for the spatial second derivative

∂ 2F∂x2

j

i≈

F ji+1−2F j

i +F ji−1

(∆x)2

This gives the PDE as approximately

F j+1i −F j

i

∆t= D

(

F ji+1−2F j

i +F ji−1

(∆x)2

)

now makeF j+1i the subject

F j+1i = ∆tD

(

F ji+1−2F j

i +F ji−1

(∆x)2

)

+F ji

F j+1i = γF j

i+1+(1−2γ)F ji +F j

i−1 (7.2)

where

γ =D∆t(∆x)2

Equation (7.2) is known as theForwardTime CenteredSpace (FTCS) approximation tothe diffusion equation.

The FTCS equation enables solutions at time levelj+1 to be calculated from informationat time levelj.

Given that we know the initial condition (levelj = 0) then we are able to use this tocalculate the solution at time levelj = 1. This is then used to get the solution at levelj = 2 and so on.

At a given time level you use the FTCS equation to sweep acrossin thei direction calcu-lating the value ofF j+1

i .

The FTCS method is anexplicit method as at each time step there is an explicit equationfor the values at the next time step.

Page 137: MATLAB Tutorial

Index FTCS METHOD 129

7.2.2 Template for the FTCS Method

t = 0

x = a x = b

ii−1 i+1

j

j +1

utut

utut

utut

utut

utut

utut

utut

utut

utut

utut

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rsrs rs

rs Initial condition – known F(x,0) = F0(x)ut Boundary conditions – knownF(a, t) = Fa(t) andF(b, t) = Fb(t)bc Interior points – unknown

b

Page 138: MATLAB Tutorial

130 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.2.3 FTCS example

Solve the heat diffusion problem of a steel bar of length 2m with one end held at 0◦C theother end at 0◦C and initially the bar is at at 100◦C.

∂T∂ t

= D∂ 2T∂x2

where for steelD = 5×10−6 m2/sThe boundary and initial conditions can be written

T (0, t) = 0 T (2, t) = 0 T (x,0) = 100

Find the temperature distribution at timest = 60,120,180, . . .600 minutes

The exact solution can be found analytically using the method of separation of variablesto be

T (x, t) =∞

∑m=1

amexp

(−Dm2π2t4

)

sin(mπx

2

)

where

am =−200((−1)m −1)

0 0.5 1 1.5 20

10

20

30

40

50

60

70

80

90

100

x

Tem

pera

ture

Temperature in metal bar at steps of 60 minutes

numericalexact

Page 139: MATLAB Tutorial

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 boundariesNx=10; % number of x points to usedx=(b-a)/Nx; % x stepx=[a:dx:b]; % x pointsfa=0; fb=0; % boundary valuesD=5e-6; % steel bar diffusivity = 5.0x10ˆ-6 mˆ2/sdt=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 inde x 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

% plot initial temperature distributionplot(x,T,x,T,’r--’); xlabel(’x’); ylabel(’Temperature ’);title(’ Temperature in metal bar at steps of 60 minutes’)hold on%% step through the time in steps of dt from dt to tfinalfor t=dt:dt:tfinal% sweep across the bar calculating the Temperature

for i=2:NxTnext(i)=gamma * (T(i-1)+T(i+1))+(1-2 * gamma)* T(i);

endTnext(1)=fa; Tnext(Nx+1)=fb;T=Tnext; % update the temperature

% exact solution from separation of variables methodTexact=0;for m=1:20 % use 20 terms in the sum, is that enough?

Am=-200* ((-1)ˆm-1)/(m * pi);Texact=Texact+Am * exp(-D * (mˆ2 * piˆ2/4). * t). * sin(m * pi/2. * x);

end

% plot the numerical and the exact solution (red dashed)plot(x,T,x,Texact,’r--’);legend(’numerical’,’exact’)

end % end of the time loop

hold off

Page 140: MATLAB Tutorial

132 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.2.4 Stability

The stability of a time stepping numerical method is all to dowith whether errors grow ordecay as you step in time.

Consider the forward time approximation to the time derivative at the point(xi, t j)

∂F∂ t

j

i≈ F j+1

i −T ji

∆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 dampedout?

It turns out that for an FTCS (Forward Time Centred Space) approximation to the diffu-sion equation as given by

F j+1i = γ(T j

i+1+T ji−1)+(1−2γ)T j

i

where

γ =D∆t(∆x)2

that if

γ <12

then the method isstableand errors do not grow

γ >12

then the method isunstableand errors grow

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 below12 at all times. This is particularly important if thediffusion coefficient varies in the problem.

How does this effect the usefulness of the FTCS method?

If you have a metal bar with diffusion constant ofD = 5×10−6m2/s of length 2m andyou want 100 points along the bar to get an accurate representation of the temperature inthe 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?

Page 141: MATLAB Tutorial

Index FTCS METHOD 133

7.2.5 Unstable Example

Go back to original FTCS example.

For the original problem what would happen if we increase thetime step fromdt = 3600seconds todt = 7200 seconds. Thenγ = 0.9

0 0.5 1 1.5 2−150

−100

−50

0

50

100

150

200

250

x

Tem

pera

ture

Temperature in metal bar at steps of 120 minutes

numericalexact

Disaster !

γ = Ddt/(dx)2 > 0.5

so the FTCS method is unstable and hence useless.If you wanted to use this time step you would need to increase the x step size to getγbelow 0.5. But then the solution would be less accurate.

Theγ < 0.5 condition for the FTCS method to be stable is in practice avery restrictivecondition as a halving of the spatial step (twice as manyx points) results in a time step afactor of 4 smaller. If any degree of accuracy is required (that is a smallx step is required)then method can take a very long time to run as a very small timestep is required.

Page 142: MATLAB Tutorial

134 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.3 Crank-Nicolson method

In contrast to the FTCS method theCrank-Nicolson method uses an average of the oldand new time step for the spatial derivative.

∂F∂ t

j

i= D

12

(

∂ 2F∂x2

j+1

i+

∂ 2F∂x2

j

i

)

F j+1i −F j

i

∆t=

D2

(

F j+1i+1 −2F j+1

i +F j+1i−1

(∆x)2 +F j

i+1−2F ji +F j

i−1

(∆x)2

)

Which rearranges to

−γ2

(

F j+1i+1 +F j+1

i−1

)

+(1+ γ)F j+1i =

γ2

(

F ji+1+F j

i−1

)

+(1− γ)F ji

where

γ =D∆t(∆x)2

The left hand side has the unknowns at timej + 1 the right hand side the known valuesat time j. These are for each value ofi as you step across the region. Hence it is reallynequations inn unknowns (recall the finite-difference work for ODEs).

This is known as animplicit method as there is now no longer an explicit equation for thesolution at timej + 1 in terms of time levelj values but rather a system of equations tosolve.

7.3.1 Template

ii−1 i+1

j

j +1

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

b bb

Page 143: MATLAB Tutorial

Index CRANK-NICOLSON METHOD 135

7.3.2 Matrix Representation

The boundary conditions ati = 0 and i = Nx are known so the equations are slightlydifferent fori = 1 andi = Nx −1 to account for this.

The system of equations can be written in matrix form (recallthe finite difference workon ODEs) as

AF j+1 = BF j +d j

where

A =

1+ γ −γ/2 0 . . . 0 0−γ/2 1+ γ −γ/2 . . . 0 0

......

.... . .

......

0 0 0 . . . 1+ γ −γ/20 0 0 . . . −γ/2 1+ γ

B =

1− γ γ/2 0 . . . 0 0γ/2 1− γ γ/2 . . . 0 0

......

.... . .

......

0 0 0 . . . 1− γ γ/20 0 0 . . . γ/2 1− γ

d j =γ2

F ja +F j+1

a

00...0

F jb +F j+1

b

F j =

F j1

F j20...0

F jNx−1

F j+1 =

F j+11

F j+120...0

F j+1Nx−1

This system of equations can be easily solved in MATLAB usingthe ’backslash divide’command. For example something likeFnext=A\(B * F+d)

One of the advantages of theimplicit scheme such as Crank-Nicolson is that it isstableirespective of the value ofγ. That is errors do not build up to catastrophic proportions evenfor large time steps. Hence large time steps can be taken onlylimited by the accuracy ateach step.

This is the major advantage of the implicit Crank-Nicolson scheme over the explicitFTCSscheme. This stability comes at a price, namely that a matrixequation must be solved ateach time step. This is not too bad in this case as the matrixA is tri-diagonal and hencethere are fast and efficient methods available to invert it. Also since in this caseA doesnot change from one tme step to the next then it is even possible to determine the inverseof 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 schemesare usually stable.

Page 144: MATLAB Tutorial

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◦Cthe other end at 0◦C and initially the bar is at at 0◦C.

∂T∂ t

= D∂ 2T∂x2

where for steelD = 5×10−6 m2/sThe boundary and initial conditions can be written

T (0, t) = 100 T (2, t) = 0 T (x,0) = 0

Find the temperature distribution at timest = 4,8,12. . .40 hours

0 0.5 1 1.5 20

10

20

30

40

50

60

70

80

90

100

x

Tem

pera

ture

Temperature in bar at steps of 4 hours

Page 145: MATLAB Tutorial

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 metho d%% 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 pointsfa=100; fb=0; % boundary valuesD=5e-6; % diffusivity = 5x10ˆ-6 mˆ2/sdt=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 conditionTplot=[fa;T;fb]; % add in the boundary condition for plotplot(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 matricesAleft=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 th is)for t=dt:dt:tfinal % take time steps of dt

d=gamma* [fa;zeros(n-3,1);fb];% adjust for known boundary valuesT=Aleft\(Aright * T+d); % solve for T using backslash divideif (mod(t,dt * 100)==0) % plot each 100th step

Tplot=[fa;T;fb]; % add boundary values for plottingplot(x,Tplot);

endendprint -depsc cranknichold off

Page 146: MATLAB Tutorial

138 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.4 Method of lines

Themethod of linesis a way of numerically solving PDEs using the efficient techniques(eg Runge-Kutta) developed to solve ODEs.

Consider the 1D diffusion equation

∂T∂ t

= D∂ 2T∂x2

with Dirichlet boundary conditions at the boundariesx = a andx = b and initial conditiongiven by

T (a, t) = fa(t) T (b, t) = fb(t) T (x,0) = f0(x)

discretise this in thex-direction using the variablei to indicate whichx point you areat. That is, divide thex-direction intoN subintervals withi = 0,1,2, . . . ,N. Use centredspace approximation to the second derivative at each of those points but leave the timederivative as it is. So we get

∂Ti

∂ t≈ D

Ti−1−2Ti +Ti+1

(∆x)2

whereTi is the temperature at theith space position and∆x = (b−a)/N.

Now T0 andTN are known because of the boundary condition.

T0 = fa(t) TN = fb(t)

Fori = 1,2,3, . . . ,N−1 we now have asystemof (N−1) ODEs in the variablesT1,T2,T3, . . . ,TN−1.For example

dT1

dt= β (T0−2T1+T2) = β ( fa −2T1 +T2)

dT2

dt= β (T1−2T2+T3)

dT3

dt= β (T2−2T3+T4)

. . . = . . .

dTN−2

dt= β (TN−3−2TN−2+TN−1)

dTN−1

dt= β (TN−2−2TN−1+TN) = β (TN−2−2TN−1 + fb)

whereβ = D/(∆x)2.

With the initial condition giving us the initial values for each ofT1,T2,T3, . . . ,TN−1.

This system of ODEs is easily solved numerically using any ofthe ODE solvers men-tioned previously (eg Euler’s, midpoint, Runge-Kutta, ode45,. . . ). The more points usedacross the domain (larger the value ofN) the more accurate the solution will be and thelonger it will take to run.

Page 147: MATLAB Tutorial

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

n=50; xa=0; xb=2;dx=(xb-xa)/nTa=100; Tb=50;timeend=1e6;D=1e-6beta=D/(dx. * dx)numsteps=10timestep=timeend/numsteps;

% setup the time points solution is wanted attspan=timestep:timestep:timeend;

% x is the interior points,% w the entire domain (ie put boundary points in)x=xa+dx:dx:xb-dx;w=[xa x xb];

% set up initial quadratic temperature profile and plot ity0=100-25 * x. * (x-1);plot(w,[Ta y0 Tb],’r-’)title(’ Heat Diffusion using Method of Lines’)xlabel(’x’); ylabel(’Temperature’)hold on

% integrate the system of equations[t,y]=ode45(’molf’,tspan,y0);

% plot the solution, including the boundary valuesfor i=2:numsteps

v=[Ta y(i,:) Tb];plot(w,v,’b-’)

end

print -depsc molhold off

Page 148: MATLAB Tutorial

140 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

MATLAB code

function f=molf(t,y);%% molf.m% the system of equationsglobal 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

Heat diffusion in a steel bar withD = 1×10−6 m2/s

∂T∂ t

= D∂ 2T∂x2

withT (0, t) = 100 T (2, t) = 50 T (x,0) = 100−25x(x−1)

0 0.5 1 1.5 250

60

70

80

90

100

110 Heat Diffusion using Method of Lines

x

Tem

pera

ture

Page 149: MATLAB Tutorial

Index OTHER BOUNDARY CONDITIONS 141

7.5 Other boundary conditions

So far all the examples have used Dirichlet Boundary Conditions (the value of the functiongiven on the boundary). What changes if instead the boundaryconditions are Neumann orCauchy type? Now the value of the function is not known and hence the boundary shouldalso be considered an unknown point.

ExampleLeft edge has a Dirichlet (u = 0) boundary condition and hence the value is known. Rightedge has a Cauchy (∂u

∂ r = h(25−u)) boundary condition and hence the value is unknown.

bcut

bcut

bcut

bcut

bc

bc

bc

bc

rs

bc

bc

bc

bc

rs

bc

bc

bc

bc

rs

bc

bc

bc

bc

rs

bc

bc

bc

bc

rs

bc

bc

bc

bc

rs

bc

bc

bc

bc

rs

bc

bc

bc

bc

rsrs rs

∂u∂ r = h(25−u)

boundaryvalueunknown

u = 0boundaryvalueknown

rs known initial conditionbc unknown pointsut known boundary points

ExampleThe different types of boundary conditions can all be derived from Newton’s Law ofcooling. For example consider a steel bar where the end atx = 0 loses heat according toNewton’s Law of cooling with a heat transfer coefficient ofh, and an ambient temperatureof Ta, that is atx = 0

∂T∂x

= h(T −Ta)

If h = 0 then the boundary is fully insulated as no heat can flow through that boundary

∂T∂x

= 0 insulated

If h is very large then divide each side byh to give approximately

T = Ta Dirichlet condition

Each of these three boundary conditions need to be treated slightly differently.

Page 150: MATLAB Tutorial

142 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.5.1 Neumann Boundary Condition

The function value is not known at the boundary so the boundary is an unknown value thatmust also be determined. After discretizing in thex-direction use centred differencing onthe derivative term, for example centred differencing about i = 0 gives

∂T∂x

i=0≈ T1−T−1

2∆x

whereT−1 is afictitious point . That is it is an artificial point that is not in the domain ofinterest. So how does this help?

Consider the case of an insulated boundary∂T∂x = 0.

Using the above expression and the fact that the boundary is insulated so that∂T∂x = 0 we

get a relationship betweenT−1 andT1 in this case this is simply that

T−1 = T1

But recall that we are dealing with the Diffusion Equation

∂T∂ t

= D∂ 2T∂x2

using centred differencing on the second derivative aroundthe pointi = 0 results in

∂ 2T∂x2

i=0≈ T−1−2T0+T1

(∆x)2

the fictitious pointT−1 can then be replaced byT1 to give

∂ 2T∂x2

i=0≈ 2T1−2T0

(∆x)2

Hence it is possible to incorporate the boundary condition into the scheme that is usedwith the value of the temperature at the boundary being one ofthe unknowns.

For example in the Method of Lines the system of equations is modified to include anextra equation for theT0 point and theT1 equation is also altered to still include theT0

which is unknown, the other equations remain unchanged.

dT0

dt= β (T−1−2T0 +T1) = β (2T1−2T0)

dT1

dt= β (T0−2T1+T2)

dT2

dt= β (T1−2T2+T3)

. . . = . . .

dTN−2

dt= β (TN−3−2TN−2+TN−1)

dTN−1

dt= β (TN−2−2TN−1+TN) = β (TN−2−2TN−1 + fb)

Page 151: MATLAB Tutorial

Index OTHER BOUNDARY CONDITIONS 143

ExampleHeat diffusion in a steel bar withD = 1×10−6 m2/s

∂T∂ t

= D∂ 2T∂x2

with∂T∂x

(0, t) = 0 T (2, t) = 50 T (x,0) = 100−25x(x−1)

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-25 x(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

n=50; xa=0; xb=2; dx=(xb-xa)/n; Tb=50;timeend=1e6; D=1e-6; beta=D/(dx. * dx);numsteps=10; timestep=timeend/numsteps;

% setup the time points solutionis wanted attspan=timestep:timestep:timeend;

% x is the interior points and left point,% w the entire domain (ie put right boundary point in)x=xa:dx:xb-dx;w=[x xb];

% set up initial quadratic temperature profile and plot ity0=100-25 * x. * (x-1);plot(w,[y0 Tb],’r-’)title(’Method of Lines: Insulated left edge’)xlabel(’x’); ylabel(’Temperature’)hold on

% integrate the system of equations[t,y]=ode45(’molinsulatedf’,tspan,y0);

% plot solution, dont forget the right boundary valuefor i=2:numsteps

v=[y(i,:) Tb];plot(w,v,’b-’)

endprint -depsc molinsulatedhold off

Page 152: MATLAB Tutorial

144 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

MATLAB code

function f=molinsulatedf(t,y);%% molinsulatedf.m% the system of equationsglobal n Tb beta

% recall that MATLAb vectors start at index 1 so that T_0% is in y(1), T_1 in y(2), ... , T_n-1 in y(n)f(1)=beta * (2 * y(2)-2 * y(1));for i=2:n-1

f(i)=beta * (y(i-1)-2 * y(i)+y(i+1));end;f(n)=beta * (y(n-1)-2 * y(n)+Tb);f=f(:);return

0 0.5 1 1.5 250

60

70

80

90

100

110 Heat Diffusion using Method of Lines: Insulated left edge

x

Tem

pera

ture

Page 153: MATLAB Tutorial

Index OTHER BOUNDARY CONDITIONS 145

7.5.2 Cauchy Boundary Condition

As an example for the full Newtonian heat loss boundary condition

∂T∂x

= h(T −Ta)

the derivative of the function is given in terms of the unknown value at the boundary thatmust also be determined. After discretizing in thex-direction use centered differencingon the derivative term, for example centred differencing about i = 0 gives

∂T∂x

i=0≈ T1−T−1

2∆x= h(T0−Ta)

whereT−1 is afictitious point andTa is the ambient temperature.

This can be rearranged to get the fictitious pointT−1 in terms of the known pointsT0 andT1.

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 pointi = 0.

Page 154: MATLAB Tutorial

146 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.6 Nonlinear Example

Reaction Diffusion Example: Combustion of a sawdust layer

Consider a reaction-diffusion problem where the temperature in a fuel not only diffusesbut also causes a feedback so that a higher temperature results in a faster increase intemperature. This type of scenario is typical of a combustion problem where as a fuelburns it raises the temperature thereby making the reactionhappen even faster. Assumethat the reaction is occurring in a region of length 2m and is such that the ends are held atsome ambient temperatureTa and initially the temperature is ambient everywhere excepta small hot spot in the centre.

This could be used to model scenarios such as a small piece of hot metal falling into alayer of sawdust for example.

∂T∂ t

= D∂ 2T∂x2 +(T −Ta)

2

where for sawdustD = 0.08 m2/s. The boundary conditions are ambient temperatureT (0, t) = Ta = 25 T (2, t) = 25 and an initial condition of ambient temperature every-where except at a hotspot in the centre

T (x,0) =

{

50 if x = 1 the hot spot25 elsewhere

Not that this equationcan notbe 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

0 0.5 1 1.5 220

30

40

50

60

70

80

90

x

Tem

pera

ture

Temperature at steps of 0.1 seconds: Final Temperature 1.5 seconds

Page 155: MATLAB Tutorial

Index NONLINEAR EXAMPLE 147

MATLAB code

% reactdiff.m% Solve the heat reaction-diffusion equation on a length 2 wi th% 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 pointsD=0.08; % diffusivity of wood = 0.08 mˆ2/sdt=.001; % time step in secondstfinal=1.48; % final time in secondsgamma=D* dt/(dxˆ2)Ta=25; % ambient TemperatureT=Ta* ones(1,n+1); % ambient Temperature everywhere except hot s potT(n/2+1)=Ta+25; % hot spot at centre% plot initial temperature distributionplot(x,T); xlabel(’x’); ylabel(’Temperature’);title(’ Temperature at steps of 0.1 sec: Final Temperature 1 .5 sec’)hold onfor t=dt:dt:tfinal % take time steps of dt%step across the bar calculating the Temperature at next tim e level

for i=2:nTnext(i)=gamma * (T(i-1)+T(i+1))+(1-2 * gamma)* T(i)+dt * (T(i)-Ta)ˆ2;

endTnext(1)=Ta; Tnext(n+1)=Ta;T=Tnext; % update the temperatureif (mod(t,dt * 100)==0) % plot each 100th step

plot(x,T);end

endplot(x,T);print -depsc reactdiffhold off

Page 156: MATLAB Tutorial

148 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.7 Other co-ordinate systems

So far all the examples given have been in Cartesian co-ordinates(x,y,z). These equationscan 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 =∂ 2F∂ r2 +

1r

∂F∂ r

+1r2

∂ 2F∂θ2 +

∂ 2F∂ z2

The benefit of using a coordinate system that matches the object is that often it is possibleto reduce the dimension of the problem.

7.7.1 Disc example

Consider the case of a circular disc of metal with a radius of 50cm. The base and top areinsulated which means the only directions of interest are inthe plane of the disc ((x,y)plane) and hence the problem is two dimensional. The disc is initially at 100◦C andloses heat through the edge according to Newton’s law of cooling. The ambient air is at25◦C. The diffusivity of the metal isD = 5×10−6m2/s and the Newtonian heat transfercoeeficient ish = 10m−1.

∂u∂ r

= −h(u−ua) at r = 0.5

x

y

The governing differential equation is the 2-D heat equation

∂u∂ t

= D∇2u

which can be written in circular coordinates as

∂u∂ t

= D

(

∂ 2F∂ r2 +

1r

∂F∂ r

+1r2

∂ 2F∂θ2

)

Now since the boundary condition is applied uniformly over the boundary and the initialcondition is also uniform then the temperature in the disc will not depend on the angle

Page 157: MATLAB Tutorial

Index OTHER CO-ORDINATE SYSTEMS 149

around the disc. Hence there will not be anyθ dependence and so any derivatives withrespect toθ are zero. So the governing PDE becomes

∂u∂ t

= D

(

∂ 2u∂ r2 +

1r

∂u∂ r

)

This has reduced the problem form a 2-D one down to a 1-D one.

What about the boundary and initial conditions?

There is one initial condition as expected

u(r,0) = 100

and there should be two boundary conditions since the PDE is second order in the spacevariabler. One boundary condition is obvious, namely

∂u∂ r

= −h(u−ua) at r = 0.5

The other boundary condition is derived from the fact that the temperature is independentof θ and hence at the origin the derivative with respect tor must be zero. That is thetemperature distribution must be centred on the origin. So the other boundary conditionis

∂u∂ r

= 0 at r = 0

Look at the governing PDE. There is a problem atr = 0 as the term1r

∂u∂ r appears to be

undefined atr = 0. In fact this term is not undefined since atr = 0 ∂u∂ r = 0 as well due to

the boundary condition. This difficulty can be overcome by using L’Hopital’s Rule.

7.7.2 L’Hopital’s Rule

L’Hopitals rule is a consequence of Taylor series and statesin this particular situation that

if limr→0

f (r) = 0 and limr→0

g(r) = 0 then limr→0

f (r)g(r)

= limr→0

f ′(r)g′(r)

hence in this casef (r) = ∂u∂ r andg(r) = r so that atr = 0 the governing PDE is modified

by

∂u∂ t

= D

(

∂ 2u∂ r2 +

∂u∂ r

r

)

= D

(

∂ 2u∂ r2 +

∂ 2u∂ r2

1

)

= 2D∂ 2u∂ r2

So in any numerical method this alternative PDE must be used if at the pointr = 0.

Page 158: MATLAB Tutorial

150 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

7.7.3 FTCS Method

Now derive the FTCS system for solving this problem. The onlyspace dimension ofinterest is the radiusr = 0 to 0.5. Set up a grid in space and time. Discretise the regionsothatu j

i = u(i∆r, j∆t) with i = 0,1, . . . ,Nr and j = 0,1, . . . ,Nt with ∆r = 0.5/Nr

t = 0

r = 0 r = 0.5

i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8

j = 1

j = 2

j = 3

j = 4

j = 5

j = 6

j = 7

j = 8

j = 9

j = 10

bcbc

bcbc

bcbc

bcbc

bcbc

bcbc

bcbc

bcbc

bcbc

bcbc

∆r ∆t

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

rsrs rs

t

r

∂u∂ r = h(25−u)∂u

∂ r = 0

rs Initial condition – known u(r,0) = 100bc unknown points

Page 159: MATLAB Tutorial

Index OTHER CO-ORDINATE SYSTEMS 151

FTCS formulationProvidedr 6= 0 the governing PDE is

∂u∂ t

= D

(

∂ 2u∂ r2 +

1r

∂u∂ r

)

so use Forward Time and Centred Space finite difference around the point(i, j) to approx-imate this by

u j+1i −u j

i

∆t= D

(

u ji+1−2u j

i +u ji−1

(∆r)2 +1

i∆r

u ji+1−u j

i−1

2∆r

)

Rearranging this to get the new time level as the subject results in

u j+1i = γ

(

u ji+1−2u j

i +u ji−1

)

+γ2i

(

u ji+1−u j

i−1

)

+u ji

whereγ = D∆t(∆r)2 . This is the FTCS formulation for the internal points. Points at the two

edges (r = 0 andr = 0.5) require special attention.

External edger = 0.5Use a fictitious point beyond the region of interest. The boundary condition is∂u∂ r = −h(u−ua) which on using central finite differences is

∂u∂ r

j

Nr

≈u j

Nr+1−u jNr−1

2∆r= −h(u j

Nr−ua)

which rearranges so that the fictitious pointu jNr+1 can be written as

u jNr+1 = −2∆rh(u j

Nr−ua)+u j

Nr−1

This value is then used in the FTCS scheme above wheneveru jNr+1 is used.

Page 160: MATLAB Tutorial

152 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

Internal edge r = 0Do a similar process for the internal edge but recall that thePDE is altered there. Theboundary condition is∂u

∂ r = 0 which on using central finite differences is

∂u∂ r

j

0≈

u j1−u j

−1

2∆r= 0

which rearranges so that the fictitious pointu j−1 can be written as

u j−1 = u j

1

The PDE atr = 0 (which isi = 0) was found previously using L’Hopital’s Rule to be

∂u∂ t

= 2D∂ 2u∂ r2

so use Forward Time and Centred Space finite difference around the point(0, j) to ap-proximate this by

u j+10 −u j

0

∆t= 2D

(

u j1−2u j

0 +u j−1

(∆r)2

)

Using the value for the fictitious point (u j−1) above and rearranging gives the governing

FTCS equation for ther = 0 point as

u j+10 = 4γ

(

u j1−u j

0

)

+u j0

It is now possible to code this FTCS method in MATLAB.

Page 161: MATLAB Tutorial

Index OTHER CO-ORDINATE SYSTEMS 153

MATLAB code

% ftcs_cyl.m% Solve the heat diffusion equation on a steel disc radius 0.5 m% 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 met hod% 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 pointsL=0.5; % radius of discdr=L/n; % space stepTa=25; % ambient temperaturer=[0:dr:L]; % r pointsD=5e-6; % steel diffusivity = 5x10ˆ6 mˆ2/sh=10.0; % heat loss coefficientdt=30 % time step in secondstfinal=30000; % final time in secondsgamma=D* dt/(drˆ2)% calculate and plot initial temperature distributionT=ones(1,n+1) * 100;plot(r,T,’r-’); xlabel(’radius’); ylabel(’Temperature ’);title(’ Temperature in disc at 900 second intervals’)hold on

% loop through timefor t=dt:dt:tfinal % take time steps of dt% step across the radius calculating the Temp at the next time level% remember to index from 1 not zero hence shift all indexes up o ne

% r=0 special case uses a fictitious point beyond regionTnext(1)=4 * gamma* (T(2)-T(1))+T(1);

% internal r pointsfor 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

% external r point uses a fictitious point beyond regionTfict=T(n)-2 * dr * h* (T(n+1)-Ta); % calculate ficticous pointTnext(n+1)=gamma * (T(n)+Tfict)...

+(1-2 * gamma)* T(n+1)+gamma/(2 * (n)) * (Tfict-T(n));

% update the temperature and plot each 30th solutionT=Tnext;if (mod(t,dt * 30)==0)

plot(r,T);end

end % t loophold off

Page 162: MATLAB Tutorial

154 7. PARABOLIC EQUATIONS: DIFFUSION EQUATION Index

0 0.1 0.2 0.3 0.4 0.520

30

40

50

60

70

80

90

100

radius

Tem

pera

ture

Temperature in disc at 900 second intervals

Page 163: MATLAB Tutorial

8 Elliptic Equations: Laplace’s Equation

Elliptic equations (such as Laplace’s Equation) do not allow ’time-stepping’ since there isno ’time-like’ variable to step in. Instead the solution must somehow be found in the entiredemand simultaneously. The best method for this is to use some kind of iterative schemewhere you use the equation to update an initial guess of the solution until it converges toa steady solution. This is a sort of pseudo ’time-stepping’ but it is not necessary to keepthe intermediate ’times’ hence some short cuts and speed upscan be done.

8.1 Example with Dirichlet boundary conditions

Consider a steady state heat problem on a rectangular domainwith Dirichlet Boundaryconditions. For example a slab of wood of size 10cm (wide) by 5cm (long) that hasreached thermal equilibrium. The left and right edges are held at 20◦C the bottom at20◦C and the top is subjected to to a quadratic temperature such that the maximum in themiddle is 70◦C. What is the steady state temperature distribution in the slab?

20◦C

20◦C 20◦C

(2x(10− x)+20)◦C

x

y

5

10

The governing PDE is Laplace’s Equation in 2D

∂ 2T∂x2 +

∂ 2T∂y2 = 0

subject to the boundary conditions

T (0,y) = 20 T (10,y) = 20 T (x,0) = 20 T (x,5) = 2x(10− x)+20

Page 164: MATLAB Tutorial

156 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index

Divide the region up into a grid so that thereNx sections in thex-direction andNy sectionsin they-direction. Leti be the index for thex-direction andj the index for they-direction.So in this case

∆x =(10−0)

Nx∆y =

(5−0)

Ny

with i = 0,1, . . . ,Nx and j = 0,1, . . . ,Ny. LetTi, j be shorthand notation for the temperatureat the position(xi,y j) wherexi = 0+ i∆x andy 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

(∆x)2 +Ti, j+1−2Ti, j +Ti, j−1

(∆y)2 ≈ 0

That is the template used is a cross pattern

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

b b b

b

b

i i+1i−1

j

j +1

j−1

Rearrange the equation to get

2(

(∆y)2+(∆x)2)Ti, j = (∆y)2(Ti+1, j +Ti−1, j)

+(∆x)2(Ti, j+1+Ti, j−1)

and so we get an equation for the central point in terms of its 4neighbours as

Ti, j =1/2

(∆y)2+(∆x)2

(

(∆y)2(Ti+1, j +Ti−1, j)

+(∆x)2(Ti, j+1+Ti, j−1))

But how does this help?

Use this equation as the basis for an iterative scheme. Startwith some initial guess for thetemperature in the slab (that is an initial guess for all theTi, j), recalling that the temper-ature on the boundary is known. Now use the above equation to update the central pointfrom the values of its 4 neighbours. Sweep across thex andy directions repeatedly untilthe solution converges. So the equation is

T n+1i, j =

1/2(∆y)2+(∆x)2

(

(∆y)2(T ni+1, j +T n

i−1, j

)

+(∆x)2(T ni, j+1+T n

i, j−1

))

wheren is a counter that keeps track of which iteration you are up to.n = 0 correspondsto your original guess. You stop when the change in the solution from one iteration to thenext is less than some tolerance.

Page 165: MATLAB Tutorial

Index EXAMPLE WITH DIRICHLET BOUNDARY CONDITIONS 157

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 20V(:,Ny+1)=2 * x’. * (10-x’)+20; % boundary condition on y=5Vnext=V;number_iter=0;denom=2* (dx2+dy2); % this is used often so calculate it outside loop% loop through until solution convergeswhile delta > eps

number_iter=number_iter+1;maxdiff=0;for i=2:Nx

for j=2:NyVnext(i,j)=(dy2 * (V(i-1,j)+V(i+1,j))+dx2 * (V(i,j-1)+V(i,j+1)))/denomdV=V(i,j)-Vnext(i,j); % the difference between successiv e solutions% keep track of the maximum differenceif abs(dV) > maxdiff;

maxdiff = abs(dV);end

endendV=Vnext;delta=maxdiff;

endnumber_iter % show how many iterations were used% plot the solution in 3D in 3 ways, surface plot, mesh plot, co ntourfigure(1)surf(x,y,V’) % surf plots the surfacecolorbar % put a color bar next to it showing what each colorshading interp % use interpolation for the coloring (smooth s the colours)xlabel(’x’); ylabel(’y’); zlabel(’Temperature’);title(’Steady State Temperature’);print -depsc laplace_direct_3Dfigure(2)mesh(x,y,V’) % mesh plots the surface with the mesh stretche d overcolorbar % put a color bar next to it showing what each colorxlabel(’x’); ylabel(’y’); zlabel(’Temperature’);title(’Mesh plot of temperature’)print -depsc laplace_direct_meshfigure(3)contour(x,y,V’) % cont plots a contour plotaxis imagexlabel(’x’); ylabel(’y’); title(’Contour plot of tempera ture’)print -depsc laplace_direct_contour

Page 166: MATLAB Tutorial

158 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index

0

5

10

0

2

4

620

30

40

50

60

70

x

Steady State Temperature

y

Tem

pera

ture

20

25

30

35

40

45

50

55

60

65

70

0

5

10

0

2

4

620

30

40

50

60

70

x

Mesh plot of temperature

y

Tem

pera

ture

20

25

30

35

40

45

50

55

60

65

70

1 2 3 4 5 6 7 8 9

4.5

5

x

y

Contour plot of temperature

Page 167: MATLAB Tutorial

Index IMPROVING THE CONVERGENCE 159

8.2 Improving the convergence

8.3 Gauss-Seidel

The previous method where the new values are calculated overthe entire domain usingthe existing values is known as theJacobi method. This requires storing 2 matrices ofvalues (the old and the new) and is relatively inefficient.

The speed of the iterative scheme convergence can be substantially improved if instead ofsweeping across using the old values we make sure we use the most recently calculatedvalues. This has the added advantage of not requiring the storing of two matrices.

for i=2:Nxfor 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

endend

This is known as theGauss-Seidelmethod. Because the most recent value of the solu-tion is used it greatly improves the efficiency of the method.Typically the Gauss-Seidelmethod uses approximately 25% to 50% less iterations for thesame level of accuracy.

Can you see any problems with this method?

How could you improve on it?

Page 168: MATLAB Tutorial

160 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index

8.3.1 Successive Over Relaxation

It turns out that the Gauss-Seidel method always converges monotonically to the solution.For example

It is quicker if there is an overshoot so that the solution converges in an oscillatory manner

This is obtained by artificially increasing the incrementalchange to the solution

for i=2:Nxfor 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

endend

Introduce a new parameterω which is the ’over-relaxation’ amount. This method isknown asSuccessive Over-Relaxation.If 0 < ω < 1 then the method is under-relaxed and converges slower.If 1 < ω < 2 then the method is over-relaxed and converges quicker.If ω > 2 then the method does not converge.

Page 169: MATLAB Tutorial

Index GAUSS-SEIDEL 161

The optimum value ofω (the one resulting in the fastest convergence) is not a specificvalue but is different for each problem. So how do you know what value to use? Thebest method is to find the approximate value by considering the problem with fewer gridpoints and then using that optimal value for the full problemwith many more grid points.

A typical behaviour of the number of iterations versus the relaxation parameter is forthe number of iterations to slowly decrease asω increases to the optimal value and thento rapidly rise (see figure below). Hence it is usually best totake a value ofω that isslightly less than the optimal value to ensure that you stay in the region where the bestconvergence 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 34for ω ≈ 1.83. This results in the program taking approximately a thirdof the time to runfor no more effort.

Successive Over-relaxation is particularly important forvery large complicated problemsin many dimensions where run time and convergence issues become critical. Most com-mercial software will involve some form of Successive Over-relaxation when solvingsteady state problems such as Laplace’s equation.

1 1.2 1.4 1.6 1.8 230

40

50

60

70

80

90

100

110

120Iterations verses omega for SOR

omega

num

ber

of it

erat

ions

Optimal value of omega

Page 170: MATLAB Tutorial

162 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index

8.3.2 Example

Temperature in a house wall on a Canberra winter night

Consider the problem of a long house wall 2m high and 25cm thick. The wall is at thermalequilibrium. The outside temperature is−5◦C the inside temperature 22◦C. The wallfoundations are in the ground at 15◦C. The top of the wall is insulated by insulation in theceiling of the house.

The governing PDE is Laplace’s Equation in 2D

∂ 2T∂x2 +

∂ 2T∂y2 = 0

subject to the boundary conditions

∂T∂x

= h(T −22) on x = 0

∂T∂x

= −h(T +5) on x = 0.25

T (x,0) = 15

∂T∂y

= 0 on y = 2

0 0.05 0.1 0.15 0.2 0.250

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2Contours of Steady State Temperature

0

2

4

6

8

10

12

14

16

18

20

Page 171: MATLAB Tutorial

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

% Set up all the various constants, vectors and matrices need edinsidetemp=22; outsidetemp=-5; groundtemp=15;L=.25; M=2; Nx=40; Ny=160; dx=L/(Nx-1); dy=M/(Ny-1); eps= 0.2; delta=100;h=100; x=[0:dx:L]’; y=[0:dy:M];

V=groundtemp * ones(Nx,Ny); % initialise solution everywhere to bottomte mpV(1,:)=insidetemp * ones(1,Ny); % initialise inside edge to insidetempV(Nx,:)=outsidetemp * ones(1,Ny);% initialise outside edge to outsidetempnumber_iter=0;denom=2* (dxˆ2+dyˆ2); % this is used often so calculate it outside loo p

% loop through until solution convergeswhile delta > eps

number_iter=number_iter+1; maxdiff=0;% step across in x-direction doing i=1, then internal points , then i=Nx

i=1; % the inside edge i=1 V(i-1,j)=V(i+1,j)-2 * dx * h* (V(i,j)-insidetemp)j=1; % the bottom j=1 V(i,j)=bottomtempV(i,j)=groundtemp;for j=2:Ny-1

dV=(dyˆ2 * (V(i+1,j)-2 * dx * h* (V(i,j)-insidetemp)+V(i+1,j))...+dxˆ2 * (V(i,j-1)+V(i,j+1)))/denom-V(i,j);

V(i,j)=V(i,j)+dV;if abs(dV) > maxdiff; maxdiff = abs(dV); end

endj=Ny; % the top insulated so V(i,j+1)=V(i,j-1)dV=(dyˆ2 * (V(i+1,j)-2 * dx * h* (V(i,j)-insidetemp)+V(i+1,j))...

+dxˆ2 * (V(i,j-1)+V(i,j-1)))/denom-V(i,j);V(i,j)=V(i,j)+dV;

if abs(dV) > maxdiff; maxdiff = abs(dV); end

% the interior points i=2,...,Nx-1for i=2:Nx-1

j=1; % the bottom j=1 V(i,j)=groundtempV(i,j)=groundtemp;for j=2:Ny-1

dV=(dyˆ2 * (V(i-1,j)+V(i+1,j))+dxˆ2 * (V(i,j-1)+V(i,j+1)))/denom - V(i,j);V(i,j)=V(i,j)+dV;if abs(dV) > maxdiff; maxdiff = abs(dV); end

endj=Ny; % the top j=Ny insulated so V(i,j+1)=V(i,j-1)dV=(dyˆ2 * (V(i-1,j)+V(i+1,j))+dxˆ2 * (V(i,j-1)+V(i,j-1)))/denom - V(i,j);V(i,j)=V(i,j)+dV;

if abs(dV) > maxdiff; maxdiff = abs(dV); endend

i=Nx; % the outside edge i=Nx V(i+1,j)=V(i-1,j)-2 * dx * h* (V(i,j)-outsidetemp)j=1; % the bottom j=1 V(i,j)=bottomtempV(i,j)=groundtemp;for j=2:Ny-1

dV=(dyˆ2 * (V(i-1,j)+V(i-1,j)-2 * dx * h* (V(i,j)-outsidetemp))...+dxˆ2 * (V(i,j-1)+V(i,j+1)))/denom-V(i,j);

V(i,j)=V(i,j)+dV;if abs(dV) > maxdiff; maxdiff = abs(dV); end

endj=Ny; % insulated so V(i,j+1)=V(i,j-1)dV=(dyˆ2 * (V(i-1,j)+V(i-1,j)-2 * dx * h* (V(i,j)-outsidetemp))...

+dxˆ2 * (V(i,j-1)+V(i,j-1)))/denom-V(i,j);V(i,j)=V(i,j)+dV;

if abs(dV) > maxdiff; maxdiff = abs(dV); end

delta=maxdiff;fprintf(’number of iterations = %3.0f max(dV) = %6.4f\n’,n umber_iter,maxdiff)

end % while loop finishes when convergence is obtained to acc uracy eps

contlevels=[outsidetemp:1:max(max(V))];contour(x,y,V’,contlevels) % plot contours of the solutio n at contlevelstitle(’Contours of Steady State Temperature’);colorbarprint -depsc wall_contour

Page 172: MATLAB Tutorial

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 topsatisfies a quadratic temperature distribution with maximum 1◦C at the centre and 0◦C atthe edge.

Write down the governing equation, boundary conditions andfind the steady state tem-perature distribution using MATLAB.

Page 173: MATLAB Tutorial

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 z eroV(:,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

% loop through until solution convergeswhile delta > eps

number_iter=number_iter+1;maxdiff=0;for j=2:Nz

i=1;Vnext=(4 * dzˆ2 * (V(i+1,j))+drˆ2 * (V(i,j-1)...

+V(i,j+1)))/(2 * drˆ2+4 * dzˆ2);V(i,j)=Vnext;for i=2:Nr

Vnext=(dzˆ2 * (V(i-1,j)+V(i+1,j))+drˆ2 * (V(i,j-1)+V(i,j+1))...+dzˆ2/(2 * (i-1)) * (V(i+1,j)-V(i-1,j)))/denom;

dV=V(i,j)-Vnext;V(i,j)=Vnext;if abs(dV) > maxdiff; maxdiff = abs(dV); end

endenddelta=maxdiff;

end

number_iter % show how many iterations were usedfigure(1)mesh(r,z,V’) % plot the solution in 3Dtitle(’Steady State Temperature’);print -depsc laplace_cylinder_3D

figure(2)contour(r,z,V’) % plot contours of the solutiontitle(’Contours of Steady State Temperature’);print -depsc laplace_cylinder_contours

Page 174: MATLAB Tutorial

166 8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION Index

00.2

0.40.6

0.81

0

0.5

1

1.5

20

0.2

0.4

0.6

0.8

1

Steady State Temperature

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2Contours of Steady State Temperature

Page 175: MATLAB Tutorial

9 Hyperbolic Equations: Wave Equation

9.1 Finite Differences

The wave equation∂ 2F∂ t2 = c2∇2F

allows time stepping as was used for the diffusion equation but with a significant differ-ence at the beginning of the process. Since the time derivative is 2nd order there are 2initial conditions. For example for a one dimensional problem they could be

F(x,0) = F0(x)∂F∂ t

(x,0) = V0(x)

Divide the region up into a grid so that there areNx sections in thex-direction andNt

sections in thet-direction. Leti be the index for thex-direction andj the index for thet-direction. So in this case LetF j

i be shorthand notation for the value at the position(xi, t j).Using centred time and space finite differences to approximate the derivatives in the waveequation gives

F j+1i −2F j

i +F j−1i

(∆t)2 = c2F ji+1−2F j

i +F ji−1

(∆x)2

This can be rearranged so that the solution at the new time (j +1) can be written in termsof the current time (j) and the previous time (j−1) as

F j+1i = γ2

(

F ji+1−2F j

i +F ji−1

)

+2F ji −F j−1

i

where

γ2 =c2(∆t)2

(∆x)2

That is the template used is a cross pattern

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

b b

b

b

rs

i i+1i−1

j

j +1

j−1

The significant difference between this scheme and the FTCS scheme for the diffusionequation is that here the new value depends on the previoustwo time steps. The problem

Page 176: MATLAB Tutorial

168 9. HYPERBOLIC EQUATIONS: WAVE EQUATION Index

with this is at the beginning of the process as initially onlyvalues at one time level (theinitial time) are known. To overcome this the easiest methodis to make a forward timeapproximation of the initial derivative boundary condition to get values at the next timelevel. If the initial derivative boundary condition at theith space point is

∂F∂ t

i= V0i

then approximate this by a forward time difference to get

∂F∂ t

i=

F1i −F0

i

∆t= V0i

Rearrange to get values at time levelj = 1 as

F1i = F0

i +V0i∆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 fromstability 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 aparabolic shape betweenx = 25 and 33cm and then released. The governing differentialequation is the one dimensional wave equation

∂ 2F∂ t2 = c2∂ 2F

∂x2

subject to the boundary conditions

F(0, t) = 0 F(100, t) = 0

and initial conditions

F(x,0) = given parabolic shape∂F∂ t

(x,0) = 0

Page 177: MATLAB Tutorial

Index EXAMPLE 169

MATLAB code

% waveqn.m Finite difference solution to the wave equationg2 = .6; c = 30; N = 500; L = 100; dx = L/(N);x = [dx:dx:L-dx]’; n = length(x); dt = sqrt(g2) * dx/c;% set initial pulse for L/6 < x < L/4 as a quadratic

xleft = x(round(n/6)); xright = x(round(n/4));Y0 = zeros(n,1);I = find(x >= xleft & x <= xright);Y0(I) = (x(I)-xleft). * (xright-x(I));Y0 = Y0/max(Y0);

%A = sparse(diag(2 * (1-g2) * ones(n,1)) + ...

g2* diag(ones(n-1,1),1) + g2 * diag(ones(n-1,1),-1));Y1 = Y0; % use same initial condition for first 2 time stepsfor i = 2:1:400 % loop through time step

t = i * dt;Y2 = A* Y1-Y0; % calculate solution at next timeY0 = Y1; Y1 = Y2; % update solutions

end% plot solutionplot([0 0],[1 -1]); hold on; plot([100 100],[1 -1]);text(-8,0,’y’); text(-8,.95,’1’); text(-10,-.95,’-1’) ;plot(x,Y1); axis([0 L -1 1]); axis off; hold off

1

−1

y

t = 0

y

1

−1

t = 0.10328

y

1

−1

t = 0.20656

y

1

−1

t = 0.30984

y

1

−1

t = 0.41312

y

1

−1

t = 0.5164

y

1

−1

t = 0.61968

y

1

−1

t = 0.72296

y

1

−1

t = 0.82624

y

1

−1

t = 0.92952

y

1

−1

t = 1.0328

y

1

−1

t = 1.1361

y

1

−1

t = 1.2394

y

1

−1

t = 1.3426

y

1

−1

t = 1.4459

Page 178: MATLAB Tutorial

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 noticeform 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. Thisis numerical error beginning to show up. This sort of error istypical of what is found withnumerical solutions of hyperbolic equations.

y

1

−1

Page 179: MATLAB Tutorial

10 Concluding Remarks

10.1 Irregular Boundaries

All the examples used for PDEs have involved regular boundaries that are easily describedwithin the coordinate system in use (cartesian, cylindrical etc). These kinds of problemsinvolving regular boundaries are ideally suited to finite difference methods as it is rela-tively simple to set up a grid that accurately represents theshape of interest.

Shapes with irregular boundaries are better dealt with using Finite Element Methodsrather than Finite Difference Methods. This is because for the finite element method meshpoints can be arbitrarily placed on the boundary and the meshis ’triangularated’ thus al-lowing for complicated shapes to be easily covered. Contrast this to the finite differencemethod where typically you use a regular grid which fitting complicated boundaries tois often difficult. One difficulty with the finite element method is then choosing how todistribute the mesh points. This needs some knowledge of howyou expect the solution tobehave.

10.2 Commercial Software

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 othersare more multipurpose.

MATLAB PDE Toolbox:solves fairly general Parabolic, Hyperbolic, and Ellipticequations 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 verypowerful.

FLUENT:Solves many general PDEs using finite element methods. Primarily used for heat andmass flow problems.

FlexPDE:Solves many general PDEs using finite element methods. Best at 2D, the demo version isavailable free (for Windows and Linux) athttp://www.pdesolutions.com. It only takes afew minutes to download and has numerous very good examples.Easy to use.

There are numerous others as well. Most commercial softwareuses finite elements ratherthan finite differences specifically so that more complicated regions can be analysed.

Page 180: MATLAB Tutorial

172 10. CONCLUDING REMARKS Index

10.3 Finally

This has only been a very brief look at numerical solutions ofODEs and PDEs. Thecomplexities and subtleties go way beyond what we have presented here. Hopefully youhave now an appreciation of at least some of the ideas of numerical solutions and why itis important to be able to calculate at least some analytic solutions.

Page 181: MATLAB Tutorial

Index

analytic solutions,119

boundary conditions,117, 141Cauchy,117, 141, 145Dirichlet, 117, 141mixed,117, 141, 145Neumann,117, 141, 142

BVP, 79relaxation,84shooting,80, 82

Cauchy boundary condition,117, 141, 145centre,68competition model,70convergence,159convolution theorem,27Crank-Nicolson method,134critical points,68, 70, 74cylindrical coordinates,115, 148

delta function,25diag,88diagonal matrix,87, 92, 135diary,7differential eigenvalue equation,101diffusion equation,111, 112, 125, 138Dirac delta function,25Dirichlet boundary condition,117, 138, 141,

155discretisation,126, 156, 167disease,73drum,123

eigenvalue,7, 75electrical potential,113elliptic PDE,124, 155errors,44Euler’s method,43

example,45explicit, 135

Fastflo,171finite differences,84, 125, 156, 167, 171finite element,93finite elements,171FlexPDE,171Fluent,171FTCS,126, 150, 151ftcs,167

example,130

template,129functions,9

passing names,11vector input,10

fzero,13, 102

Gauss-Seidel,92, 159ground water,113

heat conduction,112Heaviside function,23help,8hyperbolic PDE,124, 167

implicit, 135initial conditions,116irregular boundaries,171iterative scheme,156IVP, 41

intoduction,41numerical,43system,41, 101

Jacobi method,159jacobian,70, 75

L’Hopitals rule,149Laplace transform,23

convolution theorem,27definition,23derivative,26, 27first shifting theorem,26integral,27inversion,29MATLAB, 33second shifting theorem,26systems,38theorems,27

Laplace’s equation,111, 113, 155Laplacian,113, 148linear,110linear equations,6Lokta-Volterra,68lookfor, 8

M files, 1running,7web site,1

MATLABilaplace,36

Page 182: MATLAB Tutorial

174 INDEX Index

laplace,28Laplace transform,33partial fractions,33residue,33

MATLAB commands,20diag,88diary,7eig,7fzero,13help,8lookfor, 8ode23,54ode45,14, 56odeset,59ones,88quad,11quiver,66

MATLAB PDE toolbox,171matrix equations,92method of lines,138, 142midpoint method,50, 54

Neumann boundary condition,117, 141, 142Newtonian heat loss,141, 145node,75

ode23,54ode45

example,14options,59Runge-Kutta,56

ones,88order,44

parabolic PDE,124, 125partial fractions,29

distinct factors,29inversion,31MATLAB, 33non linear factors,31repeated factors,30

PDE,109classification,124definitions,110elliptic, 124, 155examples,109hyperbolic,124, 167linear,110parabolic,124, 125separation of variables,119

phase plane,63

plotting,33D, 5data,4

Poisson’s equation,115predator-prey,68projectile,60

quiver,66

radar,114relaxation method,84

example,85Runge-Kutta,53, 138

fourth order,56second order,53

running scripts,7

saddle,68, 75seismic,114sending output to a file,7shooting,102

example,82shooting method,80, 82Simpson’s rule,11SIR disease model,73software,171sonar,114spherical coordinates,115, 148stability,132, 135, 168step function,23step size,46

algorithm,47successive over relaxation,160susceptible,73system of equations,41

Taylor series,43, 53, 54, 79, 149

vector field,66velocity potential,113

wave equation,111, 114, 167web site,i, 1