This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA

Download & View **Matlab Tutorial** as PDF for free.

**Words:**38,008**Pages:**182

Loading documents preview...

ZPEM3311 Mathematical Methods for Differential Equations Numerical Methods and Techniques using MATLAB

c

Dr. G.N. Mercer

Index

Preface

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

Using the electronic version of these notes These notes are hyperlinked. All green text is a link to somewhere else within this document. For example the contents page links to the appropriate page in the text, the numbers in the index link to the page reference, the word Index in the header of most pages links to the index and the page numbers in the header on each page link back to the contents page. There are also some internal linked words that take you to the relevant text. Links to external web pages are red in colour. Provided your PDF reader (eg Adobe Acrobat Reader) is set up correctly these links should open the appropriate page in your web browser (eg Microsoft Internet Explorer, Netscape, Mozilla).

MATLAB code The MATLAB codes used in these notes are available at the course webCT site. MATLAB code in the text is usually shown in purple and the resulting output in blue. If you have any difficulties with any part of the course do not hesitate in contacting me to sort them out. Geoff Mercer Room G12 Science South School of Physical, Environmental and Mathematical Sciences Phone: 6268 8734 Email:[email protected]

ii

Index

Contents

1 Introduction to MATLAB 1.1 Access to MATLAB . . . . . . . . . . . . . 1.2 Using MATLAB . . . . . . . . . . . . . . . 1.3 A few simple examples . . . . . . . . . . . . 1.4 Running MATLAB scripts . . . . . . . . . . 1.5 Sending output to a file . . . . . . . . . . . . 1.6 MATLAB help facility and lookfor command 1.7 Functions in MATLAB . . . . . . . . . . . . 1.8 Functions in MATLAB – vector input . . . . 1.9 Passing function names . . . . . . . . . . . . 1.10 MATLAB finding zeros . . . . . . . . . . . . 1.11 An Introduction to solving DEs numerically . 1.12 Symbolic Manipulation . . . . . . . . . . . . 1.12.1 Differentiating using MATLAB . . . 1.12.2 Integrating using MATLAB . . . . . 1.12.3 Solving equations . . . . . . . . . . . 1.13 Some Elementary MATLAB Commands . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

2 Laplace Transforms 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . 2.2 Some standard results in Laplace Transforms . . . 2.2.1 Heaviside function . . . . . . . . . . . . . 2.2.2 Dirac delta function . . . . . . . . . . . . . 2.2.3 First shifting theorem . . . . . . . . . . . . 2.2.4 The second shifting theorem . . . . . . . . 2.2.5 Transforms of derivatives . . . . . . . . . . 2.2.6 Multiplication by t . . . . . . . . . . . . . 2.2.7 Integral theorem . . . . . . . . . . . . . . 2.2.8 Division by t . . . . . . . . . . . . . . . . 2.2.9 Convolution theorem . . . . . . . . . . . . 2.2.10 Taking Laplace transforms using MATLAB 2.3 Inversion of Laplace transforms . . . . . . . . . . 2.3.1 Partial fractions . . . . . . . . . . . . . . . 2.3.2 Application of partial fractions to inversion 2.3.3 MATLAB functions for partial fractions . . 2.3.4 Inversion using MATLAB . . . . . . . . . 2.3.5 Systems of Equations . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

1 1 1 2 7 7 8 9 10 11 13 14 17 17 19 19 20

. . . . . . . . . . . . . . . . . .

23 23 23 23 25 26 26 26 27 27 27 27 28 29 29 31 33 36 38

iv

CONTENTS

Index

3 Initial Value Problems 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . 3.2 Numerical approach . . . . . . . . . . . . . . . . . 3.3 Euler’s method . . . . . . . . . . . . . . . . . . . 3.3.1 Graphically . . . . . . . . . . . . . . . . . 3.3.2 Errors and order of the method . . . . . . . 3.3.3 Adjusting the step size . . . . . . . . . . . 3.4 Improvements to Euler’s method - midpoint method 3.5 Second order Runge-Kutta method . . . . . . . . . 3.6 Fourth order Runge-Kutta method . . . . . . . . . 3.7 ode45 . . . . . . . . . . . . . . . . . . . . . . . . 3.8 Projectile example . . . . . . . . . . . . . . . . . 4 Phase Plane Analysis 4.1 Introduction . . . . . 4.1.1 Quiver plots . 4.2 Predator-Prey model 4.3 Competition model . 4.4 SIR disease model . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

5 Boundary Value Problems 5.1 Introduction . . . . . . . . . . . . . . . . . . . 5.2 Shooting method . . . . . . . . . . . . . . . . 5.2.1 Shooting method using guesses . . . . 5.2.2 Shooting method using fzero . . . . . . 5.3 Relaxation methods . . . . . . . . . . . . . . . 5.3.1 Finite differences . . . . . . . . . . . . 5.3.2 Using finite differenes to solve a BVP . 5.3.3 Comments on solving matrix equations 5.3.4 Finite elements . . . . . . . . . . . . . 5.3.5 Symbolic form of a BVP . . . . . . . . 5.3.6 Finite element theory . . . . . . . . . . 5.3.7 Finite element example . . . . . . . . . 5.3.8 Finite element summary . . . . . . . . 5.4 Differential eigenvalue equations . . . . . . . . 6 Partial Differential Equations (PDEs) 6.1 Overview . . . . . . . . . . . . . . 6.2 Functions of more than one variable 6.2.1 Types of problems . . . . . 6.2.2 Examples of PDEs . . . . . 6.3 Basic definitions . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

41 41 43 43 44 44 46 50 53 56 57 60

. . . . .

63 63 66 68 70 73

. . . . . . . . . . . . . .

79 79 80 80 82 84 84 85 92 93 94 94 97 100 101

. . . . .

109 109 109 109 109 110

Index

6.4 6.5

6.6

6.7

6.8 6.9 6.10

6.11 6.12

6.13 6.14

CONTENTS 6.3.1 Order of a PDE . . . . . . . . . . . . . . . 6.3.2 Linear PDEs . . . . . . . . . . . . . . . . 6.3.3 Why use numerical methods . . . . . . . . Three common PDEs in applications . . . . . . . . The diffusion equation . . . . . . . . . . . . . . . 6.5.1 Heat conduction . . . . . . . . . . . . . . 6.5.2 Mass diffusion . . . . . . . . . . . . . . . 6.5.3 Diffusion equation in 2D and 3D . . . . . . Laplace’s equation . . . . . . . . . . . . . . . . . 6.6.1 Equilibrium solutions . . . . . . . . . . . . 6.6.2 Other applications . . . . . . . . . . . . . The wave equation . . . . . . . . . . . . . . . . . 6.7.1 Sonar, radar, seismic waves . . . . . . . . 6.7.2 2D and 3D waves . . . . . . . . . . . . . . Poisson’s equation . . . . . . . . . . . . . . . . . Other co-ordinate systems . . . . . . . . . . . . . Boundary and initial conditions . . . . . . . . . . . 6.10.1 ICs and BCs . . . . . . . . . . . . . . . . 6.10.2 Number of initial and boundary conditions 6.10.3 A typical initial condition . . . . . . . . . 6.10.4 Typical boundary conditions . . . . . . . . Types of boundary conditions . . . . . . . . . . . . Different solution approaches . . . . . . . . . . . . 6.12.1 Separation of variables . . . . . . . . . . . 6.12.2 Other approaches . . . . . . . . . . . . . . 6.12.3 Numerical techniques . . . . . . . . . . . . 6.12.4 Value of analytic solutions . . . . . . . . . Examples . . . . . . . . . . . . . . . . . . . . . . Classification of PDEs . . . . . . . . . . . . . . .

7 Parabolic Equations: Diffusion Equation 7.1 Finite differences . . . . . . . . . . . . 7.1.1 First Derivative . . . . . . . . . 7.1.2 Second Derivative . . . . . . . 7.2 FTCS method . . . . . . . . . . . . . . 7.2.1 Discretise the region . . . . . . 7.2.2 Template for the FTCS Method 7.2.3 FTCS example . . . . . . . . . 7.2.4 Stability . . . . . . . . . . . . . 7.2.5 Unstable Example . . . . . . . 7.3 Crank-Nicolson method . . . . . . . . . 7.3.1 Template . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

v . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

110 110 110 111 112 112 112 112 113 113 113 114 114 114 115 115 116 116 116 116 116 117 119 119 119 119 119 120 124

. . . . . . . . . . .

125 125 125 125 126 126 129 130 132 133 134 134

vi

CONTENTS

7.4 7.5

7.6 7.7

Index

7.3.2 Matrix Representation . . . . 7.3.3 Example . . . . . . . . . . . Method of lines . . . . . . . . . . . . Other boundary conditions . . . . . . 7.5.1 Neumann Boundary Condition 7.5.2 Cauchy Boundary Condition . Nonlinear Example . . . . . . . . . . Other co-ordinate systems . . . . . . 7.7.1 Disc example . . . . . . . . . 7.7.2 L’Hopital’s Rule . . . . . . . 7.7.3 FTCS Method . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

8 Elliptic Equations: Laplace’s Equation 8.1 Example with Dirichlet boundary conditions 8.2 Improving the convergence . . . . . . . . . 8.3 Gauss-Seidel . . . . . . . . . . . . . . . . 8.3.1 Successive Over Relaxation . . . . 8.3.2 Example . . . . . . . . . . . . . . 8.3.3 Example . . . . . . . . . . . . . . 9 Hyperbolic Equations: 9.1 Finite Differences 9.1.1 Stability . 9.2 Example . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

135 136 138 141 142 145 146 148 148 149 150

. . . . . .

155 155 159 159 160 162 164

Wave Equation 167 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

10 Concluding Remarks 171 10.1 Irregular Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 10.2 Commercial Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 10.3 Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

1 Introduction to MATLAB

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

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

1.2 Using MATLAB MATLAB is an interactive program so you can enter commands at the prompt or read in files with lines of MATLAB commands. For relatively simple tasks it is acceptable to enter commands at the prompt but for more complicated tasks it is better to store the commands in a file and then read in the file. This makes it easy to change the commands and to store them. In fact all inbuilt MATLAB commands are written as files of commands. For this reason there are vast numbers of MATLAB files (called M-files) written for various purposes available on the internet. A central repository of contributed M-files can be found at http://www.mathworks.com/matlabcentral/. For this course you should do your work using M-files as these will be handed up as part of your assignments along with the relevant output. All M-files given in the notes will be available from the course webCT page. Many of the assignment questions will be a modification of these files.

2

1. INTRODUCTION TO MATLAB

Index

1.3 A few simple examples 1. Matrix Manipulation Enter a 2× 2 matrix find its inverse, and eigenvalues and eigenvectors. Check that its inverse times itself is the identity. MATLAB code % any thing after a % sign is a comment A=[1,2;3,4] % enter the 2x2 matrix and call it A B=inv(A) % calculate the inverse of A and call it B eig(A) % calculate the eigenvalues of A [V,D]=eig(A) % calc the eigenvectors (columns of matrix V) % diagonal matrix D (eigenvalues on diagonal) check=B*A If these commands are stored in an M-file called example1.m then to run it type example1 (without the .m) from within MATLAB. (Note you have to be in the same directory as the file is saved in, or have made that directory in the path that MATLAB looks in). The MATLAB output from these commands is the following A = 1 3

2 4

B = -2.0000 1.5000

1.0000 -0.5000

ans = -0.3723 5.3723 V = -0.8246 0.5658

-0.4160 -0.9094

-0.3723 0

0 5.3723

D =

check = 1.0000 0.0000

0 1.0000

Index

3

A FEW SIMPLE EXAMPLES

2. Plotting MATLAB has very good graphics capability. The easiest method for simple plots is to define a vector (for example x) and then perform a function on each element of that vector (eg y(x)) and then plot y(x) versus x. Defining a vector is straightforward. The MATLAB command x=a:b:c defines x to be a vector of values starting at a increasing in steps of b to c. For example x=-4:3:8 results in x being the vector [−4, −1, 2, 5, 8]. Example. Plot y = x2 + sin(xπ ).

MATLAB code 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

25

20

15

10

5

0

−5 −5

0

5

Index

1. INTRODUCTION TO MATLAB

3. Plotting data It is very easy to read data in from a file, manipulate it and plot it. The command load is used to load data from an external file. For example read data from a file called datafile, calculate the line of best fit to the data and add some titles, legend and text to the plot. MATLAB code % read data from the file datafile. This puts it into % a variable matrix called datafile by default load datafile x=datafile(:,1) % put first column into variable x y=datafile(:,2) % put second column into variable y % Fit a linear regression line to the data t=[ones(size(x)) x]; coeffofregn=t\y straightline=[ones(size(x)) x]*coeffofregn; % Plot the data and line and add titles and labels plot(x,y,’ro’,x,straightline,’b-’) title(’data and line of best fit’) xlabel(’x’) ylabel(’y’) % 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) data and line of best fit 70 data line of best fit 60

50

y

4

40

30

20

10 0

This is text

1

2

3

4 x

5

6

7

8

Index

5

A FEW SIMPLE EXAMPLES

4. 3D Plotting MATLAB has very good 3D graphics capability. The easiest method is do define the domain using the meshgrid command, then define the function and then plot it using mesh or surf commands. Example. Plot z = 4x2 − 2y2 . MATLAB code

[x,y]=meshgrid(-5:0.5:5,-2:1.0:7); % define the mesh (x,y) the domain of plotting % x from -5 to 5 in steps of 0.5 % y from -2 to 7 in steps of 1. z=4*x.*x-2*y.ˆ2; % calculate z=4xˆ2-2yˆ2 % note the .* and .ˆ this is because x and y are % vectors so we need to multiple ’component by component’ mesh(x,y,z) % draw the surface as a mesh % or use surf(x,y,z) to draw it as a surface print -depsc example2a % send the plot output to a colour postscript file

100 50 0 −50 −100 10 5

5 0

0 −5

−5

6

Index

1. INTRODUCTION TO MATLAB

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 example to solve a linear equation system Ax = b there is a command known as ’backslash divide’. MATLAB code A=[1,2,3;4,5,6;7,8,10]; b=[1,1,1]’; x=A\b

% % % %

define the matrix A note the ’ to take transpose or could do b=[1;1;1] solve for x

x = -1.0000 1.0000 0.0000 6. Extracting Segments of Matrices Often you may want to extract sections of a matrix. For example all of the first row or 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 A d=A(:,2) % extract all of the second column of A

c = 1

d = 2 5 8 diary off

2

3

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 MATLAB commands into a file and then run that file. These types of files are called M-files and have the extension .m after their name. To run an M-file from MATLAB make sure you are in the directory where the file is located and then just type the name of the file without the extension (i.e. without the .m). MATLAB will then process all the commands that are in that file. Example 1. Create a file called example1.m 2. In that file put the following lines format compact A=[1,2;3,-4] C=Aˆ10 eig(C)

% % % %

this this this this

outputs in a compact form enters the 2x2 matrix A finds the tenth power of A 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 3

2 -4

C = 1395967 -4184829 ans = 1024 9765625

-2789886 8370682

1.5 Sending output to a file Often you want to keep the output of a MATLAB command. The diary commands does that for you. By typing diary example1.out all subsequent output will sent to the file example1.out as well as to the screen. To turn the diary command off use diary off

8

1. INTRODUCTION TO MATLAB

Index

1.6 MATLAB help facility and lookfor command MATLAB has a comprehensive help facility. It is accessed by typing help followed by the 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 window input and most of the resulting command window output to be appended to the named 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 be used 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 differentiate POLYDER Differentiate polynomial. FNDER Differentiate a function. DIFF Differentiate.

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-file that takes some kind of input and returns some calculations on that input. For example write a function that calculates f (x) = x3 + 4x2 + 3 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 calculate f (3) this results in the following output calcf(3) ans = 66

10

Index

1. INTRODUCTION TO MATLAB

1.8 Functions in MATLAB – vector input Since MATLAB deals with vectors of numbers it is more useful to write functions so that they can handle vectors rather than an individual number. The only change needed to achieve this is to ensure that the calculations within the function are done on an elementby-element basis by using the .* notation instead of the * notation (and equivalently .ˆ, ./ etc). For example write a function that calculates f (x) = x3 + 4x2 + 3 for an input vector x. That is, it should calculate f (x) for each value of a vector x and return the answer in a vector. MATLAB code function f=calcfvec(x) % this file is named calcfvec.m % this function calculates f(x) for a vector x and % returns the answers in the variable f % f = x.ˆ3 + 4.*x.ˆ2 + 3; return From within MATLAB type y=0:0.2:1.0 calcfvec(y) to calculate f (y) this results in the following output y=0:0.2:1.0 y = 0 calcfvec(y) ans = 3.0000

0.2000

0.4000

0.6000

0.8000

1.0000

3.1680

3.7040

4.6560

6.0720

8.0000

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. For example if you want to numerically calculate the integral of a function MATLAB has an inbuilt function that does this but somehow you need to tell it what function to integrate. Hence you need to pass the functions name. MATLAB has two inbuilt functions for calculating integrals [A,count]=quad(’funcname’,a,b,tol,trace) [A,count]=quad8(’funcname’,a,b,tol,trace) quad uses Simpson’s rule, quad8 uses a degree 8 polynomial. tol is the tolerance for the convergence using a comparison between successive approximations. trace is an additional feature for plotting a trace of the integrand used. The answer is returned in A and count is a count of how many function evaluations have been performed. funcname is the name of the function to be passed. For example if integrating the function defined in the M-file quadexamplefunc.m then use quadexamplefunc where ever you see funcname. Important Note: The function referenced by ’funcname’ must be written to take a vector input and give a vector output. Example Calculate

2 f (x) = √ π

Z x 0

2

e−t dt

For x = 1. This function is known as the error function (erf(x) in MATLAB) MATLAB code % quadexample.m format long % use lots of decimal places % the ’exact’ answer using MATLABs erf function errorfunc=erf(1) % numerically integrate using Simpsons rule (quad) integral=quad(’quadexamplefunc’,0,1,1e-6) % now do it again using an 8th order polynomial (quadl) integral=quadl(’quadexamplefunc’,0,1) % count the number of function calls using the trace switch trace=1; [integral,count]=quad(’quadexamplefunc’,0,1,1e-6,trace)

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

12

Index

1. INTRODUCTION TO MATLAB

>> quadexample errorfunc = 0.84270079294971

integral = 0.84270079342046

integral = 0.84270079427671 5 7 9 11 13 15 17

0.0000000000 0.0000000000 0.0000000000 0.2500000000 0.5000000000 0.5000000000 0.7500000000

integral = 0.84270079342046

count = 17 diary off

1.00000000e+00 5.00000000e-01 2.50000000e-01 2.50000000e-01 5.00000000e-01 2.50000000e-01 2.50000000e-01

0.8427115995 0.5204995573 0.2763263864 0.2441734868 0.3222012775 0.1906557580 0.1315451622

Index

MATLAB FINDING ZEROS

13

1.10 MATLAB finding zeros MATLAB has an inbuilt function for finding zeros of a function called fzero . Its use is r=fzero(’funcname’,x0) searches for a region with a zero of the function defined in funcname.m then uses bisection 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 value and return a real scalar value when called with feval: F=feval(FUN,X). The value X returned by FZERO is near a point where FUN changes sign, or NaN if the search fails. X = FZERO(FUN,X0), where X is a vector of length 2, assumes X0 is an interval where the sign of FUN(X0(1)) differs from the sign of FUN(X0(2)). An error occurs if this is not true. Calling FZERO with an interval guarantees FZERO will return a value near a point where FUN changes sign. X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting guess. FZERO looks for an interval containing a sign change for FUN and containing X0. If no such interval is found, NaN is returned. In this case, the search terminates when the search interval is expanded until an Inf, NaN, or complex value is 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.m f=x.ˆ3-2*x.ˆ2-9; return

14

Index

1. INTRODUCTION TO MATLAB

1.11 An Introduction to solving DEs numerically MATLAB has many ways of numerically solving Differential Equations. Here we will present one method, the details of the method and the mathematics behind it will be explained in later lectures. This will give you enough now to be able to numerically solve many of the DEs you will encounter early in this course. Consider a first order ODE dy = f (t, y) dt subject to some initial condition (at time t = 0) y(0) = y0 The MATLAB function ode45 will solve these kind of problems. Here is an excerpt from the help on ode45 ODE45 Solve non-stiff differential equations, medium order method. [T,Y] = ODE45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y’ = f(t,y) from time T0 to TFINAL with initial conditions Y0. Function ODEFUN(T,Y) must return a column vector corresponding to f(t,y). Each row in the solution array Y corresponds to a time returned in the column vector T. To obtain solutions at specific times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. Example [t,y]=ode45(@vdp1,[0 20],[2 0]); plot(t,y(:,1)); solves the system y’ = vdp1(t,y), using the default relative error tolerance 1e-3 and the default absolute tolerance of 1e-6 for each component, and plots the first component of the solution.

There are 3 things you need to define to use MATLAB to solve this type of DE. 1. The range of t you wish to calculate the solution over (TSPAN=[T0,TFINAL]) 2. The initial condition on y (Y0) 3. An M-file with the function f (t, y) (ODEFUN)

Notes 1. t is returned as a vector of all the times where the solution was determined. 2. Because ode45 is set up to solve systems of equations y is a matrix. The first dimension corresponds to t, that is solutions at the times in t, the second dimension is the number of equations which in this case is just 1. This has an impact on how the ODEFUN function is written. See the next example to see how this works.

Index Example Solve

AN INTRODUCTION TO SOLVING DES NUMERICALLY

dy = y − 2π cost dt

subject to

15

y(0) = 3

over the range t = [0, 5]. Note that you could solve this particular DE using the Integrating Factor method. The numerical solution works for DEs that can not be solved analytically. This is the point of developing numerical methods for solving DEs, they let you get solutions that can not be obtained analytically. MATLAB code % odeex1.m % solves y’= y-2*pi*cos(t) subject to y(0)=3 % over the range t=[0,5] % tspan=[0,5] % sets up the time span y0=3 % the initial condition % solve the DE and return solution in t and y [t,y]=ode45(’odeex1fun’,tspan,y0) % now plot the solution, note that t is a vector of all the % points in time where the solution was calculated and y is a % matrix whose first dimension corresponds to t and second % dimension is the number of equations (here just 1) plot(t,y(:,1)) % now send the plot to a file called odeex1.eps to be saved print -deps odeex1 MATLAB code function f=odeex1fun(t,y) % odeex1fun.m % the RHS of the system of DEs % note using f(1) and y(1) as these can be vectors % of longer than 1 if you have more than 1 equation f(1)=y(1) - 2*pi*cos(t); f=f(:); % forces f to be a column vector return

16

Index

1. INTRODUCTION TO MATLAB

5

0

−5

−10

−15

−20 0

1

2

3

4

5

The same files without all the extraneous comments MATLAB code % odeex1.m tspan=[0,5] y0=3 [t,y]=ode45(’odeex1fun’,tspan,y0) plot(t,y(:,1)) print -deps odeex1 MATLAB code function f=odeex1fun(t,y) % odeex1fun.m f(1)=y(1) - 2*pi*cos(t); f=f(:); return To solve other single equation DEs simply alter the equation (f(1)=. . . ), the time range (tspan) or the initial condition (y0).

Index

SYMBOLIC MANIPULATION

17

1.12 Symbolic Manipulation MATLAB can also do some (limited) symbolic manipulation of equations such as finding roots of equations, simplifying, differentiating and integrating. Firstly one much declare the variable(s) in question to be symbolic in nature (so that MATLAB knows they are not just a number) and then it is possible to manipulate them.

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 numbers syms x y % define a function of x f=xˆ3-3*y-cos(xˆ2)-atan(x) % differentiate f with respect to x dfdx=diff(f,x) % and the second derivative in 2 ways d2fdx2=diff(dfdx,x) d2fdx2=diff(f,x,2)

% function of 2 variables so need to do partial derivatives % define a function of x and y g=x*y-sin(x-y)+yˆ3*cos(1/y) % find the partial derivatives dgdx=diff(g,x) dgdy=diff(g,y) % find the second derivatives % either take derivatives of above or use diff with extra parameter d2gdx2=diff(dgdx,x) d2gdx2=diff(g,x,2) d2gdy2=diff(dgdy,y) d2gdy2=diff(g,y,2) % and the cross derivative is d2gdxdy=diff(dgdx,y)

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)

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 variable syms x % define a function f f=xˆ2-sin(2*x)+cosh(3*x)-exp(-x) % integrate f with repsect to x intf=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 example to solve systems of equations you can use solve and to solve differential equations symbolically you can use dsolve. These will be discussed at a later time in the course.

20

Index

1. INTRODUCTION TO MATLAB

1.13 Some Elementary MATLAB Commands Input x = [-3:2:7] x(2) y = x.ˆ2

plot(x,y)

Output Comment x = -3 -1 1 3 5 7 x is a vector that starts at –3 and goes up in steps of 2 to 7 -1 the second element of the vector x y = 9 1 1 9 25 49 using ’dot’ notation square the vector x ’component by component’ plot y versus x

50 45 40 35 30 25 20 15 10 5 0 −4

−2

0

2

4

6

8

plot y vs x with a red dash line hold the plot in place add blue circles to the plot

plot(x,y,’r--’) 50 45

hold on plot(x,y,’bo’)

40 35 30 25 20 15 10 5 0 −4

−2

0

2

4

6

8

t is a vector 0 to 2π in steps of π /50 Semicolon supressess the output.

t = 0:pi/50:2*pi; a=cos(t); 1 0.8

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

0.6 0.4

The plot is a parametric plot of a circle with green dots

0.2 0 −0.2 −0.4 −0.6 −0.8 −1 −1

c=[-3:1.0:5]; d=[-2:0.5:7]; [x,y]=meshgrid(c,d)

−0.5

0

0.5

1

3D plotting Set up the domain in c and d Make a mesh of values in x and y The function z = 4x2 − 2y2 Plot the function in 3D

100 50

z=4*x.ˆ2-2*y.ˆ2; mesh(x,y,z)

0 −50 −100 10 5

5 0

0 −5

title(’text here’) xlabel(’text here’) axis([0 5 -3 8]) text(1,20,’blah’)

−5

add a title to a plot add x axis label to a plot set the x axis to be from 0 to 5 and the y axis from -3 to 8 place some text on the plot at the point (1,20)

Index

SOME ELEMENTARY MATLAB COMMANDS

21

Input x=[1 2 3] length(x) y=x’

Output x = 1 2 3 3 y = 1 2 3

z=x*x z=x*y dot(x,y) cross(x,y) A=[1,2;3,4]

error: dimensions dont agree z = 14 vector multiplication 14 dot product of vectors x and y 0 0 0 cross product of vectors x and y A = 1 2 enter the matrix A 3 4 -0.3723 find the eigenvalues of A 5.3723 -2.0000 1.0000 find the inverse of A 1.5000 -0.5000 -2.0000 the determinant of A 37 54 A cubed (A*A*A) 81 118 1 8 each component of A cubed 27 64 1 2 all of the first row of matrix A 2 all of the second column of matrix A 4 b = 1 b is a column vector 4 c = 2.0000 Solve Ac=b using backslash divide -0.5000 0 0 a 2x2 matrix of zeros 0 0 1 1 a 2x2 matrix of ones 1 1 help on the plot command find all references to the word sinh send all output to the file file.txt hold the plot on so that more can be added to it turn the hold off so that a new plot is made read the contents of the file xdata and store the results in a variable called xdata

eig(A) inv(A) det(A) Aˆ3 A.ˆ3 A(1,:) A(:,2)

b=[1;4] c=A\b

zeros(2) ones(2) help plot lookfor sinh diary file.txt hold on hold off load xdata

Comment define a vector calculate the size of the vector y is the transpose of x

22

1. INTRODUCTION TO MATLAB

Index

2 Laplace Transforms

2.1 Introduction The majority of the theory of Laplace Transforms is given in the theoretical section of this course. Here we will investigate how MATLAB can be used to help solve Laplace Transform problems. The notation we will adopt is F(s) = L { f (t)} =

Z ∞ 0

e−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 Laplace Transforms.

2.2.1 Heaviside function The Heaviside step function is defined as (see figure) 0 t

1

H(t-a)

a

t

The Heaviside Step function is like a switch. At time t = a it ’turns on’ with value 1. The Heviside function is defined in MATLAB both as a numerical function and as a symbolic function. Its usage is heaviside(x).

24

Index

2. LAPLACE TRANSFORMS

Example Plot f (x) = H(x − 1)(x2 + 1) MATLAB code % heavisideexample.m % plot H(x-1)*(xˆ2+1) x=-2:0.05:3; y=heaviside(x-1).*(x.ˆ2+1) plot(x,y) axis([-2 3 -1 10]) print -depsc heavisideexample

10 9 8 7 6 5 4 3 2 1 0 −1 −2

−1

0

1

2

3

Index

SOME STANDARD RESULTS IN LAPLACE TRANSFORMS

25

2.2.2 Dirac delta function Suppose that a constant force, f (t), of amount A acts on a body for a short time (of duration ε ).

f(t)

force

A

t - ε/2

t + ε/2

time The impulse delivered by f (t) is I=

Z t0 +ε /2 t0 −ε /2

f (t) dt = ε A

Note that the impulse, I, is a constant. Suppose I = 1, then A = 1/ε . As ε → 0, we must have A → ∞. The width of the rectangle gets smaller and the height gets larger so that the area (I) stays equal to 1.

force

time

26

Index

2. LAPLACE TRANSFORMS

We can see that in the limit as ε → 0 f (t) = Z ∞

−∞

0, ∞,

t 6= t0 t = t0 ,

and f (t) dt = 1.

The Dirac delta function1 , δ (t), is defined by the following properties 1.

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

2.

Z 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 limit of a distribution of step functions (as in the previous diagram). It can also be thought of as the derivative of the Heaviside function.

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−as F1 (s) where H(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) has n well defined derivatives then: L

1 The

n

f (n) (t)

o

= sn F(s) − sn−1 f (0) − sn−2 f ′ (0) − · · · − f (n−1) (0).

delta function was introduced by the British physicist P.A.M Dirac (1902-1984) in the paper “The Physical Interpretation of the Quantum Mechanics”, Proceedings of the Royal Society of London, Series A113, 1926-1927, pp. 621-641.

Index

SOME STANDARD RESULTS IN LAPLACE TRANSFORMS

27

2.2.6 Multiplication by t L {t f (t)} = −F ′ (s) In general L {t n f (t)} = (−1)n

d n F(s) dsn

2.2.7 Integral theorem The Laplace transform of an integral is: Z t 1 L f (τ ) d τ = F(s) s 0

2.2.8 Division by t A corresponding theorem for integration is L

Z∞ 1 f (t) = F(s)ds t s

2.2.9 Convolution theorem Often when we cannot get an inverse in terms of a known function we have to use the convolution theorem L

−1

{F(s)G(s)} =

Z t 0

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

28

2. LAPLACE TRANSFORMS

Index

2.2.10 Taking Laplace transforms using MATLAB MATLAB can do Laplace Transforms and their inversions symbolically. Firstly you must declare the variables that are used (eg t and s) as symbolic variables using the syms command. The laplace function is then used to find the Laplace Transform. Example For example find the Laplace Transform of f (t) = e4t sin(3t) MATLAB code % find the Laplace Transform of % f(t)=exp(4*t)*sin(3*t) syms s t % make s and t symbolic variables Fs=laplace(exp(4*t)*sin(3*t))

results in

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

Example −3t 2 Find Laplace Transform of the derivative of f (t) = e sinh(5t) + t , that is find dthe −3t 2 L dt e sinh(5t) + t

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 variables Fs=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

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 the complex plane and finding residues around the singularities. Generally this is difficult to do and beyond the scope of this course. It is much easier to modify our function F(s) to look like something we already know the inverse transform of. For example we already know that s L {cos at} = 2 s + a2 so that the inverse of 2s −1 L = 2 cos 3t. s2 + 9 All that is necessary is to get our solution in the same form as the tables and then do the same process as the Laplace transform but in reverse.

2.3.1 Partial fractions Partial Fractions are useful to convert complicated inversions into a form that can be used from the tables. Partial fraction are used to convert a complicated rational expression such as y = G(s)/F(s) into a sum of simpler terms each of which can be inverted using the tables. F(s) must be written in a form so that it is a product of polynomials of as a low order as possible.

Distinct Linear Factors If the denominator has distinct linear factors then it is a simple matter of writing the expression as a sum of terms with those linear factors as denominators and unknown numerators. For example A B 3s + 13 = + (s + 3)(s + 1) s + 3 s + 1 where A and B are the unknown numerators that have to be found. To find these rewrite the right hand side over a common denominator so that it resembles the left hand side and then equate the coefficients of the different powers of s in the numerators. 3s + 13 A B = + (s + 3)(s + 1) s+3 s+1 A(s + 1) + B(s + 3) = (s + 3)(s + 1) (A + B)s + A + 3B = (s + 3)(s + 1) So then the coefficient of s gives A+B = 3 and the constant term (s to the zero power) gives A + 3B = 13

30

Index

2. LAPLACE TRANSFORMS

This is 2 equation in 2 unknowns which can be solved to give A = −2 Hence

B = 5.

3s + 13 −2 5 = + (s + 3)(s + 1) s + 3 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 A B +Cs 2s + 1 = + 2 s(s − 2) s (s − 2)2 A(s − 2)2 + (B +Cs)s = s(s − 2)2 where we thus have 2s + 1 = A(s − 2)2 + Bs +Cs2 so equating coefficients we get: O(s2 ) : O(s1 ) : O(s0 ) :

0 = A +C 2 = −4A + B 1 = 4A

Or alternatively substitute values for s. Either way we get 1 −1 A = , B = 3, C = 4 4 and so

2s + 1 1/4 3 − s/4 = + s(s − 2)2 s (s − 2)2

If the factor is repeated n times then the unknown numerator must be a polynomial of degree n − 1. The numerator must always have a degree one less than the denominator. For example 2s + 1 A + Bs C + Ds + Es2 + Fs3 = + s2 (s − 5)4 s2 (s − 5)4 Method 2 Alternatively (this method often leads to a better form for doing the inversion) the unknown right hand side can be written as A D E 2s + 1 = + + 2 s(s − 2) s s − 2 (s − 2)2 where we thus have 2s + 1 = A(s − 2)2 + Ds(s − 2) + Es

Index

INVERSION OF LAPLACE TRANSFORMS

31

so equating coefficients we get: O(s2 ) : O(s1 ) : O(s0 ) :

0 = A+D 2 = −4A − 2D + E 1 = 4A

hence giving: 1 −1 5 A= , D= , E= 4 4 2 1/4 −1/4 5/2 2s + 1 = + + 2 s(s − 2) s (s − 2) (s − 2)2

Using this method you must make sure there are as many terms involving the factor as it is repeated. For example 2s + 1 s2 (s − 5)4

=

A B C D E F + 2+ + + + 2 3 s s s − 5 (s − 5) (s − 5) (s − 5)4

Non Linear Factors If the factors are not linear then use method 1 as for repeated linear factors. That is use a numerator that has degree one less than the term in the denominator. For example if the denominator has a quadratic that can not be reduced any further then the corresponding numerator is linear A Bs +C 1 = + 2 2 s(s + 4) s s +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 5 + s+3 s+1

(by partial fractions)

hence enabling the solution: −2e−3t + 5e−t . Example −1/4 5/2 2s + 1 −1 1/4 −1 = L + + (using partial fractions) L s(s − 2)2 s (s − 2) (s − 2)2 1 1 2t 5 2t − e + e t (using shifting theorem) = 4 4 2

32

Index

2. LAPLACE TRANSFORMS

Example 1 s(s2 + 4)

=

A Bs +C + 2 s s +4

We thus get 1 = A(s2 + 4) + (Bs +C)s and by equating coefficients we get: O(s2 ) : O(s1 ) : O(s0 ) :

0 = A+B 0 = C 1 = 4A

easily giving the solution A = 41 , B = − 41 ,C = 0 and hence the inverse 1 1/4 −1 −1 1/4 L = L − 2 s(s2 + 4) s s +4 1 1 sin 2t − = 4 4 2 This following example uses the derivative formulae to transform a differential equation into 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 where Y (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 3s + 13 (s + 3)(s + 1) 5 −2 + (using partial fractions, see before) = s+3 s+1

Y =

As we can invert to obtain

L eat = 1/(s − a)

y(t) = −2e−3t + 5e−t . This illustrates how a transformation simplifies the solution to differential equations.

Index

INVERSION OF LAPLACE TRANSFORMS

33

2.3.3 MATLAB functions for partial fractions MATLAB has commands to do partial fractions (residue and poly). Residue The residue function takes a ratio of polynomials B(s) A(s) and returns the corresponding partial fraction representation of it. It takes as input the coefficients of the polynomials B(s) and A(s) (in descending powers of s) and returns the coefficients and the pole values (the zeros of the denominator). help residue RESIDUE Partial-fraction expansion (residues). [R,P,K] = RESIDUE(B,A) finds the residues, poles and direct term of a 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 and denominator polynomials in descending powers of s. The residues are returned in the column vector R, the pole locations in column vector P, and the direct terms in row vector K. The number of poles is n = length(A)-1 = length(R) = length(P). The direct term coefficient vector is empty if length(B) < length(A), otherwise length(K) = length(B)-length(A)+1. If P(j) = ... = P(j+m-1) is a pole of multplicity m, then the expansion 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 arguments, converts the partial fraction expansion back to the polynomials with coefficients in B and A. Warning: Numerically, the partial fraction expansion of a ratio of polynomials represents an ill-posed problem. If the denominator polynomial, A(s), is near a polynomial with multiple roots, then small changes in the data, including roundoff errors, can make arbitrarily large changes in the resulting poles and residues. Problem formulations making use of state-space or zero-pole representations are preferable. See also POLY, ROOTS, DECONV.

34

Index

2. LAPLACE TRANSFORMS

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 s A=[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

1

2

A = -15

R = 2 1 P = -5 3 K = []

The interpretation of this is as follows K is the remainder, K empty means the partial fraction representation has no remainder P 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 so here 2 1 F(s) = + s+5 s−3

Index

INVERSION OF LAPLACE TRANSFORMS

35

Example MATLAB can also deal with repeated roots but care must be taken when interpretting the output. For example find the partial fraction representation of F(s) =

s2 + 4s − 2 s3 − 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 s A=[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

1

0

-3

A = -2

R = 1.1111 -0.1111 1.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 remainder P 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/9 −1/9 5/3 + + s − 2 s + 1 (s + 1)2

36

Index

2. LAPLACE TRANSFORMS

2.3.4 Inversion using MATLAB Often it is not necessary to do the partial fraction steps yourself as MATLAB can do some inversions symbolically using the MATLAB command ilaplace. Example Find the inversion of F(s) =

s s2 − 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 variables Fs=s/(sˆ2-2*s+1) ft=ilaplace(Fs)

results in

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

Index

INVERSION OF LAPLACE TRANSFORMS

37

More complicated expressions are also straightforward including ones that result in Heaviside 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 variables Fs=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)

38

Index

2. LAPLACE TRANSFORMS

2.3.5 Systems of Equations Laplace Transforms can be used to solve systems of differential equations. By taking the Laplace Transform of a system of differential equations it is converted into an algebraic system in the transformed variable. These can be solved to get the transformed functions and then inverted to find the solutions to the original system. Example Solve the system dx1 = −x1 + x2 dt dx2 = 2x1 dt subject to the initial conditions x1 (0) = 0 and x2 (0) = 1. First take the Laplace Transform of both equations (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 get X1 (s) and X2 (s). Substitute the first equation into the second for X2 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 get X2 X2 = (s + 1)X1 =

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

Note that MATLAB’s solve function could be used to solve the system for X1 and X2 . Now use partial fractions then invert to get the solution to the original system as X1 = X2 =

1 1 1 1 − 3 s−1 3 s+2

2 1 1 1 + 3 s−1 3 s+2

⇒

x1 (t) =

1 t 1 −2t e − e 3 3

⇒

x2 (t) =

2 t 1 −2t e + e 3 3

Index

INVERSION OF LAPLACE TRANSFORMS

Example Solve the system 2

dx1 dx2 + − 2x1 = 1 dt dt

dx1 dx2 + − 3x1 − 3x2 = 2 dt dt subject to the initial conditions x1 (0) = 0 and x2 (0) = 0. First take the Laplace Transform of both equations 2(sX1 − x1 (0)) + (sX2 − x2 (0)) − 2X1 =

1 s

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

2 s

Now apply the initial conditions and rearrange to get (2s − 2)X1(s) + sX2(s) =

1 s

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

2 s

Now solve this to get X1 (s) and X2 (s). Rearrange the first equation to get X2 =

1 s

− (2s − 2)X1 s

Substitute into the second equation and simplify to find X1 1

(s − 3)X1(s) + (s − 3) s

− (2s − 2)X1 2 = s s

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 (collect X1 terms) (s − 3)(−s + 2)sX1 = s + 3 X1 =

(simplify)

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

Substitute in to get X2 X2 =

1 s

− (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)

39

40

Index

2. LAPLACE TRANSFORMS

Now use partial fractions to get X1 =

−1 1 5 1 −2 + + 2 s 2 s−2 s−3

X2 =

8 1 −1 1 −5 1 + + 6 s 2 s−2 3 s−3

Take the inverse Laplace Transform to get the solution to the original system as −1 5 + e2t − 2e3t 2 2 −5 2t 8 3t −1 + e + e x2 (t) = 6 2 3

x1 (t) =

3 Initial Value Problems

3.1 Introduction An Initial Value Problem (IVP) is a differential equation of nth order where the function value and the first n − 1 derivatives are all specified at one given point known as the initial point. For examples y′ = 2y2 y′′′ + 3y′′ + 7y′ − 3y = 0

with y(1) = 3

with y′′ (2) = 2 y′ (2) = −1

y(2) = 0.5

Also it can be a system of first order differential equations with each function given a value at the same point. For example y′′′ + 3y′′ + 7y′ − 3y = 0 can be written as

with y′′ (2) = 2 y′ (2) = −1

y(2) = 0.5

42

Index

3. INITIAL VALUE PROBLEMS

A numerical solution to this system of equations over t = [2, 6] using MATLAB is MATLAB code % system1.m tspan=[2,6] % note starts at t=2 now y0=[0.5,-1,2] % note its a vector now [t,y]=ode45(’system1fun’,tspan,y0) % plot all y, y’ and y’’ on one graph and add a legend plot(t,y(:,1),t,y(:,2),’r-.’,t,y(:,3),’g:’) legend(’y’,’dy/dt’,’d2y/dt2’) print -depsc system1 MATLAB code function f=system1fun(t,y) % system1fun.m f(1)=y(2); f(2)=y(3); f(3)=-3*y(3)-7*y(2)+3*y(1); f=f(:); return

2.5 y dy/dt d2y/dt2

2 1.5 1 0.5 0 −0.5 −1 2

2.5

3

3.5

4

4.5

5

5.5

6

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’s Series can be used to expand about that point to get an approximation to the solution at neighbouring points. So for instance if you know the about the function at the point a then information at a point x is given by f (x) = f (a) + f ′ (a)(x − a) +

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

If you know something about f ′ (a), f ′′ (a),. . . then you can approximate the solution at the point x. The process for solving IVPs numerically is to start at the initial point and step forward using information provided by the governing differential equation. This can be done in a variety of ways. The simplest is Euler’s method.

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 line at each point that you step forward at. Recall the definition of the derivative as dy y(x + ∆x) − y(x) ≈ dx ∆x If we are solving the IVP dy = f (x, y) dx

with y(x0 ) = y0

we start at the position x0 and hence y(x0 ) = y0 is known then dy y(x0 + ∆x) − y(x0 ) (x0 , y0 ) = f (x0 , y0 ) ≈ dx ∆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 where yi is the approximation at the point xi = x0 + i∆x and fi = f (xi , yi ). Euler’s method is equivalent to approximating the solution by using the first two terms in the Taylor Series (the function value and the first derivative) and ignoring the higher order terms. The first ignored term is 1 1 ′′ f (a)(x − a)2 = f ′′ (a)(∆x)2 2! 2! so each step in the process has an error of the order of (∆x)2 .

44

3. INITIAL VALUE PROBLEMS

Index

3.3.1 Graphically

3.3.2 Errors and order of the method Assuming we are calculating the solution from x = a to x = b using N steps then ∆x = (b − a)/N. The error in each step is proportional to (∆x)2 as this is the first term neglected in the Taylor Series so then total 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 a first order method. A pth order method is one whose error is proportional to (∆x) p . The higher the order the method the more accurate it is. This usually comes at a price of being more difficult to program. First order methods are usually very slow if you want any reasonable amount of accuracy. Why is this?

Index

EULER’S METHOD

45

Example Solve y′ = y

with y(0) = 1

over the range x = [0, 1]. This has exact solution y = ex . MATLAB code % eulerexample.m % % solve y’=y subject to y(0)=1 % find the result at x=1 and calculate the error. % clear all; N=100; % number of steps used a=0; b=1; % endpoints of solution dx=(b-a)/N; % step size % save x and y values in vectors so we can plot solution x(1)=a; y(1)=1; % initial x and y values for i=1:N f=y(i); % calculate the function value at (xi,yi) y(i+1)=y(i)+dx*f; x(i+1)=x(i)+dx; end plot(x,y,’b-’) axis([0 1 1 2.8]) hold on exact=exp(x); plot(x,exact,’r--’); legend(’Euler’,’Exact’,2) hold off print -depsc eulerexample error=abs(exact(N+1)-y(N+1))

Results of running the above code for different number of points (N = 10, 100, 1000, 10000) and hence different step sizes N 10 100 1000 10000

∆x 0.1 0.01 0.001 0.0001

yN 2.59374246010000 2.70481382942153 2.71692393223590 2.71814592682523

error 0.12453936835904 0.01346799903752 0.00135789622315 0.00013590163356

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

46

Index

3. INITIAL VALUE PROBLEMS

Exact solution and Euler’s method with N = 10 and N = 100 applied to y′ = y

with y(0) = 1

2.8 2.6

2.8 Euler Exact

2.6

2.4

2.4

2.2

2.2

2

2

1.8

1.8

1.6

1.6

1.4

1.4

1.2

1.2

1 0

0.2

0.4

0.6

0.8

N = 10, ∆x = 0.1

1

1 0

Euler Exact

0.2

0.4

0.6

0.8

1

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 size to get a more accurate solution at the cost of increased computing time? All you know about Euler’s method is that the error is proportional to the step size (∆x) since it is a first order method, but you have no idea of what that constant of proportionality is. It can change for each different problem and even within the one problem. What would be ideal if we had some way of changing the step size so that in areas where the solution changes rapidly we choose a small step size and in areas where it changes slowly we can take a larger step size so that the error is about the same at each step. This is something we would want to be able to do for any method not just Euler’s method. All well written numerical ODE solvers will use some kind of adjustable step size that takes account of how fast the solution is varying and the size of the errors involved.

Index

EULER’S METHOD

47

Algorithm for adjusting the step size 1. For each step compare the result of using one step of size ∆x with the result of using two steps of size ∆x 2 . Call the difference between the solutions ∆y. If the method you are using is a pth order method then the error from a single step is (∆x) p+1 so ∆x ∆y = y(xi + ∆x) − y(xi + 2 ) 2 p+1 p+1 ! ∆x ∆x ≈ k(∆x) p+1 − k +k 2 2 k(∆x) p+1 = k(∆x) p+1 − 2p 1 = k(∆x) p+1 1 − p 2 = k′ (∆x) p+1

2. If you wish to make your solution accurate to some predetermined accuracy (call it ε ) then |∆y/y| < ε . This corresponds to some ideal step size which we will call δ x. Then ε 1/(p+1) |∆y/y| = ε = k(δ x) p+1 so δ x = k in reality we used a step size ∆x which had |∆y/y| = k(∆x)

p+1

so ∆x =

1 |∆y/y| k

1/(p+1)

The ratio of the step sizes (R) is then a measure of how good our step size is compared to the ideal step size

δx R= = ∆x

εy ∆y

1/(p+1)

3. If R rel="nofollow"> 1 then step size is too small so it can be increased at the next step. If R < 1 then step size is too big so it must be decreased at the current step and the process repeated until R > 1. Use the size of R as a guide to how much to increase or decrease the step size. For example let ∆xnew = mR∆xold where m is a tuning parameter usually taken to be a little less than one (eg m = 0.95 or thereabouts).

48

3. INITIAL VALUE PROBLEMS

Index

MATLAB code % euleradaptive.m % Euler method with adaptive time stepping % % dy/dx + y = exp(-x) + 1 with y(0)=1 % which has exact solution y = xexp(-x) + 1 % clear all; p=1; m=0.95; eps=5.e-4; a=0; b=1.0; % endpoints of solution dx(1)=0.1; % initial step size % save x,y and dx values in vectors so we can plot solution x(1)=a; y(1)=a*exp(-a)+1; % initial x and y values i=1; while x(i) < b % keep going until you reach the end point R=0; while R < 1 % calculate solution in one step yone=y(i)+dx(i)*feval(’eulerf’,x(i),y(i)); % calculate solution in two steps yhalf=y(i)+dx(i)/2*feval(’eulerf’,x(i),y(i)); ytwo=yhalf+dx(i)/2*feval(’eulerf’,x(i)+dx(i)/2,yhalf); R=(abs(eps*ytwo/(yone-ytwo)))ˆ(1/(p+1)); dx(i)=m*R*dx(i); end y(i+1)=ytwo; x(i+1)=x(i)+dx(i); dx(i+1)=dx(i); i=i+1; end for j=1:i exact(j)=x(j)*exp(-x(j))+1; end plot(x,y,’ro’) hold on plot(x,exact,’b-’); legend(’Adaptive’,’Exact’,2) hold off print -depsc euleradaptive plot(x,dx,’ro’) print -depsc euleradaptivedx MATLAB code function fxy=eulerf(xx,yy) fxy=exp(-xx)-yy+1; return

Index

EULER’S METHOD

49

Below are plots of the Euler method and adaptive Euler method and also a plot showing the step size used. Notice that the step size increases as the solution flattens as larger steps can be taken for the same error. 1.4 1.35

Adaptive Exact

1.3 1.25 1.2 1.15 1.1 1.05 1 0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.2

1.4

Exact, Euler and Adaptive Euler solution 0.08 0.075 0.07 0.065 0.06 0.055 0.05 0.045 0.04 0.035 0.03 0

0.2

0.4

0.6

0.8

1

Showing how the step size changes

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 slope at the midpoint. This in general will be a better approximation to the function. Graphically

First estimate the value of y at the midpoint using Euler’s method yi+1/2 = yi + f (xi , yi )∆x/2 The slope at the midpoint is then approximated by f (xi+1/2 , yi+1/2 ) so use this in an Euler like formulation to get the formula yi+1 = yi + f (xi+1/2 , yi+1/2 )∆x This is known as the midpoint method. It can be shown that the midpoint method is a second order method (as opposed to Euler’s method that is first order) but this comes at the price of having to calculate twice the number of function evaluations (since we calculate the function at the midpoints as well). How will this effect the methods efficiency? That is, how will the extra function evaluations be offset by the improvement in the calculation as it is a higher order method?

Index

IMPROVEMENTS TO EULER’S METHOD - MIDPOINT METHOD

51

Example Solve y′ = xy

with y(0) = 1 2 /2

on [0, 1]. This has exact solution y = ex

.

1.8 1.7

Euler Exact Midpoint

1.6 1.5 1.4 1.3 1.2 1.1 1 0

0.2

0.4

0.6

0.8

1

Midpoint method and Euler method with N = 10, ∆x = 0.1

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

N

∆x

error Euler

10 100 1000 20 200

0.1 0.01 0.001 0.05 0.005

0.10161087269011 0.01090081247050 0.00109823249901 0.05277960435958 0.00547295587046

flops∗ Euler 60 600 6000 120 1200

error Midpoint 0.00257111404563 0.00002730016798 0.00000027460827 0.00066472539344 0.00000684735089

flops∗ Midpoint 120 1200 12000 240 2400

∗

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

The Midpoint method is a second order method. That is the total error is proportional to (∆x)2 . Hence for a 10 fold decrease in the step size (∆x) you get an approximate 100 fold decrease in the error. For the same amount of computing effort (flops) the Midpoint method is far superior to Euler’s method. Just compare the errors for the Euler 120 flops calculation (N = 20, error≈ 0.05278) with the Midpoint 120 flops calculation (N = 10, error≈ 0.00257).

52

3. INITIAL VALUE PROBLEMS

MATLAB code % midpoint.m % % solve y’=xy subject to y(0)=1 % find the result at x=1 % for Euler and Midpoint methods and count flops. % clear all; N=10; % number of points, a=0; b=1; % initial and end points dx=(b-a)/N; % step size x(1)=a; y(1)=1; % initial x, y(Euler) values z(1)=y(1); % initial z(Midpoint) value flops(0) for i=1:N % Euler method y(i+1)=y(i)+dx*feval(’midf’,x(i),y(i)); x(i+1)=x(i)+dx; end eulerflops=flops flops(0); for i=1:N % Midpoint method zhalf=z(i)+dx/2*feval(’midf’,x(i),z(i)); z(i+1)=z(i)+dx*feval(’midf’,x(i)+dx/2,zhalf); x(i+1)=x(i)+dx; end midptflops=flops plot(x,y,’b-’) hold on exact=exp(0.5.*x.ˆ2); plot(x,exact,’go’); plot(x,z,’r-’) legend(’Euler’,’Exact’,’Midpoint’,2) hold off print -depsc midptexample erroreuler=abs(exact(N+1)-y(N+1)) errormidpt=abs(exact(N+1)-z(N+1))

MATLAB code function f=midf(xx,yy) % midf.m f=xx*yy; return

Index

Index

SECOND ORDER RUNGE-KUTTA METHOD

53

3.5 Second order Runge-Kutta method

Named after 2 German Mathematicians, Carl David Tolme Runge (1856–1927) and Wilhelm Kutta (1867–1944) The previous methods (Euler, Midpoint) have all used some approximation to the slope. Euler: the slope at the left point Midpoint: the slope at the midpoint. The Runge-Kutta method uses a weighted average of the the slope of the left point and some as yet unknown intermediate point. So the general formula is yi+1 = yi + ∆x favg 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 undetermined point given by xi′ = xi + α ∆x yi′ = yi + β fi ∆x The method consists of finding the values of the weights a, b and the position of the intermediate point given by α and β . Recall the Taylor Series expansion for yi+1 = y(xi + ∆x) is dy (∆x)2 d 2 y |i + |i + . . . dx 2 dx2 (∆x)2 d f = yi + ∆x fi + |i + . . . 2 dx (∆x)2 ∂ f ∂ f dy |i + |i + . . . = yi + ∆x fi + 2 ∂x ∂ y dx (∆x)2 ∂ f ∂f = yi + ∆x fi + |i + f |i + . . . 2 ∂x ∂y

yi+1 = yi + ∆x

But expanding fi′ in a 2D Taylor Series gives fi′ = f (xi′ , yi′ ) = f (xi + α ∆x, yi + β fi ∆x) ∂f ∂f = f (xi , yi ) + α ∆x |i + β fi ∆x |i + . . . ∂x ∂y So the Runge-Kutta formulation gives yi+1 = yi + ∆x favg = yi + ∆x (a fi + b fi′ )

54

Index

3. INITIAL VALUE PROBLEMS

∂f ∂f ≈ yi + ∆x a fi + b f (xi , yi ) + α ∆x |i + β fi ∆x |i ∂x ∂y ∂f ∂f = yi + ∆x a fi + b fi + bα ∆x |i + bβ fi ∆x |i ∂x ∂y

Comparing this with the Taylor Series expansion for yi+1 gives a+b = 1

bα =

1 2

bβ =

1 2

Which is 3 equations in the 4 unknowns a, b, α and β so there is still some degree of freedom of choice. For example setting a=0

b=1

α =β =

1 2

gives the midpoint method. These methods are second order. Because there is still a degree of freedom in the equations for specific problems it is possible to make a choice of a, b, α and β that will make the method third order. This method has been precoded in MATLAB and is used with a call to ode23 To solve the IVP

dy = ode f unc(t, y) with y(a) = c dt over the interval [a, b] a call to ode23 would be like y0=[c]; tspan=[a b]; [t,y] = ode23(’odefunc’,tspan,y0);

Use help ode23 to find out more about the MATLAB command and how to use it. In particular how to choose the accuracy. ode23 also uses an adaptive step size as well if it needs to find the solution more accurately in a given region of the interval.

Index

SECOND ORDER RUNGE-KUTTA METHOD

55

Example Solve

dy = ty with y(0) = 1 dt

on [0, 1]. This has exact solution y = et

2 /2

.

MATLAB code % o23example.m % solve dy/dt=odefunc(t,y) on [a,b] given y(a)=c a=0; b=1; c=1; % interval and initial value y0=[c]; % initial condition tspan=[a b]; % interval of integration [t,y]=ode23(’odefunc’,tspan,y0);% soln in vectors t and y plot(t,y) hold on exact=exp(0.5.*t.ˆ2); plot(t,exact,’ro’) legend(’ode23’,’Exact’,2) print -depsc o23example MATLAB code function f=odefunc(t,y) % odefunc.m % the rhs of the IVP dy/dt=f(t,y) f=t*y; return

1.8 1.7

ode23 Exact

1.6 1.5 1.4 1.3 1.2 1.1 1 0

0.2

0.4

0.6

0.8

1

56

3. INITIAL VALUE PROBLEMS

Index

3.6 Fourth order Runge-Kutta method In exactly the same way as the second order Runge-Kutta method was developed higher order methods can be developed. By including more sampling points in the interval greater accuracy can be obtained. The second order method used the left-hand point and one other point to determine an approximation to the solution. Hence two function evaluations were required for each step. The Runge-Kutta fourth order method uses the left-hand point and 3 other points to hence four function evaluations are required for each step. The method is fourth order accurate that is the total error is proportional to (∆x)4 . 1 yi+1 = yi + (z1 + 2z2 + 2z3 + z4 ) 6 where z1 = f (xi , yi )∆x 1 1 z2 = f (xi + ∆x, yi + z1 )∆x 2 2 1 1 z3 = f (xi + ∆x, yi + z2 )∆x 2 2 z4 = f (xi+1 , yi + z3 )∆x The MATLAB command for using this method is ode45 and is used exactly the same way as ode23. The fourth order Runge-Kutta method is by far the most commonly used method for solving IVPs.

Index

ODE45

57

3.7 ode45 >> help ode45 ODE45 Solve non-stiff differential equations, medium order method. [T,Y] = ODE45(’F’,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y’ = F(t,y) from time T0 to TFINAL with initial conditions Y0. ’F’ is a string containing the name of an ODE file. Function F(T,Y) must return a column vector. Each row in solution array Y corresponds to a time returned in column vector T. To obtain solutions at specific times T0, T1, ..., TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. [T,Y] = ODE45(’F’,TSPAN,Y0,OPTIONS) solves as above with default integration parameters replaced by values in OPTIONS, an argument created with the ODESET function. See ODESET for details. Commonly used options are scalar relative error tolerance ’RelTol’ (1e-3 by default) and vector of absolute error tolerances ’AbsTol’ (all components 1e-6 by default). [T,Y] = ODE45(’F’,TSPAN,Y0,OPTIONS,P1,P2,...) passes the additional parameters P1,P2,... to the ODE file as F(T,Y,FLAG,P1,P2,...) (see ODEFILE). Use OPTIONS = [] as a place holder if no options are set. It is possible to specify TSPAN, Y0 and OPTIONS in the ODE file (see ODEFILE). If TSPAN or Y0 is empty, then ODE45 calls the ODE file [TSPAN,Y0,OPTIONS] = F([],[],’init’) to obtain any values not supplied in the ODE45 argument list. Empty arguments 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 error tolerance 1e-4 and absolute tolerances of 1e-4 for the first two components and 1e-5 for the third. When called with no output arguments, as in this example, ODE45 calls the default output function ODEPLOT to plot the solution as it is computed. See also ODEFILE and other ODE solvers: options handling: output functions: odefile examples:

ODE23, ODE113, ODE15S, ODE23S, ODE23T ODESET, ODEGET ODEPLOT, ODEPHAS2, ODEPHAS3, ODEPRINT ORBITODE, ORBT2ODE, RIGIDODE, VDPODE

58

Index

3. INITIAL VALUE PROBLEMS

Example Solve y′′ = t 2 cos(t)y s.t. y(0) = 2 and y′ (0) = 3 MATLAB code % o45ex1.m % solve y’’=tˆ2*y*cos(t)on [0,5] given y(0)=2, y’(0)=3 % this can be written as a system of first order DEs % y1’=y2 y1(0)=2 % y2’=tˆ2*y1*cos(t) y2(0)=3 a=0; b=5; % interval y0=[2 3]; % initial condition tspan=[a:.1:b]; % interval of integration [t,y]=ode23(’o45ex1func’,tspan,y0);% soln in vectors t and y plot(t,y(:,1),t,y(:,2),’r--’) % plot y1 and y2 versus t legend(’y’,’dy/dt’,2) xlabel(’t’); ylabel(’y and dy/dt’) print -depsc o45ex1 % output to postscript file MATLAB code function f=o45ex1func(t,y) % o45ex1func.m % the RHS of the system of DEs f(1)=y(2); f(2)=tˆ2.*y(1).*cos(t); f=f(:); % forces f to be a column vector return 30 y dy/dt 20

y and dy/dt

10

0

−10

−20

−30 0

1

2

3 t

4

5

Index

ODE45

59

The MATLAB function ode45 has a vast array of options that can be used. Finding solutions at given points. use tspan = [T0 T1 ... TFINAL] eg tspan=[0.1 0.2 0.3 0.4 0.5] will return solutions at t = 0.1, 0.2 etc. Error tolerances There is an extra parameter that can be added to the parameter list called ’options’. The call to ode45 is then ode45(’func’,tspan,y0,options) The options parameter is set using the command odeset. For example options = odeset(’RelTol’,1e-3,’AbsTol’,[1e-4 1e-4 1e-5]); Would set the relative error tolerance to 1 × 10−3 and the absolute error tolerance to 1 × 10−4 for the first two components and 1 × 10−5 for the third. This is useful if you know (or expect) that some of the variables will be substantially different is magnitude than others. Stopping the integration It is possible to stop the integration when certain events occur. For example when any of the 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 so that if ode45 passes it a flag with a value of ’events’ it determines the .m file that tells it information about the stopping criteria. If the flag is empty it determines the .m file that is actually used to calculate the integration (the DEs).

60

Index

3. INITIAL VALUE PROBLEMS

3.8 Projectile example Consider the problem of a projectile fired from a fixed position. If you know the initial velocity and angle of the projectile it is possible to determine the trajectory of the projectile. Other factors such as drag on the projectile and lift can also be taken into account. A system of differential that governs the motion of the projectile is given by 1 dv = −mg sin φ − ρ ACD v2 dt 2 1 dφ mv = −mg cos φ + ρ ACL v2 dt 2 m

where φ = angle from horizontal, v = velocity, m = mass. ρ = density, A = area, CD drag coefficient, CL lift coefficient, g gravity. The trajectory of the projectile is governed by dx = v cos φ dt dy = v sin φ dt If the initial position (x, y coordinates), initial velocity (v) and angle (φ ) are known then the trajectory of the projectile can be easily determined. This is a system of 4 nonlinear differential equations in the 4 unknowns x, y, v and φ . Solving these equations analytically is not possible but solving them numerically is very easy using MATLAB. Rearrange the equations into a standard form where the left hand side is just the derivative of each variable.

dv dt dφ dt dx dt dy dt

ρ ACD v2 2m −g cos φ ρ ACL v = + v 2m

= −g sin φ −

= v cos φ = v sin φ

Then just numerically integrate these equations forward in time form the known initial position, velocity and angle.

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 for % the velocity, angle, x and y % format compact v0=10; % initial velocity theta=0; while theta >= 0 theta=input(’enter theta, use < 0 to stop...’) if theta<0 break else % initial point [velocity angle x y] y0=[v0 theta*pi/180 0 0]; % make the time span large enough that it gets to the target tspan=[0 1]; [t,y]=ode45(’projectiledes’,tspan,y0); plot(y(:,3),y(:,4)) % plot the trajectory hold on % determine how long the solution vector is lgthy=length(y); range=y(lgthy,3) % the range is the last x value end end hold off print -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 ball g=9.8; rho=1.23; A=0.038; m=0.42; D=0.22; Cd=0.20; Cl=0.05; v=y(1); phi=y(2); f(1)=(-g.*sin(phi)-rho.*A.*Cd./(2..*m).*v.ˆ2); % velocity f(2)=(-g.*cos(phi)./v + rho.*A.*Cl./(2.*m).*v);% angle f(3)=v.*cos(phi); % x coordinate f(4)=v.*sin(phi); % y coordinate f=f(:); % force f to be a column vector return

62

Index

3. INITIAL VALUE PROBLEMS

>> projectile enter theta, use theta = 30 range = 8.2272 enter theta, use theta = 45 range = 6.7203 enter theta, use theta = 60 range = 4.7443 enter theta, use theta = -1 >> diary off

< 0 to stop...30

< 0 to stop...45

< 0 to stop...60

< 0 to stop...-1

4 3.5 3 2.5 2 1.5 1 0.5 0 0

2

4

6

8

10

MATLAB can be set up to integrate the DE until a specific event occurs, for example here that might be when y = 0 (ie when the projectile hits the ground). You can then use fzero to find the initial angle that gives a specific range (ie hits the target).

4 Phase Plane Analysis

4.1 Introduction Phase plane analysis is useful in determining the behaviour of solutions to systems of differential equations without necessarily solving the system analytically. More importantly this technique is not limited to studying linear equations. For systems of 2 autonomous first order differential equations it is possible to plot solutions of one dependent variable against the other dependent variable with the time dependence varying along the curves. This is known as a phase plane. The goal is to sketch enough solution curves so that the general behaviour of the system can be summarised by looking at the graph. The system dx1 = x2 dt dx2 = −2x1 − x2 dt has phase plane consisting of spirals converging to the origin.

5 4 3

x2

2 1 0 −1 −2 −1

−0.5

0

0.5

x

1 1

1.5

2

2.5

64

4. PHASE PLANE ANALYSIS

MATLAB code

% ppexample.m % drawing phase plane for % x1’=x2 % x2’=-2x1-x2 % tend=10; tspan=[0 tend]; y0=[0 5]; [t,y]=ode45(’ppexamplef’,tspan,y0); plot(y(:,1),y(:,2)) xlabel(’x_1’); ylabel(’x_2’); print -depsc ppexample MATLAB code

function f=ppexamplef(t,y) % shootingexamplef.m f(1)=y(2); f(2)=-2*y(1)-y(2); f=f(:); return

Index

Index

65

INTRODUCTION

Or with multiple initial points put the whole thing into a for loop and update the initial points as you go through the loop. MATLAB code

% ppexample2.m % drawing phase plane for % x1’=x2 % x2’=-2x1-x2 % tend=10; tspan=[0 tend]; for s=-2:1:3 y0=[s 5]; [t,y]=ode45(’ppexamplef’,tspan,y0); plot(y(:,1),y(:,2)) xlabel(’y_1’); ylabel(’y_2’); hold on end hold off print -depsc ppexample2

5 4 3 2

y

2

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

−1

0

1

y1

2

3

4

5

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 DEs is to draw the velocity field. The MATLAB command quiver is designed to do precisely this. help quiver QUIVER Quiver plot. QUIVER(X,Y,U,V) plots velocity vectors as arrows with components (u,v) at the points (x,y). The matrices X,Y,U,V must all be the same size and contain corresponding position and velocity components (X and Y can also be vectors to specify a uniform grid). QUIVER automatically scales the arrows to fit within the grid. QUIVER(U,V) plots velocity vectors at equally spaced points in the x-y plane. QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the arrows 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 for the velocity vectors. Any marker in LINESPEC is drawn at the base instead of an arrow on the tip. Use a marker of ’.’ to specify no marker at all. See PLOT for other possibilities. 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 on quiver(x,y,px,py), hold off, axis image See also FEATHER, QUIVER3, PLOT. diary off

Index

INTRODUCTION

67

MATLAB code % vfield.m % % plots velocity vectors for the following system % dx/dt = y % dy/dt = -2x-y % a=-5; b=5; % size of the grid [x,y]=meshgrid(a:1:b,a:1:b);% a square grid (a to b)x(a to b) dx=y; % the DEs dy=-2*x-y; % quiver(x,y,dx,dy,’r’) % plot the vector field axis square % make the plot square axis tight % make axis tight on data hold on w=[a-1 b+1]; plot(w,0*w,’k-’,0*w,w,’k-’) % adds x and y axes to plot xlabel(’x’); ylabel(’y’); hold off % make an eps version of the plot called vfield.eps print -depsc vfield % if you dont want the arrow heads then just use % quiver(x,y,dx,dy,’.’); % the arrows are automatically scaled to fit the plot space % if you want bigger or smaller arrows use a scaling % quiver(x,y,dx,dy,3) % makes the arrow 3 times longer % for more help type help quiver

5 4 3 2

y

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

0 x

5

68

Index

4. PHASE PLANE ANALYSIS

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). Let y1 be the number of predators and y2 be the number of prey then the governing differential equations are dy1 = −ay1 + by1 y2 dt dy2 = −cy1 y2 + dy2 dt Here the terms are explained as −ay1 dy2 by1 y2 −cy1 y2

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

Critical points Set derivatives equal to zero and solve for y1 and y2 .

This gives the critical points as (y1 , y2 ) = (0, 0) and (y1 , y2 ) = (d/c, a/b) Calculate the Jacobian and classify each of the critical points. The Jacobian is " ∂ # ∂ ∂ y1 (−ay1 + by1 y2 ) ∂ y2 (−ay1 + by1 y2 ) J(y1 , y2 ) = ∂ ∂ ∂ y1 (−cy1 y2 + dy2 ) ∂ y2 (−cy1 y2 + dy2 ) −a + by2 by1 = −cy2 −cy1 + d So substituting each critical point gives J(0, 0) =

−a 0 0 d

Which is a diagonal matrix so the eigenvalues are λ1 = −a and λ2 = d hence (0, 0) is a saddle. 0 bd/c J(d/c, a/b) = −ac/b 0 √ Calculating the eigenvalues gives λ = ±i ad and so (d/c, a/b) is (probably) a centre. How does the population of both the predator and prey species vary with time?

Index

PREDATOR-PREY MODEL

69

MATLAB MATLAB code % predprey.m % Predator-Prey system % clear all global a b c d % variables global to all functions a=0.1; b=0.002; c=0.0025; d=0.2; y0=[10 50]; % initial point tspan=[0 120]; % range to integrate over [t,y]=ode45(’predpreyf’,tspan,y0); plot(t,y(:,1),’b-’) % plot predator popoluation vs time hold on % hold the plot plot(t,y(:,2),’r--’) % plot prey popoluation vs time legend(’predator’,’prey’) hold off % turn off plot hold print -depsc predprey % send plot to postscript file plot(y(:,1),y(:,2),’g-’) % plot trajectories in (y1,y2)space print -depsc predpreytraj% send plot to postscript file MATLAB code function f=predpreyf(t,y) % predpreyf.m global a b c d f(1)=-a*y(1) + b*y(1)*y(2); f(2)=-c*y(1)*y(2) + d*y(2); f=f(:); return 300

300 predator prey 250

250

200

200

150

150

100

100

50

50

0 0

20

40

60

Time plot

80

100

120

0 0

50

100

150

200

250

A trajectory in (y1 , y2 ) space

300

70

Index

4. PHASE PLANE ANALYSIS

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 r1 y1 (K1 − y1 − ay2 ) = dt K1 r2 dy2 y2 (K2 − y2 − by1 ) = dt K2 If there is no second species (y2 = 0) then the first population grows until it approaches the steady population K1 . Similarly for the other species. Critical points Set derivatives equal to zero and solve for y1 and y2 .

(y1 , y2 ) = (0, 0) (y1 , y2 ) = (K1 , 0) (y1 , y2 ) = (0, K2) and a fourth point is located at the intersection of the two lines K1 − y1 − ay2 = 0 and K2 − y2 − by1 = 0. Look at the Jacobian to classify each critical point. MATLAB code % compjacobian.m % format compact clear all a=0.75; b=3.0; r1=0.2;

r2=0.1;

K1=50;

K2=100;

% store critical points in a vector y1=[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

Index

COMPETITION MODEL

>> compjacobian For critical point ( 0.00 Jac = 0.2000 0 0 0.1000 V = 1 0 0 1 D = 0.2000 0 0 0.1000

0.00)

For critical Jac = -0.2000 0 V = 1.0000 0 D = -0.2000 0

0.00)

point (50.00 -0.1500 -0.0500 -0.7071 0.7071 0 -0.0500

For critical point ( 0.00 100.00) Jac = -0.1000 0 -0.3000 -0.1000 V = 0 0.0000 1.0000 1.0000 D = -0.1000 0 0 -0.1000 For critical Jac = -0.0800 -0.1200 V = -0.6661 -0.7458 D = -0.1472 0 >> diary off

point (20.00 40.00) -0.0600 -0.0400 0.4885 -0.8726 0 0.0272

71

72

Index

4. PHASE PLANE ANALYSIS

How does the population of both the species vary with time? Does one species dominate the other? MATLAB code % competition.m % clear all global a b r1 r2 K1 K2 % variables global to all functions a=0.75; b=3.0; r1=0.2; r2=0.1; K1=50; K2=100; for i=0:10 % loop through various initial points for j=0:1 y0=[j*50+5 i*10+5]; % initial points tspan=[0 200]; % range to integrate over [t,y]=ode45(’competitionf’,tspan,y0); plot(y(:,1),y(:,2)) % plot trajectories in (y1,y2)space hold on end end print -depsc competitiontraj % send plot to postscript file hold off % turn off plot hold MATLAB code function f=competitionf(t,y) % competitionf.m global a b r1 r2 K1 K2 % variables global to all functions f(1)=r1/K1*y(1)*(K1 - y(1) - a*y(2)); f(2)=r2/K2*y(2)*(K2 - y(2) - b*y(1)); f=f(:); return

120

100

80

60

40

20

0 0

10

20

30

40

50

60

Index

SIR DISEASE MODEL

73

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 percentage of the population need to be inoculated to prevent the spread of a disease. Or simply as a guide to how widespread a disease will become if left alone. Consider a fixed population of size K. A fixed population size is a reasonable assumption if the birth rate is approximately the same as the death rate and the disease does not actually kill the person. For instance measles in the developed world is rarely a fatal disease and has no impact on the overall birth or death rate. Consider three distinct classes of people: Susceptibles (S): Infectives (I): Recovered (R):

those people who are susceptible to catching the disease those people who have the disease and can pass it on those people who have had the disease and recovered from it. They are no longer susceptible as they have immunity now.

This is (for obvious reasons) known as an SIR model. The total population is fixed so that S+R+I = K The general birth rate of people (who are all born susceptible) is µ . Because the population is assumed to be constant the death rate must also be µ but any of susceptibles, infectives or recovereds can die. The disease is assumed not to alter the death rate. Consider how the population of susceptibles can change. Susceptibles catching the disease and becoming infective, births of susceptibles, or dying. To catch the disease a susceptible must come in contact with an infective hence the chance of catching the disease is proportional (with constant β ) to the product of the number of susceptibles and infectives. The DE that models the susceptible population is therefore dS = −β SI + µ K − µ S dt

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 the infective population is therefore dI = β SI − γ I − µ I dt

contact

recover

death

74

Index

4. PHASE PLANE ANALYSIS

Consider how the population of recovereds can change. They are infectives who have recovered, or they can die. The DE that models the recovered population is therefore dR = γI − µR dt

recovered

death

Note that the recovered equation does not effect the susceptible or infective equations and in fact since the population is assumed constant the number of recovereds can always be determined from the number of susceptibles and the number of infectives. This last equation is not needed in the model. So we have two first oder nonlinear differential equations in two unknowns (S and I). dS = −β SI + µ K − µ S dt dI = β SI − γ I − µ I dt These are nonlinear equations hence we need to calculate the critical points and analyse the behaviour near those critical points. Critical points Set derivatives equal to zero and solve for S and I. 0 = −β SI + µ K − µ S 0 = β SI − γ I − µ I

Index

SIR DISEASE MODEL

Look at the Jacobian to classify each critical point. " ∂ ∂ S (−β SI + µ K − µ S) J(S, I) = ∂ ∂ S (β SI − γ I − µ I) =

"

−β I − µ

−β S

βI

βS−γ −µ

∂ ∂I

(−β SI + µ K − µ S)

∂ ∂I

(β SI − γ I − µ I)

75

#

#

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

−µ 0

−β K β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 the S → K, I → 0, that is the disease dies out. If β K − γ − µ > 0 then the critical point (S, I) = (K, 0) is a saddle and hence (K, 0) is not the final state. So what is the final state in his case? Presumably it must be the other critical point since the model is bounded. Why is the model bounded? Example Consider the case with K = 100, β = 0.001, µ = 0.02, γ = 0.03

76

4. PHASE PLANE ANALYSIS

Index

MATLAB code % sir.m % this M-file plots velocity vectors for the SIR model, plots % some trajectories, calculates e’values and e’vectors of Jacobian % global beta K mu gamma % makes these global variables format compact beta=0.001; K=100; mu=0.02; gamma=0.03; % % use quiver to plot the vector field a=0; b=100; % size of the grid [S,I]=meshgrid(a:10:b,a:10:b);% a square grid (a to b)x(a to b) dS=-beta*S.*I+mu*K-mu*S; % the DEs dI=beta*S.*I-gamma*I-mu*I; % quiver(S,I,dS,dI,2.5,’r’) % plot vector field scaled by 2.5 axis square % make the plot square axis([a b a b]) % restrict the axes to be a to b hold on w=[a b]; plot(w,0*w,’k-’,0*w,w,’k-’) % adds x and y axes to plot xlabel(’Susceptibles’); ylabel(’Infectives’); title(’SIR model’) % % now add some trajectories by numerically solving the DE tspan=[0 250]; for icS=0:100:100 for icI=5:10:95 initcond=[icS icI]; [t,y]=ode45(’sirfunc’,tspan,initcond); plot(y(:,1),y(:,2),’b-’) end end hold off print -depsc sirtraj % % now set up the critical points and calculate the Jacobian % and the eigenvalues and eigenvectors so you can classify the % critical points cpS=[K (gamma+mu)/beta]; cpI=[0 mu*K/(gamma+mu)-mu/beta]; for i=1:1:2 fprintf(’\nCritical point number %d is (%5.2f,%5.2f) \n’,... i,cpS(i),cpI(i)) Jacobian=[-beta*cpI(i)-mu -beta*cpS(i);... beta*cpI(i) beta*cpS(i)-gamma-mu] [Jeigvec, Jeigval]=eig(Jacobian) end;

MATLAB code function f=sirfunc(t,y) % sirfunc.m % the DEs for the SIR model global beta K mu gamma S=y(1); I=y(2); f(1)=-beta*S.*I+mu*K-mu*S; f(2)=beta*S.*I-gamma*I-mu*I; f=f(:); return

Index

SIR DISEASE MODEL

sir Critical point number 1 is (100.00, 0.00) Jacobian = -0.0200 -0.1000 0 0.0500 Jeigvec = 1.0000 -0.8192 0 0.5735 Jeigval = -0.0200 0 0 0.0500 Critical point number 2 is (50.00,20.00) Jacobian = -0.0400 -0.0500 0.0200 0.0000 Jeigvec = 0.8452 0.8452 -0.3381 - 0.4140i -0.3381 + 0.4140i Jeigval = -0.0200 + 0.0245i 0 0 -0.0200 - 0.0245i diary off

SIR model 100 90 80

Infectives

70 60 50 40 30 20 10 0 0

20

40 60 Susceptibles

80

100

77

78

4. PHASE PLANE ANALYSIS

Index

5 Boundary Value Problems

5.1 Introduction In contrast to Initial Value Problems where all function values and derivatives are given at one point Boundary Value Problems (BVP) have function values and derivatives at two points. Hence it is not possible to construct a Taylor Series about one point and use this to build up the solution as was done with IVPs. Examples y′′ + 3y′ + 2y = x y′′′′ + y′′ + 2y = sin x

where

y(a) = 1

where

y(a) = 1

y(b) = 3

y′ (a) = 3 y′′ (a) = 6

y(b) = 3

BVPs do not necessarily have unique solutions . For example y′′ + ω 2 y = 0

where

y(0) = 0 y(π ) = 0

There are two general methods for solving BVPs numerically, shooting method and relaxation method.

80

Index

5. BOUNDARY VALUE PROBLEMS

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 for y′ (0). Now the problem is an IVP. 2. Integrate forward to x = 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)=3 clear all c=1; % the first guess for y’(0) tspan=[0 2]; while c > -999 % loop through different c values c=input(’enter c, -1000 to stop ’); if c==-1000 break; end % if c=-1000 jump out of loop y0=[1 c]; % initial condition [y(0) y’(0)] [t,y]=ode45(’shootingexamplef’,tspan,y0); plot(t,y(:,1)) % plot current solution hold on % keep the plot lgthy=length(y); % find the length of the vector y disp([y(lgthy,1)]) % show y(x=2) value cc=num2str(c); % convert value of c to a string variable text(2.1,y(lgthy,1),cc) % write value of c on rhs of graph end text(2.1,5,’c guess’) % write c guess on the plot at (2.1,5) hold off print -deps shootingexample MATLAB code function f=shootingexamplef(t,y) % shootingexamplef.m % the DE y’’ + 3y’ + 2y = 2x written as a system f(1)=y(2); f(2)=-3*y(2)-2*y(1)+2*t; f=f(:); return

Index

SHOOTING METHOD

81

6 5.5 5

c guess

4.5 4 3.5

20 18

3

16

2.5 10

2 1.5 1 0

0.5

1

1.5

2

Hence there is a solution that has y(2) = 3 somewhere between y′ (0) = 16 and y′ (0) = 18. How do you go about finding this value of y′ (0) that gives the correct value of y(2) ? Treat the problem as a zero finding problem. That is find the value of y′ (0) = c that results in f (c) = y(2) − 3 = 0. So use one of MATLABs methods for finding zeros of functions.

82

Index

5. BOUNDARY VALUE PROBLEMS

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 input the guess for the initial slope and returns the error in the right hand boundary condition. Then call this function from fzero to find the correct slope that integrates to the correct right hand boundary condition. MATLAB code % shootingexamplemain.m % the main M-file for using the shooting method to solve the % DE defined in shootingexamplef.m % Uses fzero to solve the equation y’(0)=3 where y’(0) is % determined in shootingfunc.m by shooting forward using ode45 % myguess=4; % a guess at the value of y’(0) actualvalue=fzero(’shootingfunc’,myguess) % use ode45 to get correct solution using actualvalue and plot tspan=[0 2]; y0=[1 actualvalue]; [t,y]=ode45(’shootingexamplef’,tspan,y0); plot(t,y(:,1)) print -deps shootingexampleplot MATLAB code function f=shootingfunc(c) % set up as a function, the input (c) is the initial slope % y’(0) and the output (f) is the difference between the value % of y at x=2 and the number 3 (since y(2)=3 for the correct % solution. This function is called by MATLABs fzero % % shootingfunc.m % trying to solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3 % using y(0)=1 and y’(0)=c tspan=[0 2]; y0=[1 c]; [t,y]=ode45(’shootingexamplef’,tspan,y0); f=y(length(y),1)-3; % set function value to y(2)-3 return MATLAB code function f=shootingexamplef(t,y) % shootingexamplef.m % the DE y’’ + 3y’ + 2y = 2x written as a system f(1)=y(2); f(2)=-3*y(2)-2*y(1)+2*t; f=f(:); return

Index

83

SHOOTING METHOD

>> shootingexamplemain actualvalue = 16.9727

5.5 5 4.5 4 3.5 3 2.5 2 1.5 1 0

0.5

1

1.5

2

84

Index

5. BOUNDARY VALUE PROBLEMS

5.3 Relaxation methods Relaxation methods differ substantially from the previous shooting methods. The function is not integrated but rather an approximate solution is made that fits the governing differential equation. This results in a system of equations where the unknowns are the values of the function at the interior points. To do this we need to determine some approximations to the derivatives in any given differential equation.

5.3.1 Finite differences Finite differences is the procedure where we replace any derivatives by a finite difference approximation. For example recall the definition of the derivative dy y(x + ∆x) − y(x) ≈ dx ∆x alternatively this could be written yn+1 − yn dy ≈ dx n xn+1 − xn =

yn+1 − yn h

where yn = y(xn ) and h is the difference in x steps or just the space between x points. This is known as a forward approximation to the first derivative as it uses the current point (n) and the next point (n + 1). There is also a backward approximation that uses the current point (n) and the previous point (n − 1). This is given by dy yn − yn−1 ≈ dx n h Both the forward and backward approximations are first order accurate, that is the first neglected term is order ∆x.

There is in fact a second order accurate approximation known as the central approximation given by dy yn+1 − yn−1 ≈ dx n 2h These can be used to find approximations to the higher derivatives.

yn+1 − 2yn + yn−1 d 2 y ≈ 2 dx n h2

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

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 is simply solving a system of linear equations which is simple in MATLAB. Algorithm 1. Divide up the region into N equal subintervals. Note that the differential equation must be true at each grid point in the region. 2. Replace the derivatives in the differential equation by thier finite difference approximations at each grid point in the region. 3. If possible use the boundary conditions to replace some of the values. This is usually only possible for the equations at each end of the interval. 4. Rearrange the equations so that they are written as a matrix system of equations. The unknowns are the value of the solution at each grid point. 5. Solve the matrix system of equations. This then gives the solution at each grid point. Example Numerically solve y′′ + 3y′ + 2y = 2x

where

y(0) = 1

y(2) = 3

1. First divide up the region into N subintervals of width h where h=

b−a N

so xn = a + hn for n = 0 . . . N 2. Replace each derivative by its approximate value at each interior point. yn+1 − yn−1 yn+1 − 2yn + yn−1 +3 + 2yn ≈ 2xn 2 h 2h

for n = 1 . . . N − 1

This results in N − 1 equations in the N − 1 unknowns y1 , y2 , . . . yN−1 . 3. The values at the edge points are known (y0 = y(a) = 1, yN = y(b) = 3 in this case) The equations for n = 1 and n = N − 1 are slightly different to the others (n = 2, . . . N − 2) as they involve y0 and YN respectively which are known as they are just the boundary values. y2 − 2y1 + y0 y2 − y0 +3 + 2y1 ≈ 2x1 2 h 2h

for

n=1

yn+1 − yn−1 yn+1 − 2yn + yn−1 +3 + 2yn ≈ 2xn 2 h 2h

for

n = 2 . . .N − 2

yN − 2yN−1 + yN−2 yN − yN−2 +3 + 2yN−1 ≈ 2xN−1 2 h 2h

for n = N − 1

86

Index

5. BOUNDARY VALUE PROBLEMS

4. Mutiply by h2 to simplify, collect all terms with the same unknown value and written out in full we have (1 − 3h/2)y0 + (2h2 − 2)y1 + (1 + 3h/2)y2 = 2h2 x1 (1 − 3h/2)y1 + (2h2 − 2)y2 + (1 + 3h/2)y3 = 2h2 x2 (1 − 3h/2)y2 + (2h2 − 2)y3 + (1 + 3h/2)y4 = 2h2 x3 .. .

.. .

.. .

(1 − 3h/2)yN−3 + (2h2 − 2)yN−2 + (1 + 3h/2)yN−1 = 2h2 xN−2 (1 − 3h/2)yN−2 + (2h2 − 2)yN−1 + (1 + 3h/2)yN = 2h2 xN−1 Now y0 and yN are known (since they are the boundary conditions) so put them on the right hand side with all the other known parts gives (2h2 − 2)y1 + (1 + 3h/2)y2 = 2h2 x1 − (1 − 3h/2)y0 (1 − 3h/2)yn−1 + (2h2 − 2)yn + (1 + 3h/2)yn+1 = 2h2 xn

for

n = 2...N − 2

(1 − 3h/2)yN−2 + (2h2 − 2)yN−1 = 2h2 xN−1 − (1 + 3h/2)yN 5. This is a system of N − 1 equations in the N − 1 unknowns y1 , y2 , . . . yN−1 . Substituting the known end points (y0 = 1 and yN = 3) and writing in matrix form this can be easily solved in MATLAB using any number of solution methods depending on the size of the matrix (direct inversion, iterations etc.). In matrix form it can be written AY = b where the matrix A has coefficients as above, Y is the vectors of unknowns Y = (y1 , y2 , . . . , yN−1 )T and b is the known right hand side vector.

Index

RELAXATION METHODS

2h2 − 2

1 − 3h/2 0 0 A= .. . 0 0

1 − 3h/2

0

0

0

...

2h2 − 2

1 + 3h/2

0

0

...

1 − 3h/2

2h2 − 2

1 + 3h/2

0

...

0

1 − 3h/2

2h2 − 2

1 + 3h/2

...

.. .

.. .

.. .

.. .

.. .

...

0

0

1 − 3h/2

2h2 − 2

...

0

0

0

1 − 3h/2

b=

2h2 x1 − (1 − 3h/2)y0 2h2 x2 2h2 x3 2h2 x4 .. . 2h2 xN−2 2h2 xN−1 − (1 + 3h/2)yN

87

0

0 0 .. . 1 + 3h/2 2h2 − 2 0

What do you notice about the matrix A? You will see that it is ’tri-diagonal’ (only 3 diagonals are non-zero) and each diagonal has the same value. This makes it particularly easy to program in MATLAB using the diag command. Once you have coded A, and b it is a simple matter to solve the system AY = b to get the solution Y . This is then the approximation to the solution of the original ODE. Coding the diagonal matrix A is relatively easy in MATLAB as there is a command diag that is used to enter values into a diagonal matrix. The ones command is also useful for making vectors of a certain length with the same value in each position. Solving the system AY = b is easy using the ’backslash divide’ command y=A\b .

88

5. BOUNDARY VALUE PROBLEMS

Index

>> help diag DIAG Diagonal matrices and diagonals of a matrix. DIAG(V,K) when V is a vector with N components is a square matrix of order N+ABS(K) with the elements of V on the K-th diagonal. K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 is below the main diagonal. DIAG(V) is the same as DIAG(V,0) and puts V on the main diagonal. DIAG(X,K) when X is a matrix is a column vector formed from the elements of the K-th diagonal of X. DIAG(X) is the main diagonal of X. DIAG(DIAG(X)) is a diagonal matrix. Example m = 5; diag(-m:m) + diag(ones(2*m,1),1) + diag(ones(2*m,1),-1) produces a tridiagonal matrix of order 2*m+1. See also SPDIAGS, TRIU, TRIL. Overloaded methods help 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 an M-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

Index

89

RELAXATION METHODS

MATLAB code % fdexample.m % % solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3 % using finite differences a=0; b=2; % endpoints y0=1; yN=3; % y values at endpoints N=100; % number of points h=(b-a)/N; % x step size x=a+h:h:b-h; % set up vector of x points (interior points) % set up the matrix and solve Ay=RHS for y A=(2*hˆ2-2)*diag(ones(1,N-1)); % main diagonal elements A=A+(1-3*h/2)*diag(ones(1,N-2),-1); % one below main diagonal A=A+(1+3*h/2)*diag(ones(1,N-2),1); % one above main diagonal RHS=2*hˆ2*x; RHS(1)=RHS(1)-(1-3*h/2)*y0; RHS(N-1)=RHS(N-1)-(1+3*h/2)*yN; y=A\RHS’; plot(x,y)

5.5 5 4.5 4 3.5 3 2.5 2 1.5 1 0

0.5

1

1.5

2

90

Index

5. BOUNDARY VALUE PROBLEMS

Example Solve y′′ + 5y′ + 6y = cos x using finite differences.

where

y(0) = 2

y(3) = 6

Index

91

RELAXATION METHODS

MATLAB code % fdexample2.m % % solve y’’ + 5y’ + 6y = cos x y(0)=2 y(3)=6 % using finite differences a=0; b=3; % endpoints ya=2; yb=6; % y values at endpoints N=100; % number of points h=(b-a)/N; % x step size x=a+h:h:b-h; % set up vector of x points (interior points) % set up the matrix and solve Ay=RHS for y A=(6*hˆ2-2)*diag(ones(1,N-1)); % main diagonal elements A=A+(1-5*h/2)*diag(ones(1,N-2),-1); % one below main diagonal A=A+(1+5*h/2)*diag(ones(1,N-2),1); % one above main diagonal RHS=hˆ2*cos(x); RHS(1)=RHS(1)-(1-5*h/2)*ya; RHS(N-1)=RHS(N-1)-(1+5*h/2)*yb; y=A\RHS’; plot(x,y)

N=5

N = 10

160

400

140

350

120

300

100

250 80

200 60

150

40 20

100

0

50

−20 0.5

1

1.5

2

2.5

0 0

0.5

1

N = 20 400

350

350

300

300

250

250

200

200

150

150

100

100

50

50 0.5

1

1.5

2

2.5

3

2

2.5

3

N = 100

400

0 0

1.5

2

2.5

3

0 0

0.5

1

1.5

92

5. BOUNDARY VALUE PROBLEMS

Index

5.3.3 Comments on solving matrix equations There are many numerical problems with solving the matrix equation Ax = b The matrix equations that result from the finite difference method are tridiagonal. That is they have entries down the 3 central diagonals and zeros elsewhere. These are relatively efficient to solve Ax = b for. Suggested Solution Methods 1. Inverse This can be very slow for large matrices. What if inverse doesn’t exist? 2. Iteration Make a guess and use the equations to refine that guess until some convergence criteria is met. Usually the best method for large matrices (bigger than 25 × 25). Most common method is known as the Gauss-Seidel Iteration method 3. Row reduction then back-substitution

Index

RELAXATION METHODS

93

5.3.4 Finite elements Finite differences are only one of the many method of solving BVPs. Another very popular method is known as the finite element method. The concept underlying finite difference methods is that approximations are made to the derivatives in the differential equation. In contrast to this for finite element methods the solution is approximated by a sequence of model functions and the error in using these functions is minimized. For finite element methods it is more convenient to work on a BVP that has homogeneous boundary conditions. That is the value of the function at the end points is zero. Fortunately we can make a change of variable to ensure this. For example consider the problem y′′ + p(x)y′ + q(x) = r(x)

y(xL ) = yL

y(xR ) = yR

we can transform this to a problem with homogeneous boundary conditions by the transformation y(x) = Y (x) + ax + b Which can be shown to give a=

1 (yL − yR ) xL − xR

b=

1 (−xR yL + xL yR ) xL − xR

Example What does y′′ + 3y′ + 2y = 5x2

y(0) = 2

y(2) = 3

become when it is transformed to have homogeneous boundary conditions

94

Index

5. BOUNDARY VALUE PROBLEMS

5.3.5 Symbolic form of a BVP Consider an ordinary differential equation with homogeneous boundary conditions this can be written in sysmbolic form as L y = r(x)

y(xL ) = 0 y(xR ) = 0

where L is a sysmbol that represents all the differential operators in the differential equation. Example The ODE y′′ + 3x2 y′′ + 2ex y = 5x2 can be represented as 2

L y = 5x

where

d2 d L ≡ 2 + 3x2 + 2ex dx dx

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 of n independent expansion functions. Call these φi (x) for i = 1, . . . , n. The exact form of these functions will be specified later. The crux of the finite element method is that the approximate solution (denoted Y (x)) is written as some linear combination of these expansion functions, namely n

y(x) ≈ Y (x) = ∑ ci φi (x) i=1

The aim is to find the coefficients ci to get the best solution possible. 2. Construct what is known as a residual function (denoted ∆(x)) which is a measure of the error of the approximate solution ∆(x) = L Y (x) − r(x) n

= L

∑ ciφi(x) − r(x)

i=1 n

=

∑ ciL φi(x) − r(x)

i=1

Index

RELAXATION METHODS

95

3. Establish a set of conditions that allow us to find the coefficients ci . To do this choose a set of weight functions wi (x) for i = 1, . . . , n. Again these are as yet unspecified. These weight functions have the following properties (a) They are normalised so that Z xR

w j (x)dx = 1

xL

j = 1, . . . , n

for

(b) The weighted residual for each fucntion is zero Z 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 coefficients ci to be found. By substituting the definition of ∆(x) above into this expression we get for each j = 1, . . ., n Z xR xL

Z xR xL

Z xR xL

w j (x)

"

n

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

∑ ciL φi(x) − r(x)

i=1

n

w j (x) ∑ ci L φi (x)dx − i=1

n

∑ ci

i=1

Z xR xL

Z xR xL

dx = 0

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

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

Z xR xL

w j (x)r(x)dx

If the weight functions (w j (x)) and the expansion functions (φ j (x)) are specified then the right hand side of the last expression is known. bj = and the part

Z xR xL

Z xR xL

w j (x)r(x)dx

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

is also known. Hence the equation reduces to n

∑ ci M ji = b j

for

j = 1, . . . , n

i=1

This is just the matrix equation Mc = b So provided w j (x) and φ j (x) are specified then the coefficients to use as the linear combination of the expansion functions to give the approximate solution are given by solving Mc = b. This gives the ci in n

y(x) ≈ Y (x) = ∑ ci φi (x) i=1

and so the approximate solution has been found.

96

Index

5. BOUNDARY VALUE PROBLEMS

Choice of weight and expansion functions There are many choices that can be made for the weight functions (w j (x)) and the expansion functions (φ j (x)). Different choices give different types of methods. By far the most common and what has come to be known as the collocation method is to choose them equal, namely w j (x) = φ j (x). The finite element method uses “triangular hat” functions for both the weight functions (wi (x)) and the expansion functions (φi (x)).

φi (x) =

1 h (x − xi−1 )

for

xi−1 < x < xi

1 h (xi+1 − x)

for

xi < x < xi+1

0

otherwise

These functions are triangles that span 3 grid points and are zero everwhere else. For example φ3 is a triangle that begins at zero at x2 increases to a maximum at x3 and then decreases to zero at x4 .

φ3

x0

x1

x2

x3

x4

x5

x6

The benefit of these functions is that they are local. That it is only nonzero in the immediate neighbourhood of the central point. This guarantees that the resulting matrix eqaution will be sparse (have many zero entries) and hence can be solved effeciently using iterative methods. These triangular hat functions are useful as they have some special properties that make calculating the M matrix easier. F(x)

φi φi±1 ′ φi±1

φi′′ ′′ φi±1 1 x x2 x3

Z xR xL

φi F(x) dx

2h/3 h/6 ±1/2 −2/h 1/h h ih2 (i2 + 1/6)h3 i(i2 + 1/2)h4

(zero for all other indices) (zero for all other indices) (zero for all other indices)

Index

97

RELAXATION METHODS

5.3.7 Finite element example Solve y′′ + y = 1 Now L ≡

d2 dx2

y(0) = 0

y(π /2) = 0

+ 1 so to calculate M ji break it down into each term such that Z π /2

φ j (x)L φi (x)dx 2 Z π /2 d = + 1 φi (x)dx φ j (x) dx2 0 Z π /2 Z π /2 d 2 φi (x) φ j (x) φ j (x)φi (x)dx = dx + dx2 0 0

M ji =

0

and so from the table of special properties of the triangular functions −2/h 1/h 0 ... 0 0 2h/3 h/6 0 1/h −2/h 1/h . . . 0 0 h/6 2h/3 h/6 .. . . . .. + .. .. .. . . . .. . M= . . . . . . . . 0 0 0 . . . −2/h 1/h 0 0 0 0 0 0 . . . 1/h −2/h 0 0 0 Calculating the right hand side gives bj = =

Z π /2 0

Z π /2 0

and so from the tables

Now find the coefficients ci in

w j (x)r(x)dx

φ j (x) 1 dx

h h h .. .

b= h h

n

y(x) ≈ Y (x) = ∑ ci φi (x) i=1

by solving the matrix equation Mc = b with M and b above.

... ... .. .

0 0 .. .

0 0 .. .

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

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 all global a b N a=0; b=pi/2; N=10; % N interior points h=(b-a)/(N+1); xk=a+h:h:b-h; % interior points % set up and solve Mc=RHS M=(-2+2/3*hˆ2)*diag(ones(1,N)); % main diagonal elements M=M+(1+hˆ2/6)*diag(ones(1,N-1),-1); % one below main diagonal M=M+(1+hˆ2/6)*diag(ones(1,N-1),1); % one above main diagonal M=M/h; % multiplicative factor RHS=h*ones(N,1); % right hand side vector c=M\RHS % solve for c the coefficients % plot numerical solution and exact solution and calculate error npts=26; x=a:(b-a)/(npts-1):b; % vector to calculate solution at % call feexamplef.m which is a function that calculates the solution % at a given point i.e. it works out y(x)=sum(c(i)*phi_i(x)) % where the phi_i are the ’triangular hat’ functions for k=1:npts y(k)=feval(’feexamplef’,c,x(k)); end plot(x,y) hold on exact=-sin(x)-cos(x)+1; plot(x,exact,’ro’) ylabel(’exact and numerical’) error=abs(exact-y); maxerror=max(error) hold off

Index

99

RELAXATION METHODS

MATLAB code

function f=feexamplef(c,x) % feexamplef.m % function that calculates y(x)=sum(c(i)*phi_i(x)) % where the phi_i are the ’triangular hat’ functions centered around x % global a b N h=(b-a)/(N+1); xk=a+h:h:b-h; if (x <= xk(1)) % left most function f=c(1)*(x-a)/h; end for i=1:N-1 % all interior functions that have left and right parts if (x >= xk(i)) & (x <= xk(i+1)) right=c(i)*(xk(i+1)-x)/h; left =c(i+1)*(x-xk(i))/h; f=left+right; end end if (x >= xk(N)) % right most function f=c(N)*(b-x)/h; end return

0

−0.05

−0.1

−0.15

−0.2

−0.25

−0.3

−0.35

−0.4

−0.45

0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

100

Index

5. BOUNDARY VALUE PROBLEMS

5.3.8 Finite element summary For the BVP L y = r(x)

y(xL ) = 0 y(xR ) = 0

1. Choose n expansion functions φi (x) for i = 1, 2, . . ., n. 2. Choose n weight functions wi (x) for i = 1, 2, . . ., n. 3. Let wi (x) = φi (x) =

n

1 h (x − xi−1 )

for xi−1 < x < xi

1 h (xi+1 − x)

for xi < x < xi+1

0

otherwise

4. Numerical solution Y (x) = ∑ ci φi (x) i=1

5. Solve Mc = b for the ci ’s where M ji =

Z xf

bj =

x0

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

Z xf x0

w j (x)r(x) dx

6. Properties of triangular functions F(x)

φi φi±1 ′ φi±1

φi′′ ′′ φi±1 1 x x2 x3

Z xf x0

φi F(x) dx

2h/3 h/6 ±1/2 −2/h 1/h h ih2 (i2 + 1/6)h3 i(i2 + 1/2)h4

(zero for all other indices) (zero for all other indices) (zero for all other indices)

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

101

5.4 Differential eigenvalue equations Recall the example y′′ + ω 2 y = 0

where

y(0) = 0 y(π ) = 0

which has solution many solutions each one of which is yn = An sin ωn x where An is an unknown constant and ωn = n = 0, ±1, ±2, . . . This equation is known as a Differential Eigenvalue Equation as there are only particular values (eigenvalues) for which a solution exists. How would you solve this numerically given you don’t know the eigenvalues ω ? Treat the eigenvalues as one of the unknowns of the problem. But how ? We know that the eigenvalue is a constant so we can write a differential equation for it as dω =0 dx but then we also need an extra boundary condition.

ω (0) = ω0 where ω0 is the eigenvalue we need to find. So putting y = y1 , dy/dx = y2 and ω = y3 the system to date is dy1 = y2 dx dy2 = −(y3 )2 y1 dx dy3 = 0 dx subject to y1 (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 is unknown (ω0 ) and one of them is at x = π not x = 0. To be able to integrate forward from x = 0 we need three initial conditions at x = 0 and we also need three known conditions in total for the problem to have a unique solution.

102

5. BOUNDARY VALUE PROBLEMS

Index

To use a shooting method you need 3 boundary conditions at the same point so introduce a new boundary condition y2 (0) = c where c is a value that has to be determined to satisfy the boundary conditions at the other end and ω0 is a value that also has to be determined. But there is still not enough information to find the solution as there are only 2 known boundary conditions (y1 (0) = 0 y1 (π ) = 0) so we must introduce another boundary condition that is considered to be known. Looking at the analytic solutions that were found earlier yn = An sin ωn x note that they are only defined to within some arbitrary constant (An ). To specify a specific solution another condition would also have to be used. This condition is known as the normalising condition and consists of choosing some given value for one of the unknown values. So in fact we are free to choose the value of y2 (0) = c to be any (non zero) value as this just fixes the unknown constant. The system of equations is now dy1 = y2 dx dy2 = −(y3 )2 y1 dx dy3 = 0 dx subject to 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 at x = 0 that has to be determined to satisfy the boundary conditions at x = π . So choose a value for ω0 integrate forward to x = π and check the value of y1 (π ) if it is equal to zero it is the correct ω0 if it is not zero then change the ω0 guess. This can be done using the MATLAB command fzero.

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

103

MATLAB code function f=deig(omega0) % deig.m % % solving y’’ + omegaˆ2 y = 0 y(0)=0 y(pi)=0 % This function takes as input the value of omega0 and returns % the value at y(pi). If this equals zero then that value of % omega0 gave a valid solution to the ODE. % Note the system of equations becomes 3rd order! % This function is called by fzero to find the value of omega0 % that gives the correct solution. % format compact tspan=[0 pi]; y0=[0 1 omega0]; % y_1(0)=0, y_2(0)=1, y_3(0)=omega0 [t,y]=ode45(’deigf’,tspan,y0); plot(t,y(:,1)); i=length(y); % determine length of solution vector f=y(i,1); % set function value = y_1(pi) return MATLAB code function f=deigf(t,y) % deigf.m f(1)=y(2); f(2)=-y(3).ˆ2.*y(1); f(3)=0; f=f(:); % forces f to be a column vector return

104

Index

5. BOUNDARY VALUE PROBLEMS

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

1.2

1

0.8

0.6

0.4

0.2

0 0

0.5

1

1.5

2

2.5

3

3.5

2.5

3

3.5

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

0.5 0.4 0.3 0.2 0.1 0 −0.1 −0.2 −0.3 −0.4 −0.5 0

0.5

1

1.5

2

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

105

Example Consider the differential equation √ dz = 3 z − αt2 dt

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 a particular value (or values) that make the equation satisfied. How do you find that particular value?

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

MATLAB code function f=findalpha(alphaguess) % findalpha.f % this function takes as input a guess for the constant % alpha and returns y(1)-4 % if this is close to zero then its a good guess y0=[2 alphaguess]; % values at t=0 tspan=[0 1]; % integrate equation over t=(0,1) [t,y]=ode45(’alphaf’,tspan,y0); lgthy=length(y); % determine how long the vector is % f is a measure of how good the solution matches the desired % boundary condition which is y(1)=4 % if f is close to zero then this is a good solution. currentguess=alphaguess ; f=y(lgthy,1)-4; sprintf(’Current guess for alpha =%10.8f y(1)-4 = %10.8f’... ,currentguess,f) return ;

MATLAB code function f=alphaf(t,y) % alphaf.m the governing DEs the alpha % eigenvalue problem % % y(1) is z, y(2) is alpha % z=y(1); alpha=y(2); % set up the functions f(1)=3*sqrt(z)-alpha*t.ˆ2; % the DE f(2)=0; % since alpha is a constant f=f(:); % force f to be a column vector return ;

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

alpha initialguess = 11 ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha correctalpha = 11.38377327554915 diary off

=11.00000000

y(1)-4 = 0.15451914

=10.68887302

y(1)-4 = 0.27958057

=11.31112698

y(1)-4 = 0.02927189

=10.56000000

y(1)-4 = 0.33132966

=11.44000000

y(1)-4 = -0.02266307

=11.38366126

y(1)-4 = 0.00004514

=11.38377326

y(1)-4 = 0.00000001

=11.38377328

y(1)-4 = 0.00000000

=11.38377328

y(1)-4 = 0.00000000

5

4.5

4

3.5

3

2.5

2 0

0.2

107

0.4

0.6

0.8

1

108

5. BOUNDARY VALUE PROBLEMS

Index

6 Partial Differential Equations (PDEs)

6.1 Overview Partial differential equations arise in a lot of applications. In this introductory section, I will try to give you a bit of a feeling for the diversity of some of these applications. First, however, I will define exactly what a partial differential equation (PDE) is and define what is 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 than one variable. For example, the temperature in a room will normally be a function of both time and the position in the room — four different variables t, x, y and z. To describe rates of change with respect to each of these variables we need partial derivatives.

6.2.2 Examples of PDEs A partial differential equation is a differential equation with partial derivatives. Some examples are

∂ u ∂ 2u = 2, ∂t ∂x

∂f = ∂x

∂f ∂y

2

,

∂c ∂c ∂c + + = c. ∂x ∂y ∂z

(6.1)

In the first example the dependent variable u is a function of the two independent variables x and t. In the second example the dependent variable f is a function of x and y and in the third example the dependent variable c is a function of x, y, and z.

110

Index

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

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 which variable. In the above examples, the order of the 1st PDE in equation (6.1) is 2, the order of the 2nd and 3rd PDEs in equation (6.1) are both 1.

6.3.2 Linear PDEs A PDE is said to be linear if it is a linear combination of the dependent variable and its various partial derivatives. For example,

∂ u ∂ 2u = 2, ∂t ∂x

∂f ∂f ∂f +x =z , ∂x ∂y ∂z

∂ 4c ∂ 2c + +c = 0 ∂ x4 ∂ x2

(6.2)

∂ 4c + c2 = 0 ∂ x4

(6.3)

are linear, whereas

∂u = ∂t

∂u ∂x

2

,

∂f = ef , ∂x

are not.

6.3.3 Why use numerical methods For analytic methods we will be concentrating on linear PDEs. In general nonlinear PDEs are too difficult to solve analytically with notable exceptions. One reason for this is that linear PDEs allow superposition. This means that if we have any two solutions of a PDE then their sum is also a solution. This is a useful way of building up the entire solution of a linear PDE analytically. The method of superposition (adding solutions) does not work for nonlinear PDEs. Numerical methods aren’t restricted to solving just linear problems. This is the entire point of studying numerical solutions to these types of problems in that they can not be solved by any other means. The analytic methods used for linear PDEs often provide a useful check on the numerical methods in particular circumstances. This is important if you want to have any confidence in your numerical solutions. Broadly speaking there are three types of PDEs. The type of numerical solution method is different for each type. What we will do is study an example of each type.

Index

THREE COMMON PDES IN APPLICATIONS

111

6.4 Three common PDEs in applications Three of the most common PDEs in applications are the wave equation, the diffusion equation and Laplace’s equation. These equations are defined as follows:

1-D Diffusion equation:

∂F ∂ 2F =D 2 ∂t ∂x

2-D Laplace’s equation:

∂ 2F ∂ 2F + =0 ∂ x2 ∂ y2

1-D Wave equation:

2 ∂ 2F 2∂ F =c ∂ t2 ∂ x2

112

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.5 The diffusion equation The diffusion equation in one dimension is

∂F ∂ 2F =D 2 ∂t ∂x

(6.4)

where D is a positive constant and is called the diffusion coefficient. This partial differential equation occurs in studies of heat conduction and diffusion of mass (eg. pollutants or chemicals).

6.5.1 Heat conduction In heat conduction the dependent variable F(x,t) is the temperature inside a material, where heat is conducted in only one direction. Solving this PDE gives the temperature at any point and at any time. The derivation of the diffusion equation when related to heat uses the principle of conservation of heat energy. The RHS of (6.4) physically comes from the net conduction of heat through the material, whereas the LHS comes from the heat being used to raise the temperature of the material. The diffusion equation when related to heat is sometmes known as the heat equation.

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 a pollution spreading out in 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 or three dimensions. For heat conduction, we again use conservation of heat energy. In two and three dimensions, the diffusion equations are 2 2 ∂F ∂ F ∂ 2F ∂F ∂ F ∂ 2F ∂ 2F (6.5) =D + 2 , =D + 2 + 2 . ∂t ∂ x2 ∂y ∂t ∂ x2 ∂y ∂z respectively. We can also write these as

∂F = D∇2 F. ∂t

Index

LAPLACE’S EQUATION

113

6.6 Laplace’s equation 6.6.1 Equilibrium solutions Laplace’s equation, in two dimensions, is

∂ 2F ∂ 2F + 2 =0 ∂ x2 ∂y

(6.6)

This equation arises in many different applications. One common application is in 2D or 3D heat conduction problems, where we assume the temperature is in thermal equilibrium. In thermal equilibrium, there is no time dependence on the temperature. Setting the time derivative in (6.5) to zero, we thus obtain (6.6).

6.6.2 Other applications Laplace’s equation also arises in electrostatics (where the electrical potential, measured in Volts, satisfies Laplace’s equation) and in fluid mechanics (where the velocity potential satisfies Laplace’s equation). Laplace’s equation also arises in ground-water flow examples. More generally, we write Lapalce’s equation as ∇2 F = 0.

114

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.7 The wave equation The wave equation in one dimension is 2 ∂ 2F 2∂ F = c ∂ t2 ∂ x2

(6.7)

where c is a constant. This partial differential equation describes small amplitude wave propagation for a number of applications. Here the constant c is the speed of a wave in a given medium.

6.7.1 Sonar, radar, seismic waves The wave equation arises in sonar, electromagnetic waves, radar, and seismic prospecting for example.

6.7.2 2D and 3D waves In two and three dimensions, the wave equations are 2 2 ∂ 2F ∂ 2F ∂ 2F ∂ 2F ∂ 2F 2 ∂ F 2 ∂ F =c + 2 , =c + 2 + 2 ∂ t2 ∂ x2 ∂y ∂ t2 ∂ x2 ∂y ∂z respectively. More generally, we write

∂ 2F = c2 ∇2 F. ∂ t2

(6.8)

Index

POISSON’S EQUATION

115

6.8 Poisson’s equation A forced Laplace’s equation is known as a Poisson Equation. ∇2 F = 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 equations can also be considered in other co-ordinate systems such as cylindrical (r, θ , z) or spherical (ρ , φ , θ ). For example Laplace’s equation in cylindrical co-ordinates is

∂ 2F 1 ∂ F 1 ∂ 2F ∂ 2F + ∇ F= 2 + + ∂r r ∂ r r 2 ∂ θ 2 ∂ z2 2

and in spherical co-ordinates is

∂ 2F 2 ∂ F ∂ 2F 1 1 ∂ 2 F cot θ ∂ F + + + 2 + ∇ F= ∂ ρ 2 ρ ∂ ρ ρ 2 sin2 θ ∂ φ 2 ρ 2 ∂ θ 2 ρ ∂θ 2

Sometimes this can make the solution process (both analytic and numerical) easier. Can you think of an example?

116

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.10 Boundary and initial conditions 6.10.1 ICs and BCs Partial differential equations require additional conditions to obtain unique solutions. Conditions on time are called initial conditions, usually at t = 0. Conditions on spatial variables are called boundary conditions. Boundary conditions refer to the endpoints of 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 the respective derivatives in the PDE. The diffusion equation (6.4) requires one initial condition since its time derivative is of order one. It requires two boundary conditions, since the x-derivative is second-order. For the wave equation (6.7) we need to specify two initial conditions (since there is a second-order derivative with respect to t) and two boundary conditions. Laplace’s equation (6.6) requires four boundary conditions, two for the xvariable and two for the y-variable.

6.10.3 A typical initial condition A typical initial condition is f (x, 0) = 100. This states the initial value of f is a constant value 100.

6.10.4 Typical boundary conditions Typical boundary conditions include F(0,t) = 30,

∂F (10,t) = 100. ∂x

The first condition specifies the value of F, at the point x = 0, and the second specifies the value of the derivative of f at the point x = 10, for all time.

Index

TYPES OF BOUNDARY CONDITIONS

117

6.11 Types of boundary conditions There are 3 types of Boundary Conditions. The type of boundary condition has an impact on which numerical solution method you can use for a problem hence it is important to be able to classify the boundary conditions. Dirichlet The values of the function are given at the boundary. For example F(0,t) = 30

Neumann The value of the normal derivative is given at the boundary. For example ∂∂Fx (10,t) = 100 which can also be written ∂∂Fx = 100 at x = 10

118

Index

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Cauchy Values of both the function and its normal derivative are given at the boundary. For example ∂∂Fx = F at x = 10

Classify (with reason) each boundary condition of the following PDEs as Dirichlet, Neumann or Cauchy. 2

(a)

d2T dy2

+ ddxT2 = 4 with T (0, y) = 0 T (x, 0) = 2

(b)

dT dt

=

d2 T dx2

with T (0,t) = 0

(c)

dT dt

=

d2T dx2

with T (0,t) = sint

dT dx (L,t) =

T (L, y) = sin y

dT dy (x, M) =

0 T (x, 0) = f (x)

dT dx (L,t) =

T − Ta

T (x, 0) = f (x)

0

Index

DIFFERENT SOLUTION APPROACHES

119

6.12 Different solution approaches Partial differential equations (PDEs) can be solved analytically or numerically. It is important to realize that not all PDEs have solutions. Numerical techniques can be used on a wider range of PDEs but analytical techniques sometimes give more information or can be 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 more ordinary differential equations. The approach we will explore in this course is separation of variables. This involves assuming the dependent variable as a product of functions of each independent variable.

6.12.2 Other approaches We only have time in this course to learn the above method for solving PDEs. But you should at least be aware that other techniques exist. One technique is to use Laplace transforms. The Laplace transform of an ordinary differential equation reduces the differential equation to an algebraic equation. Applying Laplace transforms to a PDE (which is a function of two variables) reduces the PDE to an ODE which is a function of one variable. One then solves the ODE and then inverts the Laplace transform. Another method for solving PDEs involves finding transformations of variables. These are called similarity solutions.

6.12.3 Numerical techniques Most practicing engineers or mathematicians would use numerical techniques for solving PDEs. These are often customised to a particular class of applications. The two most common numerical techniques for solving PDEs are finite differences and finite elements. In finite differences, the partial derivatives are approximated with derivative quotients, evaluated on a rectangular grid. In finite elements, the region is divided up into small panels, or elements, and the quantity you are solving for is expressed as a basis of simple functions defines over just the finite element. Both finite difference and finite element methods lead to matrix equations where the matrices can be large sparse matrices.

6.12.4 Value of analytic solutions While numerical techniques are often used for practical problems, knowledge of the basic analytical solutions, such as those obtained in this course, can provide valuable incite into the problems and may also provide a simple check on the accuracy of the numerical program in particular circumstances. It is always a wise idea to check numerical methods on a solution with a known analytic solution to try and guarantee that the method and the coding is correct.

120

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.13 Examples Heat in a rod Consider an insulated rod of steel of length L were one end is held at 100◦ C and the other can lose heat according to Newton’s Law of Cooling (heat loss is proportional to the difference in the temperature of the rod and the surrounding ambient air). The ambient air is at 25◦ C. Write down the governing equation, the initial and boundary conditions.

Index

EXAMPLES

121

Steady state Temperature in a slab Consider a slab of wood of size 10cm (wide) by 5cm (long) by 2cm (high) that has reached thermal equilibrium. If the bottom of the slab is insulated, the top loses heat by Newton’s Law of Cooling, the sides are held at 20◦ C, write down the governing equation and boundary conditions.

122

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

Plucked String Consider a string of length L that is held fixed at both ends. The string is pulled out to a triangular shape and then released. Write down the governing equation, boundary and initial conditions.

Index

EXAMPLES

123

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

124

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.14 Classification of PDEs The numerical solution of the 3 standard types of linear PDEs (Diffusion, Laplaces and Wave equations) differs for each type of equation. In fact these PDEs are examples of more general classes of PDEs known as Parabolic, Elliptic and Hyperbolic PDEs. The numerical methods for solving PDEs differ depending on which type of PDE you are solving. Consider a general PDE that depends on 2 variables (a and b).

A

∂ 2u ∂ 2u ∂ 2u ∂u ∂u + 2B +C = F(a, b, u, , ) 2 2 ∂a ∂ a∂ b ∂b ∂a ∂b

where A, B and C are constants and F(a, b, u, ∂∂ ua , ∂∂ ub ) is an arbitrary function of those variables. The classification of a PDE (and hence the type of numerical method used to solve it) depends on the the quantity B2 − AC. B2 − AC = 0 B2 − AC < 0 B2 − AC > 0

Parabolic Elliptic Hyperbolic

eg Diffusion equation eg Laplaces equation eg Wave equation

One Dimensional Diffusion equation Has A = D, B = 0, C = 0, a = x (the space variable), b = t (the time variable) and the function F = ∂∂ut .

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

One Dimensional Wave equation , a = x (the space variable), b = t (the time variable) and F = 0. Has A = 1, B = 0, C = −1 c2

7 Parabolic Equations: Diffusion Equation

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

7.1.1 First Derivative Recall the finite difference approximations First derivative Forward Approximation dy yn+1 − yn ≈ dx n xn+1 − xn =

yn+1 − yn ∆x

where yn = y(xn ) and ∆x is the difference in x steps or just the space between x points. First derivative Backward Approximation dy yn − yn−1 ≈ dx n ∆x

Both the forward and backward approximations are first order accurate, that is the first neglected term is order ∆x.

First derivative Central Approximation given by yn+1 − yn−1 dy ≈ dx n 2∆x

7.1.2 Second Derivative Second derivative Central Approximation given by yn+1 − 2yn + yn−1 d 2 y ≈ 2 dx n (∆x)2

126

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Index

7.2 FTCS method

Consider the diffusion equation in one space dimension

∂F ∂ 2F =D 2 ∂t ∂x

(7.1)

with boundary conditions F(a,t) = Fa (t)

F(b,t) = Fb (t)

and initial condition F(x, 0) = F0 (x) We require the solution in the region a ≤ 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 we calculate the solution. This is known as discretising the region. Take Nx points in the x direction so that the spacing between points is then given by ∆x =

b−a Nx

and let the variable i be the index that refers to the discrete x points. That is xi = a + i∆x

i = 0, 1, . . ., Nx

Do a similar process for the t direction. Take Nt points in the t direction so that the spacing between time steps is then given by ∆t =

T −0 Nt

and let the variable j be the index that refers to the discrete time steps. That is t j = j∆t

j = 0, 1, . . ., Nt

This has now divided the (x,t) plane into a grid. The aim is to calculate the solution at each of the grid points.

Index

127

FTCS METHOD

t

t =0

j = 10

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=9

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=8

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=7

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=6

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=5

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=4

t u

bc

bc

bc

bc

bc

bc

bc

t u

bc

bc

bc

t u

bc

∆t

j=3

t u

bc

bc

bc

j=2

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=1

t u

bc

bc

bc

bc

bc

bc

bc

t u

rs

rs

rs

rs

rs

rs

rs

rs

rs

i=0

i=1

i=2

i=3

i=4

i=5

i=6

i=7

i=8

x=a sr t u bc

∆x

x=b

Initial condition – known F(x, 0) = F0 (x) Boundary conditions – known F(a,t) = Fa (t) and F(b,t) = Fb (t) Interior points – unknown

x

128

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

j

Let the solution at the point (xi ,t j ) be denoted by Fi , that is Fi j = F(xi ,t j ) = F(a + i∆x, j∆t) Now approximate the diffusion equation

∂ F ∂ 2F = 2 ∂t ∂x around the point (xi ,t j ) by using a forward finite difference approximation for the time derivative j+1 j ∂ F j Fi − Fi ≈ ∂ t i ∆t and a centred finite difference approximation for the spatial second derivative j j j j Fi+1 − 2Fi + Fi−1 ∂ 2 F ≈ ∂ x2 i (∆x)2

This gives the PDE as approximately Fi

now make Fi

j+1

j+1

j

j

j

j

Fi+1 − 2Fi + Fi−1 (∆x)2

− Fi =D ∆t

!

the subject j

Fi

j+1

Fi

j+1

j

j

F − 2Fi + Fi−1 = ∆tD i+1 (∆x)2

!

= γ Fi+1 + (1 − 2γ )Fi + Fi−1 j

j

where

γ=

j

+ Fi

j

(7.2)

D∆t (∆x)2

Equation (7.2) is known as the Forward Time Centered Space (FTCS) approximation to the diffusion equation. The FTCS equation enables solutions at time level j +1 to be calculated from information at time level j. Given that we know the initial condition (level j = 0) then we are able to use this to calculate the solution at time level j = 1. This is then used to get the solution at level j = 2 and so on. At a given time level you use the FTCS equation to sweep across in the i direction calcuj+1 lating the value of Fi . The FTCS method is an explicit method as at each time step there is an explicit equation for the values at the next time step.

Index

FTCS METHOD

7.2.2 Template for the FTCS Method

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t j+1 u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

rs

rs

rs

rs

rs

rs

rs

rs

rs

i−1

i

i+1

j

t =0

x=a sr t u bc

x=b

Initial condition – known F(x, 0) = F0 (x) Boundary conditions – known F(a,t) = Fa (t) and F(b,t) = Fb (t) Interior points – unknown

129

130

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

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

∂T ∂ 2T =D 2 ∂t ∂x where for steel D = 5 × 10−6 m2 /s The boundary and initial conditions can be written T (0,t) = 0

T (2,t) = 0

T (x, 0) = 100

Find the temperature distribution at times t = 60, 120, 180, . . .600 minutes The exact solution can be found analytically using the method of separation of variables to be ∞ mπ x −Dm2 π 2t sin T (x,t) = ∑ am exp 4 2 m=1

where

am =

−200 ((−1)m − 1) mπ

Temperature in metal bar at steps of 60 minutes 100 numerical exact

90 80

Temperature

70 60 50 40 30 20 10 0 0

0.5

1 x

1.5

2

Index

FTCS METHOD

MATLAB code % ftcsdiffusion.m % Solve heat diffusion in a steel bar of length 2 % with ends at 0oC and initially the bar is 100oC. % Use FTCS finite difference method % dT/dt = D d2T/dx2 % subject to boundary conditions T(0,t)=0 T(2,t)=0 % and initial condition T(x,0)=100 % clear all; a=0; b=2; % x boundaries Nx=10; % number of x points to use dx=(b-a)/Nx; % x step x=[a:dx:b]; % x points fa=0; fb=0; % boundary values D=5e-6; % steel bar diffusivity = 5.0x10ˆ-6 mˆ2/s dt=3600 % time step in seconds (=60 minutes) tfinal=36000; % final time in seconds (=600 minutes) gamma=D*dt/(dxˆ2) T(1)=fa; T(Nx+1)=fb;% boundary conditions, note that index for % i starts at 1 not 0 as matlab doesnt % allow zero indexing T=100*ones(1,Nx+1); % initial temperature in bar is 100 % plot initial temperature distribution plot(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 tfinal for t=dt:dt:tfinal % sweep across the bar calculating the Temperature for i=2:Nx Tnext(i)=gamma*(T(i-1)+T(i+1))+(1-2*gamma)*T(i); end Tnext(1)=fa; Tnext(Nx+1)=fb; T=Tnext; % update the temperature % exact solution from separation of variables method Texact=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

131

132

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.2.4 Stability The stability of a time stepping numerical method is all to do with whether errors grow or decay as you step in time. Consider the forward time approximation to the time derivative at the point (xi ,t j ) j+1 j ∂ F j Fi − Ti ≈ ∂ t i ∆t

This has an error of order (∆t)2 so at each time step there is an error introduced. The question is do these errors build up and swamp the solution or do they get damped out? It turns out that for an FTCS (Forward Time Centred Space) approximation to the diffusion equation as given by Fi

j+1

= γ (Ti+1 + Ti−1 ) + (1 − 2γ )Ti j

where

γ= that if

γ<

1 2

γ>

j

j

D∆t (∆x)2

then the method is stable and errors do not grow 1 2

then the method is unstable and 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 below 12 at all times. This is particularly important if the diffusion coefficient varies in the problem. How does this effect the usefulness of the FTCS method? If you have a metal bar with diffusion constant of D = 5 × 10−6 m2 /s of length 2m and you want 100 points along the bar to get an accurate representation of the temperature in the bar what value of the time step do you need to use?

Now if you double the number of space points how is the time step effected?

Index

FTCS METHOD

133

7.2.5 Unstable Example Go back to original FTCS example. For the original problem what would happen if we increase the time step from dt = 3600 seconds to dt = 7200 seconds. Then γ = 0.9

Temperature in metal bar at steps of 120 minutes 250 numerical exact

200

Temperature

150 100 50 0 −50 −100 −150 0

0.5

1 x

1.5

2

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 a very restrictive condition as a halving of the spatial step (twice as many x points) results in a time step a factor of 4 smaller. If any degree of accuracy is required (that is a small x step is required) then method can take a very long time to run as a very small time step is required.

134

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.3 Crank-Nicolson method In contrast to the FTCS method the Crank-Nicolson method uses an average of the old and new time step for the spatial derivative. j+1 j! ∂ F j ∂ 2 F 1 ∂ 2 F =D + ∂ t i 2 ∂ x2 i ∂ x2 i Fi

j+1

j

− Fi D = ∆t 2

j+1

j+1

j+1

j

j

j

Fi+1 − 2Fi + Fi−1 F − 2Fi + Fi−1 + i+1 2 (∆x) (∆x)2

!

Which rearranges to γ j −γ j+1 j+1 j j+1 j Fi+1 + Fi−1 + (1 + γ )Fi Fi+1 + Fi−1 + (1 − γ )Fi = 2 2

where

γ=

D∆t (∆x)2

The left hand side has the unknowns at time j + 1 the right hand side the known values at time j. These are for each value of i as you step across the region. Hence it is really n equations in n unknowns (recall the finite-difference work for ODEs). This is known as an implicit method as there is now no longer an explicit equation for the solution at time j + 1 in terms of time level j values but rather a system of equations to solve.

7.3.1 Template

bc

bc

bc

bc

bc

j+1

bc

bc

bc

bc

bc

j

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

i−1

i

i+1

Index

135

CRANK-NICOLSON METHOD

7.3.2 Matrix Representation The boundary conditions at i = 0 and i = Nx are known so the equations are slightly different for i = 1 and i = Nx − 1 to account for this. The system of equations can be written in matrix form (recall the finite difference work on ODEs) as AF j+1 = BF j + d j where

A=

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

B=

γ dj = 2

j j+1 Fa + Fa

0 0 .. . 0 j j+1 Fb + Fb

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

j F =

... ... .. .

0 0 .. .

0 0 .. .

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

... ... .. .

0 0 .. .

0 0 .. .

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

j

F1 j F2 0 .. . 0 j FNx −1

j+1 F =

F1j+1 F2j+1 0 .. . 0 j+1 FNx −1

This system of equations can be easily solved in MATLAB using the ’backslash divide’ command. For example something like Fnext=A\(B*F+d) One of the advantages of the implicit scheme such as Crank-Nicolson is that it is stable irespective of the value of γ . That is errors do not build up to catastrophic proportions even for large time steps. Hence large time steps can be taken only limited by the accuracy at each step. This is the major advantage of the implicit Crank-Nicolson scheme over the explicit FTCS scheme. This stability comes at a price, namely that a matrix equation must be solved at each time step. This is not too bad in this case as the matrix A is tri-diagonal and hence there are fast and efficient methods available to invert it. Also since in this case A does not change from one tme step to the next then it is even possible to determine the inverse of A once and store it for future use thereby speeding up the calculations even more. As a general rule explicit schemes suffer from stability problems and implicit schemes are usually stable.

136

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

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

∂T ∂ 2T =D 2 ∂t ∂x where for steel D = 5 × 10−6 m2 /s The boundary and initial conditions can be written T (0,t) = 100

T (2,t) = 0

T (x, 0) = 0

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

Temperature in bar at steps of 4 hours 100 90 80

Temperature

70 60 50 40 30 20 10 0 0

0.5

1 x

1.5

2

Index

CRANK-NICOLSON METHOD

137

MATLAB code % cranknic.m % Solve the heat diffusion equation on a steel bar of length 2 % with one end held at 100oC the other at 0oC and initially % the bar is 0oC. Use Crank-Nicolson finite difference method % % dT/dt = D d2T/dx2 boundary conditions T(0,t)=100 T(L,t)=0 % and initial condition T(x,0)=0 % clear all; n=40; L=2; dx=L/n; x=[0:dx:L]; % x points fa=100; fb=0; % boundary values D=5e-6; % diffusivity = 5x10ˆ-6 mˆ2/s dt=144 % time step in seconds (plot every 100th) tfinal=144000; % final time in seconds (=4 hours) gamma=D*dt/(dxˆ2) % T=zeros(n-1,1); % initial condition Tplot=[fa;T;fb]; % add in the boundary condition for plot plot(x,Tplot); xlabel(’x’); ylabel(’Temperature’); title(’ Temperature in bar at steps of 4 hours’) hold on % % set up left and right hand tri-diagonal matrices Aleft=diag((1+gamma)*ones(n-1,1))... +diag((-gamma/2)*ones(n-2,1),1)... +diag((-gamma/2)*ones(n-2,1),-1); Aright=diag((1-gamma)*ones(n-1,1))... +diag((gamma/2)*ones(n-2,1),1)... +diag((gamma/2)*ones(n-2,1),-1); % % loop through time calculating T at each new step and plot % each 100th calculation (see the mod function for how to do this) for t=dt:dt:tfinal % take time steps of dt d=gamma*[fa;zeros(n-3,1);fb];% adjust for known boundary values T=Aleft\(Aright*T+d); % solve for T using backslash divide if (mod(t,dt*100)==0) % plot each 100th step Tplot=[fa;T;fb]; % add boundary values for plotting plot(x,Tplot); end end print -depsc cranknic hold off

138

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Index

7.4 Method of lines The method of lines is a way of numerically solving PDEs using the efficient techniques (eg Runge-Kutta) developed to solve ODEs. Consider the 1D diffusion equation

∂T ∂ 2T =D 2 ∂t ∂x with Dirichlet boundary conditions at the boundaries x = a and x = b and initial condition given by T (a,t) = fa (t) T (b,t) = fb (t) T (x, 0) = f0 (x) discretise this in the x-direction using the variable i to indicate which x point you are at. That is, divide the x-direction into N subintervals with i = 0, 1, 2, . . ., N. Use centred space approximation to the second derivative at each of those points but leave the time derivative as it is. So we get

∂ Ti Ti−1 − 2Ti + Ti+1 ≈D ∂t (∆x)2 where Ti is the temperature at the ith space position and ∆x = (b − a)/N. Now T0 and TN are known because of the boundary condition. T0 = fa (t)

TN = fb (t)

For i = 1, 2, 3, . . ., N −1 we now have a system of (N −1) ODEs in the variables T1 , T2 , T3 , . . . , TN−1 . For example dT1 = β (T0 − 2T1 + T2 ) = β ( fa − 2T1 + T2 ) dt dT2 = β (T1 − 2T2 + T3 ) dt dT3 = β (T2 − 2T3 + T4 ) dt ... = ... dTN−2 = β (TN−3 − 2TN−2 + TN−1 ) dt dTN−1 = β (TN−2 − 2TN−1 + TN ) = β (TN−2 − 2TN−1 + fb ) dt where β = D/(∆x)2 . With the initial condition giving us the initial values for each of T1 , T2 , T3 , . . ., TN−1 . This system of ODEs is easily solved numerically using any of the ODE solvers mentioned previously (eg Euler’s, midpoint, Runge-Kutta, ode45,. . . ). The more points used across the domain (larger the value of N) the more accurate the solution will be and the longer it will take to run.

Index

METHOD OF LINES

139

MATLAB code % % % % % % % %

mol.m method of lines on diffusion equation bar length 2m, with T(0,t)=100, T(2,t)=50 initially quadratic temperature profile T(x,0)=100-25x(x-1) diffusion coefficient of D=1x10ˆ(-6) m2/s integrated from t=0 to 1,000,000sec in steps of 100,000sec

global n Ta Tb beta n=50; xa=0; xb=2; dx=(xb-xa)/n Ta=100; Tb=50; timeend=1e6; D=1e-6 beta=D/(dx.*dx) numsteps=10 timestep=timeend/numsteps; % setup the time points solution is wanted at tspan=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 it y0=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 values for i=2:numsteps v=[Ta y(i,:) Tb]; plot(w,v,’b-’) end print -depsc mol hold off

140

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

MATLAB code function f=molf(t,y); % % molf.m % the system of equations global n Ta Tb beta f(1)=beta*(Ta-2*y(1)+y(2)); for i=2:n-2 f(i)=beta*(y(i-1)-2*y(i)+y(i+1)); end; f(n-1)=beta*(y(n-2)-2*y(n-1)+Tb); f=f(:); return

Heat diffusion in a steel bar with D = 1 × 10−6 m2 /s

∂T ∂ 2T =D 2 ∂t ∂x with T (0,t) = 100

T (2,t) = 50

T (x, 0) = 100 − 25x(x − 1)

Heat Diffusion using Method of Lines 110

100

Temperature

90

80

70

60

50 0

0.5

1 x

1.5

2

Index

OTHER BOUNDARY CONDITIONS

141

7.5 Other boundary conditions So far all the examples have used Dirichlet Boundary Conditions (the value of the function given on the boundary). What changes if instead the boundary conditions are Neumann or Cauchy type? Now the value of the function is not known and hence the boundary should also be considered an unknown point. Example Left edge has a Dirichlet (u = 0) boundary condition and hence the value is known. Right edge has a Cauchy ( ∂∂ ur = h(25 − u)) boundary condition and hence the value is unknown.

t u

bc

bc

bc

bc

bc

bc

bc

bc

u=0 u t boundary value t u known

bc

bc

bc

bc

bc

bc

bc

∂u bc ∂ r = h(25 − u)

bc

bc

bc

bc

bc

bc

bc

boundary bcvalue unknown

t u

bc

bc

bc

bc

bc

bc

bc

bc

rs

rs

rs

rs

rs

rs

rs

rs

rs

sr bc t u

known initial condition unknown points known boundary points

Example The different types of boundary conditions can all be derived from Newton’s Law of cooling. For example consider a steel bar where the end at x = 0 loses heat according to Newton’s Law of cooling with a heat transfer coefficient of h, and an ambient temperature of Ta , that is at x = 0

∂T = h(T − Ta ) ∂x If h = 0 then the boundary is fully insulated as no heat can flow through that boundary

∂T = 0 insulated ∂x If h is very large then divide each side by h to give approximately T = Ta

Dirichlet condition

Each of these three boundary conditions need to be treated slightly differently.

142

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.5.1 Neumann Boundary Condition The function value is not known at the boundary so the boundary is an unknown value that must also be determined. After discretizing in the x-direction use centred differencing on the derivative term, for example centred differencing about i = 0 gives ∂ T T1 − T−1 ≈ ∂ x i=0 2∆x

where T−1 is a fictitious point. That is it is an artificial point that is not in the domain of interest. So how does this help? Consider the case of an insulated boundary

∂T ∂x

= 0.

Using the above expression and the fact that the boundary is insulated so that get a relationship between T−1 and T1 in this case this is simply that

∂T ∂x

= 0 we

T−1 = T1 But recall that we are dealing with the Diffusion Equation

∂T ∂ 2T =D 2 ∂t ∂x using centred differencing on the second derivative around the point i = 0 results in ∂ 2 T T−1 − 2T0 + T1 ≈ 2 ∂ x i=0 (∆x)2 the fictitious point T−1 can then be replaced by T1 to give 2T1 − 2T0 ∂ 2 T ≈ ∂ x2 i=0 (∆x)2

Hence it is possible to incorporate the boundary condition into the scheme that is used with the value of the temperature at the boundary being one of the unknowns. For example in the Method of Lines the system of equations is modified to include an extra equation for the T0 point and the T1 equation is also altered to still include the T0 which is unknown, the other equations remain unchanged. dT0 = β (T−1 − 2T0 + T1 ) = β (2T1 − 2T0 ) dt dT1 = β (T0 − 2T1 + T2 ) dt dT2 = β (T1 − 2T2 + T3 ) dt ... = ... dTN−2 = β (TN−3 − 2TN−2 + TN−1 ) dt dTN−1 = β (TN−2 − 2TN−1 + TN ) = β (TN−2 − 2TN−1 + fb ) dt

Index

OTHER BOUNDARY CONDITIONS

143

Example Heat diffusion in a steel bar with D = 1 × 10−6 m2 /s

∂T ∂ 2T =D 2 ∂t ∂x

with

∂T (0,t) = 0 ∂x MATLAB code % % % % % % % %

T (2,t) = 50

T (x, 0) = 100 − 25x(x − 1)

molinsulated.m method of lines on diffusion equation bar length 2m, with dT/dx(0,t)=0, T(2,t)=50 initially a quadratic temperature profile T(x,0)=100-25x(x-1) diffusion coefficient of D=1x10ˆ(-6) m2/s integrated from t=0 to 1,000,000 sec in steps of 100,000 sec

global n Tb beta n=50; xa=0; timeend=1e6; numsteps=10;

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

% setup the time points solutionis wanted at tspan=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 it y0=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 value for i=2:numsteps v=[y(i,:) Tb]; plot(w,v,’b-’) end print -depsc molinsulated hold off

144

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

MATLAB code function f=molinsulatedf(t,y); % % molinsulatedf.m % the system of equations global 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

Heat Diffusion using Method of Lines: Insulated left edge 110

100

Temperature

90

80

70

60

50 0

0.5

1 x

1.5

2

Index

OTHER BOUNDARY CONDITIONS

145

7.5.2 Cauchy Boundary Condition As an example for the full Newtonian heat loss boundary condition

∂T = h(T − Ta ) ∂x the derivative of the function is given in terms of the unknown value at the boundary that must also be determined. After discretizing in the x-direction use centered differencing on the derivative term, for example centred differencing about i = 0 gives ∂ T T1 − T−1 ≈ = h(T0 − Ta ) ∂ x i=0 2∆x where T−1 is a fictitious point and Ta is the ambient temperature.

This can be rearranged to get the fictitious point T−1 in terms of the known points T0 and T1 . T−1 = T1 − 2∆xh(T0 − Ta ) Now use this to expression to incorporate the Cauchy boundary condition into the expression for the second derivative at the point i = 0.

146

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

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 diffuses but also causes a feedback so that a higher temperature results in a faster increase in temperature. This type of scenario is typical of a combustion problem where as a fuel burns it raises the temperature thereby making the reaction happen even faster. Assume that the reaction is occurring in a region of length 2m and is such that the ends are held at some ambient temperature Ta and initially the temperature is ambient everywhere except a small hot spot in the centre. This could be used to model scenarios such as a small piece of hot metal falling into a layer of sawdust for example.

∂T ∂ 2T = D 2 + (T − Ta )2 ∂t ∂x where for sawdust D = 0.08 m2 /s. The boundary conditions are ambient temperature T (0,t) = Ta = 25 T (2,t) = 25 and an initial condition of ambient temperature everywhere except at a hotspot in the centre 50 if x = 1 the hot spot T (x, 0) = 25 elsewhere Not that this equation can not be solved analytically! Use FTCS finite differences to get Ti, j+1 = γ (Ti+1, j + Ti−1, j ) + (1 − 2γ )Ti, j + ∆t(Ti, j − Ta )2

Temperature at steps of 0.1 seconds: Final Temperature 1.5 seconds 90 80

Temperature

70 60 50 40 30 20 0

0.5

1 x

1.5

2

Index

NONLINEAR EXAMPLE

147

MATLAB code % reactdiff.m % Solve the heat reaction-diffusion equation on a length 2 with % both ends at ambient=25oC and initially the bar is ambient. % A hot spot of T=50oC at the centre (x=1) % Use FTCS finite difference method % % dT/dt = D d2T/dx2 + (T-Ta)ˆ2 % subject to boundary conditions T(0,t)=Ta T(L,t)=Ta % and initial condition T(x,0)=Ta (for x=/=1) and T(1,0)=50 % clear all; n=100; L=2; dx=L/n; x=[0:dx:L]; % x points D=0.08; % diffusivity of wood = 0.08 mˆ2/s dt=.001; % time step in seconds tfinal=1.48; % final time in seconds gamma=D*dt/(dxˆ2) Ta=25; % ambient Temperature T=Ta*ones(1,n+1); % ambient Temperature everywhere except hot spot T(n/2+1)=Ta+25; % hot spot at centre % plot initial temperature distribution plot(x,T); xlabel(’x’); ylabel(’Temperature’); title(’ Temperature at steps of 0.1 sec: Final Temperature 1.5 sec’) hold on for t=dt:dt:tfinal % take time steps of dt %step across the bar calculating the Temperature at next time level for i=2:n Tnext(i)=gamma*(T(i-1)+T(i+1))+(1-2*gamma)*T(i)+dt*(T(i)-Ta)ˆ2; end Tnext(1)=Ta; Tnext(n+1)=Ta; T=Tnext; % update the temperature if (mod(t,dt*100)==0) % plot each 100th step plot(x,T); end end plot(x,T); print -depsc reactdiff hold off

148

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.7 Other co-ordinate systems So far all the examples given have been in Cartesian co-ordinates (x, y, z). These equations can also be considered in other co-ordinate systems such as cylindrical (r, θ , z) or spherical (ρ , φ , θ ). For example the Laplacian in cylindrical co-ordinates is ∇2 F =

∂ 2F 1 ∂ F 1 ∂ 2F ∂ 2F + + + ∂ r 2 r ∂ r r 2 ∂ θ 2 ∂ z2

The benefit of using a coordinate system that matches the object is that often it is possible to reduce the dimension of the problem.

7.7.1 Disc example Consider the case of a circular disc of metal with a radius of 50cm. The base and top are insulated which means the only directions of interest are in the plane of the disc ((x, y) plane) and hence the problem is two dimensional. The disc is initially at 100◦C and loses heat through the edge according to Newton’s law of cooling. The ambient air is at 25◦C. The diffusivity of the metal is D = 5 × 10−6m2 /s and the Newtonian heat transfer coeeficient is h = 10m−1 .

y ∂u = −h(u − ua ) at r = 0.5 ∂r

x

The governing differential equation is the 2-D heat equation

∂u = D∇2 u ∂t which can be written in circular coordinates as 2 ∂u ∂ F 1 ∂ F 1 ∂ 2F =D + + ∂t ∂ r2 r ∂ r r2 ∂ θ 2 Now since the boundary condition is applied uniformly over the boundary and the initial condition is also uniform then the temperature in the disc will not depend on the angle

Index

OTHER CO-ORDINATE SYSTEMS

149

around the disc. Hence there will not be any θ dependence and so any derivatives with respect to θ are zero. So the governing PDE becomes 2 ∂u ∂ u 1 ∂u =D + ∂t ∂ r2 r ∂ 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 space variable r. One boundary condition is obvious, namely

∂u = −h(u − ua ) at r = 0.5 ∂r The other boundary condition is derived from the fact that the temperature is independent of θ and hence at the origin the derivative with respect to r must be zero. That is the temperature distribution must be centred on the origin. So the other boundary condition is ∂u = 0 at r = 0 ∂r Look at the governing PDE. There is a problem at r = 0 as the term 1r ∂∂ ur appears to be undefined at r = 0. In fact this term is not undefined since at r = 0 ∂∂ ur = 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 states in this particular situation that if

lim f (r) = 0 and

r→0

hence in this case f (r) = by

∂u ∂r

lim g(r) = 0

r→0

f ′ (r) f (r) = lim ′ r→0 g (r) r→0 g(r)

then

lim

and g(r) = r so that at r = 0 the governing PDE is modified

∂u ∂ 2 u ∂∂ ur = D + ∂t ∂ r2 r

!

∂ 2u

∂ 2 u ∂ r2 = D + ∂ r2 1 = 2D

!

∂ 2u ∂ r2

So in any numerical method this alternative PDE must be used if at the point r = 0.

150

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.7.3 FTCS Method Now derive the FTCS system for solving this problem. The only space dimension of interest is the radius r = 0 to 0.5. Set up a grid in space and time. Discretise the region so j that ui = u(i∆r, j∆t) with i = 0, 1, . . ., Nr and j = 0, 1, . . ., Nt with ∆r = 0.5/Nr

t

∂u ∂r

j = 10

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=9

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=8

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=7

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=6

bc

bc

bc

bc

bc

bc

bc

bc

bc

=0

t =0

j=5

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=4

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

∆r

bc

∆t

j=3

bc

bc

bc

bc

j=2

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=1

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

rs

rs

rs

rs

rs

rs

rs

rs

i=0

i=1

i=2

i=3

i=4

i=5

i=6

i=7

i=8

r=0 sr bc

∂u ∂r

Initial condition – known u(r, 0) = 100 unknown points

= h(25 − u)

r

r = 0.5

Index

OTHER CO-ORDINATE SYSTEMS

151

FTCS formulation Provided r 6= 0 the governing PDE is 2 ∂u ∂ u 1 ∂u =D + ∂t ∂ r2 r ∂ r so use Forward Time and Centred Space finite difference around the point (i, j) to approximate this by ! j j j j j j+1 j ui+1 − 2ui + ui−1 ui − ui 1 ui+1 − ui−1 =D + ∆t (∆r)2 i∆r 2∆r Rearranging this to get the new time level as the subject results in γ j j j j j j j+1 ui+1 − ui−1 + ui ui = γ ui+1 − 2ui + ui−1 + 2i

D∆t where γ = (∆r) 2 . This is the FTCS formulation for the internal points. Points at the two edges (r = 0 and r = 0.5) require special attention.

External edge r = 0.5 Use a fictitious point beyond the region of interest. The boundary condition is ∂u ∂ r = −h(u − ua ) which on using central finite differences is j j uNr +1 − uNr −1 ∂ u j j ≈ = −h(uNr − ua ) ∂ r Nr 2∆r

which rearranges so that the fictitious point uNj r +1 can be written as j

j

j

uNr +1 = −2∆rh(uNr − ua ) + uNr −1 j

This value is then used in the FTCS scheme above whenever uNr +1 is used.

152

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Index

Internal edge r = 0 Do a similar process for the internal edge but recall that the PDE is altered there. The boundary condition is ∂∂ ur = 0 which on using central finite differences is j j ∂ u j u1 − u−1 =0 ≈ ∂ r 0 2∆r j

which rearranges so that the fictitious point u−1 can be written as j

j

u−1 = u1 The PDE at r = 0 (which is i = 0) was found previously using L’Hopital’s Rule to be

∂u ∂ 2u = 2D 2 ∂t ∂r so use Forward Time and Centred Space finite difference around the point (0, j) to approximate this by ! j j j j+1 j u1 − 2u0 + u−1 u0 − u0 = 2D ∆t (∆r)2 j

Using the value for the fictitious point (u−1 ) above and rearranging gives the governing FTCS equation for the r = 0 point as j+1 j j j u0 = 4γ u1 − u0 + u0 It is now possible to code this FTCS method in MATLAB.

Index

OTHER CO-ORDINATE SYSTEMS

153

MATLAB code % ftcs_cyl.m % Solve the heat diffusion equation on a steel disc radius 0.5m % with Newton’s law of cooling dT/dr=-h(T-Ta) Ta=25oC at r=0.5m % initially the disc is 100oC. Use FTCS finite difference method % dT/dt = D d2T/dr2 + D 1/r dT/dr % initial condition T(x,0)=100 % boundary conditions dT/dr=0 at r=0 % dT/dr=-h(T-25) at r=0.5m % clear all; n=20; % number of points L=0.5; % radius of disc dr=L/n; % space step Ta=25; % ambient temperature r=[0:dr:L]; % r points D=5e-6; % steel diffusivity = 5x10ˆ6 mˆ2/s h=10.0; % heat loss coefficient dt=30 % time step in seconds tfinal=30000; % final time in seconds gamma=D*dt/(drˆ2) % calculate and plot initial temperature distribution T=ones(1,n+1)*100; plot(r,T,’r-’); xlabel(’radius’); ylabel(’Temperature’); title(’ Temperature in disc at 900 second intervals’) hold on % loop through time for 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 one % r=0 special case uses a fictitious point beyond region Tnext(1)=4*gamma*(T(2)-T(1))+T(1); % internal r points for i=2:n Tnext(i)=gamma*(T(i-1)-2*T(i)+T(i+1))... +gamma/(2*(i-1))*(T(i+1)-T(i-1))+T(i); end % external r point uses a fictitious point beyond region Tfict=T(n)-2*dr*h*(T(n+1)-Ta); % calculate ficticous point Tnext(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 solution T=Tnext; if (mod(t,dt*30)==0) plot(r,T); end end % t loop hold off

154

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Temperature in disc at 900 second intervals 100 90

Temperature

80 70 60 50 40 30 20 0

0.1

0.2

0.3 radius

0.4

0.5

8 Elliptic Equations: Laplace’s Equation

Elliptic equations (such as Laplace’s Equation) do not allow ’time-stepping’ since there is no ’time-like’ variable to step in. Instead the solution must somehow be found in the entire demand simultaneously. The best method for this is to use some kind of iterative scheme where you use the equation to update an initial guess of the solution until it converges to a steady solution. This is a sort of pseudo ’time-stepping’ but it is not necessary to keep the intermediate ’times’ hence some short cuts and speed ups can be done.

8.1 Example with Dirichlet boundary conditions Consider a steady state heat problem on a rectangular domain with Dirichlet Boundary conditions. For example a slab of wood of size 10cm (wide) by 5cm (long) that has reached thermal equilibrium. The left and right edges are held at 20◦ C the bottom at 20◦ C and the top is subjected to to a quadratic temperature such that the maximum in the middle is 70◦ C. What is the steady state temperature distribution in the slab?

y 5

(2x(10 − x) + 20)◦C

20◦C

20◦C

20◦C

10

x

The governing PDE is Laplace’s Equation in 2D

∂ 2T ∂ 2T + 2 =0 ∂ x2 ∂y subject to the boundary conditions T (0, y) = 20

T (10, y) = 20

T (x, 0) = 20

T (x, 5) = 2x(10 − x) + 20

156

Index

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

Divide the region up into a grid so that there Nx sections in the x-direction and Ny sections in the y-direction. Let i be the index for the x-direction and j the index for the y-direction. So in this case (10 − 0) (5 − 0) ∆x = ∆y = Nx Ny with i = 0, 1, . . ., Nx and j = 0, 1, . . ., Ny . Let Ti, j be shorthand notation for the temperature at the position (xi , y j ) where xi = 0 + i∆x and y j = 0 + j∆y. Turning to the governing PDE discretise the second derivative terms using centred differencing in the appropriate direction to get Ti+1, j − 2Ti, j + Ti−1, j Ti, j+1 − 2Ti, j + Ti, j−1 + ≈0 (∆x)2 (∆y)2 That is the template used is a cross pattern

bc

bc

bc

bc

bc

j+1

bc

bc

cb

bc

bc

j

bc

bc

cb

cb

bc

bc

bc

cb

bc

bc

bc

bc

bc

bc

bc

i−1

i

i+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 4 neighbours as Ti, j =

1/2 2 2 (∆y) T + T + (∆x) T + T i+1, j i−1, j i, j+1 i, j−1 (∆y)2 + (∆x)2

But how does this help? Use this equation as the basis for an iterative scheme. Start with some initial guess for the temperature in the slab (that is an initial guess for all the Ti, j ), recalling that the temperature on the boundary is known. Now use the above equation to update the central point from the values of its 4 neighbours. Sweep across the x and y directions repeatedly until the solution converges. So the equation is Ti,n+1 j =

1/2 (∆y)2 + (∆x)2

n n 2 n n (∆y)2 Ti+1, j + Ti−1, j + (∆x) Ti, j+1 + Ti, j−1

where n is a counter that keeps track of which iteration you are up to. n = 0 corresponds to your original guess. You stop when the change in the solution from one iteration to the next is less than some tolerance.

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 20 V(:,Ny+1)=2*x’.*(10-x’)+20; % boundary condition on y=5 Vnext=V; number_iter=0; denom=2*(dx2+dy2); % this is used often so calculate it outside loop % loop through until solution converges while delta > eps number_iter=number_iter+1; maxdiff=0; for i=2:Nx for j=2:Ny Vnext(i,j)=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(V(i,j-1)+V(i,j+1)))/den dV=V(i,j)-Vnext(i,j); % the difference between successive solut % keep track of the maximum difference if abs(dV) > maxdiff; maxdiff = abs(dV); end end end V=Vnext; delta=maxdiff; end number_iter % show how many iterations were used % plot the solution in 3D in 3 ways, surface plot, mesh plot, contour figure(1) surf(x,y,V’) % surf plots the surface colorbar % put a color bar next to it showing what each color shading interp % use interpolation for the coloring (smooths the col xlabel(’x’); ylabel(’y’); zlabel(’Temperature’); title(’Steady State Temperature’); print -depsc laplace_direct_3D figure(2) mesh(x,y,V’) % mesh plots the surface with the mesh stretched over colorbar % put a color bar next to it showing what each color xlabel(’x’); ylabel(’y’); zlabel(’Temperature’); title(’Mesh plot of temperature’) print -depsc laplace_direct_mesh figure(3) contour(x,y,V’) % cont plots a contour plot axis image xlabel(’x’); ylabel(’y’); title(’Contour plot of temperature’) print -depsc laplace_direct_contour

Steady State EQUATION Temperature 8. ELLIPTIC EQUATIONS: LAPLACE’S

158

Index 70

Temperature

65 70

60

60

55

50

50

40

45 40

30

35

20 6

30 10

4

25

5

2

20

0 0

y

x

Mesh plot of temperature 70

Temperature

65 70

60

60

55

50

50

40

45 40

30

35

20 6

30 10

4

25

5

2

20

0 0

y

x

y

Contour plot of temperature 5 4.5 1

2

3

4

5 x

6

7

8

9

Index

IMPROVING THE CONVERGENCE

159

8.2 Improving the convergence 8.3 Gauss-Seidel The previous method where the new values are calculated over the entire domain using the existing values is known as the Jacobi method. This requires storing 2 matrices of values (the old and the new) and is relatively inefficient. The speed of the iterative scheme convergence can be substantially improved if instead of sweeping across using the old values we make sure we use the most recently calculated values. This has the added advantage of not requiring the storing of two matrices. for i=2:Nx for j=2:Ny Vnext=(dyˆ2*(V(i-1,j)+V(i+1,j))+dxˆ2*(V(i,j-1)+V(i,j+1)))/denom; dV=V(i,j)-Vnext; V(i,j)=V(i,j)+dV; if abs(dV) > maxdiff; maxdiff = abs(dV); end end end This is known as the Gauss-Seidel method. Because the most recent value of the solution is used it greatly improves the efficiency of the method. Typically the Gauss-Seidel method uses approximately 25% to 50% less iterations for the same level of accuracy. Can you see any problems with this method?

How could you improve on it?

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 incremental change to the solution for i=2:Nx for j=2:Ny Vnext=(dyˆ2*(V(i-1,j)+V(i+1,j))+dxˆ2*(V(i,j-1)+V(i,j+1)))/denom; dV=V(i,j)-Vnext; >>> V(i,j)=V(i,j)+omega*dV; <<< if abs(dV) > maxdiff; maxdiff = abs(dV); end end end Introduce a new parameter ω which is the ’over-relaxation’ amount. This method is known as Successive 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.

Index

GAUSS-SEIDEL

161

The optimum value of ω (the one resulting in the fastest convergence) is not a specific value but is different for each problem. So how do you know what value to use? The best method is to find the approximate value by considering the problem with fewer grid points and then using that optimal value for the full problem with many more grid points. A typical behaviour of the number of iterations versus the relaxation parameter is for the number of iterations to slowly decrease as ω increases to the optimal value and then to rapidly rise (see figure below). Hence it is usually best to take a value of ω that is slightly less than the optimal value to ensure that you stay in the region where the best convergence is obtained. Note that for this example (solving Laplaces equation as before) the number of iterations decreased from about 115 for ω = 1 (no relaxation) to about 34 for ω ≈ 1.83. This results in the program taking approximately a third of the time to run for no more effort. Successive Over-relaxation is particularly important for very large complicated problems in many dimensions where run time and convergence issues become critical. Most commercial software will involve some form of Successive Over-relaxation when solving steady state problems such as Laplace’s equation.

Iterations verses omega for SOR 120 110

number of iterations

100 90 80 70 60 50 40 30 1

Optimal value of omega

1.2

1.4

1.6 omega

1.8

2

162

Index

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

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 thermal equilibrium. The outside temperature is −5◦C the inside temperature 22◦C. The wall foundations are in the ground at 15◦C. The top of the wall is insulated by insulation in the ceiling of the house. The governing PDE is Laplace’s Equation in 2D

∂ 2T ∂ 2T + 2 =0 ∂ x2 ∂y subject to the boundary conditions

∂T = h(T − 22) on ∂x

x=0

∂T = −h(T + 5) on ∂x

x = 0.25

T (x, 0) = 15

∂T = 0 on ∂y

y=2

Contours of Steady State Temperature 2

20

1.8

18

1.6

16

1.4

14

1.2

12 10

1

8

0.8

6

0.6

4

0.4

2 0.2 0 0 0

0.05

0.1

0.15

0.2

0.25

Index

GAUSS-SEIDEL

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 needed insidetemp=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); % V(1,:)=insidetemp*ones(1,Ny); % V(Nx,:)=outsidetemp*ones(1,Ny);% number_iter=0; denom=2*(dxˆ2+dyˆ2); %

initialise solution everywhere to bottomtemp initialise inside edge to insidetemp initialise outside edge to outsidetemp this is used often so calculate it outside loop

% loop through until solution converges while 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)=bottomtemp V(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 end j=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-1 for i=2:Nx-1 j=1; % the bottom j=1 V(i,j)=groundtemp V(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 end j=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); end end 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)=bottomtemp V(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 end j=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’,number_iter,maxdiff) end % while loop finishes when convergence is obtained to accuracy eps contlevels=[outsidetemp:1:max(max(V))]; contour(x,y,V’,contlevels) % plot contours of the solution at contlevels title(’Contours of Steady State Temperature’); colorbar print -depsc wall_contour

163

164

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

Index

8.3.3 Example A cylinder of height 2m and radius 1m has the bottom and sides held at 0◦C and the top satisfies a quadratic temperature distribution with maximum 1◦C at the centre and 0◦C at the edge. Write down the governing equation, boundary conditions and find the steady state temperature distribution using MATLAB.

Index

GAUSS-SEIDEL

165

MATLAB code % laplace_cylinder.m % Solve Laplaces equation d2u/dz2 + 1/r du/dr + d2u/dr2 = 0 % subject to du/dr(0,z)=0 u(r,0)=0 u(r,2)=1-rˆ2 u(1,z)=0 % L=1; M=2; Nz=30; Nr=20; dr=L/Nr; dz=M/Nz; eps=0.001; delta=1; r=0:dr:L; z=0:dz:M; V=zeros(Nr+1,Nz+1); % initialise solution everywhere to zero V(:,Nz+1)=(1-r.ˆ2)’; % boundary condition on z=2 (j=Nz+1) number_iter=0; denom=2*(drˆ2+dzˆ2); % used often so calculate outside loop % loop through until solution converges while 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 end end delta=maxdiff; end number_iter % show how many iterations were used figure(1) mesh(r,z,V’) % plot the solution in 3D title(’Steady State Temperature’); print -depsc laplace_cylinder_3D figure(2) contour(r,z,V’) % plot contours of the solution title(’Contours of Steady State Temperature’); print -depsc laplace_cylinder_contours

166

Index

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

Steady State Temperature

1

0.8

0.6

0.4

0.2

0 2 1

1.5 0.8

1

0.6 0.4

0.5 0.2 0

0

Contours of Steady State Temperature 2

1.8

1.6

1.4

1.2

1

0.8

0.6

0.4

0.2

0

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

9 Hyperbolic Equations: Wave Equation

9.1 Finite Differences The wave equation

∂ 2F = c2 ∇2 F 2 ∂t allows time stepping as was used for the diffusion equation but with a significant difference at the beginning of the process. Since the time derivative is 2nd order there are 2 initial conditions. For example for a one dimensional problem they could be ∂F (x, 0) = V0 (x) ∂t

F(x, 0) = F0 (x)

Divide the region up into a grid so that there are Nx sections in the x-direction and Nt sections in the t-direction. Let i be the index for the x-direction and j the index for the tj direction. So in this case Let Fi 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 wave equation gives j j j j+1 j j−1 Fi − 2Fi + Fi 2 Fi+1 − 2Fi + Fi−1 =c (∆t)2 (∆x)2 This can be rearranged so that the solution at the new time ( j + 1) can be written in terms of the current time ( j) and the previous time ( j − 1) as j+1 j j−1 j j j Fi = γ 2 Fi+1 − 2Fi + Fi−1 + 2Fi − Fi where

γ2 =

c2 (∆t)2 (∆x)2

That is the template used is a cross pattern

bc

bc

bc

bc

bc

j+1

bc

bc

rsbc

bc

bc

j

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

i−1

i

i+1

j−1

The significant difference between this scheme and the FTCS scheme for the diffusion equation is that here the new value depends on the previous two time steps. The problem

168

Index

9. HYPERBOLIC EQUATIONS: WAVE EQUATION

with this is at the beginning of the process as initially only values at one time level (the initial time) are known. To overcome this the easiest method is to make a forward time approximation of the initial derivative boundary condition to get values at the next time level. If the initial derivative boundary condition at the ith space point is ∂ F = V0 i ∂ t i

then approximate this by a forward time difference to get Fi1 − Fi0 ∂ F = V0 i = ∂ t i ∆t Rearrange to get values at time level j = 1 as

Fi1 = Fi0 +V0 i ∆t Now the first two time levels are known and the method can proceed.

9.1.1 Stability As with the FTCS method for the diffusion equation this explicit method also suffers from stability problems. If γ 2 < 1 the method is stable. If γ 2 > 1 the method is unstable and not useable.

9.2 Example Consider a string 1 metre long that is fixed at the ends. Initial the string is pulled to a parabolic shape between x = 25 and 33cm and then released. The governing differential equation is the one dimensional wave equation 2 ∂ 2F 2∂ F = c ∂ t2 ∂ x2

subject to the boundary conditions F(0,t) = 0

F(100,t) = 0

and initial conditions F(x, 0) = given parabolic shape

∂F (x, 0) = 0 ∂t

Index

EXAMPLE

169

MATLAB code % waveqn.m Finite difference solution to the wave equation g2 = .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 steps for i = 2:1:400 % loop through time step t = i*dt; Y2 = A*Y1-Y0; % calculate solution at next time Y0 = Y1; Y1 = Y2; % update solutions end % plot solution plot([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

t=0

1

t = 0.10328

1

y

y

y

−1

−1

−1

1

t = 0.30984

1

t = 0.41312

1

y

y

y

−1

−1

−1

1

t = 0.61968

1

t = 0.72296

1

y

y

y

−1

−1

−1

1

t = 0.92952

1

t = 1.0328

1

y

y

y

−1

−1

−1

1

t = 1.2394

1

t = 1.3426

1

y

y

y

−1

−1

−1

t = 0.20656

t = 0.5164

t = 0.82624

t = 1.1361

t = 1.4459

170

9. HYPERBOLIC EQUATIONS: WAVE EQUATION

Index

You will notice that the initial pulse broke into two waves each moving in opposite directions. This is typical behaviour for solutions of the wave equation. You will also notice form the time snapshots that the wave reflects from the rigid (fixed) ends. A closer look at a future time plot reveals small wiggles developing in the solution. This is numerical error beginning to show up. This sort of error is typical of what is found with numerical solutions of hyperbolic equations.

1

y

−1

10 Concluding Remarks

10.1 Irregular Boundaries All the examples used for PDEs have involved regular boundaries that are easily described within the coordinate system in use (cartesian, cylindrical etc). These kinds of problems involving regular boundaries are ideally suited to finite difference methods as it is relatively simple to set up a grid that accurately represents the shape of interest. Shapes with irregular boundaries are better dealt with using Finite Element Methods rather than Finite Difference Methods. This is because for the finite element method mesh points can be arbitrarily placed on the boundary and the mesh is ’triangularated’ thus allowing for complicated shapes to be easily covered. Contrast this to the finite difference method where typically you use a regular grid which fitting complicated boundaries to is often difficult. One difficulty with the finite element method is then choosing how to distribute the mesh points. This needs some knowledge of how you expect the solution to behave.

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 others are more multipurpose. MATLAB PDE Toolbox: solves fairly general Parabolic, Hyperbolic, and Elliptic equations using finite element methods. Graphical interface for inputting equations and boundary conditions. FASTFLO: Solves almost any PDE using finite element methods. Relatively difficult to use but very powerful. FLUENT: Solves many general PDEs using finite element methods. Primarily used for heat and mass flow problems. FlexPDE: Solves many general PDEs using finite element methods. Best at 2D, the demo version is available free (for Windows and Linux) at http://www.pdesolutions.com. It only takes a few minutes to download and has numerous very good examples. Easy to use. There are numerous others as well. Most commercial software uses finite elements rather than finite differences specifically so that more complicated regions can be analysed.

172

10. CONCLUDING REMARKS

Index

10.3 Finally This has only been a very brief look at numerical solutions of ODEs and PDEs. The complexities and subtleties go way beyond what we have presented here. Hopefully you have now an appreciation of at least some of the ideas of numerical solutions and why it is important to be able to calculate at least some analytic solutions.

Index analytic solutions, 119 boundary conditions, 117, 141 Cauchy, 117, 141, 145 Dirichlet, 117, 141 mixed, 117, 141, 145 Neumann, 117, 141, 142 BVP, 79 relaxation, 84 shooting, 80, 82 Cauchy boundary condition, 117, 141, 145 centre, 68 competition model, 70 convergence, 159 convolution theorem, 27 Crank-Nicolson method, 134 critical points, 68, 70, 74 cylindrical coordinates, 115, 148 delta function, 25 diag, 88 diagonal matrix, 87, 92, 135 diary, 7 differential eigenvalue equation, 101 diffusion equation, 111, 112, 125, 138 Dirac delta function, 25 Dirichlet boundary condition, 117, 138, 141, 155 discretisation, 126, 156, 167 disease, 73 drum, 123 eigenvalue, 7, 75 electrical potential, 113 elliptic PDE, 124, 155 errors, 44 Euler’s method, 43 example, 45 explicit, 135 Fastflo, 171 finite differences, 84, 125, 156, 167, 171 finite element, 93 finite elements, 171 FlexPDE, 171 Fluent, 171 FTCS, 126, 150, 151 ftcs, 167 example, 130

template, 129 functions, 9 passing names, 11 vector input, 10 fzero, 13, 102 Gauss-Seidel, 92, 159 ground water, 113 heat conduction, 112 Heaviside function, 23 help, 8 hyperbolic PDE, 124, 167 implicit, 135 initial conditions, 116 irregular boundaries, 171 iterative scheme, 156 IVP, 41 intoduction, 41 numerical, 43 system, 41, 101 Jacobi method, 159 jacobian, 70, 75 L’Hopitals rule, 149 Laplace transform, 23 convolution theorem, 27 definition, 23 derivative, 26, 27 first shifting theorem, 26 integral, 27 inversion, 29 MATLAB, 33 second shifting theorem, 26 systems, 38 theorems, 27 Laplace’s equation, 111, 113, 155 Laplacian, 113, 148 linear, 110 linear equations, 6 Lokta-Volterra, 68 lookfor, 8 M files, 1 running, 7 web site, 1 MATLAB ilaplace, 36

174

INDEX

laplace, 28 Laplace transform, 33 partial fractions, 33 residue, 33 MATLAB commands, 20 diag, 88 diary, 7 eig, 7 fzero, 13 help, 8 lookfor, 8 ode23, 54 ode45, 14, 56 odeset, 59 ones, 88 quad, 11 quiver, 66 MATLAB PDE toolbox, 171 matrix equations, 92 method of lines, 138, 142 midpoint method, 50, 54

Index plotting, 3 3D, 5 data, 4 Poisson’s equation, 115 predator-prey, 68 projectile, 60 quiver, 66 radar, 114 relaxation method, 84 example, 85 Runge-Kutta, 53, 138 fourth order, 56 second order, 53 running scripts, 7

saddle, 68, 75 seismic, 114 sending output to a file, 7 shooting, 102 example, 82 shooting method, 80, 82 Neumann boundary condition, 117, 141, 142 Simpson’s rule, 11 Newtonian heat loss, 141, 145 SIR disease model, 73 node, 75 software, 171 sonar, 114 ode23, 54 spherical coordinates, 115, 148 ode45 stability, 132, 135, 168 example, 14 step function, 23 options, 59 step size, 46 Runge-Kutta, 56 algorithm, 47 ones, 88 successive over relaxation, 160 order, 44 susceptible, 73 system of equations, 41 parabolic PDE, 124, 125 partial fractions, 29 Taylor series, 43, 53, 54, 79, 149 distinct factors, 29 inversion, 31 vector field, 66 MATLAB, 33 velocity potential, 113 non linear factors, 31 wave equation, 111, 114, 167 repeated factors, 30 web site, i, 1 PDE, 109 classification, 124 definitions, 110 elliptic, 124, 155 examples, 109 hyperbolic, 124, 167 linear, 110 parabolic, 124, 125 separation of variables, 119 phase plane, 63

c

Dr. G.N. Mercer

Index

Preface

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

Using the electronic version of these notes These notes are hyperlinked. All green text is a link to somewhere else within this document. For example the contents page links to the appropriate page in the text, the numbers in the index link to the page reference, the word Index in the header of most pages links to the index and the page numbers in the header on each page link back to the contents page. There are also some internal linked words that take you to the relevant text. Links to external web pages are red in colour. Provided your PDF reader (eg Adobe Acrobat Reader) is set up correctly these links should open the appropriate page in your web browser (eg Microsoft Internet Explorer, Netscape, Mozilla).

MATLAB code The MATLAB codes used in these notes are available at the course webCT site. MATLAB code in the text is usually shown in purple and the resulting output in blue. If you have any difficulties with any part of the course do not hesitate in contacting me to sort them out. Geoff Mercer Room G12 Science South School of Physical, Environmental and Mathematical Sciences Phone: 6268 8734 Email:[email protected]

ii

Index

Contents

1 Introduction to MATLAB 1.1 Access to MATLAB . . . . . . . . . . . . . 1.2 Using MATLAB . . . . . . . . . . . . . . . 1.3 A few simple examples . . . . . . . . . . . . 1.4 Running MATLAB scripts . . . . . . . . . . 1.5 Sending output to a file . . . . . . . . . . . . 1.6 MATLAB help facility and lookfor command 1.7 Functions in MATLAB . . . . . . . . . . . . 1.8 Functions in MATLAB – vector input . . . . 1.9 Passing function names . . . . . . . . . . . . 1.10 MATLAB finding zeros . . . . . . . . . . . . 1.11 An Introduction to solving DEs numerically . 1.12 Symbolic Manipulation . . . . . . . . . . . . 1.12.1 Differentiating using MATLAB . . . 1.12.2 Integrating using MATLAB . . . . . 1.12.3 Solving equations . . . . . . . . . . . 1.13 Some Elementary MATLAB Commands . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

2 Laplace Transforms 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . 2.2 Some standard results in Laplace Transforms . . . 2.2.1 Heaviside function . . . . . . . . . . . . . 2.2.2 Dirac delta function . . . . . . . . . . . . . 2.2.3 First shifting theorem . . . . . . . . . . . . 2.2.4 The second shifting theorem . . . . . . . . 2.2.5 Transforms of derivatives . . . . . . . . . . 2.2.6 Multiplication by t . . . . . . . . . . . . . 2.2.7 Integral theorem . . . . . . . . . . . . . . 2.2.8 Division by t . . . . . . . . . . . . . . . . 2.2.9 Convolution theorem . . . . . . . . . . . . 2.2.10 Taking Laplace transforms using MATLAB 2.3 Inversion of Laplace transforms . . . . . . . . . . 2.3.1 Partial fractions . . . . . . . . . . . . . . . 2.3.2 Application of partial fractions to inversion 2.3.3 MATLAB functions for partial fractions . . 2.3.4 Inversion using MATLAB . . . . . . . . . 2.3.5 Systems of Equations . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

1 1 1 2 7 7 8 9 10 11 13 14 17 17 19 19 20

. . . . . . . . . . . . . . . . . .

23 23 23 23 25 26 26 26 27 27 27 27 28 29 29 31 33 36 38

iv

CONTENTS

Index

3 Initial Value Problems 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . 3.2 Numerical approach . . . . . . . . . . . . . . . . . 3.3 Euler’s method . . . . . . . . . . . . . . . . . . . 3.3.1 Graphically . . . . . . . . . . . . . . . . . 3.3.2 Errors and order of the method . . . . . . . 3.3.3 Adjusting the step size . . . . . . . . . . . 3.4 Improvements to Euler’s method - midpoint method 3.5 Second order Runge-Kutta method . . . . . . . . . 3.6 Fourth order Runge-Kutta method . . . . . . . . . 3.7 ode45 . . . . . . . . . . . . . . . . . . . . . . . . 3.8 Projectile example . . . . . . . . . . . . . . . . . 4 Phase Plane Analysis 4.1 Introduction . . . . . 4.1.1 Quiver plots . 4.2 Predator-Prey model 4.3 Competition model . 4.4 SIR disease model . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

5 Boundary Value Problems 5.1 Introduction . . . . . . . . . . . . . . . . . . . 5.2 Shooting method . . . . . . . . . . . . . . . . 5.2.1 Shooting method using guesses . . . . 5.2.2 Shooting method using fzero . . . . . . 5.3 Relaxation methods . . . . . . . . . . . . . . . 5.3.1 Finite differences . . . . . . . . . . . . 5.3.2 Using finite differenes to solve a BVP . 5.3.3 Comments on solving matrix equations 5.3.4 Finite elements . . . . . . . . . . . . . 5.3.5 Symbolic form of a BVP . . . . . . . . 5.3.6 Finite element theory . . . . . . . . . . 5.3.7 Finite element example . . . . . . . . . 5.3.8 Finite element summary . . . . . . . . 5.4 Differential eigenvalue equations . . . . . . . . 6 Partial Differential Equations (PDEs) 6.1 Overview . . . . . . . . . . . . . . 6.2 Functions of more than one variable 6.2.1 Types of problems . . . . . 6.2.2 Examples of PDEs . . . . . 6.3 Basic definitions . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

. . . . .

. . . . . . . . . . . . . .

. . . . .

. . . . . . . . . . .

41 41 43 43 44 44 46 50 53 56 57 60

. . . . .

63 63 66 68 70 73

. . . . . . . . . . . . . .

79 79 80 80 82 84 84 85 92 93 94 94 97 100 101

. . . . .

109 109 109 109 109 110

Index

6.4 6.5

6.6

6.7

6.8 6.9 6.10

6.11 6.12

6.13 6.14

CONTENTS 6.3.1 Order of a PDE . . . . . . . . . . . . . . . 6.3.2 Linear PDEs . . . . . . . . . . . . . . . . 6.3.3 Why use numerical methods . . . . . . . . Three common PDEs in applications . . . . . . . . The diffusion equation . . . . . . . . . . . . . . . 6.5.1 Heat conduction . . . . . . . . . . . . . . 6.5.2 Mass diffusion . . . . . . . . . . . . . . . 6.5.3 Diffusion equation in 2D and 3D . . . . . . Laplace’s equation . . . . . . . . . . . . . . . . . 6.6.1 Equilibrium solutions . . . . . . . . . . . . 6.6.2 Other applications . . . . . . . . . . . . . The wave equation . . . . . . . . . . . . . . . . . 6.7.1 Sonar, radar, seismic waves . . . . . . . . 6.7.2 2D and 3D waves . . . . . . . . . . . . . . Poisson’s equation . . . . . . . . . . . . . . . . . Other co-ordinate systems . . . . . . . . . . . . . Boundary and initial conditions . . . . . . . . . . . 6.10.1 ICs and BCs . . . . . . . . . . . . . . . . 6.10.2 Number of initial and boundary conditions 6.10.3 A typical initial condition . . . . . . . . . 6.10.4 Typical boundary conditions . . . . . . . . Types of boundary conditions . . . . . . . . . . . . Different solution approaches . . . . . . . . . . . . 6.12.1 Separation of variables . . . . . . . . . . . 6.12.2 Other approaches . . . . . . . . . . . . . . 6.12.3 Numerical techniques . . . . . . . . . . . . 6.12.4 Value of analytic solutions . . . . . . . . . Examples . . . . . . . . . . . . . . . . . . . . . . Classification of PDEs . . . . . . . . . . . . . . .

7 Parabolic Equations: Diffusion Equation 7.1 Finite differences . . . . . . . . . . . . 7.1.1 First Derivative . . . . . . . . . 7.1.2 Second Derivative . . . . . . . 7.2 FTCS method . . . . . . . . . . . . . . 7.2.1 Discretise the region . . . . . . 7.2.2 Template for the FTCS Method 7.2.3 FTCS example . . . . . . . . . 7.2.4 Stability . . . . . . . . . . . . . 7.2.5 Unstable Example . . . . . . . 7.3 Crank-Nicolson method . . . . . . . . . 7.3.1 Template . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . .

v . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

110 110 110 111 112 112 112 112 113 113 113 114 114 114 115 115 116 116 116 116 116 117 119 119 119 119 119 120 124

. . . . . . . . . . .

125 125 125 125 126 126 129 130 132 133 134 134

vi

CONTENTS

7.4 7.5

7.6 7.7

Index

7.3.2 Matrix Representation . . . . 7.3.3 Example . . . . . . . . . . . Method of lines . . . . . . . . . . . . Other boundary conditions . . . . . . 7.5.1 Neumann Boundary Condition 7.5.2 Cauchy Boundary Condition . Nonlinear Example . . . . . . . . . . Other co-ordinate systems . . . . . . 7.7.1 Disc example . . . . . . . . . 7.7.2 L’Hopital’s Rule . . . . . . . 7.7.3 FTCS Method . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

8 Elliptic Equations: Laplace’s Equation 8.1 Example with Dirichlet boundary conditions 8.2 Improving the convergence . . . . . . . . . 8.3 Gauss-Seidel . . . . . . . . . . . . . . . . 8.3.1 Successive Over Relaxation . . . . 8.3.2 Example . . . . . . . . . . . . . . 8.3.3 Example . . . . . . . . . . . . . . 9 Hyperbolic Equations: 9.1 Finite Differences 9.1.1 Stability . 9.2 Example . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

. . . . . .

. . . . . . . . . . .

135 136 138 141 142 145 146 148 148 149 150

. . . . . .

155 155 159 159 160 162 164

Wave Equation 167 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

10 Concluding Remarks 171 10.1 Irregular Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 10.2 Commercial Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 10.3 Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

1 Introduction to MATLAB

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

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

1.2 Using MATLAB MATLAB is an interactive program so you can enter commands at the prompt or read in files with lines of MATLAB commands. For relatively simple tasks it is acceptable to enter commands at the prompt but for more complicated tasks it is better to store the commands in a file and then read in the file. This makes it easy to change the commands and to store them. In fact all inbuilt MATLAB commands are written as files of commands. For this reason there are vast numbers of MATLAB files (called M-files) written for various purposes available on the internet. A central repository of contributed M-files can be found at http://www.mathworks.com/matlabcentral/. For this course you should do your work using M-files as these will be handed up as part of your assignments along with the relevant output. All M-files given in the notes will be available from the course webCT page. Many of the assignment questions will be a modification of these files.

2

1. INTRODUCTION TO MATLAB

Index

1.3 A few simple examples 1. Matrix Manipulation Enter a 2× 2 matrix find its inverse, and eigenvalues and eigenvectors. Check that its inverse times itself is the identity. MATLAB code % any thing after a % sign is a comment A=[1,2;3,4] % enter the 2x2 matrix and call it A B=inv(A) % calculate the inverse of A and call it B eig(A) % calculate the eigenvalues of A [V,D]=eig(A) % calc the eigenvectors (columns of matrix V) % diagonal matrix D (eigenvalues on diagonal) check=B*A If these commands are stored in an M-file called example1.m then to run it type example1 (without the .m) from within MATLAB. (Note you have to be in the same directory as the file is saved in, or have made that directory in the path that MATLAB looks in). The MATLAB output from these commands is the following A = 1 3

2 4

B = -2.0000 1.5000

1.0000 -0.5000

ans = -0.3723 5.3723 V = -0.8246 0.5658

-0.4160 -0.9094

-0.3723 0

0 5.3723

D =

check = 1.0000 0.0000

0 1.0000

Index

3

A FEW SIMPLE EXAMPLES

2. Plotting MATLAB has very good graphics capability. The easiest method for simple plots is to define a vector (for example x) and then perform a function on each element of that vector (eg y(x)) and then plot y(x) versus x. Defining a vector is straightforward. The MATLAB command x=a:b:c defines x to be a vector of values starting at a increasing in steps of b to c. For example x=-4:3:8 results in x being the vector [−4, −1, 2, 5, 8]. Example. Plot y = x2 + sin(xπ ).

MATLAB code 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

25

20

15

10

5

0

−5 −5

0

5

Index

1. INTRODUCTION TO MATLAB

3. Plotting data It is very easy to read data in from a file, manipulate it and plot it. The command load is used to load data from an external file. For example read data from a file called datafile, calculate the line of best fit to the data and add some titles, legend and text to the plot. MATLAB code % read data from the file datafile. This puts it into % a variable matrix called datafile by default load datafile x=datafile(:,1) % put first column into variable x y=datafile(:,2) % put second column into variable y % Fit a linear regression line to the data t=[ones(size(x)) x]; coeffofregn=t\y straightline=[ones(size(x)) x]*coeffofregn; % Plot the data and line and add titles and labels plot(x,y,’ro’,x,straightline,’b-’) title(’data and line of best fit’) xlabel(’x’) ylabel(’y’) % 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) data and line of best fit 70 data line of best fit 60

50

y

4

40

30

20

10 0

This is text

1

2

3

4 x

5

6

7

8

Index

5

A FEW SIMPLE EXAMPLES

4. 3D Plotting MATLAB has very good 3D graphics capability. The easiest method is do define the domain using the meshgrid command, then define the function and then plot it using mesh or surf commands. Example. Plot z = 4x2 − 2y2 . MATLAB code

[x,y]=meshgrid(-5:0.5:5,-2:1.0:7); % define the mesh (x,y) the domain of plotting % x from -5 to 5 in steps of 0.5 % y from -2 to 7 in steps of 1. z=4*x.*x-2*y.ˆ2; % calculate z=4xˆ2-2yˆ2 % note the .* and .ˆ this is because x and y are % vectors so we need to multiple ’component by component’ mesh(x,y,z) % draw the surface as a mesh % or use surf(x,y,z) to draw it as a surface print -depsc example2a % send the plot output to a colour postscript file

100 50 0 −50 −100 10 5

5 0

0 −5

−5

6

Index

1. INTRODUCTION TO MATLAB

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 example to solve a linear equation system Ax = b there is a command known as ’backslash divide’. MATLAB code A=[1,2,3;4,5,6;7,8,10]; b=[1,1,1]’; x=A\b

% % % %

define the matrix A note the ’ to take transpose or could do b=[1;1;1] solve for x

x = -1.0000 1.0000 0.0000 6. Extracting Segments of Matrices Often you may want to extract sections of a matrix. For example all of the first row or 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 A d=A(:,2) % extract all of the second column of A

c = 1

d = 2 5 8 diary off

2

3

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 MATLAB commands into a file and then run that file. These types of files are called M-files and have the extension .m after their name. To run an M-file from MATLAB make sure you are in the directory where the file is located and then just type the name of the file without the extension (i.e. without the .m). MATLAB will then process all the commands that are in that file. Example 1. Create a file called example1.m 2. In that file put the following lines format compact A=[1,2;3,-4] C=Aˆ10 eig(C)

% % % %

this this this this

outputs in a compact form enters the 2x2 matrix A finds the tenth power of A 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 3

2 -4

C = 1395967 -4184829 ans = 1024 9765625

-2789886 8370682

1.5 Sending output to a file Often you want to keep the output of a MATLAB command. The diary commands does that for you. By typing diary example1.out all subsequent output will sent to the file example1.out as well as to the screen. To turn the diary command off use diary off

8

1. INTRODUCTION TO MATLAB

Index

1.6 MATLAB help facility and lookfor command MATLAB has a comprehensive help facility. It is accessed by typing help followed by the 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 window input and most of the resulting command window output to be appended to the named 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 be used 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 differentiate POLYDER Differentiate polynomial. FNDER Differentiate a function. DIFF Differentiate.

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-file that takes some kind of input and returns some calculations on that input. For example write a function that calculates f (x) = x3 + 4x2 + 3 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 calculate f (3) this results in the following output calcf(3) ans = 66

10

Index

1. INTRODUCTION TO MATLAB

1.8 Functions in MATLAB – vector input Since MATLAB deals with vectors of numbers it is more useful to write functions so that they can handle vectors rather than an individual number. The only change needed to achieve this is to ensure that the calculations within the function are done on an elementby-element basis by using the .* notation instead of the * notation (and equivalently .ˆ, ./ etc). For example write a function that calculates f (x) = x3 + 4x2 + 3 for an input vector x. That is, it should calculate f (x) for each value of a vector x and return the answer in a vector. MATLAB code function f=calcfvec(x) % this file is named calcfvec.m % this function calculates f(x) for a vector x and % returns the answers in the variable f % f = x.ˆ3 + 4.*x.ˆ2 + 3; return From within MATLAB type y=0:0.2:1.0 calcfvec(y) to calculate f (y) this results in the following output y=0:0.2:1.0 y = 0 calcfvec(y) ans = 3.0000

0.2000

0.4000

0.6000

0.8000

1.0000

3.1680

3.7040

4.6560

6.0720

8.0000

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. For example if you want to numerically calculate the integral of a function MATLAB has an inbuilt function that does this but somehow you need to tell it what function to integrate. Hence you need to pass the functions name. MATLAB has two inbuilt functions for calculating integrals [A,count]=quad(’funcname’,a,b,tol,trace) [A,count]=quad8(’funcname’,a,b,tol,trace) quad uses Simpson’s rule, quad8 uses a degree 8 polynomial. tol is the tolerance for the convergence using a comparison between successive approximations. trace is an additional feature for plotting a trace of the integrand used. The answer is returned in A and count is a count of how many function evaluations have been performed. funcname is the name of the function to be passed. For example if integrating the function defined in the M-file quadexamplefunc.m then use quadexamplefunc where ever you see funcname. Important Note: The function referenced by ’funcname’ must be written to take a vector input and give a vector output. Example Calculate

2 f (x) = √ π

Z x 0

2

e−t dt

For x = 1. This function is known as the error function (erf(x) in MATLAB) MATLAB code % quadexample.m format long % use lots of decimal places % the ’exact’ answer using MATLABs erf function errorfunc=erf(1) % numerically integrate using Simpsons rule (quad) integral=quad(’quadexamplefunc’,0,1,1e-6) % now do it again using an 8th order polynomial (quadl) integral=quadl(’quadexamplefunc’,0,1) % count the number of function calls using the trace switch trace=1; [integral,count]=quad(’quadexamplefunc’,0,1,1e-6,trace)

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

12

Index

1. INTRODUCTION TO MATLAB

>> quadexample errorfunc = 0.84270079294971

integral = 0.84270079342046

integral = 0.84270079427671 5 7 9 11 13 15 17

0.0000000000 0.0000000000 0.0000000000 0.2500000000 0.5000000000 0.5000000000 0.7500000000

integral = 0.84270079342046

count = 17 diary off

1.00000000e+00 5.00000000e-01 2.50000000e-01 2.50000000e-01 5.00000000e-01 2.50000000e-01 2.50000000e-01

0.8427115995 0.5204995573 0.2763263864 0.2441734868 0.3222012775 0.1906557580 0.1315451622

Index

MATLAB FINDING ZEROS

13

1.10 MATLAB finding zeros MATLAB has an inbuilt function for finding zeros of a function called fzero . Its use is r=fzero(’funcname’,x0) searches for a region with a zero of the function defined in funcname.m then uses bisection 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 value and return a real scalar value when called with feval: F=feval(FUN,X). The value X returned by FZERO is near a point where FUN changes sign, or NaN if the search fails. X = FZERO(FUN,X0), where X is a vector of length 2, assumes X0 is an interval where the sign of FUN(X0(1)) differs from the sign of FUN(X0(2)). An error occurs if this is not true. Calling FZERO with an interval guarantees FZERO will return a value near a point where FUN changes sign. X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting guess. FZERO looks for an interval containing a sign change for FUN and containing X0. If no such interval is found, NaN is returned. In this case, the search terminates when the search interval is expanded until an Inf, NaN, or complex value is 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.m f=x.ˆ3-2*x.ˆ2-9; return

14

Index

1. INTRODUCTION TO MATLAB

1.11 An Introduction to solving DEs numerically MATLAB has many ways of numerically solving Differential Equations. Here we will present one method, the details of the method and the mathematics behind it will be explained in later lectures. This will give you enough now to be able to numerically solve many of the DEs you will encounter early in this course. Consider a first order ODE dy = f (t, y) dt subject to some initial condition (at time t = 0) y(0) = y0 The MATLAB function ode45 will solve these kind of problems. Here is an excerpt from the help on ode45 ODE45 Solve non-stiff differential equations, medium order method. [T,Y] = ODE45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y’ = f(t,y) from time T0 to TFINAL with initial conditions Y0. Function ODEFUN(T,Y) must return a column vector corresponding to f(t,y). Each row in the solution array Y corresponds to a time returned in the column vector T. To obtain solutions at specific times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. Example [t,y]=ode45(@vdp1,[0 20],[2 0]); plot(t,y(:,1)); solves the system y’ = vdp1(t,y), using the default relative error tolerance 1e-3 and the default absolute tolerance of 1e-6 for each component, and plots the first component of the solution.

There are 3 things you need to define to use MATLAB to solve this type of DE. 1. The range of t you wish to calculate the solution over (TSPAN=[T0,TFINAL]) 2. The initial condition on y (Y0) 3. An M-file with the function f (t, y) (ODEFUN)

Notes 1. t is returned as a vector of all the times where the solution was determined. 2. Because ode45 is set up to solve systems of equations y is a matrix. The first dimension corresponds to t, that is solutions at the times in t, the second dimension is the number of equations which in this case is just 1. This has an impact on how the ODEFUN function is written. See the next example to see how this works.

Index Example Solve

AN INTRODUCTION TO SOLVING DES NUMERICALLY

dy = y − 2π cost dt

subject to

15

y(0) = 3

over the range t = [0, 5]. Note that you could solve this particular DE using the Integrating Factor method. The numerical solution works for DEs that can not be solved analytically. This is the point of developing numerical methods for solving DEs, they let you get solutions that can not be obtained analytically. MATLAB code % odeex1.m % solves y’= y-2*pi*cos(t) subject to y(0)=3 % over the range t=[0,5] % tspan=[0,5] % sets up the time span y0=3 % the initial condition % solve the DE and return solution in t and y [t,y]=ode45(’odeex1fun’,tspan,y0) % now plot the solution, note that t is a vector of all the % points in time where the solution was calculated and y is a % matrix whose first dimension corresponds to t and second % dimension is the number of equations (here just 1) plot(t,y(:,1)) % now send the plot to a file called odeex1.eps to be saved print -deps odeex1 MATLAB code function f=odeex1fun(t,y) % odeex1fun.m % the RHS of the system of DEs % note using f(1) and y(1) as these can be vectors % of longer than 1 if you have more than 1 equation f(1)=y(1) - 2*pi*cos(t); f=f(:); % forces f to be a column vector return

16

Index

1. INTRODUCTION TO MATLAB

5

0

−5

−10

−15

−20 0

1

2

3

4

5

The same files without all the extraneous comments MATLAB code % odeex1.m tspan=[0,5] y0=3 [t,y]=ode45(’odeex1fun’,tspan,y0) plot(t,y(:,1)) print -deps odeex1 MATLAB code function f=odeex1fun(t,y) % odeex1fun.m f(1)=y(1) - 2*pi*cos(t); f=f(:); return To solve other single equation DEs simply alter the equation (f(1)=. . . ), the time range (tspan) or the initial condition (y0).

Index

SYMBOLIC MANIPULATION

17

1.12 Symbolic Manipulation MATLAB can also do some (limited) symbolic manipulation of equations such as finding roots of equations, simplifying, differentiating and integrating. Firstly one much declare the variable(s) in question to be symbolic in nature (so that MATLAB knows they are not just a number) and then it is possible to manipulate them.

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 numbers syms x y % define a function of x f=xˆ3-3*y-cos(xˆ2)-atan(x) % differentiate f with respect to x dfdx=diff(f,x) % and the second derivative in 2 ways d2fdx2=diff(dfdx,x) d2fdx2=diff(f,x,2)

% function of 2 variables so need to do partial derivatives % define a function of x and y g=x*y-sin(x-y)+yˆ3*cos(1/y) % find the partial derivatives dgdx=diff(g,x) dgdy=diff(g,y) % find the second derivatives % either take derivatives of above or use diff with extra parameter d2gdx2=diff(dgdx,x) d2gdx2=diff(g,x,2) d2gdy2=diff(dgdy,y) d2gdy2=diff(g,y,2) % and the cross derivative is d2gdxdy=diff(dgdx,y)

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)

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 variable syms x % define a function f f=xˆ2-sin(2*x)+cosh(3*x)-exp(-x) % integrate f with repsect to x intf=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 example to solve systems of equations you can use solve and to solve differential equations symbolically you can use dsolve. These will be discussed at a later time in the course.

20

Index

1. INTRODUCTION TO MATLAB

1.13 Some Elementary MATLAB Commands Input x = [-3:2:7] x(2) y = x.ˆ2

plot(x,y)

Output Comment x = -3 -1 1 3 5 7 x is a vector that starts at –3 and goes up in steps of 2 to 7 -1 the second element of the vector x y = 9 1 1 9 25 49 using ’dot’ notation square the vector x ’component by component’ plot y versus x

50 45 40 35 30 25 20 15 10 5 0 −4

−2

0

2

4

6

8

plot y vs x with a red dash line hold the plot in place add blue circles to the plot

plot(x,y,’r--’) 50 45

hold on plot(x,y,’bo’)

40 35 30 25 20 15 10 5 0 −4

−2

0

2

4

6

8

t is a vector 0 to 2π in steps of π /50 Semicolon supressess the output.

t = 0:pi/50:2*pi; a=cos(t); 1 0.8

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

0.6 0.4

The plot is a parametric plot of a circle with green dots

0.2 0 −0.2 −0.4 −0.6 −0.8 −1 −1

c=[-3:1.0:5]; d=[-2:0.5:7]; [x,y]=meshgrid(c,d)

−0.5

0

0.5

1

3D plotting Set up the domain in c and d Make a mesh of values in x and y The function z = 4x2 − 2y2 Plot the function in 3D

100 50

z=4*x.ˆ2-2*y.ˆ2; mesh(x,y,z)

0 −50 −100 10 5

5 0

0 −5

title(’text here’) xlabel(’text here’) axis([0 5 -3 8]) text(1,20,’blah’)

−5

add a title to a plot add x axis label to a plot set the x axis to be from 0 to 5 and the y axis from -3 to 8 place some text on the plot at the point (1,20)

Index

SOME ELEMENTARY MATLAB COMMANDS

21

Input x=[1 2 3] length(x) y=x’

Output x = 1 2 3 3 y = 1 2 3

z=x*x z=x*y dot(x,y) cross(x,y) A=[1,2;3,4]

error: dimensions dont agree z = 14 vector multiplication 14 dot product of vectors x and y 0 0 0 cross product of vectors x and y A = 1 2 enter the matrix A 3 4 -0.3723 find the eigenvalues of A 5.3723 -2.0000 1.0000 find the inverse of A 1.5000 -0.5000 -2.0000 the determinant of A 37 54 A cubed (A*A*A) 81 118 1 8 each component of A cubed 27 64 1 2 all of the first row of matrix A 2 all of the second column of matrix A 4 b = 1 b is a column vector 4 c = 2.0000 Solve Ac=b using backslash divide -0.5000 0 0 a 2x2 matrix of zeros 0 0 1 1 a 2x2 matrix of ones 1 1 help on the plot command find all references to the word sinh send all output to the file file.txt hold the plot on so that more can be added to it turn the hold off so that a new plot is made read the contents of the file xdata and store the results in a variable called xdata

eig(A) inv(A) det(A) Aˆ3 A.ˆ3 A(1,:) A(:,2)

b=[1;4] c=A\b

zeros(2) ones(2) help plot lookfor sinh diary file.txt hold on hold off load xdata

Comment define a vector calculate the size of the vector y is the transpose of x

22

1. INTRODUCTION TO MATLAB

Index

2 Laplace Transforms

2.1 Introduction The majority of the theory of Laplace Transforms is given in the theoretical section of this course. Here we will investigate how MATLAB can be used to help solve Laplace Transform problems. The notation we will adopt is F(s) = L { f (t)} =

Z ∞ 0

e−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 Laplace Transforms.

2.2.1 Heaviside function The Heaviside step function is defined as (see figure) 0 t

1

H(t-a)

a

t

The Heaviside Step function is like a switch. At time t = a it ’turns on’ with value 1. The Heviside function is defined in MATLAB both as a numerical function and as a symbolic function. Its usage is heaviside(x).

24

Index

2. LAPLACE TRANSFORMS

Example Plot f (x) = H(x − 1)(x2 + 1) MATLAB code % heavisideexample.m % plot H(x-1)*(xˆ2+1) x=-2:0.05:3; y=heaviside(x-1).*(x.ˆ2+1) plot(x,y) axis([-2 3 -1 10]) print -depsc heavisideexample

10 9 8 7 6 5 4 3 2 1 0 −1 −2

−1

0

1

2

3

Index

SOME STANDARD RESULTS IN LAPLACE TRANSFORMS

25

2.2.2 Dirac delta function Suppose that a constant force, f (t), of amount A acts on a body for a short time (of duration ε ).

f(t)

force

A

t - ε/2

t + ε/2

time The impulse delivered by f (t) is I=

Z t0 +ε /2 t0 −ε /2

f (t) dt = ε A

Note that the impulse, I, is a constant. Suppose I = 1, then A = 1/ε . As ε → 0, we must have A → ∞. The width of the rectangle gets smaller and the height gets larger so that the area (I) stays equal to 1.

force

time

26

Index

2. LAPLACE TRANSFORMS

We can see that in the limit as ε → 0 f (t) = Z ∞

−∞

0, ∞,

t 6= t0 t = t0 ,

and f (t) dt = 1.

The Dirac delta function1 , δ (t), is defined by the following properties 1.

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

2.

Z 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 limit of a distribution of step functions (as in the previous diagram). It can also be thought of as the derivative of the Heaviside function.

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−as F1 (s) where H(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) has n well defined derivatives then: L

1 The

n

f (n) (t)

o

= sn F(s) − sn−1 f (0) − sn−2 f ′ (0) − · · · − f (n−1) (0).

delta function was introduced by the British physicist P.A.M Dirac (1902-1984) in the paper “The Physical Interpretation of the Quantum Mechanics”, Proceedings of the Royal Society of London, Series A113, 1926-1927, pp. 621-641.

Index

SOME STANDARD RESULTS IN LAPLACE TRANSFORMS

27

2.2.6 Multiplication by t L {t f (t)} = −F ′ (s) In general L {t n f (t)} = (−1)n

d n F(s) dsn

2.2.7 Integral theorem The Laplace transform of an integral is: Z t 1 L f (τ ) d τ = F(s) s 0

2.2.8 Division by t A corresponding theorem for integration is L

Z∞ 1 f (t) = F(s)ds t s

2.2.9 Convolution theorem Often when we cannot get an inverse in terms of a known function we have to use the convolution theorem L

−1

{F(s)G(s)} =

Z t 0

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

28

2. LAPLACE TRANSFORMS

Index

2.2.10 Taking Laplace transforms using MATLAB MATLAB can do Laplace Transforms and their inversions symbolically. Firstly you must declare the variables that are used (eg t and s) as symbolic variables using the syms command. The laplace function is then used to find the Laplace Transform. Example For example find the Laplace Transform of f (t) = e4t sin(3t) MATLAB code % find the Laplace Transform of % f(t)=exp(4*t)*sin(3*t) syms s t % make s and t symbolic variables Fs=laplace(exp(4*t)*sin(3*t))

results in

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

Example −3t 2 Find Laplace Transform of the derivative of f (t) = e sinh(5t) + t , that is find dthe −3t 2 L dt e sinh(5t) + t

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 variables Fs=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

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 the complex plane and finding residues around the singularities. Generally this is difficult to do and beyond the scope of this course. It is much easier to modify our function F(s) to look like something we already know the inverse transform of. For example we already know that s L {cos at} = 2 s + a2 so that the inverse of 2s −1 L = 2 cos 3t. s2 + 9 All that is necessary is to get our solution in the same form as the tables and then do the same process as the Laplace transform but in reverse.

2.3.1 Partial fractions Partial Fractions are useful to convert complicated inversions into a form that can be used from the tables. Partial fraction are used to convert a complicated rational expression such as y = G(s)/F(s) into a sum of simpler terms each of which can be inverted using the tables. F(s) must be written in a form so that it is a product of polynomials of as a low order as possible.

Distinct Linear Factors If the denominator has distinct linear factors then it is a simple matter of writing the expression as a sum of terms with those linear factors as denominators and unknown numerators. For example A B 3s + 13 = + (s + 3)(s + 1) s + 3 s + 1 where A and B are the unknown numerators that have to be found. To find these rewrite the right hand side over a common denominator so that it resembles the left hand side and then equate the coefficients of the different powers of s in the numerators. 3s + 13 A B = + (s + 3)(s + 1) s+3 s+1 A(s + 1) + B(s + 3) = (s + 3)(s + 1) (A + B)s + A + 3B = (s + 3)(s + 1) So then the coefficient of s gives A+B = 3 and the constant term (s to the zero power) gives A + 3B = 13

30

Index

2. LAPLACE TRANSFORMS

This is 2 equation in 2 unknowns which can be solved to give A = −2 Hence

B = 5.

3s + 13 −2 5 = + (s + 3)(s + 1) s + 3 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 A B +Cs 2s + 1 = + 2 s(s − 2) s (s − 2)2 A(s − 2)2 + (B +Cs)s = s(s − 2)2 where we thus have 2s + 1 = A(s − 2)2 + Bs +Cs2 so equating coefficients we get: O(s2 ) : O(s1 ) : O(s0 ) :

0 = A +C 2 = −4A + B 1 = 4A

Or alternatively substitute values for s. Either way we get 1 −1 A = , B = 3, C = 4 4 and so

2s + 1 1/4 3 − s/4 = + s(s − 2)2 s (s − 2)2

If the factor is repeated n times then the unknown numerator must be a polynomial of degree n − 1. The numerator must always have a degree one less than the denominator. For example 2s + 1 A + Bs C + Ds + Es2 + Fs3 = + s2 (s − 5)4 s2 (s − 5)4 Method 2 Alternatively (this method often leads to a better form for doing the inversion) the unknown right hand side can be written as A D E 2s + 1 = + + 2 s(s − 2) s s − 2 (s − 2)2 where we thus have 2s + 1 = A(s − 2)2 + Ds(s − 2) + Es

Index

INVERSION OF LAPLACE TRANSFORMS

31

so equating coefficients we get: O(s2 ) : O(s1 ) : O(s0 ) :

0 = A+D 2 = −4A − 2D + E 1 = 4A

hence giving: 1 −1 5 A= , D= , E= 4 4 2 1/4 −1/4 5/2 2s + 1 = + + 2 s(s − 2) s (s − 2) (s − 2)2

Using this method you must make sure there are as many terms involving the factor as it is repeated. For example 2s + 1 s2 (s − 5)4

=

A B C D E F + 2+ + + + 2 3 s s s − 5 (s − 5) (s − 5) (s − 5)4

Non Linear Factors If the factors are not linear then use method 1 as for repeated linear factors. That is use a numerator that has degree one less than the term in the denominator. For example if the denominator has a quadratic that can not be reduced any further then the corresponding numerator is linear A Bs +C 1 = + 2 2 s(s + 4) s s +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 5 + s+3 s+1

(by partial fractions)

hence enabling the solution: −2e−3t + 5e−t . Example −1/4 5/2 2s + 1 −1 1/4 −1 = L + + (using partial fractions) L s(s − 2)2 s (s − 2) (s − 2)2 1 1 2t 5 2t − e + e t (using shifting theorem) = 4 4 2

32

Index

2. LAPLACE TRANSFORMS

Example 1 s(s2 + 4)

=

A Bs +C + 2 s s +4

We thus get 1 = A(s2 + 4) + (Bs +C)s and by equating coefficients we get: O(s2 ) : O(s1 ) : O(s0 ) :

0 = A+B 0 = C 1 = 4A

easily giving the solution A = 41 , B = − 41 ,C = 0 and hence the inverse 1 1/4 −1 −1 1/4 L = L − 2 s(s2 + 4) s s +4 1 1 sin 2t − = 4 4 2 This following example uses the derivative formulae to transform a differential equation into 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 where Y (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 3s + 13 (s + 3)(s + 1) 5 −2 + (using partial fractions, see before) = s+3 s+1

Y =

As we can invert to obtain

L eat = 1/(s − a)

y(t) = −2e−3t + 5e−t . This illustrates how a transformation simplifies the solution to differential equations.

Index

INVERSION OF LAPLACE TRANSFORMS

33

2.3.3 MATLAB functions for partial fractions MATLAB has commands to do partial fractions (residue and poly). Residue The residue function takes a ratio of polynomials B(s) A(s) and returns the corresponding partial fraction representation of it. It takes as input the coefficients of the polynomials B(s) and A(s) (in descending powers of s) and returns the coefficients and the pole values (the zeros of the denominator). help residue RESIDUE Partial-fraction expansion (residues). [R,P,K] = RESIDUE(B,A) finds the residues, poles and direct term of a 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 and denominator polynomials in descending powers of s. The residues are returned in the column vector R, the pole locations in column vector P, and the direct terms in row vector K. The number of poles is n = length(A)-1 = length(R) = length(P). The direct term coefficient vector is empty if length(B) < length(A), otherwise length(K) = length(B)-length(A)+1. If P(j) = ... = P(j+m-1) is a pole of multplicity m, then the expansion 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 arguments, converts the partial fraction expansion back to the polynomials with coefficients in B and A. Warning: Numerically, the partial fraction expansion of a ratio of polynomials represents an ill-posed problem. If the denominator polynomial, A(s), is near a polynomial with multiple roots, then small changes in the data, including roundoff errors, can make arbitrarily large changes in the resulting poles and residues. Problem formulations making use of state-space or zero-pole representations are preferable. See also POLY, ROOTS, DECONV.

34

Index

2. LAPLACE TRANSFORMS

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 s A=[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

1

2

A = -15

R = 2 1 P = -5 3 K = []

The interpretation of this is as follows K is the remainder, K empty means the partial fraction representation has no remainder P 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 so here 2 1 F(s) = + s+5 s−3

Index

INVERSION OF LAPLACE TRANSFORMS

35

Example MATLAB can also deal with repeated roots but care must be taken when interpretting the output. For example find the partial fraction representation of F(s) =

s2 + 4s − 2 s3 − 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 s A=[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

1

0

-3

A = -2

R = 1.1111 -0.1111 1.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 remainder P 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/9 −1/9 5/3 + + s − 2 s + 1 (s + 1)2

36

Index

2. LAPLACE TRANSFORMS

2.3.4 Inversion using MATLAB Often it is not necessary to do the partial fraction steps yourself as MATLAB can do some inversions symbolically using the MATLAB command ilaplace. Example Find the inversion of F(s) =

s s2 − 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 variables Fs=s/(sˆ2-2*s+1) ft=ilaplace(Fs)

results in

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

Index

INVERSION OF LAPLACE TRANSFORMS

37

More complicated expressions are also straightforward including ones that result in Heaviside 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 variables Fs=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)

38

Index

2. LAPLACE TRANSFORMS

2.3.5 Systems of Equations Laplace Transforms can be used to solve systems of differential equations. By taking the Laplace Transform of a system of differential equations it is converted into an algebraic system in the transformed variable. These can be solved to get the transformed functions and then inverted to find the solutions to the original system. Example Solve the system dx1 = −x1 + x2 dt dx2 = 2x1 dt subject to the initial conditions x1 (0) = 0 and x2 (0) = 1. First take the Laplace Transform of both equations (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 get X1 (s) and X2 (s). Substitute the first equation into the second for X2 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 get X2 X2 = (s + 1)X1 =

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

Note that MATLAB’s solve function could be used to solve the system for X1 and X2 . Now use partial fractions then invert to get the solution to the original system as X1 = X2 =

1 1 1 1 − 3 s−1 3 s+2

2 1 1 1 + 3 s−1 3 s+2

⇒

x1 (t) =

1 t 1 −2t e − e 3 3

⇒

x2 (t) =

2 t 1 −2t e + e 3 3

Index

INVERSION OF LAPLACE TRANSFORMS

Example Solve the system 2

dx1 dx2 + − 2x1 = 1 dt dt

dx1 dx2 + − 3x1 − 3x2 = 2 dt dt subject to the initial conditions x1 (0) = 0 and x2 (0) = 0. First take the Laplace Transform of both equations 2(sX1 − x1 (0)) + (sX2 − x2 (0)) − 2X1 =

1 s

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

2 s

Now apply the initial conditions and rearrange to get (2s − 2)X1(s) + sX2(s) =

1 s

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

2 s

Now solve this to get X1 (s) and X2 (s). Rearrange the first equation to get X2 =

1 s

− (2s − 2)X1 s

Substitute into the second equation and simplify to find X1 1

(s − 3)X1(s) + (s − 3) s

− (2s − 2)X1 2 = s s

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 (collect X1 terms) (s − 3)(−s + 2)sX1 = s + 3 X1 =

(simplify)

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

Substitute in to get X2 X2 =

1 s

− (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)

39

40

Index

2. LAPLACE TRANSFORMS

Now use partial fractions to get X1 =

−1 1 5 1 −2 + + 2 s 2 s−2 s−3

X2 =

8 1 −1 1 −5 1 + + 6 s 2 s−2 3 s−3

Take the inverse Laplace Transform to get the solution to the original system as −1 5 + e2t − 2e3t 2 2 −5 2t 8 3t −1 + e + e x2 (t) = 6 2 3

x1 (t) =

3 Initial Value Problems

3.1 Introduction An Initial Value Problem (IVP) is a differential equation of nth order where the function value and the first n − 1 derivatives are all specified at one given point known as the initial point. For examples y′ = 2y2 y′′′ + 3y′′ + 7y′ − 3y = 0

with y(1) = 3

with y′′ (2) = 2 y′ (2) = −1

y(2) = 0.5

Also it can be a system of first order differential equations with each function given a value at the same point. For example y′′′ + 3y′′ + 7y′ − 3y = 0 can be written as

with y′′ (2) = 2 y′ (2) = −1

y(2) = 0.5

42

Index

3. INITIAL VALUE PROBLEMS

A numerical solution to this system of equations over t = [2, 6] using MATLAB is MATLAB code % system1.m tspan=[2,6] % note starts at t=2 now y0=[0.5,-1,2] % note its a vector now [t,y]=ode45(’system1fun’,tspan,y0) % plot all y, y’ and y’’ on one graph and add a legend plot(t,y(:,1),t,y(:,2),’r-.’,t,y(:,3),’g:’) legend(’y’,’dy/dt’,’d2y/dt2’) print -depsc system1 MATLAB code function f=system1fun(t,y) % system1fun.m f(1)=y(2); f(2)=y(3); f(3)=-3*y(3)-7*y(2)+3*y(1); f=f(:); return

2.5 y dy/dt d2y/dt2

2 1.5 1 0.5 0 −0.5 −1 2

2.5

3

3.5

4

4.5

5

5.5

6

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’s Series can be used to expand about that point to get an approximation to the solution at neighbouring points. So for instance if you know the about the function at the point a then information at a point x is given by f (x) = f (a) + f ′ (a)(x − a) +

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

If you know something about f ′ (a), f ′′ (a),. . . then you can approximate the solution at the point x. The process for solving IVPs numerically is to start at the initial point and step forward using information provided by the governing differential equation. This can be done in a variety of ways. The simplest is Euler’s method.

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 line at each point that you step forward at. Recall the definition of the derivative as dy y(x + ∆x) − y(x) ≈ dx ∆x If we are solving the IVP dy = f (x, y) dx

with y(x0 ) = y0

we start at the position x0 and hence y(x0 ) = y0 is known then dy y(x0 + ∆x) − y(x0 ) (x0 , y0 ) = f (x0 , y0 ) ≈ dx ∆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 where yi is the approximation at the point xi = x0 + i∆x and fi = f (xi , yi ). Euler’s method is equivalent to approximating the solution by using the first two terms in the Taylor Series (the function value and the first derivative) and ignoring the higher order terms. The first ignored term is 1 1 ′′ f (a)(x − a)2 = f ′′ (a)(∆x)2 2! 2! so each step in the process has an error of the order of (∆x)2 .

44

3. INITIAL VALUE PROBLEMS

Index

3.3.1 Graphically

3.3.2 Errors and order of the method Assuming we are calculating the solution from x = a to x = b using N steps then ∆x = (b − a)/N. The error in each step is proportional to (∆x)2 as this is the first term neglected in the Taylor Series so then total 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 a first order method. A pth order method is one whose error is proportional to (∆x) p . The higher the order the method the more accurate it is. This usually comes at a price of being more difficult to program. First order methods are usually very slow if you want any reasonable amount of accuracy. Why is this?

Index

EULER’S METHOD

45

Example Solve y′ = y

with y(0) = 1

over the range x = [0, 1]. This has exact solution y = ex . MATLAB code % eulerexample.m % % solve y’=y subject to y(0)=1 % find the result at x=1 and calculate the error. % clear all; N=100; % number of steps used a=0; b=1; % endpoints of solution dx=(b-a)/N; % step size % save x and y values in vectors so we can plot solution x(1)=a; y(1)=1; % initial x and y values for i=1:N f=y(i); % calculate the function value at (xi,yi) y(i+1)=y(i)+dx*f; x(i+1)=x(i)+dx; end plot(x,y,’b-’) axis([0 1 1 2.8]) hold on exact=exp(x); plot(x,exact,’r--’); legend(’Euler’,’Exact’,2) hold off print -depsc eulerexample error=abs(exact(N+1)-y(N+1))

Results of running the above code for different number of points (N = 10, 100, 1000, 10000) and hence different step sizes N 10 100 1000 10000

∆x 0.1 0.01 0.001 0.0001

yN 2.59374246010000 2.70481382942153 2.71692393223590 2.71814592682523

error 0.12453936835904 0.01346799903752 0.00135789622315 0.00013590163356

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

46

Index

3. INITIAL VALUE PROBLEMS

Exact solution and Euler’s method with N = 10 and N = 100 applied to y′ = y

with y(0) = 1

2.8 2.6

2.8 Euler Exact

2.6

2.4

2.4

2.2

2.2

2

2

1.8

1.8

1.6

1.6

1.4

1.4

1.2

1.2

1 0

0.2

0.4

0.6

0.8

N = 10, ∆x = 0.1

1

1 0

Euler Exact

0.2

0.4

0.6

0.8

1

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 size to get a more accurate solution at the cost of increased computing time? All you know about Euler’s method is that the error is proportional to the step size (∆x) since it is a first order method, but you have no idea of what that constant of proportionality is. It can change for each different problem and even within the one problem. What would be ideal if we had some way of changing the step size so that in areas where the solution changes rapidly we choose a small step size and in areas where it changes slowly we can take a larger step size so that the error is about the same at each step. This is something we would want to be able to do for any method not just Euler’s method. All well written numerical ODE solvers will use some kind of adjustable step size that takes account of how fast the solution is varying and the size of the errors involved.

Index

EULER’S METHOD

47

Algorithm for adjusting the step size 1. For each step compare the result of using one step of size ∆x with the result of using two steps of size ∆x 2 . Call the difference between the solutions ∆y. If the method you are using is a pth order method then the error from a single step is (∆x) p+1 so ∆x ∆y = y(xi + ∆x) − y(xi + 2 ) 2 p+1 p+1 ! ∆x ∆x ≈ k(∆x) p+1 − k +k 2 2 k(∆x) p+1 = k(∆x) p+1 − 2p 1 = k(∆x) p+1 1 − p 2 = k′ (∆x) p+1

2. If you wish to make your solution accurate to some predetermined accuracy (call it ε ) then |∆y/y| < ε . This corresponds to some ideal step size which we will call δ x. Then ε 1/(p+1) |∆y/y| = ε = k(δ x) p+1 so δ x = k in reality we used a step size ∆x which had |∆y/y| = k(∆x)

p+1

so ∆x =

1 |∆y/y| k

1/(p+1)

The ratio of the step sizes (R) is then a measure of how good our step size is compared to the ideal step size

δx R= = ∆x

εy ∆y

1/(p+1)

3. If R rel="nofollow"> 1 then step size is too small so it can be increased at the next step. If R < 1 then step size is too big so it must be decreased at the current step and the process repeated until R > 1. Use the size of R as a guide to how much to increase or decrease the step size. For example let ∆xnew = mR∆xold where m is a tuning parameter usually taken to be a little less than one (eg m = 0.95 or thereabouts).

48

3. INITIAL VALUE PROBLEMS

Index

MATLAB code % euleradaptive.m % Euler method with adaptive time stepping % % dy/dx + y = exp(-x) + 1 with y(0)=1 % which has exact solution y = xexp(-x) + 1 % clear all; p=1; m=0.95; eps=5.e-4; a=0; b=1.0; % endpoints of solution dx(1)=0.1; % initial step size % save x,y and dx values in vectors so we can plot solution x(1)=a; y(1)=a*exp(-a)+1; % initial x and y values i=1; while x(i) < b % keep going until you reach the end point R=0; while R < 1 % calculate solution in one step yone=y(i)+dx(i)*feval(’eulerf’,x(i),y(i)); % calculate solution in two steps yhalf=y(i)+dx(i)/2*feval(’eulerf’,x(i),y(i)); ytwo=yhalf+dx(i)/2*feval(’eulerf’,x(i)+dx(i)/2,yhalf); R=(abs(eps*ytwo/(yone-ytwo)))ˆ(1/(p+1)); dx(i)=m*R*dx(i); end y(i+1)=ytwo; x(i+1)=x(i)+dx(i); dx(i+1)=dx(i); i=i+1; end for j=1:i exact(j)=x(j)*exp(-x(j))+1; end plot(x,y,’ro’) hold on plot(x,exact,’b-’); legend(’Adaptive’,’Exact’,2) hold off print -depsc euleradaptive plot(x,dx,’ro’) print -depsc euleradaptivedx MATLAB code function fxy=eulerf(xx,yy) fxy=exp(-xx)-yy+1; return

Index

EULER’S METHOD

49

Below are plots of the Euler method and adaptive Euler method and also a plot showing the step size used. Notice that the step size increases as the solution flattens as larger steps can be taken for the same error. 1.4 1.35

Adaptive Exact

1.3 1.25 1.2 1.15 1.1 1.05 1 0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.2

1.4

Exact, Euler and Adaptive Euler solution 0.08 0.075 0.07 0.065 0.06 0.055 0.05 0.045 0.04 0.035 0.03 0

0.2

0.4

0.6

0.8

1

Showing how the step size changes

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 slope at the midpoint. This in general will be a better approximation to the function. Graphically

First estimate the value of y at the midpoint using Euler’s method yi+1/2 = yi + f (xi , yi )∆x/2 The slope at the midpoint is then approximated by f (xi+1/2 , yi+1/2 ) so use this in an Euler like formulation to get the formula yi+1 = yi + f (xi+1/2 , yi+1/2 )∆x This is known as the midpoint method. It can be shown that the midpoint method is a second order method (as opposed to Euler’s method that is first order) but this comes at the price of having to calculate twice the number of function evaluations (since we calculate the function at the midpoints as well). How will this effect the methods efficiency? That is, how will the extra function evaluations be offset by the improvement in the calculation as it is a higher order method?

Index

IMPROVEMENTS TO EULER’S METHOD - MIDPOINT METHOD

51

Example Solve y′ = xy

with y(0) = 1 2 /2

on [0, 1]. This has exact solution y = ex

.

1.8 1.7

Euler Exact Midpoint

1.6 1.5 1.4 1.3 1.2 1.1 1 0

0.2

0.4

0.6

0.8

1

Midpoint method and Euler method with N = 10, ∆x = 0.1

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

N

∆x

error Euler

10 100 1000 20 200

0.1 0.01 0.001 0.05 0.005

0.10161087269011 0.01090081247050 0.00109823249901 0.05277960435958 0.00547295587046

flops∗ Euler 60 600 6000 120 1200

error Midpoint 0.00257111404563 0.00002730016798 0.00000027460827 0.00066472539344 0.00000684735089

flops∗ Midpoint 120 1200 12000 240 2400

∗

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

The Midpoint method is a second order method. That is the total error is proportional to (∆x)2 . Hence for a 10 fold decrease in the step size (∆x) you get an approximate 100 fold decrease in the error. For the same amount of computing effort (flops) the Midpoint method is far superior to Euler’s method. Just compare the errors for the Euler 120 flops calculation (N = 20, error≈ 0.05278) with the Midpoint 120 flops calculation (N = 10, error≈ 0.00257).

52

3. INITIAL VALUE PROBLEMS

MATLAB code % midpoint.m % % solve y’=xy subject to y(0)=1 % find the result at x=1 % for Euler and Midpoint methods and count flops. % clear all; N=10; % number of points, a=0; b=1; % initial and end points dx=(b-a)/N; % step size x(1)=a; y(1)=1; % initial x, y(Euler) values z(1)=y(1); % initial z(Midpoint) value flops(0) for i=1:N % Euler method y(i+1)=y(i)+dx*feval(’midf’,x(i),y(i)); x(i+1)=x(i)+dx; end eulerflops=flops flops(0); for i=1:N % Midpoint method zhalf=z(i)+dx/2*feval(’midf’,x(i),z(i)); z(i+1)=z(i)+dx*feval(’midf’,x(i)+dx/2,zhalf); x(i+1)=x(i)+dx; end midptflops=flops plot(x,y,’b-’) hold on exact=exp(0.5.*x.ˆ2); plot(x,exact,’go’); plot(x,z,’r-’) legend(’Euler’,’Exact’,’Midpoint’,2) hold off print -depsc midptexample erroreuler=abs(exact(N+1)-y(N+1)) errormidpt=abs(exact(N+1)-z(N+1))

MATLAB code function f=midf(xx,yy) % midf.m f=xx*yy; return

Index

Index

SECOND ORDER RUNGE-KUTTA METHOD

53

3.5 Second order Runge-Kutta method

Named after 2 German Mathematicians, Carl David Tolme Runge (1856–1927) and Wilhelm Kutta (1867–1944) The previous methods (Euler, Midpoint) have all used some approximation to the slope. Euler: the slope at the left point Midpoint: the slope at the midpoint. The Runge-Kutta method uses a weighted average of the the slope of the left point and some as yet unknown intermediate point. So the general formula is yi+1 = yi + ∆x favg 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 undetermined point given by xi′ = xi + α ∆x yi′ = yi + β fi ∆x The method consists of finding the values of the weights a, b and the position of the intermediate point given by α and β . Recall the Taylor Series expansion for yi+1 = y(xi + ∆x) is dy (∆x)2 d 2 y |i + |i + . . . dx 2 dx2 (∆x)2 d f = yi + ∆x fi + |i + . . . 2 dx (∆x)2 ∂ f ∂ f dy |i + |i + . . . = yi + ∆x fi + 2 ∂x ∂ y dx (∆x)2 ∂ f ∂f = yi + ∆x fi + |i + f |i + . . . 2 ∂x ∂y

yi+1 = yi + ∆x

But expanding fi′ in a 2D Taylor Series gives fi′ = f (xi′ , yi′ ) = f (xi + α ∆x, yi + β fi ∆x) ∂f ∂f = f (xi , yi ) + α ∆x |i + β fi ∆x |i + . . . ∂x ∂y So the Runge-Kutta formulation gives yi+1 = yi + ∆x favg = yi + ∆x (a fi + b fi′ )

54

Index

3. INITIAL VALUE PROBLEMS

∂f ∂f ≈ yi + ∆x a fi + b f (xi , yi ) + α ∆x |i + β fi ∆x |i ∂x ∂y ∂f ∂f = yi + ∆x a fi + b fi + bα ∆x |i + bβ fi ∆x |i ∂x ∂y

Comparing this with the Taylor Series expansion for yi+1 gives a+b = 1

bα =

1 2

bβ =

1 2

Which is 3 equations in the 4 unknowns a, b, α and β so there is still some degree of freedom of choice. For example setting a=0

b=1

α =β =

1 2

gives the midpoint method. These methods are second order. Because there is still a degree of freedom in the equations for specific problems it is possible to make a choice of a, b, α and β that will make the method third order. This method has been precoded in MATLAB and is used with a call to ode23 To solve the IVP

dy = ode f unc(t, y) with y(a) = c dt over the interval [a, b] a call to ode23 would be like y0=[c]; tspan=[a b]; [t,y] = ode23(’odefunc’,tspan,y0);

Use help ode23 to find out more about the MATLAB command and how to use it. In particular how to choose the accuracy. ode23 also uses an adaptive step size as well if it needs to find the solution more accurately in a given region of the interval.

Index

SECOND ORDER RUNGE-KUTTA METHOD

55

Example Solve

dy = ty with y(0) = 1 dt

on [0, 1]. This has exact solution y = et

2 /2

.

MATLAB code % o23example.m % solve dy/dt=odefunc(t,y) on [a,b] given y(a)=c a=0; b=1; c=1; % interval and initial value y0=[c]; % initial condition tspan=[a b]; % interval of integration [t,y]=ode23(’odefunc’,tspan,y0);% soln in vectors t and y plot(t,y) hold on exact=exp(0.5.*t.ˆ2); plot(t,exact,’ro’) legend(’ode23’,’Exact’,2) print -depsc o23example MATLAB code function f=odefunc(t,y) % odefunc.m % the rhs of the IVP dy/dt=f(t,y) f=t*y; return

1.8 1.7

ode23 Exact

1.6 1.5 1.4 1.3 1.2 1.1 1 0

0.2

0.4

0.6

0.8

1

56

3. INITIAL VALUE PROBLEMS

Index

3.6 Fourth order Runge-Kutta method In exactly the same way as the second order Runge-Kutta method was developed higher order methods can be developed. By including more sampling points in the interval greater accuracy can be obtained. The second order method used the left-hand point and one other point to determine an approximation to the solution. Hence two function evaluations were required for each step. The Runge-Kutta fourth order method uses the left-hand point and 3 other points to hence four function evaluations are required for each step. The method is fourth order accurate that is the total error is proportional to (∆x)4 . 1 yi+1 = yi + (z1 + 2z2 + 2z3 + z4 ) 6 where z1 = f (xi , yi )∆x 1 1 z2 = f (xi + ∆x, yi + z1 )∆x 2 2 1 1 z3 = f (xi + ∆x, yi + z2 )∆x 2 2 z4 = f (xi+1 , yi + z3 )∆x The MATLAB command for using this method is ode45 and is used exactly the same way as ode23. The fourth order Runge-Kutta method is by far the most commonly used method for solving IVPs.

Index

ODE45

57

3.7 ode45 >> help ode45 ODE45 Solve non-stiff differential equations, medium order method. [T,Y] = ODE45(’F’,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y’ = F(t,y) from time T0 to TFINAL with initial conditions Y0. ’F’ is a string containing the name of an ODE file. Function F(T,Y) must return a column vector. Each row in solution array Y corresponds to a time returned in column vector T. To obtain solutions at specific times T0, T1, ..., TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1 ... TFINAL]. [T,Y] = ODE45(’F’,TSPAN,Y0,OPTIONS) solves as above with default integration parameters replaced by values in OPTIONS, an argument created with the ODESET function. See ODESET for details. Commonly used options are scalar relative error tolerance ’RelTol’ (1e-3 by default) and vector of absolute error tolerances ’AbsTol’ (all components 1e-6 by default). [T,Y] = ODE45(’F’,TSPAN,Y0,OPTIONS,P1,P2,...) passes the additional parameters P1,P2,... to the ODE file as F(T,Y,FLAG,P1,P2,...) (see ODEFILE). Use OPTIONS = [] as a place holder if no options are set. It is possible to specify TSPAN, Y0 and OPTIONS in the ODE file (see ODEFILE). If TSPAN or Y0 is empty, then ODE45 calls the ODE file [TSPAN,Y0,OPTIONS] = F([],[],’init’) to obtain any values not supplied in the ODE45 argument list. Empty arguments 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 error tolerance 1e-4 and absolute tolerances of 1e-4 for the first two components and 1e-5 for the third. When called with no output arguments, as in this example, ODE45 calls the default output function ODEPLOT to plot the solution as it is computed. See also ODEFILE and other ODE solvers: options handling: output functions: odefile examples:

ODE23, ODE113, ODE15S, ODE23S, ODE23T ODESET, ODEGET ODEPLOT, ODEPHAS2, ODEPHAS3, ODEPRINT ORBITODE, ORBT2ODE, RIGIDODE, VDPODE

58

Index

3. INITIAL VALUE PROBLEMS

Example Solve y′′ = t 2 cos(t)y s.t. y(0) = 2 and y′ (0) = 3 MATLAB code % o45ex1.m % solve y’’=tˆ2*y*cos(t)on [0,5] given y(0)=2, y’(0)=3 % this can be written as a system of first order DEs % y1’=y2 y1(0)=2 % y2’=tˆ2*y1*cos(t) y2(0)=3 a=0; b=5; % interval y0=[2 3]; % initial condition tspan=[a:.1:b]; % interval of integration [t,y]=ode23(’o45ex1func’,tspan,y0);% soln in vectors t and y plot(t,y(:,1),t,y(:,2),’r--’) % plot y1 and y2 versus t legend(’y’,’dy/dt’,2) xlabel(’t’); ylabel(’y and dy/dt’) print -depsc o45ex1 % output to postscript file MATLAB code function f=o45ex1func(t,y) % o45ex1func.m % the RHS of the system of DEs f(1)=y(2); f(2)=tˆ2.*y(1).*cos(t); f=f(:); % forces f to be a column vector return 30 y dy/dt 20

y and dy/dt

10

0

−10

−20

−30 0

1

2

3 t

4

5

Index

ODE45

59

The MATLAB function ode45 has a vast array of options that can be used. Finding solutions at given points. use tspan = [T0 T1 ... TFINAL] eg tspan=[0.1 0.2 0.3 0.4 0.5] will return solutions at t = 0.1, 0.2 etc. Error tolerances There is an extra parameter that can be added to the parameter list called ’options’. The call to ode45 is then ode45(’func’,tspan,y0,options) The options parameter is set using the command odeset. For example options = odeset(’RelTol’,1e-3,’AbsTol’,[1e-4 1e-4 1e-5]); Would set the relative error tolerance to 1 × 10−3 and the absolute error tolerance to 1 × 10−4 for the first two components and 1 × 10−5 for the third. This is useful if you know (or expect) that some of the variables will be substantially different is magnitude than others. Stopping the integration It is possible to stop the integration when certain events occur. For example when any of the 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 so that if ode45 passes it a flag with a value of ’events’ it determines the .m file that tells it information about the stopping criteria. If the flag is empty it determines the .m file that is actually used to calculate the integration (the DEs).

60

Index

3. INITIAL VALUE PROBLEMS

3.8 Projectile example Consider the problem of a projectile fired from a fixed position. If you know the initial velocity and angle of the projectile it is possible to determine the trajectory of the projectile. Other factors such as drag on the projectile and lift can also be taken into account. A system of differential that governs the motion of the projectile is given by 1 dv = −mg sin φ − ρ ACD v2 dt 2 1 dφ mv = −mg cos φ + ρ ACL v2 dt 2 m

where φ = angle from horizontal, v = velocity, m = mass. ρ = density, A = area, CD drag coefficient, CL lift coefficient, g gravity. The trajectory of the projectile is governed by dx = v cos φ dt dy = v sin φ dt If the initial position (x, y coordinates), initial velocity (v) and angle (φ ) are known then the trajectory of the projectile can be easily determined. This is a system of 4 nonlinear differential equations in the 4 unknowns x, y, v and φ . Solving these equations analytically is not possible but solving them numerically is very easy using MATLAB. Rearrange the equations into a standard form where the left hand side is just the derivative of each variable.

dv dt dφ dt dx dt dy dt

ρ ACD v2 2m −g cos φ ρ ACL v = + v 2m

= −g sin φ −

= v cos φ = v sin φ

Then just numerically integrate these equations forward in time form the known initial position, velocity and angle.

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 for % the velocity, angle, x and y % format compact v0=10; % initial velocity theta=0; while theta >= 0 theta=input(’enter theta, use < 0 to stop...’) if theta<0 break else % initial point [velocity angle x y] y0=[v0 theta*pi/180 0 0]; % make the time span large enough that it gets to the target tspan=[0 1]; [t,y]=ode45(’projectiledes’,tspan,y0); plot(y(:,3),y(:,4)) % plot the trajectory hold on % determine how long the solution vector is lgthy=length(y); range=y(lgthy,3) % the range is the last x value end end hold off print -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 ball g=9.8; rho=1.23; A=0.038; m=0.42; D=0.22; Cd=0.20; Cl=0.05; v=y(1); phi=y(2); f(1)=(-g.*sin(phi)-rho.*A.*Cd./(2..*m).*v.ˆ2); % velocity f(2)=(-g.*cos(phi)./v + rho.*A.*Cl./(2.*m).*v);% angle f(3)=v.*cos(phi); % x coordinate f(4)=v.*sin(phi); % y coordinate f=f(:); % force f to be a column vector return

62

Index

3. INITIAL VALUE PROBLEMS

>> projectile enter theta, use theta = 30 range = 8.2272 enter theta, use theta = 45 range = 6.7203 enter theta, use theta = 60 range = 4.7443 enter theta, use theta = -1 >> diary off

< 0 to stop...30

< 0 to stop...45

< 0 to stop...60

< 0 to stop...-1

4 3.5 3 2.5 2 1.5 1 0.5 0 0

2

4

6

8

10

MATLAB can be set up to integrate the DE until a specific event occurs, for example here that might be when y = 0 (ie when the projectile hits the ground). You can then use fzero to find the initial angle that gives a specific range (ie hits the target).

4 Phase Plane Analysis

4.1 Introduction Phase plane analysis is useful in determining the behaviour of solutions to systems of differential equations without necessarily solving the system analytically. More importantly this technique is not limited to studying linear equations. For systems of 2 autonomous first order differential equations it is possible to plot solutions of one dependent variable against the other dependent variable with the time dependence varying along the curves. This is known as a phase plane. The goal is to sketch enough solution curves so that the general behaviour of the system can be summarised by looking at the graph. The system dx1 = x2 dt dx2 = −2x1 − x2 dt has phase plane consisting of spirals converging to the origin.

5 4 3

x2

2 1 0 −1 −2 −1

−0.5

0

0.5

x

1 1

1.5

2

2.5

64

4. PHASE PLANE ANALYSIS

MATLAB code

% ppexample.m % drawing phase plane for % x1’=x2 % x2’=-2x1-x2 % tend=10; tspan=[0 tend]; y0=[0 5]; [t,y]=ode45(’ppexamplef’,tspan,y0); plot(y(:,1),y(:,2)) xlabel(’x_1’); ylabel(’x_2’); print -depsc ppexample MATLAB code

function f=ppexamplef(t,y) % shootingexamplef.m f(1)=y(2); f(2)=-2*y(1)-y(2); f=f(:); return

Index

Index

65

INTRODUCTION

Or with multiple initial points put the whole thing into a for loop and update the initial points as you go through the loop. MATLAB code

% ppexample2.m % drawing phase plane for % x1’=x2 % x2’=-2x1-x2 % tend=10; tspan=[0 tend]; for s=-2:1:3 y0=[s 5]; [t,y]=ode45(’ppexamplef’,tspan,y0); plot(y(:,1),y(:,2)) xlabel(’y_1’); ylabel(’y_2’); hold on end hold off print -depsc ppexample2

5 4 3 2

y

2

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

−1

0

1

y1

2

3

4

5

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 DEs is to draw the velocity field. The MATLAB command quiver is designed to do precisely this. help quiver QUIVER Quiver plot. QUIVER(X,Y,U,V) plots velocity vectors as arrows with components (u,v) at the points (x,y). The matrices X,Y,U,V must all be the same size and contain corresponding position and velocity components (X and Y can also be vectors to specify a uniform grid). QUIVER automatically scales the arrows to fit within the grid. QUIVER(U,V) plots velocity vectors at equally spaced points in the x-y plane. QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the arrows 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 for the velocity vectors. Any marker in LINESPEC is drawn at the base instead of an arrow on the tip. Use a marker of ’.’ to specify no marker at all. See PLOT for other possibilities. 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 on quiver(x,y,px,py), hold off, axis image See also FEATHER, QUIVER3, PLOT. diary off

Index

INTRODUCTION

67

MATLAB code % vfield.m % % plots velocity vectors for the following system % dx/dt = y % dy/dt = -2x-y % a=-5; b=5; % size of the grid [x,y]=meshgrid(a:1:b,a:1:b);% a square grid (a to b)x(a to b) dx=y; % the DEs dy=-2*x-y; % quiver(x,y,dx,dy,’r’) % plot the vector field axis square % make the plot square axis tight % make axis tight on data hold on w=[a-1 b+1]; plot(w,0*w,’k-’,0*w,w,’k-’) % adds x and y axes to plot xlabel(’x’); ylabel(’y’); hold off % make an eps version of the plot called vfield.eps print -depsc vfield % if you dont want the arrow heads then just use % quiver(x,y,dx,dy,’.’); % the arrows are automatically scaled to fit the plot space % if you want bigger or smaller arrows use a scaling % quiver(x,y,dx,dy,3) % makes the arrow 3 times longer % for more help type help quiver

5 4 3 2

y

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

0 x

5

68

Index

4. PHASE PLANE ANALYSIS

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). Let y1 be the number of predators and y2 be the number of prey then the governing differential equations are dy1 = −ay1 + by1 y2 dt dy2 = −cy1 y2 + dy2 dt Here the terms are explained as −ay1 dy2 by1 y2 −cy1 y2

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

Critical points Set derivatives equal to zero and solve for y1 and y2 .

This gives the critical points as (y1 , y2 ) = (0, 0) and (y1 , y2 ) = (d/c, a/b) Calculate the Jacobian and classify each of the critical points. The Jacobian is " ∂ # ∂ ∂ y1 (−ay1 + by1 y2 ) ∂ y2 (−ay1 + by1 y2 ) J(y1 , y2 ) = ∂ ∂ ∂ y1 (−cy1 y2 + dy2 ) ∂ y2 (−cy1 y2 + dy2 ) −a + by2 by1 = −cy2 −cy1 + d So substituting each critical point gives J(0, 0) =

−a 0 0 d

Which is a diagonal matrix so the eigenvalues are λ1 = −a and λ2 = d hence (0, 0) is a saddle. 0 bd/c J(d/c, a/b) = −ac/b 0 √ Calculating the eigenvalues gives λ = ±i ad and so (d/c, a/b) is (probably) a centre. How does the population of both the predator and prey species vary with time?

Index

PREDATOR-PREY MODEL

69

MATLAB MATLAB code % predprey.m % Predator-Prey system % clear all global a b c d % variables global to all functions a=0.1; b=0.002; c=0.0025; d=0.2; y0=[10 50]; % initial point tspan=[0 120]; % range to integrate over [t,y]=ode45(’predpreyf’,tspan,y0); plot(t,y(:,1),’b-’) % plot predator popoluation vs time hold on % hold the plot plot(t,y(:,2),’r--’) % plot prey popoluation vs time legend(’predator’,’prey’) hold off % turn off plot hold print -depsc predprey % send plot to postscript file plot(y(:,1),y(:,2),’g-’) % plot trajectories in (y1,y2)space print -depsc predpreytraj% send plot to postscript file MATLAB code function f=predpreyf(t,y) % predpreyf.m global a b c d f(1)=-a*y(1) + b*y(1)*y(2); f(2)=-c*y(1)*y(2) + d*y(2); f=f(:); return 300

300 predator prey 250

250

200

200

150

150

100

100

50

50

0 0

20

40

60

Time plot

80

100

120

0 0

50

100

150

200

250

A trajectory in (y1 , y2 ) space

300

70

Index

4. PHASE PLANE ANALYSIS

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 r1 y1 (K1 − y1 − ay2 ) = dt K1 r2 dy2 y2 (K2 − y2 − by1 ) = dt K2 If there is no second species (y2 = 0) then the first population grows until it approaches the steady population K1 . Similarly for the other species. Critical points Set derivatives equal to zero and solve for y1 and y2 .

(y1 , y2 ) = (0, 0) (y1 , y2 ) = (K1 , 0) (y1 , y2 ) = (0, K2) and a fourth point is located at the intersection of the two lines K1 − y1 − ay2 = 0 and K2 − y2 − by1 = 0. Look at the Jacobian to classify each critical point. MATLAB code % compjacobian.m % format compact clear all a=0.75; b=3.0; r1=0.2;

r2=0.1;

K1=50;

K2=100;

% store critical points in a vector y1=[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

Index

COMPETITION MODEL

>> compjacobian For critical point ( 0.00 Jac = 0.2000 0 0 0.1000 V = 1 0 0 1 D = 0.2000 0 0 0.1000

0.00)

For critical Jac = -0.2000 0 V = 1.0000 0 D = -0.2000 0

0.00)

point (50.00 -0.1500 -0.0500 -0.7071 0.7071 0 -0.0500

For critical point ( 0.00 100.00) Jac = -0.1000 0 -0.3000 -0.1000 V = 0 0.0000 1.0000 1.0000 D = -0.1000 0 0 -0.1000 For critical Jac = -0.0800 -0.1200 V = -0.6661 -0.7458 D = -0.1472 0 >> diary off

point (20.00 40.00) -0.0600 -0.0400 0.4885 -0.8726 0 0.0272

71

72

Index

4. PHASE PLANE ANALYSIS

How does the population of both the species vary with time? Does one species dominate the other? MATLAB code % competition.m % clear all global a b r1 r2 K1 K2 % variables global to all functions a=0.75; b=3.0; r1=0.2; r2=0.1; K1=50; K2=100; for i=0:10 % loop through various initial points for j=0:1 y0=[j*50+5 i*10+5]; % initial points tspan=[0 200]; % range to integrate over [t,y]=ode45(’competitionf’,tspan,y0); plot(y(:,1),y(:,2)) % plot trajectories in (y1,y2)space hold on end end print -depsc competitiontraj % send plot to postscript file hold off % turn off plot hold MATLAB code function f=competitionf(t,y) % competitionf.m global a b r1 r2 K1 K2 % variables global to all functions f(1)=r1/K1*y(1)*(K1 - y(1) - a*y(2)); f(2)=r2/K2*y(2)*(K2 - y(2) - b*y(1)); f=f(:); return

120

100

80

60

40

20

0 0

10

20

30

40

50

60

Index

SIR DISEASE MODEL

73

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 percentage of the population need to be inoculated to prevent the spread of a disease. Or simply as a guide to how widespread a disease will become if left alone. Consider a fixed population of size K. A fixed population size is a reasonable assumption if the birth rate is approximately the same as the death rate and the disease does not actually kill the person. For instance measles in the developed world is rarely a fatal disease and has no impact on the overall birth or death rate. Consider three distinct classes of people: Susceptibles (S): Infectives (I): Recovered (R):

those people who are susceptible to catching the disease those people who have the disease and can pass it on those people who have had the disease and recovered from it. They are no longer susceptible as they have immunity now.

This is (for obvious reasons) known as an SIR model. The total population is fixed so that S+R+I = K The general birth rate of people (who are all born susceptible) is µ . Because the population is assumed to be constant the death rate must also be µ but any of susceptibles, infectives or recovereds can die. The disease is assumed not to alter the death rate. Consider how the population of susceptibles can change. Susceptibles catching the disease and becoming infective, births of susceptibles, or dying. To catch the disease a susceptible must come in contact with an infective hence the chance of catching the disease is proportional (with constant β ) to the product of the number of susceptibles and infectives. The DE that models the susceptible population is therefore dS = −β SI + µ K − µ S dt

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 the infective population is therefore dI = β SI − γ I − µ I dt

contact

recover

death

74

Index

4. PHASE PLANE ANALYSIS

Consider how the population of recovereds can change. They are infectives who have recovered, or they can die. The DE that models the recovered population is therefore dR = γI − µR dt

recovered

death

Note that the recovered equation does not effect the susceptible or infective equations and in fact since the population is assumed constant the number of recovereds can always be determined from the number of susceptibles and the number of infectives. This last equation is not needed in the model. So we have two first oder nonlinear differential equations in two unknowns (S and I). dS = −β SI + µ K − µ S dt dI = β SI − γ I − µ I dt These are nonlinear equations hence we need to calculate the critical points and analyse the behaviour near those critical points. Critical points Set derivatives equal to zero and solve for S and I. 0 = −β SI + µ K − µ S 0 = β SI − γ I − µ I

Index

SIR DISEASE MODEL

Look at the Jacobian to classify each critical point. " ∂ ∂ S (−β SI + µ K − µ S) J(S, I) = ∂ ∂ S (β SI − γ I − µ I) =

"

−β I − µ

−β S

βI

βS−γ −µ

∂ ∂I

(−β SI + µ K − µ S)

∂ ∂I

(β SI − γ I − µ I)

75

#

#

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

−µ 0

−β K β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 the S → K, I → 0, that is the disease dies out. If β K − γ − µ > 0 then the critical point (S, I) = (K, 0) is a saddle and hence (K, 0) is not the final state. So what is the final state in his case? Presumably it must be the other critical point since the model is bounded. Why is the model bounded? Example Consider the case with K = 100, β = 0.001, µ = 0.02, γ = 0.03

76

4. PHASE PLANE ANALYSIS

Index

MATLAB code % sir.m % this M-file plots velocity vectors for the SIR model, plots % some trajectories, calculates e’values and e’vectors of Jacobian % global beta K mu gamma % makes these global variables format compact beta=0.001; K=100; mu=0.02; gamma=0.03; % % use quiver to plot the vector field a=0; b=100; % size of the grid [S,I]=meshgrid(a:10:b,a:10:b);% a square grid (a to b)x(a to b) dS=-beta*S.*I+mu*K-mu*S; % the DEs dI=beta*S.*I-gamma*I-mu*I; % quiver(S,I,dS,dI,2.5,’r’) % plot vector field scaled by 2.5 axis square % make the plot square axis([a b a b]) % restrict the axes to be a to b hold on w=[a b]; plot(w,0*w,’k-’,0*w,w,’k-’) % adds x and y axes to plot xlabel(’Susceptibles’); ylabel(’Infectives’); title(’SIR model’) % % now add some trajectories by numerically solving the DE tspan=[0 250]; for icS=0:100:100 for icI=5:10:95 initcond=[icS icI]; [t,y]=ode45(’sirfunc’,tspan,initcond); plot(y(:,1),y(:,2),’b-’) end end hold off print -depsc sirtraj % % now set up the critical points and calculate the Jacobian % and the eigenvalues and eigenvectors so you can classify the % critical points cpS=[K (gamma+mu)/beta]; cpI=[0 mu*K/(gamma+mu)-mu/beta]; for i=1:1:2 fprintf(’\nCritical point number %d is (%5.2f,%5.2f) \n’,... i,cpS(i),cpI(i)) Jacobian=[-beta*cpI(i)-mu -beta*cpS(i);... beta*cpI(i) beta*cpS(i)-gamma-mu] [Jeigvec, Jeigval]=eig(Jacobian) end;

MATLAB code function f=sirfunc(t,y) % sirfunc.m % the DEs for the SIR model global beta K mu gamma S=y(1); I=y(2); f(1)=-beta*S.*I+mu*K-mu*S; f(2)=beta*S.*I-gamma*I-mu*I; f=f(:); return

Index

SIR DISEASE MODEL

sir Critical point number 1 is (100.00, 0.00) Jacobian = -0.0200 -0.1000 0 0.0500 Jeigvec = 1.0000 -0.8192 0 0.5735 Jeigval = -0.0200 0 0 0.0500 Critical point number 2 is (50.00,20.00) Jacobian = -0.0400 -0.0500 0.0200 0.0000 Jeigvec = 0.8452 0.8452 -0.3381 - 0.4140i -0.3381 + 0.4140i Jeigval = -0.0200 + 0.0245i 0 0 -0.0200 - 0.0245i diary off

SIR model 100 90 80

Infectives

70 60 50 40 30 20 10 0 0

20

40 60 Susceptibles

80

100

77

78

4. PHASE PLANE ANALYSIS

Index

5 Boundary Value Problems

5.1 Introduction In contrast to Initial Value Problems where all function values and derivatives are given at one point Boundary Value Problems (BVP) have function values and derivatives at two points. Hence it is not possible to construct a Taylor Series about one point and use this to build up the solution as was done with IVPs. Examples y′′ + 3y′ + 2y = x y′′′′ + y′′ + 2y = sin x

where

y(a) = 1

where

y(a) = 1

y(b) = 3

y′ (a) = 3 y′′ (a) = 6

y(b) = 3

BVPs do not necessarily have unique solutions . For example y′′ + ω 2 y = 0

where

y(0) = 0 y(π ) = 0

There are two general methods for solving BVPs numerically, shooting method and relaxation method.

80

Index

5. BOUNDARY VALUE PROBLEMS

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 for y′ (0). Now the problem is an IVP. 2. Integrate forward to x = 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)=3 clear all c=1; % the first guess for y’(0) tspan=[0 2]; while c > -999 % loop through different c values c=input(’enter c, -1000 to stop ’); if c==-1000 break; end % if c=-1000 jump out of loop y0=[1 c]; % initial condition [y(0) y’(0)] [t,y]=ode45(’shootingexamplef’,tspan,y0); plot(t,y(:,1)) % plot current solution hold on % keep the plot lgthy=length(y); % find the length of the vector y disp([y(lgthy,1)]) % show y(x=2) value cc=num2str(c); % convert value of c to a string variable text(2.1,y(lgthy,1),cc) % write value of c on rhs of graph end text(2.1,5,’c guess’) % write c guess on the plot at (2.1,5) hold off print -deps shootingexample MATLAB code function f=shootingexamplef(t,y) % shootingexamplef.m % the DE y’’ + 3y’ + 2y = 2x written as a system f(1)=y(2); f(2)=-3*y(2)-2*y(1)+2*t; f=f(:); return

Index

SHOOTING METHOD

81

6 5.5 5

c guess

4.5 4 3.5

20 18

3

16

2.5 10

2 1.5 1 0

0.5

1

1.5

2

Hence there is a solution that has y(2) = 3 somewhere between y′ (0) = 16 and y′ (0) = 18. How do you go about finding this value of y′ (0) that gives the correct value of y(2) ? Treat the problem as a zero finding problem. That is find the value of y′ (0) = c that results in f (c) = y(2) − 3 = 0. So use one of MATLABs methods for finding zeros of functions.

82

Index

5. BOUNDARY VALUE PROBLEMS

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 input the guess for the initial slope and returns the error in the right hand boundary condition. Then call this function from fzero to find the correct slope that integrates to the correct right hand boundary condition. MATLAB code % shootingexamplemain.m % the main M-file for using the shooting method to solve the % DE defined in shootingexamplef.m % Uses fzero to solve the equation y’(0)=3 where y’(0) is % determined in shootingfunc.m by shooting forward using ode45 % myguess=4; % a guess at the value of y’(0) actualvalue=fzero(’shootingfunc’,myguess) % use ode45 to get correct solution using actualvalue and plot tspan=[0 2]; y0=[1 actualvalue]; [t,y]=ode45(’shootingexamplef’,tspan,y0); plot(t,y(:,1)) print -deps shootingexampleplot MATLAB code function f=shootingfunc(c) % set up as a function, the input (c) is the initial slope % y’(0) and the output (f) is the difference between the value % of y at x=2 and the number 3 (since y(2)=3 for the correct % solution. This function is called by MATLABs fzero % % shootingfunc.m % trying to solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3 % using y(0)=1 and y’(0)=c tspan=[0 2]; y0=[1 c]; [t,y]=ode45(’shootingexamplef’,tspan,y0); f=y(length(y),1)-3; % set function value to y(2)-3 return MATLAB code function f=shootingexamplef(t,y) % shootingexamplef.m % the DE y’’ + 3y’ + 2y = 2x written as a system f(1)=y(2); f(2)=-3*y(2)-2*y(1)+2*t; f=f(:); return

Index

83

SHOOTING METHOD

>> shootingexamplemain actualvalue = 16.9727

5.5 5 4.5 4 3.5 3 2.5 2 1.5 1 0

0.5

1

1.5

2

84

Index

5. BOUNDARY VALUE PROBLEMS

5.3 Relaxation methods Relaxation methods differ substantially from the previous shooting methods. The function is not integrated but rather an approximate solution is made that fits the governing differential equation. This results in a system of equations where the unknowns are the values of the function at the interior points. To do this we need to determine some approximations to the derivatives in any given differential equation.

5.3.1 Finite differences Finite differences is the procedure where we replace any derivatives by a finite difference approximation. For example recall the definition of the derivative dy y(x + ∆x) − y(x) ≈ dx ∆x alternatively this could be written yn+1 − yn dy ≈ dx n xn+1 − xn =

yn+1 − yn h

where yn = y(xn ) and h is the difference in x steps or just the space between x points. This is known as a forward approximation to the first derivative as it uses the current point (n) and the next point (n + 1). There is also a backward approximation that uses the current point (n) and the previous point (n − 1). This is given by dy yn − yn−1 ≈ dx n h Both the forward and backward approximations are first order accurate, that is the first neglected term is order ∆x.

There is in fact a second order accurate approximation known as the central approximation given by dy yn+1 − yn−1 ≈ dx n 2h These can be used to find approximations to the higher derivatives.

yn+1 − 2yn + yn−1 d 2 y ≈ 2 dx n h2

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

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 is simply solving a system of linear equations which is simple in MATLAB. Algorithm 1. Divide up the region into N equal subintervals. Note that the differential equation must be true at each grid point in the region. 2. Replace the derivatives in the differential equation by thier finite difference approximations at each grid point in the region. 3. If possible use the boundary conditions to replace some of the values. This is usually only possible for the equations at each end of the interval. 4. Rearrange the equations so that they are written as a matrix system of equations. The unknowns are the value of the solution at each grid point. 5. Solve the matrix system of equations. This then gives the solution at each grid point. Example Numerically solve y′′ + 3y′ + 2y = 2x

where

y(0) = 1

y(2) = 3

1. First divide up the region into N subintervals of width h where h=

b−a N

so xn = a + hn for n = 0 . . . N 2. Replace each derivative by its approximate value at each interior point. yn+1 − yn−1 yn+1 − 2yn + yn−1 +3 + 2yn ≈ 2xn 2 h 2h

for n = 1 . . . N − 1

This results in N − 1 equations in the N − 1 unknowns y1 , y2 , . . . yN−1 . 3. The values at the edge points are known (y0 = y(a) = 1, yN = y(b) = 3 in this case) The equations for n = 1 and n = N − 1 are slightly different to the others (n = 2, . . . N − 2) as they involve y0 and YN respectively which are known as they are just the boundary values. y2 − 2y1 + y0 y2 − y0 +3 + 2y1 ≈ 2x1 2 h 2h

for

n=1

yn+1 − yn−1 yn+1 − 2yn + yn−1 +3 + 2yn ≈ 2xn 2 h 2h

for

n = 2 . . .N − 2

yN − 2yN−1 + yN−2 yN − yN−2 +3 + 2yN−1 ≈ 2xN−1 2 h 2h

for n = N − 1

86

Index

5. BOUNDARY VALUE PROBLEMS

4. Mutiply by h2 to simplify, collect all terms with the same unknown value and written out in full we have (1 − 3h/2)y0 + (2h2 − 2)y1 + (1 + 3h/2)y2 = 2h2 x1 (1 − 3h/2)y1 + (2h2 − 2)y2 + (1 + 3h/2)y3 = 2h2 x2 (1 − 3h/2)y2 + (2h2 − 2)y3 + (1 + 3h/2)y4 = 2h2 x3 .. .

.. .

.. .

(1 − 3h/2)yN−3 + (2h2 − 2)yN−2 + (1 + 3h/2)yN−1 = 2h2 xN−2 (1 − 3h/2)yN−2 + (2h2 − 2)yN−1 + (1 + 3h/2)yN = 2h2 xN−1 Now y0 and yN are known (since they are the boundary conditions) so put them on the right hand side with all the other known parts gives (2h2 − 2)y1 + (1 + 3h/2)y2 = 2h2 x1 − (1 − 3h/2)y0 (1 − 3h/2)yn−1 + (2h2 − 2)yn + (1 + 3h/2)yn+1 = 2h2 xn

for

n = 2...N − 2

(1 − 3h/2)yN−2 + (2h2 − 2)yN−1 = 2h2 xN−1 − (1 + 3h/2)yN 5. This is a system of N − 1 equations in the N − 1 unknowns y1 , y2 , . . . yN−1 . Substituting the known end points (y0 = 1 and yN = 3) and writing in matrix form this can be easily solved in MATLAB using any number of solution methods depending on the size of the matrix (direct inversion, iterations etc.). In matrix form it can be written AY = b where the matrix A has coefficients as above, Y is the vectors of unknowns Y = (y1 , y2 , . . . , yN−1 )T and b is the known right hand side vector.

Index

RELAXATION METHODS

2h2 − 2

1 − 3h/2 0 0 A= .. . 0 0

1 − 3h/2

0

0

0

...

2h2 − 2

1 + 3h/2

0

0

...

1 − 3h/2

2h2 − 2

1 + 3h/2

0

...

0

1 − 3h/2

2h2 − 2

1 + 3h/2

...

.. .

.. .

.. .

.. .

.. .

...

0

0

1 − 3h/2

2h2 − 2

...

0

0

0

1 − 3h/2

b=

2h2 x1 − (1 − 3h/2)y0 2h2 x2 2h2 x3 2h2 x4 .. . 2h2 xN−2 2h2 xN−1 − (1 + 3h/2)yN

87

0

0 0 .. . 1 + 3h/2 2h2 − 2 0

What do you notice about the matrix A? You will see that it is ’tri-diagonal’ (only 3 diagonals are non-zero) and each diagonal has the same value. This makes it particularly easy to program in MATLAB using the diag command. Once you have coded A, and b it is a simple matter to solve the system AY = b to get the solution Y . This is then the approximation to the solution of the original ODE. Coding the diagonal matrix A is relatively easy in MATLAB as there is a command diag that is used to enter values into a diagonal matrix. The ones command is also useful for making vectors of a certain length with the same value in each position. Solving the system AY = b is easy using the ’backslash divide’ command y=A\b .

88

5. BOUNDARY VALUE PROBLEMS

Index

>> help diag DIAG Diagonal matrices and diagonals of a matrix. DIAG(V,K) when V is a vector with N components is a square matrix of order N+ABS(K) with the elements of V on the K-th diagonal. K = 0 is the main diagonal, K > 0 is above the main diagonal and K < 0 is below the main diagonal. DIAG(V) is the same as DIAG(V,0) and puts V on the main diagonal. DIAG(X,K) when X is a matrix is a column vector formed from the elements of the K-th diagonal of X. DIAG(X) is the main diagonal of X. DIAG(DIAG(X)) is a diagonal matrix. Example m = 5; diag(-m:m) + diag(ones(2*m,1),1) + diag(ones(2*m,1),-1) produces a tridiagonal matrix of order 2*m+1. See also SPDIAGS, TRIU, TRIL. Overloaded methods help 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 an M-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

Index

89

RELAXATION METHODS

MATLAB code % fdexample.m % % solve y’’ + 3y’ + 2y = 2x y(0)=1 y(2)=3 % using finite differences a=0; b=2; % endpoints y0=1; yN=3; % y values at endpoints N=100; % number of points h=(b-a)/N; % x step size x=a+h:h:b-h; % set up vector of x points (interior points) % set up the matrix and solve Ay=RHS for y A=(2*hˆ2-2)*diag(ones(1,N-1)); % main diagonal elements A=A+(1-3*h/2)*diag(ones(1,N-2),-1); % one below main diagonal A=A+(1+3*h/2)*diag(ones(1,N-2),1); % one above main diagonal RHS=2*hˆ2*x; RHS(1)=RHS(1)-(1-3*h/2)*y0; RHS(N-1)=RHS(N-1)-(1+3*h/2)*yN; y=A\RHS’; plot(x,y)

5.5 5 4.5 4 3.5 3 2.5 2 1.5 1 0

0.5

1

1.5

2

90

Index

5. BOUNDARY VALUE PROBLEMS

Example Solve y′′ + 5y′ + 6y = cos x using finite differences.

where

y(0) = 2

y(3) = 6

Index

91

RELAXATION METHODS

MATLAB code % fdexample2.m % % solve y’’ + 5y’ + 6y = cos x y(0)=2 y(3)=6 % using finite differences a=0; b=3; % endpoints ya=2; yb=6; % y values at endpoints N=100; % number of points h=(b-a)/N; % x step size x=a+h:h:b-h; % set up vector of x points (interior points) % set up the matrix and solve Ay=RHS for y A=(6*hˆ2-2)*diag(ones(1,N-1)); % main diagonal elements A=A+(1-5*h/2)*diag(ones(1,N-2),-1); % one below main diagonal A=A+(1+5*h/2)*diag(ones(1,N-2),1); % one above main diagonal RHS=hˆ2*cos(x); RHS(1)=RHS(1)-(1-5*h/2)*ya; RHS(N-1)=RHS(N-1)-(1+5*h/2)*yb; y=A\RHS’; plot(x,y)

N=5

N = 10

160

400

140

350

120

300

100

250 80

200 60

150

40 20

100

0

50

−20 0.5

1

1.5

2

2.5

0 0

0.5

1

N = 20 400

350

350

300

300

250

250

200

200

150

150

100

100

50

50 0.5

1

1.5

2

2.5

3

2

2.5

3

N = 100

400

0 0

1.5

2

2.5

3

0 0

0.5

1

1.5

92

5. BOUNDARY VALUE PROBLEMS

Index

5.3.3 Comments on solving matrix equations There are many numerical problems with solving the matrix equation Ax = b The matrix equations that result from the finite difference method are tridiagonal. That is they have entries down the 3 central diagonals and zeros elsewhere. These are relatively efficient to solve Ax = b for. Suggested Solution Methods 1. Inverse This can be very slow for large matrices. What if inverse doesn’t exist? 2. Iteration Make a guess and use the equations to refine that guess until some convergence criteria is met. Usually the best method for large matrices (bigger than 25 × 25). Most common method is known as the Gauss-Seidel Iteration method 3. Row reduction then back-substitution

Index

RELAXATION METHODS

93

5.3.4 Finite elements Finite differences are only one of the many method of solving BVPs. Another very popular method is known as the finite element method. The concept underlying finite difference methods is that approximations are made to the derivatives in the differential equation. In contrast to this for finite element methods the solution is approximated by a sequence of model functions and the error in using these functions is minimized. For finite element methods it is more convenient to work on a BVP that has homogeneous boundary conditions. That is the value of the function at the end points is zero. Fortunately we can make a change of variable to ensure this. For example consider the problem y′′ + p(x)y′ + q(x) = r(x)

y(xL ) = yL

y(xR ) = yR

we can transform this to a problem with homogeneous boundary conditions by the transformation y(x) = Y (x) + ax + b Which can be shown to give a=

1 (yL − yR ) xL − xR

b=

1 (−xR yL + xL yR ) xL − xR

Example What does y′′ + 3y′ + 2y = 5x2

y(0) = 2

y(2) = 3

become when it is transformed to have homogeneous boundary conditions

94

Index

5. BOUNDARY VALUE PROBLEMS

5.3.5 Symbolic form of a BVP Consider an ordinary differential equation with homogeneous boundary conditions this can be written in sysmbolic form as L y = r(x)

y(xL ) = 0 y(xR ) = 0

where L is a sysmbol that represents all the differential operators in the differential equation. Example The ODE y′′ + 3x2 y′′ + 2ex y = 5x2 can be represented as 2

L y = 5x

where

d2 d L ≡ 2 + 3x2 + 2ex dx dx

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 of n independent expansion functions. Call these φi (x) for i = 1, . . . , n. The exact form of these functions will be specified later. The crux of the finite element method is that the approximate solution (denoted Y (x)) is written as some linear combination of these expansion functions, namely n

y(x) ≈ Y (x) = ∑ ci φi (x) i=1

The aim is to find the coefficients ci to get the best solution possible. 2. Construct what is known as a residual function (denoted ∆(x)) which is a measure of the error of the approximate solution ∆(x) = L Y (x) − r(x) n

= L

∑ ciφi(x) − r(x)

i=1 n

=

∑ ciL φi(x) − r(x)

i=1

Index

RELAXATION METHODS

95

3. Establish a set of conditions that allow us to find the coefficients ci . To do this choose a set of weight functions wi (x) for i = 1, . . . , n. Again these are as yet unspecified. These weight functions have the following properties (a) They are normalised so that Z xR

w j (x)dx = 1

xL

j = 1, . . . , n

for

(b) The weighted residual for each fucntion is zero Z 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 coefficients ci to be found. By substituting the definition of ∆(x) above into this expression we get for each j = 1, . . ., n Z xR xL

Z xR xL

Z xR xL

w j (x)

"

n

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

∑ ciL φi(x) − r(x)

i=1

n

w j (x) ∑ ci L φi (x)dx − i=1

n

∑ ci

i=1

Z xR xL

Z xR xL

dx = 0

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

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

Z xR xL

w j (x)r(x)dx

If the weight functions (w j (x)) and the expansion functions (φ j (x)) are specified then the right hand side of the last expression is known. bj = and the part

Z xR xL

Z xR xL

w j (x)r(x)dx

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

is also known. Hence the equation reduces to n

∑ ci M ji = b j

for

j = 1, . . . , n

i=1

This is just the matrix equation Mc = b So provided w j (x) and φ j (x) are specified then the coefficients to use as the linear combination of the expansion functions to give the approximate solution are given by solving Mc = b. This gives the ci in n

y(x) ≈ Y (x) = ∑ ci φi (x) i=1

and so the approximate solution has been found.

96

Index

5. BOUNDARY VALUE PROBLEMS

Choice of weight and expansion functions There are many choices that can be made for the weight functions (w j (x)) and the expansion functions (φ j (x)). Different choices give different types of methods. By far the most common and what has come to be known as the collocation method is to choose them equal, namely w j (x) = φ j (x). The finite element method uses “triangular hat” functions for both the weight functions (wi (x)) and the expansion functions (φi (x)).

φi (x) =

1 h (x − xi−1 )

for

xi−1 < x < xi

1 h (xi+1 − x)

for

xi < x < xi+1

0

otherwise

These functions are triangles that span 3 grid points and are zero everwhere else. For example φ3 is a triangle that begins at zero at x2 increases to a maximum at x3 and then decreases to zero at x4 .

φ3

x0

x1

x2

x3

x4

x5

x6

The benefit of these functions is that they are local. That it is only nonzero in the immediate neighbourhood of the central point. This guarantees that the resulting matrix eqaution will be sparse (have many zero entries) and hence can be solved effeciently using iterative methods. These triangular hat functions are useful as they have some special properties that make calculating the M matrix easier. F(x)

φi φi±1 ′ φi±1

φi′′ ′′ φi±1 1 x x2 x3

Z xR xL

φi F(x) dx

2h/3 h/6 ±1/2 −2/h 1/h h ih2 (i2 + 1/6)h3 i(i2 + 1/2)h4

(zero for all other indices) (zero for all other indices) (zero for all other indices)

Index

97

RELAXATION METHODS

5.3.7 Finite element example Solve y′′ + y = 1 Now L ≡

d2 dx2

y(0) = 0

y(π /2) = 0

+ 1 so to calculate M ji break it down into each term such that Z π /2

φ j (x)L φi (x)dx 2 Z π /2 d = + 1 φi (x)dx φ j (x) dx2 0 Z π /2 Z π /2 d 2 φi (x) φ j (x) φ j (x)φi (x)dx = dx + dx2 0 0

M ji =

0

and so from the table of special properties of the triangular functions −2/h 1/h 0 ... 0 0 2h/3 h/6 0 1/h −2/h 1/h . . . 0 0 h/6 2h/3 h/6 .. . . . .. + .. .. .. . . . .. . M= . . . . . . . . 0 0 0 . . . −2/h 1/h 0 0 0 0 0 0 . . . 1/h −2/h 0 0 0 Calculating the right hand side gives bj = =

Z π /2 0

Z π /2 0

and so from the tables

Now find the coefficients ci in

w j (x)r(x)dx

φ j (x) 1 dx

h h h .. .

b= h h

n

y(x) ≈ Y (x) = ∑ ci φi (x) i=1

by solving the matrix equation Mc = b with M and b above.

... ... .. .

0 0 .. .

0 0 .. .

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

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 all global a b N a=0; b=pi/2; N=10; % N interior points h=(b-a)/(N+1); xk=a+h:h:b-h; % interior points % set up and solve Mc=RHS M=(-2+2/3*hˆ2)*diag(ones(1,N)); % main diagonal elements M=M+(1+hˆ2/6)*diag(ones(1,N-1),-1); % one below main diagonal M=M+(1+hˆ2/6)*diag(ones(1,N-1),1); % one above main diagonal M=M/h; % multiplicative factor RHS=h*ones(N,1); % right hand side vector c=M\RHS % solve for c the coefficients % plot numerical solution and exact solution and calculate error npts=26; x=a:(b-a)/(npts-1):b; % vector to calculate solution at % call feexamplef.m which is a function that calculates the solution % at a given point i.e. it works out y(x)=sum(c(i)*phi_i(x)) % where the phi_i are the ’triangular hat’ functions for k=1:npts y(k)=feval(’feexamplef’,c,x(k)); end plot(x,y) hold on exact=-sin(x)-cos(x)+1; plot(x,exact,’ro’) ylabel(’exact and numerical’) error=abs(exact-y); maxerror=max(error) hold off

Index

99

RELAXATION METHODS

MATLAB code

function f=feexamplef(c,x) % feexamplef.m % function that calculates y(x)=sum(c(i)*phi_i(x)) % where the phi_i are the ’triangular hat’ functions centered around x % global a b N h=(b-a)/(N+1); xk=a+h:h:b-h; if (x <= xk(1)) % left most function f=c(1)*(x-a)/h; end for i=1:N-1 % all interior functions that have left and right parts if (x >= xk(i)) & (x <= xk(i+1)) right=c(i)*(xk(i+1)-x)/h; left =c(i+1)*(x-xk(i))/h; f=left+right; end end if (x >= xk(N)) % right most function f=c(N)*(b-x)/h; end return

0

−0.05

−0.1

−0.15

−0.2

−0.25

−0.3

−0.35

−0.4

−0.45

0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

100

Index

5. BOUNDARY VALUE PROBLEMS

5.3.8 Finite element summary For the BVP L y = r(x)

y(xL ) = 0 y(xR ) = 0

1. Choose n expansion functions φi (x) for i = 1, 2, . . ., n. 2. Choose n weight functions wi (x) for i = 1, 2, . . ., n. 3. Let wi (x) = φi (x) =

n

1 h (x − xi−1 )

for xi−1 < x < xi

1 h (xi+1 − x)

for xi < x < xi+1

0

otherwise

4. Numerical solution Y (x) = ∑ ci φi (x) i=1

5. Solve Mc = b for the ci ’s where M ji =

Z xf

bj =

x0

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

Z xf x0

w j (x)r(x) dx

6. Properties of triangular functions F(x)

φi φi±1 ′ φi±1

φi′′ ′′ φi±1 1 x x2 x3

Z xf x0

φi F(x) dx

2h/3 h/6 ±1/2 −2/h 1/h h ih2 (i2 + 1/6)h3 i(i2 + 1/2)h4

(zero for all other indices) (zero for all other indices) (zero for all other indices)

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

101

5.4 Differential eigenvalue equations Recall the example y′′ + ω 2 y = 0

where

y(0) = 0 y(π ) = 0

which has solution many solutions each one of which is yn = An sin ωn x where An is an unknown constant and ωn = n = 0, ±1, ±2, . . . This equation is known as a Differential Eigenvalue Equation as there are only particular values (eigenvalues) for which a solution exists. How would you solve this numerically given you don’t know the eigenvalues ω ? Treat the eigenvalues as one of the unknowns of the problem. But how ? We know that the eigenvalue is a constant so we can write a differential equation for it as dω =0 dx but then we also need an extra boundary condition.

ω (0) = ω0 where ω0 is the eigenvalue we need to find. So putting y = y1 , dy/dx = y2 and ω = y3 the system to date is dy1 = y2 dx dy2 = −(y3 )2 y1 dx dy3 = 0 dx subject to y1 (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 is unknown (ω0 ) and one of them is at x = π not x = 0. To be able to integrate forward from x = 0 we need three initial conditions at x = 0 and we also need three known conditions in total for the problem to have a unique solution.

102

5. BOUNDARY VALUE PROBLEMS

Index

To use a shooting method you need 3 boundary conditions at the same point so introduce a new boundary condition y2 (0) = c where c is a value that has to be determined to satisfy the boundary conditions at the other end and ω0 is a value that also has to be determined. But there is still not enough information to find the solution as there are only 2 known boundary conditions (y1 (0) = 0 y1 (π ) = 0) so we must introduce another boundary condition that is considered to be known. Looking at the analytic solutions that were found earlier yn = An sin ωn x note that they are only defined to within some arbitrary constant (An ). To specify a specific solution another condition would also have to be used. This condition is known as the normalising condition and consists of choosing some given value for one of the unknown values. So in fact we are free to choose the value of y2 (0) = c to be any (non zero) value as this just fixes the unknown constant. The system of equations is now dy1 = y2 dx dy2 = −(y3 )2 y1 dx dy3 = 0 dx subject to 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 at x = 0 that has to be determined to satisfy the boundary conditions at x = π . So choose a value for ω0 integrate forward to x = π and check the value of y1 (π ) if it is equal to zero it is the correct ω0 if it is not zero then change the ω0 guess. This can be done using the MATLAB command fzero.

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

103

MATLAB code function f=deig(omega0) % deig.m % % solving y’’ + omegaˆ2 y = 0 y(0)=0 y(pi)=0 % This function takes as input the value of omega0 and returns % the value at y(pi). If this equals zero then that value of % omega0 gave a valid solution to the ODE. % Note the system of equations becomes 3rd order! % This function is called by fzero to find the value of omega0 % that gives the correct solution. % format compact tspan=[0 pi]; y0=[0 1 omega0]; % y_1(0)=0, y_2(0)=1, y_3(0)=omega0 [t,y]=ode45(’deigf’,tspan,y0); plot(t,y(:,1)); i=length(y); % determine length of solution vector f=y(i,1); % set function value = y_1(pi) return MATLAB code function f=deigf(t,y) % deigf.m f(1)=y(2); f(2)=-y(3).ˆ2.*y(1); f(3)=0; f=f(:); % forces f to be a column vector return

104

Index

5. BOUNDARY VALUE PROBLEMS

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

1.2

1

0.8

0.6

0.4

0.2

0 0

0.5

1

1.5

2

2.5

3

3.5

2.5

3

3.5

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

0.5 0.4 0.3 0.2 0.1 0 −0.1 −0.2 −0.3 −0.4 −0.5 0

0.5

1

1.5

2

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

105

Example Consider the differential equation √ dz = 3 z − αt2 dt

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 a particular value (or values) that make the equation satisfied. How do you find that particular value?

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

MATLAB code function f=findalpha(alphaguess) % findalpha.f % this function takes as input a guess for the constant % alpha and returns y(1)-4 % if this is close to zero then its a good guess y0=[2 alphaguess]; % values at t=0 tspan=[0 1]; % integrate equation over t=(0,1) [t,y]=ode45(’alphaf’,tspan,y0); lgthy=length(y); % determine how long the vector is % f is a measure of how good the solution matches the desired % boundary condition which is y(1)=4 % if f is close to zero then this is a good solution. currentguess=alphaguess ; f=y(lgthy,1)-4; sprintf(’Current guess for alpha =%10.8f y(1)-4 = %10.8f’... ,currentguess,f) return ;

MATLAB code function f=alphaf(t,y) % alphaf.m the governing DEs the alpha % eigenvalue problem % % y(1) is z, y(2) is alpha % z=y(1); alpha=y(2); % set up the functions f(1)=3*sqrt(z)-alpha*t.ˆ2; % the DE f(2)=0; % since alpha is a constant f=f(:); % force f to be a column vector return ;

Index

DIFFERENTIAL EIGENVALUE EQUATIONS

alpha initialguess = 11 ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha ans = Current guess for alpha correctalpha = 11.38377327554915 diary off

=11.00000000

y(1)-4 = 0.15451914

=10.68887302

y(1)-4 = 0.27958057

=11.31112698

y(1)-4 = 0.02927189

=10.56000000

y(1)-4 = 0.33132966

=11.44000000

y(1)-4 = -0.02266307

=11.38366126

y(1)-4 = 0.00004514

=11.38377326

y(1)-4 = 0.00000001

=11.38377328

y(1)-4 = 0.00000000

=11.38377328

y(1)-4 = 0.00000000

5

4.5

4

3.5

3

2.5

2 0

0.2

107

0.4

0.6

0.8

1

108

5. BOUNDARY VALUE PROBLEMS

Index

6 Partial Differential Equations (PDEs)

6.1 Overview Partial differential equations arise in a lot of applications. In this introductory section, I will try to give you a bit of a feeling for the diversity of some of these applications. First, however, I will define exactly what a partial differential equation (PDE) is and define what is 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 than one variable. For example, the temperature in a room will normally be a function of both time and the position in the room — four different variables t, x, y and z. To describe rates of change with respect to each of these variables we need partial derivatives.

6.2.2 Examples of PDEs A partial differential equation is a differential equation with partial derivatives. Some examples are

∂ u ∂ 2u = 2, ∂t ∂x

∂f = ∂x

∂f ∂y

2

,

∂c ∂c ∂c + + = c. ∂x ∂y ∂z

(6.1)

In the first example the dependent variable u is a function of the two independent variables x and t. In the second example the dependent variable f is a function of x and y and in the third example the dependent variable c is a function of x, y, and z.

110

Index

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

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 which variable. In the above examples, the order of the 1st PDE in equation (6.1) is 2, the order of the 2nd and 3rd PDEs in equation (6.1) are both 1.

6.3.2 Linear PDEs A PDE is said to be linear if it is a linear combination of the dependent variable and its various partial derivatives. For example,

∂ u ∂ 2u = 2, ∂t ∂x

∂f ∂f ∂f +x =z , ∂x ∂y ∂z

∂ 4c ∂ 2c + +c = 0 ∂ x4 ∂ x2

(6.2)

∂ 4c + c2 = 0 ∂ x4

(6.3)

are linear, whereas

∂u = ∂t

∂u ∂x

2

,

∂f = ef , ∂x

are not.

6.3.3 Why use numerical methods For analytic methods we will be concentrating on linear PDEs. In general nonlinear PDEs are too difficult to solve analytically with notable exceptions. One reason for this is that linear PDEs allow superposition. This means that if we have any two solutions of a PDE then their sum is also a solution. This is a useful way of building up the entire solution of a linear PDE analytically. The method of superposition (adding solutions) does not work for nonlinear PDEs. Numerical methods aren’t restricted to solving just linear problems. This is the entire point of studying numerical solutions to these types of problems in that they can not be solved by any other means. The analytic methods used for linear PDEs often provide a useful check on the numerical methods in particular circumstances. This is important if you want to have any confidence in your numerical solutions. Broadly speaking there are three types of PDEs. The type of numerical solution method is different for each type. What we will do is study an example of each type.

Index

THREE COMMON PDES IN APPLICATIONS

111

6.4 Three common PDEs in applications Three of the most common PDEs in applications are the wave equation, the diffusion equation and Laplace’s equation. These equations are defined as follows:

1-D Diffusion equation:

∂F ∂ 2F =D 2 ∂t ∂x

2-D Laplace’s equation:

∂ 2F ∂ 2F + =0 ∂ x2 ∂ y2

1-D Wave equation:

2 ∂ 2F 2∂ F =c ∂ t2 ∂ x2

112

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.5 The diffusion equation The diffusion equation in one dimension is

∂F ∂ 2F =D 2 ∂t ∂x

(6.4)

where D is a positive constant and is called the diffusion coefficient. This partial differential equation occurs in studies of heat conduction and diffusion of mass (eg. pollutants or chemicals).

6.5.1 Heat conduction In heat conduction the dependent variable F(x,t) is the temperature inside a material, where heat is conducted in only one direction. Solving this PDE gives the temperature at any point and at any time. The derivation of the diffusion equation when related to heat uses the principle of conservation of heat energy. The RHS of (6.4) physically comes from the net conduction of heat through the material, whereas the LHS comes from the heat being used to raise the temperature of the material. The diffusion equation when related to heat is sometmes known as the heat equation.

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 a pollution spreading out in 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 or three dimensions. For heat conduction, we again use conservation of heat energy. In two and three dimensions, the diffusion equations are 2 2 ∂F ∂ F ∂ 2F ∂F ∂ F ∂ 2F ∂ 2F (6.5) =D + 2 , =D + 2 + 2 . ∂t ∂ x2 ∂y ∂t ∂ x2 ∂y ∂z respectively. We can also write these as

∂F = D∇2 F. ∂t

Index

LAPLACE’S EQUATION

113

6.6 Laplace’s equation 6.6.1 Equilibrium solutions Laplace’s equation, in two dimensions, is

∂ 2F ∂ 2F + 2 =0 ∂ x2 ∂y

(6.6)

This equation arises in many different applications. One common application is in 2D or 3D heat conduction problems, where we assume the temperature is in thermal equilibrium. In thermal equilibrium, there is no time dependence on the temperature. Setting the time derivative in (6.5) to zero, we thus obtain (6.6).

6.6.2 Other applications Laplace’s equation also arises in electrostatics (where the electrical potential, measured in Volts, satisfies Laplace’s equation) and in fluid mechanics (where the velocity potential satisfies Laplace’s equation). Laplace’s equation also arises in ground-water flow examples. More generally, we write Lapalce’s equation as ∇2 F = 0.

114

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.7 The wave equation The wave equation in one dimension is 2 ∂ 2F 2∂ F = c ∂ t2 ∂ x2

(6.7)

where c is a constant. This partial differential equation describes small amplitude wave propagation for a number of applications. Here the constant c is the speed of a wave in a given medium.

6.7.1 Sonar, radar, seismic waves The wave equation arises in sonar, electromagnetic waves, radar, and seismic prospecting for example.

6.7.2 2D and 3D waves In two and three dimensions, the wave equations are 2 2 ∂ 2F ∂ 2F ∂ 2F ∂ 2F ∂ 2F 2 ∂ F 2 ∂ F =c + 2 , =c + 2 + 2 ∂ t2 ∂ x2 ∂y ∂ t2 ∂ x2 ∂y ∂z respectively. More generally, we write

∂ 2F = c2 ∇2 F. ∂ t2

(6.8)

Index

POISSON’S EQUATION

115

6.8 Poisson’s equation A forced Laplace’s equation is known as a Poisson Equation. ∇2 F = 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 equations can also be considered in other co-ordinate systems such as cylindrical (r, θ , z) or spherical (ρ , φ , θ ). For example Laplace’s equation in cylindrical co-ordinates is

∂ 2F 1 ∂ F 1 ∂ 2F ∂ 2F + ∇ F= 2 + + ∂r r ∂ r r 2 ∂ θ 2 ∂ z2 2

and in spherical co-ordinates is

∂ 2F 2 ∂ F ∂ 2F 1 1 ∂ 2 F cot θ ∂ F + + + 2 + ∇ F= ∂ ρ 2 ρ ∂ ρ ρ 2 sin2 θ ∂ φ 2 ρ 2 ∂ θ 2 ρ ∂θ 2

Sometimes this can make the solution process (both analytic and numerical) easier. Can you think of an example?

116

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.10 Boundary and initial conditions 6.10.1 ICs and BCs Partial differential equations require additional conditions to obtain unique solutions. Conditions on time are called initial conditions, usually at t = 0. Conditions on spatial variables are called boundary conditions. Boundary conditions refer to the endpoints of 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 the respective derivatives in the PDE. The diffusion equation (6.4) requires one initial condition since its time derivative is of order one. It requires two boundary conditions, since the x-derivative is second-order. For the wave equation (6.7) we need to specify two initial conditions (since there is a second-order derivative with respect to t) and two boundary conditions. Laplace’s equation (6.6) requires four boundary conditions, two for the xvariable and two for the y-variable.

6.10.3 A typical initial condition A typical initial condition is f (x, 0) = 100. This states the initial value of f is a constant value 100.

6.10.4 Typical boundary conditions Typical boundary conditions include F(0,t) = 30,

∂F (10,t) = 100. ∂x

The first condition specifies the value of F, at the point x = 0, and the second specifies the value of the derivative of f at the point x = 10, for all time.

Index

TYPES OF BOUNDARY CONDITIONS

117

6.11 Types of boundary conditions There are 3 types of Boundary Conditions. The type of boundary condition has an impact on which numerical solution method you can use for a problem hence it is important to be able to classify the boundary conditions. Dirichlet The values of the function are given at the boundary. For example F(0,t) = 30

Neumann The value of the normal derivative is given at the boundary. For example ∂∂Fx (10,t) = 100 which can also be written ∂∂Fx = 100 at x = 10

118

Index

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Cauchy Values of both the function and its normal derivative are given at the boundary. For example ∂∂Fx = F at x = 10

Classify (with reason) each boundary condition of the following PDEs as Dirichlet, Neumann or Cauchy. 2

(a)

d2T dy2

+ ddxT2 = 4 with T (0, y) = 0 T (x, 0) = 2

(b)

dT dt

=

d2 T dx2

with T (0,t) = 0

(c)

dT dt

=

d2T dx2

with T (0,t) = sint

dT dx (L,t) =

T (L, y) = sin y

dT dy (x, M) =

0 T (x, 0) = f (x)

dT dx (L,t) =

T − Ta

T (x, 0) = f (x)

0

Index

DIFFERENT SOLUTION APPROACHES

119

6.12 Different solution approaches Partial differential equations (PDEs) can be solved analytically or numerically. It is important to realize that not all PDEs have solutions. Numerical techniques can be used on a wider range of PDEs but analytical techniques sometimes give more information or can be 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 more ordinary differential equations. The approach we will explore in this course is separation of variables. This involves assuming the dependent variable as a product of functions of each independent variable.

6.12.2 Other approaches We only have time in this course to learn the above method for solving PDEs. But you should at least be aware that other techniques exist. One technique is to use Laplace transforms. The Laplace transform of an ordinary differential equation reduces the differential equation to an algebraic equation. Applying Laplace transforms to a PDE (which is a function of two variables) reduces the PDE to an ODE which is a function of one variable. One then solves the ODE and then inverts the Laplace transform. Another method for solving PDEs involves finding transformations of variables. These are called similarity solutions.

6.12.3 Numerical techniques Most practicing engineers or mathematicians would use numerical techniques for solving PDEs. These are often customised to a particular class of applications. The two most common numerical techniques for solving PDEs are finite differences and finite elements. In finite differences, the partial derivatives are approximated with derivative quotients, evaluated on a rectangular grid. In finite elements, the region is divided up into small panels, or elements, and the quantity you are solving for is expressed as a basis of simple functions defines over just the finite element. Both finite difference and finite element methods lead to matrix equations where the matrices can be large sparse matrices.

6.12.4 Value of analytic solutions While numerical techniques are often used for practical problems, knowledge of the basic analytical solutions, such as those obtained in this course, can provide valuable incite into the problems and may also provide a simple check on the accuracy of the numerical program in particular circumstances. It is always a wise idea to check numerical methods on a solution with a known analytic solution to try and guarantee that the method and the coding is correct.

120

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.13 Examples Heat in a rod Consider an insulated rod of steel of length L were one end is held at 100◦ C and the other can lose heat according to Newton’s Law of Cooling (heat loss is proportional to the difference in the temperature of the rod and the surrounding ambient air). The ambient air is at 25◦ C. Write down the governing equation, the initial and boundary conditions.

Index

EXAMPLES

121

Steady state Temperature in a slab Consider a slab of wood of size 10cm (wide) by 5cm (long) by 2cm (high) that has reached thermal equilibrium. If the bottom of the slab is insulated, the top loses heat by Newton’s Law of Cooling, the sides are held at 20◦ C, write down the governing equation and boundary conditions.

122

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

Plucked String Consider a string of length L that is held fixed at both ends. The string is pulled out to a triangular shape and then released. Write down the governing equation, boundary and initial conditions.

Index

EXAMPLES

123

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

124

6. PARTIAL DIFFERENTIAL EQUATIONS (PDES)

Index

6.14 Classification of PDEs The numerical solution of the 3 standard types of linear PDEs (Diffusion, Laplaces and Wave equations) differs for each type of equation. In fact these PDEs are examples of more general classes of PDEs known as Parabolic, Elliptic and Hyperbolic PDEs. The numerical methods for solving PDEs differ depending on which type of PDE you are solving. Consider a general PDE that depends on 2 variables (a and b).

A

∂ 2u ∂ 2u ∂ 2u ∂u ∂u + 2B +C = F(a, b, u, , ) 2 2 ∂a ∂ a∂ b ∂b ∂a ∂b

where A, B and C are constants and F(a, b, u, ∂∂ ua , ∂∂ ub ) is an arbitrary function of those variables. The classification of a PDE (and hence the type of numerical method used to solve it) depends on the the quantity B2 − AC. B2 − AC = 0 B2 − AC < 0 B2 − AC > 0

Parabolic Elliptic Hyperbolic

eg Diffusion equation eg Laplaces equation eg Wave equation

One Dimensional Diffusion equation Has A = D, B = 0, C = 0, a = x (the space variable), b = t (the time variable) and the function F = ∂∂ut .

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

One Dimensional Wave equation , a = x (the space variable), b = t (the time variable) and F = 0. Has A = 1, B = 0, C = −1 c2

7 Parabolic Equations: Diffusion Equation

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

7.1.1 First Derivative Recall the finite difference approximations First derivative Forward Approximation dy yn+1 − yn ≈ dx n xn+1 − xn =

yn+1 − yn ∆x

where yn = y(xn ) and ∆x is the difference in x steps or just the space between x points. First derivative Backward Approximation dy yn − yn−1 ≈ dx n ∆x

Both the forward and backward approximations are first order accurate, that is the first neglected term is order ∆x.

First derivative Central Approximation given by yn+1 − yn−1 dy ≈ dx n 2∆x

7.1.2 Second Derivative Second derivative Central Approximation given by yn+1 − 2yn + yn−1 d 2 y ≈ 2 dx n (∆x)2

126

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Index

7.2 FTCS method

Consider the diffusion equation in one space dimension

∂F ∂ 2F =D 2 ∂t ∂x

(7.1)

with boundary conditions F(a,t) = Fa (t)

F(b,t) = Fb (t)

and initial condition F(x, 0) = F0 (x) We require the solution in the region a ≤ 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 we calculate the solution. This is known as discretising the region. Take Nx points in the x direction so that the spacing between points is then given by ∆x =

b−a Nx

and let the variable i be the index that refers to the discrete x points. That is xi = a + i∆x

i = 0, 1, . . ., Nx

Do a similar process for the t direction. Take Nt points in the t direction so that the spacing between time steps is then given by ∆t =

T −0 Nt

and let the variable j be the index that refers to the discrete time steps. That is t j = j∆t

j = 0, 1, . . ., Nt

This has now divided the (x,t) plane into a grid. The aim is to calculate the solution at each of the grid points.

Index

127

FTCS METHOD

t

t =0

j = 10

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=9

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=8

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=7

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=6

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=5

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=4

t u

bc

bc

bc

bc

bc

bc

bc

t u

bc

bc

bc

t u

bc

∆t

j=3

t u

bc

bc

bc

j=2

t u

bc

bc

bc

bc

bc

bc

bc

t u

j=1

t u

bc

bc

bc

bc

bc

bc

bc

t u

rs

rs

rs

rs

rs

rs

rs

rs

rs

i=0

i=1

i=2

i=3

i=4

i=5

i=6

i=7

i=8

x=a sr t u bc

∆x

x=b

Initial condition – known F(x, 0) = F0 (x) Boundary conditions – known F(a,t) = Fa (t) and F(b,t) = Fb (t) Interior points – unknown

x

128

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

j

Let the solution at the point (xi ,t j ) be denoted by Fi , that is Fi j = F(xi ,t j ) = F(a + i∆x, j∆t) Now approximate the diffusion equation

∂ F ∂ 2F = 2 ∂t ∂x around the point (xi ,t j ) by using a forward finite difference approximation for the time derivative j+1 j ∂ F j Fi − Fi ≈ ∂ t i ∆t and a centred finite difference approximation for the spatial second derivative j j j j Fi+1 − 2Fi + Fi−1 ∂ 2 F ≈ ∂ x2 i (∆x)2

This gives the PDE as approximately Fi

now make Fi

j+1

j+1

j

j

j

j

Fi+1 − 2Fi + Fi−1 (∆x)2

− Fi =D ∆t

!

the subject j

Fi

j+1

Fi

j+1

j

j

F − 2Fi + Fi−1 = ∆tD i+1 (∆x)2

!

= γ Fi+1 + (1 − 2γ )Fi + Fi−1 j

j

where

γ=

j

+ Fi

j

(7.2)

D∆t (∆x)2

Equation (7.2) is known as the Forward Time Centered Space (FTCS) approximation to the diffusion equation. The FTCS equation enables solutions at time level j +1 to be calculated from information at time level j. Given that we know the initial condition (level j = 0) then we are able to use this to calculate the solution at time level j = 1. This is then used to get the solution at level j = 2 and so on. At a given time level you use the FTCS equation to sweep across in the i direction calcuj+1 lating the value of Fi . The FTCS method is an explicit method as at each time step there is an explicit equation for the values at the next time step.

Index

FTCS METHOD

7.2.2 Template for the FTCS Method

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t j+1 u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

t u

bc

bc

bc

bc

bc

bc

bc

t u

rs

rs

rs

rs

rs

rs

rs

rs

rs

i−1

i

i+1

j

t =0

x=a sr t u bc

x=b

Initial condition – known F(x, 0) = F0 (x) Boundary conditions – known F(a,t) = Fa (t) and F(b,t) = Fb (t) Interior points – unknown

129

130

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

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

∂T ∂ 2T =D 2 ∂t ∂x where for steel D = 5 × 10−6 m2 /s The boundary and initial conditions can be written T (0,t) = 0

T (2,t) = 0

T (x, 0) = 100

Find the temperature distribution at times t = 60, 120, 180, . . .600 minutes The exact solution can be found analytically using the method of separation of variables to be ∞ mπ x −Dm2 π 2t sin T (x,t) = ∑ am exp 4 2 m=1

where

am =

−200 ((−1)m − 1) mπ

Temperature in metal bar at steps of 60 minutes 100 numerical exact

90 80

Temperature

70 60 50 40 30 20 10 0 0

0.5

1 x

1.5

2

Index

FTCS METHOD

MATLAB code % ftcsdiffusion.m % Solve heat diffusion in a steel bar of length 2 % with ends at 0oC and initially the bar is 100oC. % Use FTCS finite difference method % dT/dt = D d2T/dx2 % subject to boundary conditions T(0,t)=0 T(2,t)=0 % and initial condition T(x,0)=100 % clear all; a=0; b=2; % x boundaries Nx=10; % number of x points to use dx=(b-a)/Nx; % x step x=[a:dx:b]; % x points fa=0; fb=0; % boundary values D=5e-6; % steel bar diffusivity = 5.0x10ˆ-6 mˆ2/s dt=3600 % time step in seconds (=60 minutes) tfinal=36000; % final time in seconds (=600 minutes) gamma=D*dt/(dxˆ2) T(1)=fa; T(Nx+1)=fb;% boundary conditions, note that index for % i starts at 1 not 0 as matlab doesnt % allow zero indexing T=100*ones(1,Nx+1); % initial temperature in bar is 100 % plot initial temperature distribution plot(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 tfinal for t=dt:dt:tfinal % sweep across the bar calculating the Temperature for i=2:Nx Tnext(i)=gamma*(T(i-1)+T(i+1))+(1-2*gamma)*T(i); end Tnext(1)=fa; Tnext(Nx+1)=fb; T=Tnext; % update the temperature % exact solution from separation of variables method Texact=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

131

132

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.2.4 Stability The stability of a time stepping numerical method is all to do with whether errors grow or decay as you step in time. Consider the forward time approximation to the time derivative at the point (xi ,t j ) j+1 j ∂ F j Fi − Ti ≈ ∂ t i ∆t

This has an error of order (∆t)2 so at each time step there is an error introduced. The question is do these errors build up and swamp the solution or do they get damped out? It turns out that for an FTCS (Forward Time Centred Space) approximation to the diffusion equation as given by Fi

j+1

= γ (Ti+1 + Ti−1 ) + (1 − 2γ )Ti j

where

γ= that if

γ<

1 2

γ>

j

j

D∆t (∆x)2

then the method is stable and errors do not grow 1 2

then the method is unstable and 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 below 12 at all times. This is particularly important if the diffusion coefficient varies in the problem. How does this effect the usefulness of the FTCS method? If you have a metal bar with diffusion constant of D = 5 × 10−6 m2 /s of length 2m and you want 100 points along the bar to get an accurate representation of the temperature in the bar what value of the time step do you need to use?

Now if you double the number of space points how is the time step effected?

Index

FTCS METHOD

133

7.2.5 Unstable Example Go back to original FTCS example. For the original problem what would happen if we increase the time step from dt = 3600 seconds to dt = 7200 seconds. Then γ = 0.9

Temperature in metal bar at steps of 120 minutes 250 numerical exact

200

Temperature

150 100 50 0 −50 −100 −150 0

0.5

1 x

1.5

2

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 a very restrictive condition as a halving of the spatial step (twice as many x points) results in a time step a factor of 4 smaller. If any degree of accuracy is required (that is a small x step is required) then method can take a very long time to run as a very small time step is required.

134

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.3 Crank-Nicolson method In contrast to the FTCS method the Crank-Nicolson method uses an average of the old and new time step for the spatial derivative. j+1 j! ∂ F j ∂ 2 F 1 ∂ 2 F =D + ∂ t i 2 ∂ x2 i ∂ x2 i Fi

j+1

j

− Fi D = ∆t 2

j+1

j+1

j+1

j

j

j

Fi+1 − 2Fi + Fi−1 F − 2Fi + Fi−1 + i+1 2 (∆x) (∆x)2

!

Which rearranges to γ j −γ j+1 j+1 j j+1 j Fi+1 + Fi−1 + (1 + γ )Fi Fi+1 + Fi−1 + (1 − γ )Fi = 2 2

where

γ=

D∆t (∆x)2

The left hand side has the unknowns at time j + 1 the right hand side the known values at time j. These are for each value of i as you step across the region. Hence it is really n equations in n unknowns (recall the finite-difference work for ODEs). This is known as an implicit method as there is now no longer an explicit equation for the solution at time j + 1 in terms of time level j values but rather a system of equations to solve.

7.3.1 Template

bc

bc

bc

bc

bc

j+1

bc

bc

bc

bc

bc

j

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

i−1

i

i+1

Index

135

CRANK-NICOLSON METHOD

7.3.2 Matrix Representation The boundary conditions at i = 0 and i = Nx are known so the equations are slightly different for i = 1 and i = Nx − 1 to account for this. The system of equations can be written in matrix form (recall the finite difference work on ODEs) as AF j+1 = BF j + d j where

A=

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

B=

γ dj = 2

j j+1 Fa + Fa

0 0 .. . 0 j j+1 Fb + Fb

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

j F =

... ... .. .

0 0 .. .

0 0 .. .

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

... ... .. .

0 0 .. .

0 0 .. .

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

j

F1 j F2 0 .. . 0 j FNx −1

j+1 F =

F1j+1 F2j+1 0 .. . 0 j+1 FNx −1

This system of equations can be easily solved in MATLAB using the ’backslash divide’ command. For example something like Fnext=A\(B*F+d) One of the advantages of the implicit scheme such as Crank-Nicolson is that it is stable irespective of the value of γ . That is errors do not build up to catastrophic proportions even for large time steps. Hence large time steps can be taken only limited by the accuracy at each step. This is the major advantage of the implicit Crank-Nicolson scheme over the explicit FTCS scheme. This stability comes at a price, namely that a matrix equation must be solved at each time step. This is not too bad in this case as the matrix A is tri-diagonal and hence there are fast and efficient methods available to invert it. Also since in this case A does not change from one tme step to the next then it is even possible to determine the inverse of A once and store it for future use thereby speeding up the calculations even more. As a general rule explicit schemes suffer from stability problems and implicit schemes are usually stable.

136

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

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

∂T ∂ 2T =D 2 ∂t ∂x where for steel D = 5 × 10−6 m2 /s The boundary and initial conditions can be written T (0,t) = 100

T (2,t) = 0

T (x, 0) = 0

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

Temperature in bar at steps of 4 hours 100 90 80

Temperature

70 60 50 40 30 20 10 0 0

0.5

1 x

1.5

2

Index

CRANK-NICOLSON METHOD

137

MATLAB code % cranknic.m % Solve the heat diffusion equation on a steel bar of length 2 % with one end held at 100oC the other at 0oC and initially % the bar is 0oC. Use Crank-Nicolson finite difference method % % dT/dt = D d2T/dx2 boundary conditions T(0,t)=100 T(L,t)=0 % and initial condition T(x,0)=0 % clear all; n=40; L=2; dx=L/n; x=[0:dx:L]; % x points fa=100; fb=0; % boundary values D=5e-6; % diffusivity = 5x10ˆ-6 mˆ2/s dt=144 % time step in seconds (plot every 100th) tfinal=144000; % final time in seconds (=4 hours) gamma=D*dt/(dxˆ2) % T=zeros(n-1,1); % initial condition Tplot=[fa;T;fb]; % add in the boundary condition for plot plot(x,Tplot); xlabel(’x’); ylabel(’Temperature’); title(’ Temperature in bar at steps of 4 hours’) hold on % % set up left and right hand tri-diagonal matrices Aleft=diag((1+gamma)*ones(n-1,1))... +diag((-gamma/2)*ones(n-2,1),1)... +diag((-gamma/2)*ones(n-2,1),-1); Aright=diag((1-gamma)*ones(n-1,1))... +diag((gamma/2)*ones(n-2,1),1)... +diag((gamma/2)*ones(n-2,1),-1); % % loop through time calculating T at each new step and plot % each 100th calculation (see the mod function for how to do this) for t=dt:dt:tfinal % take time steps of dt d=gamma*[fa;zeros(n-3,1);fb];% adjust for known boundary values T=Aleft\(Aright*T+d); % solve for T using backslash divide if (mod(t,dt*100)==0) % plot each 100th step Tplot=[fa;T;fb]; % add boundary values for plotting plot(x,Tplot); end end print -depsc cranknic hold off

138

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Index

7.4 Method of lines The method of lines is a way of numerically solving PDEs using the efficient techniques (eg Runge-Kutta) developed to solve ODEs. Consider the 1D diffusion equation

∂T ∂ 2T =D 2 ∂t ∂x with Dirichlet boundary conditions at the boundaries x = a and x = b and initial condition given by T (a,t) = fa (t) T (b,t) = fb (t) T (x, 0) = f0 (x) discretise this in the x-direction using the variable i to indicate which x point you are at. That is, divide the x-direction into N subintervals with i = 0, 1, 2, . . ., N. Use centred space approximation to the second derivative at each of those points but leave the time derivative as it is. So we get

∂ Ti Ti−1 − 2Ti + Ti+1 ≈D ∂t (∆x)2 where Ti is the temperature at the ith space position and ∆x = (b − a)/N. Now T0 and TN are known because of the boundary condition. T0 = fa (t)

TN = fb (t)

For i = 1, 2, 3, . . ., N −1 we now have a system of (N −1) ODEs in the variables T1 , T2 , T3 , . . . , TN−1 . For example dT1 = β (T0 − 2T1 + T2 ) = β ( fa − 2T1 + T2 ) dt dT2 = β (T1 − 2T2 + T3 ) dt dT3 = β (T2 − 2T3 + T4 ) dt ... = ... dTN−2 = β (TN−3 − 2TN−2 + TN−1 ) dt dTN−1 = β (TN−2 − 2TN−1 + TN ) = β (TN−2 − 2TN−1 + fb ) dt where β = D/(∆x)2 . With the initial condition giving us the initial values for each of T1 , T2 , T3 , . . ., TN−1 . This system of ODEs is easily solved numerically using any of the ODE solvers mentioned previously (eg Euler’s, midpoint, Runge-Kutta, ode45,. . . ). The more points used across the domain (larger the value of N) the more accurate the solution will be and the longer it will take to run.

Index

METHOD OF LINES

139

MATLAB code % % % % % % % %

mol.m method of lines on diffusion equation bar length 2m, with T(0,t)=100, T(2,t)=50 initially quadratic temperature profile T(x,0)=100-25x(x-1) diffusion coefficient of D=1x10ˆ(-6) m2/s integrated from t=0 to 1,000,000sec in steps of 100,000sec

global n Ta Tb beta n=50; xa=0; xb=2; dx=(xb-xa)/n Ta=100; Tb=50; timeend=1e6; D=1e-6 beta=D/(dx.*dx) numsteps=10 timestep=timeend/numsteps; % setup the time points solution is wanted at tspan=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 it y0=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 values for i=2:numsteps v=[Ta y(i,:) Tb]; plot(w,v,’b-’) end print -depsc mol hold off

140

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

MATLAB code function f=molf(t,y); % % molf.m % the system of equations global n Ta Tb beta f(1)=beta*(Ta-2*y(1)+y(2)); for i=2:n-2 f(i)=beta*(y(i-1)-2*y(i)+y(i+1)); end; f(n-1)=beta*(y(n-2)-2*y(n-1)+Tb); f=f(:); return

Heat diffusion in a steel bar with D = 1 × 10−6 m2 /s

∂T ∂ 2T =D 2 ∂t ∂x with T (0,t) = 100

T (2,t) = 50

T (x, 0) = 100 − 25x(x − 1)

Heat Diffusion using Method of Lines 110

100

Temperature

90

80

70

60

50 0

0.5

1 x

1.5

2

Index

OTHER BOUNDARY CONDITIONS

141

7.5 Other boundary conditions So far all the examples have used Dirichlet Boundary Conditions (the value of the function given on the boundary). What changes if instead the boundary conditions are Neumann or Cauchy type? Now the value of the function is not known and hence the boundary should also be considered an unknown point. Example Left edge has a Dirichlet (u = 0) boundary condition and hence the value is known. Right edge has a Cauchy ( ∂∂ ur = h(25 − u)) boundary condition and hence the value is unknown.

t u

bc

bc

bc

bc

bc

bc

bc

bc

u=0 u t boundary value t u known

bc

bc

bc

bc

bc

bc

bc

∂u bc ∂ r = h(25 − u)

bc

bc

bc

bc

bc

bc

bc

boundary bcvalue unknown

t u

bc

bc

bc

bc

bc

bc

bc

bc

rs

rs

rs

rs

rs

rs

rs

rs

rs

sr bc t u

known initial condition unknown points known boundary points

Example The different types of boundary conditions can all be derived from Newton’s Law of cooling. For example consider a steel bar where the end at x = 0 loses heat according to Newton’s Law of cooling with a heat transfer coefficient of h, and an ambient temperature of Ta , that is at x = 0

∂T = h(T − Ta ) ∂x If h = 0 then the boundary is fully insulated as no heat can flow through that boundary

∂T = 0 insulated ∂x If h is very large then divide each side by h to give approximately T = Ta

Dirichlet condition

Each of these three boundary conditions need to be treated slightly differently.

142

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.5.1 Neumann Boundary Condition The function value is not known at the boundary so the boundary is an unknown value that must also be determined. After discretizing in the x-direction use centred differencing on the derivative term, for example centred differencing about i = 0 gives ∂ T T1 − T−1 ≈ ∂ x i=0 2∆x

where T−1 is a fictitious point. That is it is an artificial point that is not in the domain of interest. So how does this help? Consider the case of an insulated boundary

∂T ∂x

= 0.

Using the above expression and the fact that the boundary is insulated so that get a relationship between T−1 and T1 in this case this is simply that

∂T ∂x

= 0 we

T−1 = T1 But recall that we are dealing with the Diffusion Equation

∂T ∂ 2T =D 2 ∂t ∂x using centred differencing on the second derivative around the point i = 0 results in ∂ 2 T T−1 − 2T0 + T1 ≈ 2 ∂ x i=0 (∆x)2 the fictitious point T−1 can then be replaced by T1 to give 2T1 − 2T0 ∂ 2 T ≈ ∂ x2 i=0 (∆x)2

Hence it is possible to incorporate the boundary condition into the scheme that is used with the value of the temperature at the boundary being one of the unknowns. For example in the Method of Lines the system of equations is modified to include an extra equation for the T0 point and the T1 equation is also altered to still include the T0 which is unknown, the other equations remain unchanged. dT0 = β (T−1 − 2T0 + T1 ) = β (2T1 − 2T0 ) dt dT1 = β (T0 − 2T1 + T2 ) dt dT2 = β (T1 − 2T2 + T3 ) dt ... = ... dTN−2 = β (TN−3 − 2TN−2 + TN−1 ) dt dTN−1 = β (TN−2 − 2TN−1 + TN ) = β (TN−2 − 2TN−1 + fb ) dt

Index

OTHER BOUNDARY CONDITIONS

143

Example Heat diffusion in a steel bar with D = 1 × 10−6 m2 /s

∂T ∂ 2T =D 2 ∂t ∂x

with

∂T (0,t) = 0 ∂x MATLAB code % % % % % % % %

T (2,t) = 50

T (x, 0) = 100 − 25x(x − 1)

molinsulated.m method of lines on diffusion equation bar length 2m, with dT/dx(0,t)=0, T(2,t)=50 initially a quadratic temperature profile T(x,0)=100-25x(x-1) diffusion coefficient of D=1x10ˆ(-6) m2/s integrated from t=0 to 1,000,000 sec in steps of 100,000 sec

global n Tb beta n=50; xa=0; timeend=1e6; numsteps=10;

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

% setup the time points solutionis wanted at tspan=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 it y0=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 value for i=2:numsteps v=[y(i,:) Tb]; plot(w,v,’b-’) end print -depsc molinsulated hold off

144

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

MATLAB code function f=molinsulatedf(t,y); % % molinsulatedf.m % the system of equations global 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

Heat Diffusion using Method of Lines: Insulated left edge 110

100

Temperature

90

80

70

60

50 0

0.5

1 x

1.5

2

Index

OTHER BOUNDARY CONDITIONS

145

7.5.2 Cauchy Boundary Condition As an example for the full Newtonian heat loss boundary condition

∂T = h(T − Ta ) ∂x the derivative of the function is given in terms of the unknown value at the boundary that must also be determined. After discretizing in the x-direction use centered differencing on the derivative term, for example centred differencing about i = 0 gives ∂ T T1 − T−1 ≈ = h(T0 − Ta ) ∂ x i=0 2∆x where T−1 is a fictitious point and Ta is the ambient temperature.

This can be rearranged to get the fictitious point T−1 in terms of the known points T0 and T1 . T−1 = T1 − 2∆xh(T0 − Ta ) Now use this to expression to incorporate the Cauchy boundary condition into the expression for the second derivative at the point i = 0.

146

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

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 diffuses but also causes a feedback so that a higher temperature results in a faster increase in temperature. This type of scenario is typical of a combustion problem where as a fuel burns it raises the temperature thereby making the reaction happen even faster. Assume that the reaction is occurring in a region of length 2m and is such that the ends are held at some ambient temperature Ta and initially the temperature is ambient everywhere except a small hot spot in the centre. This could be used to model scenarios such as a small piece of hot metal falling into a layer of sawdust for example.

∂T ∂ 2T = D 2 + (T − Ta )2 ∂t ∂x where for sawdust D = 0.08 m2 /s. The boundary conditions are ambient temperature T (0,t) = Ta = 25 T (2,t) = 25 and an initial condition of ambient temperature everywhere except at a hotspot in the centre 50 if x = 1 the hot spot T (x, 0) = 25 elsewhere Not that this equation can not be solved analytically! Use FTCS finite differences to get Ti, j+1 = γ (Ti+1, j + Ti−1, j ) + (1 − 2γ )Ti, j + ∆t(Ti, j − Ta )2

Temperature at steps of 0.1 seconds: Final Temperature 1.5 seconds 90 80

Temperature

70 60 50 40 30 20 0

0.5

1 x

1.5

2

Index

NONLINEAR EXAMPLE

147

MATLAB code % reactdiff.m % Solve the heat reaction-diffusion equation on a length 2 with % both ends at ambient=25oC and initially the bar is ambient. % A hot spot of T=50oC at the centre (x=1) % Use FTCS finite difference method % % dT/dt = D d2T/dx2 + (T-Ta)ˆ2 % subject to boundary conditions T(0,t)=Ta T(L,t)=Ta % and initial condition T(x,0)=Ta (for x=/=1) and T(1,0)=50 % clear all; n=100; L=2; dx=L/n; x=[0:dx:L]; % x points D=0.08; % diffusivity of wood = 0.08 mˆ2/s dt=.001; % time step in seconds tfinal=1.48; % final time in seconds gamma=D*dt/(dxˆ2) Ta=25; % ambient Temperature T=Ta*ones(1,n+1); % ambient Temperature everywhere except hot spot T(n/2+1)=Ta+25; % hot spot at centre % plot initial temperature distribution plot(x,T); xlabel(’x’); ylabel(’Temperature’); title(’ Temperature at steps of 0.1 sec: Final Temperature 1.5 sec’) hold on for t=dt:dt:tfinal % take time steps of dt %step across the bar calculating the Temperature at next time level for i=2:n Tnext(i)=gamma*(T(i-1)+T(i+1))+(1-2*gamma)*T(i)+dt*(T(i)-Ta)ˆ2; end Tnext(1)=Ta; Tnext(n+1)=Ta; T=Tnext; % update the temperature if (mod(t,dt*100)==0) % plot each 100th step plot(x,T); end end plot(x,T); print -depsc reactdiff hold off

148

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.7 Other co-ordinate systems So far all the examples given have been in Cartesian co-ordinates (x, y, z). These equations can also be considered in other co-ordinate systems such as cylindrical (r, θ , z) or spherical (ρ , φ , θ ). For example the Laplacian in cylindrical co-ordinates is ∇2 F =

∂ 2F 1 ∂ F 1 ∂ 2F ∂ 2F + + + ∂ r 2 r ∂ r r 2 ∂ θ 2 ∂ z2

The benefit of using a coordinate system that matches the object is that often it is possible to reduce the dimension of the problem.

7.7.1 Disc example Consider the case of a circular disc of metal with a radius of 50cm. The base and top are insulated which means the only directions of interest are in the plane of the disc ((x, y) plane) and hence the problem is two dimensional. The disc is initially at 100◦C and loses heat through the edge according to Newton’s law of cooling. The ambient air is at 25◦C. The diffusivity of the metal is D = 5 × 10−6m2 /s and the Newtonian heat transfer coeeficient is h = 10m−1 .

y ∂u = −h(u − ua ) at r = 0.5 ∂r

x

The governing differential equation is the 2-D heat equation

∂u = D∇2 u ∂t which can be written in circular coordinates as 2 ∂u ∂ F 1 ∂ F 1 ∂ 2F =D + + ∂t ∂ r2 r ∂ r r2 ∂ θ 2 Now since the boundary condition is applied uniformly over the boundary and the initial condition is also uniform then the temperature in the disc will not depend on the angle

Index

OTHER CO-ORDINATE SYSTEMS

149

around the disc. Hence there will not be any θ dependence and so any derivatives with respect to θ are zero. So the governing PDE becomes 2 ∂u ∂ u 1 ∂u =D + ∂t ∂ r2 r ∂ 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 space variable r. One boundary condition is obvious, namely

∂u = −h(u − ua ) at r = 0.5 ∂r The other boundary condition is derived from the fact that the temperature is independent of θ and hence at the origin the derivative with respect to r must be zero. That is the temperature distribution must be centred on the origin. So the other boundary condition is ∂u = 0 at r = 0 ∂r Look at the governing PDE. There is a problem at r = 0 as the term 1r ∂∂ ur appears to be undefined at r = 0. In fact this term is not undefined since at r = 0 ∂∂ ur = 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 states in this particular situation that if

lim f (r) = 0 and

r→0

hence in this case f (r) = by

∂u ∂r

lim g(r) = 0

r→0

f ′ (r) f (r) = lim ′ r→0 g (r) r→0 g(r)

then

lim

and g(r) = r so that at r = 0 the governing PDE is modified

∂u ∂ 2 u ∂∂ ur = D + ∂t ∂ r2 r

!

∂ 2u

∂ 2 u ∂ r2 = D + ∂ r2 1 = 2D

!

∂ 2u ∂ r2

So in any numerical method this alternative PDE must be used if at the point r = 0.

150

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

7.7.3 FTCS Method Now derive the FTCS system for solving this problem. The only space dimension of interest is the radius r = 0 to 0.5. Set up a grid in space and time. Discretise the region so j that ui = u(i∆r, j∆t) with i = 0, 1, . . ., Nr and j = 0, 1, . . ., Nt with ∆r = 0.5/Nr

t

∂u ∂r

j = 10

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=9

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=8

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=7

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=6

bc

bc

bc

bc

bc

bc

bc

bc

bc

=0

t =0

j=5

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=4

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

∆r

bc

∆t

j=3

bc

bc

bc

bc

j=2

bc

bc

bc

bc

bc

bc

bc

bc

bc

j=1

bc

bc

bc

bc

bc

bc

bc

bc

bc

rs

rs

rs

rs

rs

rs

rs

rs

rs

i=0

i=1

i=2

i=3

i=4

i=5

i=6

i=7

i=8

r=0 sr bc

∂u ∂r

Initial condition – known u(r, 0) = 100 unknown points

= h(25 − u)

r

r = 0.5

Index

OTHER CO-ORDINATE SYSTEMS

151

FTCS formulation Provided r 6= 0 the governing PDE is 2 ∂u ∂ u 1 ∂u =D + ∂t ∂ r2 r ∂ r so use Forward Time and Centred Space finite difference around the point (i, j) to approximate this by ! j j j j j j+1 j ui+1 − 2ui + ui−1 ui − ui 1 ui+1 − ui−1 =D + ∆t (∆r)2 i∆r 2∆r Rearranging this to get the new time level as the subject results in γ j j j j j j j+1 ui+1 − ui−1 + ui ui = γ ui+1 − 2ui + ui−1 + 2i

D∆t where γ = (∆r) 2 . This is the FTCS formulation for the internal points. Points at the two edges (r = 0 and r = 0.5) require special attention.

External edge r = 0.5 Use a fictitious point beyond the region of interest. The boundary condition is ∂u ∂ r = −h(u − ua ) which on using central finite differences is j j uNr +1 − uNr −1 ∂ u j j ≈ = −h(uNr − ua ) ∂ r Nr 2∆r

which rearranges so that the fictitious point uNj r +1 can be written as j

j

j

uNr +1 = −2∆rh(uNr − ua ) + uNr −1 j

This value is then used in the FTCS scheme above whenever uNr +1 is used.

152

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Index

Internal edge r = 0 Do a similar process for the internal edge but recall that the PDE is altered there. The boundary condition is ∂∂ ur = 0 which on using central finite differences is j j ∂ u j u1 − u−1 =0 ≈ ∂ r 0 2∆r j

which rearranges so that the fictitious point u−1 can be written as j

j

u−1 = u1 The PDE at r = 0 (which is i = 0) was found previously using L’Hopital’s Rule to be

∂u ∂ 2u = 2D 2 ∂t ∂r so use Forward Time and Centred Space finite difference around the point (0, j) to approximate this by ! j j j j+1 j u1 − 2u0 + u−1 u0 − u0 = 2D ∆t (∆r)2 j

Using the value for the fictitious point (u−1 ) above and rearranging gives the governing FTCS equation for the r = 0 point as j+1 j j j u0 = 4γ u1 − u0 + u0 It is now possible to code this FTCS method in MATLAB.

Index

OTHER CO-ORDINATE SYSTEMS

153

MATLAB code % ftcs_cyl.m % Solve the heat diffusion equation on a steel disc radius 0.5m % with Newton’s law of cooling dT/dr=-h(T-Ta) Ta=25oC at r=0.5m % initially the disc is 100oC. Use FTCS finite difference method % dT/dt = D d2T/dr2 + D 1/r dT/dr % initial condition T(x,0)=100 % boundary conditions dT/dr=0 at r=0 % dT/dr=-h(T-25) at r=0.5m % clear all; n=20; % number of points L=0.5; % radius of disc dr=L/n; % space step Ta=25; % ambient temperature r=[0:dr:L]; % r points D=5e-6; % steel diffusivity = 5x10ˆ6 mˆ2/s h=10.0; % heat loss coefficient dt=30 % time step in seconds tfinal=30000; % final time in seconds gamma=D*dt/(drˆ2) % calculate and plot initial temperature distribution T=ones(1,n+1)*100; plot(r,T,’r-’); xlabel(’radius’); ylabel(’Temperature’); title(’ Temperature in disc at 900 second intervals’) hold on % loop through time for 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 one % r=0 special case uses a fictitious point beyond region Tnext(1)=4*gamma*(T(2)-T(1))+T(1); % internal r points for i=2:n Tnext(i)=gamma*(T(i-1)-2*T(i)+T(i+1))... +gamma/(2*(i-1))*(T(i+1)-T(i-1))+T(i); end % external r point uses a fictitious point beyond region Tfict=T(n)-2*dr*h*(T(n+1)-Ta); % calculate ficticous point Tnext(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 solution T=Tnext; if (mod(t,dt*30)==0) plot(r,T); end end % t loop hold off

154

Index

7. PARABOLIC EQUATIONS: DIFFUSION EQUATION

Temperature in disc at 900 second intervals 100 90

Temperature

80 70 60 50 40 30 20 0

0.1

0.2

0.3 radius

0.4

0.5

8 Elliptic Equations: Laplace’s Equation

Elliptic equations (such as Laplace’s Equation) do not allow ’time-stepping’ since there is no ’time-like’ variable to step in. Instead the solution must somehow be found in the entire demand simultaneously. The best method for this is to use some kind of iterative scheme where you use the equation to update an initial guess of the solution until it converges to a steady solution. This is a sort of pseudo ’time-stepping’ but it is not necessary to keep the intermediate ’times’ hence some short cuts and speed ups can be done.

8.1 Example with Dirichlet boundary conditions Consider a steady state heat problem on a rectangular domain with Dirichlet Boundary conditions. For example a slab of wood of size 10cm (wide) by 5cm (long) that has reached thermal equilibrium. The left and right edges are held at 20◦ C the bottom at 20◦ C and the top is subjected to to a quadratic temperature such that the maximum in the middle is 70◦ C. What is the steady state temperature distribution in the slab?

y 5

(2x(10 − x) + 20)◦C

20◦C

20◦C

20◦C

10

x

The governing PDE is Laplace’s Equation in 2D

∂ 2T ∂ 2T + 2 =0 ∂ x2 ∂y subject to the boundary conditions T (0, y) = 20

T (10, y) = 20

T (x, 0) = 20

T (x, 5) = 2x(10 − x) + 20

156

Index

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

Divide the region up into a grid so that there Nx sections in the x-direction and Ny sections in the y-direction. Let i be the index for the x-direction and j the index for the y-direction. So in this case (10 − 0) (5 − 0) ∆x = ∆y = Nx Ny with i = 0, 1, . . ., Nx and j = 0, 1, . . ., Ny . Let Ti, j be shorthand notation for the temperature at the position (xi , y j ) where xi = 0 + i∆x and y j = 0 + j∆y. Turning to the governing PDE discretise the second derivative terms using centred differencing in the appropriate direction to get Ti+1, j − 2Ti, j + Ti−1, j Ti, j+1 − 2Ti, j + Ti, j−1 + ≈0 (∆x)2 (∆y)2 That is the template used is a cross pattern

bc

bc

bc

bc

bc

j+1

bc

bc

cb

bc

bc

j

bc

bc

cb

cb

bc

bc

bc

cb

bc

bc

bc

bc

bc

bc

bc

i−1

i

i+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 4 neighbours as Ti, j =

1/2 2 2 (∆y) T + T + (∆x) T + T i+1, j i−1, j i, j+1 i, j−1 (∆y)2 + (∆x)2

But how does this help? Use this equation as the basis for an iterative scheme. Start with some initial guess for the temperature in the slab (that is an initial guess for all the Ti, j ), recalling that the temperature on the boundary is known. Now use the above equation to update the central point from the values of its 4 neighbours. Sweep across the x and y directions repeatedly until the solution converges. So the equation is Ti,n+1 j =

1/2 (∆y)2 + (∆x)2

n n 2 n n (∆y)2 Ti+1, j + Ti−1, j + (∆x) Ti, j+1 + Ti, j−1

where n is a counter that keeps track of which iteration you are up to. n = 0 corresponds to your original guess. You stop when the change in the solution from one iteration to the next is less than some tolerance.

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 20 V(:,Ny+1)=2*x’.*(10-x’)+20; % boundary condition on y=5 Vnext=V; number_iter=0; denom=2*(dx2+dy2); % this is used often so calculate it outside loop % loop through until solution converges while delta > eps number_iter=number_iter+1; maxdiff=0; for i=2:Nx for j=2:Ny Vnext(i,j)=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(V(i,j-1)+V(i,j+1)))/den dV=V(i,j)-Vnext(i,j); % the difference between successive solut % keep track of the maximum difference if abs(dV) > maxdiff; maxdiff = abs(dV); end end end V=Vnext; delta=maxdiff; end number_iter % show how many iterations were used % plot the solution in 3D in 3 ways, surface plot, mesh plot, contour figure(1) surf(x,y,V’) % surf plots the surface colorbar % put a color bar next to it showing what each color shading interp % use interpolation for the coloring (smooths the col xlabel(’x’); ylabel(’y’); zlabel(’Temperature’); title(’Steady State Temperature’); print -depsc laplace_direct_3D figure(2) mesh(x,y,V’) % mesh plots the surface with the mesh stretched over colorbar % put a color bar next to it showing what each color xlabel(’x’); ylabel(’y’); zlabel(’Temperature’); title(’Mesh plot of temperature’) print -depsc laplace_direct_mesh figure(3) contour(x,y,V’) % cont plots a contour plot axis image xlabel(’x’); ylabel(’y’); title(’Contour plot of temperature’) print -depsc laplace_direct_contour

Steady State EQUATION Temperature 8. ELLIPTIC EQUATIONS: LAPLACE’S

158

Index 70

Temperature

65 70

60

60

55

50

50

40

45 40

30

35

20 6

30 10

4

25

5

2

20

0 0

y

x

Mesh plot of temperature 70

Temperature

65 70

60

60

55

50

50

40

45 40

30

35

20 6

30 10

4

25

5

2

20

0 0

y

x

y

Contour plot of temperature 5 4.5 1

2

3

4

5 x

6

7

8

9

Index

IMPROVING THE CONVERGENCE

159

8.2 Improving the convergence 8.3 Gauss-Seidel The previous method where the new values are calculated over the entire domain using the existing values is known as the Jacobi method. This requires storing 2 matrices of values (the old and the new) and is relatively inefficient. The speed of the iterative scheme convergence can be substantially improved if instead of sweeping across using the old values we make sure we use the most recently calculated values. This has the added advantage of not requiring the storing of two matrices. for i=2:Nx for j=2:Ny Vnext=(dyˆ2*(V(i-1,j)+V(i+1,j))+dxˆ2*(V(i,j-1)+V(i,j+1)))/denom; dV=V(i,j)-Vnext; V(i,j)=V(i,j)+dV; if abs(dV) > maxdiff; maxdiff = abs(dV); end end end This is known as the Gauss-Seidel method. Because the most recent value of the solution is used it greatly improves the efficiency of the method. Typically the Gauss-Seidel method uses approximately 25% to 50% less iterations for the same level of accuracy. Can you see any problems with this method?

How could you improve on it?

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 incremental change to the solution for i=2:Nx for j=2:Ny Vnext=(dyˆ2*(V(i-1,j)+V(i+1,j))+dxˆ2*(V(i,j-1)+V(i,j+1)))/denom; dV=V(i,j)-Vnext; >>> V(i,j)=V(i,j)+omega*dV; <<< if abs(dV) > maxdiff; maxdiff = abs(dV); end end end Introduce a new parameter ω which is the ’over-relaxation’ amount. This method is known as Successive 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.

Index

GAUSS-SEIDEL

161

The optimum value of ω (the one resulting in the fastest convergence) is not a specific value but is different for each problem. So how do you know what value to use? The best method is to find the approximate value by considering the problem with fewer grid points and then using that optimal value for the full problem with many more grid points. A typical behaviour of the number of iterations versus the relaxation parameter is for the number of iterations to slowly decrease as ω increases to the optimal value and then to rapidly rise (see figure below). Hence it is usually best to take a value of ω that is slightly less than the optimal value to ensure that you stay in the region where the best convergence is obtained. Note that for this example (solving Laplaces equation as before) the number of iterations decreased from about 115 for ω = 1 (no relaxation) to about 34 for ω ≈ 1.83. This results in the program taking approximately a third of the time to run for no more effort. Successive Over-relaxation is particularly important for very large complicated problems in many dimensions where run time and convergence issues become critical. Most commercial software will involve some form of Successive Over-relaxation when solving steady state problems such as Laplace’s equation.

Iterations verses omega for SOR 120 110

number of iterations

100 90 80 70 60 50 40 30 1

Optimal value of omega

1.2

1.4

1.6 omega

1.8

2

162

Index

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

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 thermal equilibrium. The outside temperature is −5◦C the inside temperature 22◦C. The wall foundations are in the ground at 15◦C. The top of the wall is insulated by insulation in the ceiling of the house. The governing PDE is Laplace’s Equation in 2D

∂ 2T ∂ 2T + 2 =0 ∂ x2 ∂y subject to the boundary conditions

∂T = h(T − 22) on ∂x

x=0

∂T = −h(T + 5) on ∂x

x = 0.25

T (x, 0) = 15

∂T = 0 on ∂y

y=2

Contours of Steady State Temperature 2

20

1.8

18

1.6

16

1.4

14

1.2

12 10

1

8

0.8

6

0.6

4

0.4

2 0.2 0 0 0

0.05

0.1

0.15

0.2

0.25

Index

GAUSS-SEIDEL

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 needed insidetemp=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); % V(1,:)=insidetemp*ones(1,Ny); % V(Nx,:)=outsidetemp*ones(1,Ny);% number_iter=0; denom=2*(dxˆ2+dyˆ2); %

initialise solution everywhere to bottomtemp initialise inside edge to insidetemp initialise outside edge to outsidetemp this is used often so calculate it outside loop

% loop through until solution converges while 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)=bottomtemp V(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 end j=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-1 for i=2:Nx-1 j=1; % the bottom j=1 V(i,j)=groundtemp V(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 end j=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); end end 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)=bottomtemp V(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 end j=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’,number_iter,maxdiff) end % while loop finishes when convergence is obtained to accuracy eps contlevels=[outsidetemp:1:max(max(V))]; contour(x,y,V’,contlevels) % plot contours of the solution at contlevels title(’Contours of Steady State Temperature’); colorbar print -depsc wall_contour

163

164

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

Index

8.3.3 Example A cylinder of height 2m and radius 1m has the bottom and sides held at 0◦C and the top satisfies a quadratic temperature distribution with maximum 1◦C at the centre and 0◦C at the edge. Write down the governing equation, boundary conditions and find the steady state temperature distribution using MATLAB.

Index

GAUSS-SEIDEL

165

MATLAB code % laplace_cylinder.m % Solve Laplaces equation d2u/dz2 + 1/r du/dr + d2u/dr2 = 0 % subject to du/dr(0,z)=0 u(r,0)=0 u(r,2)=1-rˆ2 u(1,z)=0 % L=1; M=2; Nz=30; Nr=20; dr=L/Nr; dz=M/Nz; eps=0.001; delta=1; r=0:dr:L; z=0:dz:M; V=zeros(Nr+1,Nz+1); % initialise solution everywhere to zero V(:,Nz+1)=(1-r.ˆ2)’; % boundary condition on z=2 (j=Nz+1) number_iter=0; denom=2*(drˆ2+dzˆ2); % used often so calculate outside loop % loop through until solution converges while 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 end end delta=maxdiff; end number_iter % show how many iterations were used figure(1) mesh(r,z,V’) % plot the solution in 3D title(’Steady State Temperature’); print -depsc laplace_cylinder_3D figure(2) contour(r,z,V’) % plot contours of the solution title(’Contours of Steady State Temperature’); print -depsc laplace_cylinder_contours

166

Index

8. ELLIPTIC EQUATIONS: LAPLACE’S EQUATION

Steady State Temperature

1

0.8

0.6

0.4

0.2

0 2 1

1.5 0.8

1

0.6 0.4

0.5 0.2 0

0

Contours of Steady State Temperature 2

1.8

1.6

1.4

1.2

1

0.8

0.6

0.4

0.2

0

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

9 Hyperbolic Equations: Wave Equation

9.1 Finite Differences The wave equation

∂ 2F = c2 ∇2 F 2 ∂t allows time stepping as was used for the diffusion equation but with a significant difference at the beginning of the process. Since the time derivative is 2nd order there are 2 initial conditions. For example for a one dimensional problem they could be ∂F (x, 0) = V0 (x) ∂t

F(x, 0) = F0 (x)

Divide the region up into a grid so that there are Nx sections in the x-direction and Nt sections in the t-direction. Let i be the index for the x-direction and j the index for the tj direction. So in this case Let Fi 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 wave equation gives j j j j+1 j j−1 Fi − 2Fi + Fi 2 Fi+1 − 2Fi + Fi−1 =c (∆t)2 (∆x)2 This can be rearranged so that the solution at the new time ( j + 1) can be written in terms of the current time ( j) and the previous time ( j − 1) as j+1 j j−1 j j j Fi = γ 2 Fi+1 − 2Fi + Fi−1 + 2Fi − Fi where

γ2 =

c2 (∆t)2 (∆x)2

That is the template used is a cross pattern

bc

bc

bc

bc

bc

j+1

bc

bc

rsbc

bc

bc

j

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

bc

i−1

i

i+1

j−1

The significant difference between this scheme and the FTCS scheme for the diffusion equation is that here the new value depends on the previous two time steps. The problem

168

Index

9. HYPERBOLIC EQUATIONS: WAVE EQUATION

with this is at the beginning of the process as initially only values at one time level (the initial time) are known. To overcome this the easiest method is to make a forward time approximation of the initial derivative boundary condition to get values at the next time level. If the initial derivative boundary condition at the ith space point is ∂ F = V0 i ∂ t i

then approximate this by a forward time difference to get Fi1 − Fi0 ∂ F = V0 i = ∂ t i ∆t Rearrange to get values at time level j = 1 as

Fi1 = Fi0 +V0 i ∆t Now the first two time levels are known and the method can proceed.

9.1.1 Stability As with the FTCS method for the diffusion equation this explicit method also suffers from stability problems. If γ 2 < 1 the method is stable. If γ 2 > 1 the method is unstable and not useable.

9.2 Example Consider a string 1 metre long that is fixed at the ends. Initial the string is pulled to a parabolic shape between x = 25 and 33cm and then released. The governing differential equation is the one dimensional wave equation 2 ∂ 2F 2∂ F = c ∂ t2 ∂ x2

subject to the boundary conditions F(0,t) = 0

F(100,t) = 0

and initial conditions F(x, 0) = given parabolic shape

∂F (x, 0) = 0 ∂t

Index

EXAMPLE

169

MATLAB code % waveqn.m Finite difference solution to the wave equation g2 = .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 steps for i = 2:1:400 % loop through time step t = i*dt; Y2 = A*Y1-Y0; % calculate solution at next time Y0 = Y1; Y1 = Y2; % update solutions end % plot solution plot([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

t=0

1

t = 0.10328

1

y

y

y

−1

−1

−1

1

t = 0.30984

1

t = 0.41312

1

y

y

y

−1

−1

−1

1

t = 0.61968

1

t = 0.72296

1

y

y

y

−1

−1

−1

1

t = 0.92952

1

t = 1.0328

1

y

y

y

−1

−1

−1

1

t = 1.2394

1

t = 1.3426

1

y

y

y

−1

−1

−1

t = 0.20656

t = 0.5164

t = 0.82624

t = 1.1361

t = 1.4459

170

9. HYPERBOLIC EQUATIONS: WAVE EQUATION

Index

You will notice that the initial pulse broke into two waves each moving in opposite directions. This is typical behaviour for solutions of the wave equation. You will also notice form the time snapshots that the wave reflects from the rigid (fixed) ends. A closer look at a future time plot reveals small wiggles developing in the solution. This is numerical error beginning to show up. This sort of error is typical of what is found with numerical solutions of hyperbolic equations.

1

y

−1

10 Concluding Remarks

10.1 Irregular Boundaries All the examples used for PDEs have involved regular boundaries that are easily described within the coordinate system in use (cartesian, cylindrical etc). These kinds of problems involving regular boundaries are ideally suited to finite difference methods as it is relatively simple to set up a grid that accurately represents the shape of interest. Shapes with irregular boundaries are better dealt with using Finite Element Methods rather than Finite Difference Methods. This is because for the finite element method mesh points can be arbitrarily placed on the boundary and the mesh is ’triangularated’ thus allowing for complicated shapes to be easily covered. Contrast this to the finite difference method where typically you use a regular grid which fitting complicated boundaries to is often difficult. One difficulty with the finite element method is then choosing how to distribute the mesh points. This needs some knowledge of how you expect the solution to behave.

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 others are more multipurpose. MATLAB PDE Toolbox: solves fairly general Parabolic, Hyperbolic, and Elliptic equations using finite element methods. Graphical interface for inputting equations and boundary conditions. FASTFLO: Solves almost any PDE using finite element methods. Relatively difficult to use but very powerful. FLUENT: Solves many general PDEs using finite element methods. Primarily used for heat and mass flow problems. FlexPDE: Solves many general PDEs using finite element methods. Best at 2D, the demo version is available free (for Windows and Linux) at http://www.pdesolutions.com. It only takes a few minutes to download and has numerous very good examples. Easy to use. There are numerous others as well. Most commercial software uses finite elements rather than finite differences specifically so that more complicated regions can be analysed.

172

10. CONCLUDING REMARKS

Index

10.3 Finally This has only been a very brief look at numerical solutions of ODEs and PDEs. The complexities and subtleties go way beyond what we have presented here. Hopefully you have now an appreciation of at least some of the ideas of numerical solutions and why it is important to be able to calculate at least some analytic solutions.

Index analytic solutions, 119 boundary conditions, 117, 141 Cauchy, 117, 141, 145 Dirichlet, 117, 141 mixed, 117, 141, 145 Neumann, 117, 141, 142 BVP, 79 relaxation, 84 shooting, 80, 82 Cauchy boundary condition, 117, 141, 145 centre, 68 competition model, 70 convergence, 159 convolution theorem, 27 Crank-Nicolson method, 134 critical points, 68, 70, 74 cylindrical coordinates, 115, 148 delta function, 25 diag, 88 diagonal matrix, 87, 92, 135 diary, 7 differential eigenvalue equation, 101 diffusion equation, 111, 112, 125, 138 Dirac delta function, 25 Dirichlet boundary condition, 117, 138, 141, 155 discretisation, 126, 156, 167 disease, 73 drum, 123 eigenvalue, 7, 75 electrical potential, 113 elliptic PDE, 124, 155 errors, 44 Euler’s method, 43 example, 45 explicit, 135 Fastflo, 171 finite differences, 84, 125, 156, 167, 171 finite element, 93 finite elements, 171 FlexPDE, 171 Fluent, 171 FTCS, 126, 150, 151 ftcs, 167 example, 130

template, 129 functions, 9 passing names, 11 vector input, 10 fzero, 13, 102 Gauss-Seidel, 92, 159 ground water, 113 heat conduction, 112 Heaviside function, 23 help, 8 hyperbolic PDE, 124, 167 implicit, 135 initial conditions, 116 irregular boundaries, 171 iterative scheme, 156 IVP, 41 intoduction, 41 numerical, 43 system, 41, 101 Jacobi method, 159 jacobian, 70, 75 L’Hopitals rule, 149 Laplace transform, 23 convolution theorem, 27 definition, 23 derivative, 26, 27 first shifting theorem, 26 integral, 27 inversion, 29 MATLAB, 33 second shifting theorem, 26 systems, 38 theorems, 27 Laplace’s equation, 111, 113, 155 Laplacian, 113, 148 linear, 110 linear equations, 6 Lokta-Volterra, 68 lookfor, 8 M files, 1 running, 7 web site, 1 MATLAB ilaplace, 36

174

INDEX

laplace, 28 Laplace transform, 33 partial fractions, 33 residue, 33 MATLAB commands, 20 diag, 88 diary, 7 eig, 7 fzero, 13 help, 8 lookfor, 8 ode23, 54 ode45, 14, 56 odeset, 59 ones, 88 quad, 11 quiver, 66 MATLAB PDE toolbox, 171 matrix equations, 92 method of lines, 138, 142 midpoint method, 50, 54

Index plotting, 3 3D, 5 data, 4 Poisson’s equation, 115 predator-prey, 68 projectile, 60 quiver, 66 radar, 114 relaxation method, 84 example, 85 Runge-Kutta, 53, 138 fourth order, 56 second order, 53 running scripts, 7

saddle, 68, 75 seismic, 114 sending output to a file, 7 shooting, 102 example, 82 shooting method, 80, 82 Neumann boundary condition, 117, 141, 142 Simpson’s rule, 11 Newtonian heat loss, 141, 145 SIR disease model, 73 node, 75 software, 171 sonar, 114 ode23, 54 spherical coordinates, 115, 148 ode45 stability, 132, 135, 168 example, 14 step function, 23 options, 59 step size, 46 Runge-Kutta, 56 algorithm, 47 ones, 88 successive over relaxation, 160 order, 44 susceptible, 73 system of equations, 41 parabolic PDE, 124, 125 partial fractions, 29 Taylor series, 43, 53, 54, 79, 149 distinct factors, 29 inversion, 31 vector field, 66 MATLAB, 33 velocity potential, 113 non linear factors, 31 wave equation, 111, 114, 167 repeated factors, 30 web site, i, 1 PDE, 109 classification, 124 definitions, 110 elliptic, 124, 155 examples, 109 hyperbolic, 124, 167 linear, 110 parabolic, 124, 125 separation of variables, 119 phase plane, 63