MATLAB Tutorial
MATLAB Tutorial
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.
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 difculties 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 le . . . . . . . . . . . . 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 nding 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 . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
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
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 . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
iv
CONTENTS
3 Initial Value Problems 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . 3.2 Numerical approach . . . . . . . . . . . . . . . . . 3.3 Eulers 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 Eulers 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 nite 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 denitions . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
Index 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 . . . . . . Laplaces 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 . . . . . . . . . . . . . . Poissons 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 . . . . . . . . . . . . . . . . . . . . . . Classication of PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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
6.4 6.5
6.6
6.7
6.11 6.12
6.13 6.14
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 . . . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . .
vi
CONTENTS 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 LHopitals Rule . . . . . . . 7.7.3 FTCS Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Index . . . . . . . . . . . 135 136 138 141 142 145 146 148 148 149 150 155 155 159 159 160 162 164
7.4 7.5
7.6 7.7
8 Elliptic Equations: Laplaces 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 . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
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. INTRODUCTION TO MATLAB
Index
Index 2. Plotting
MATLAB has very good graphics capability. The easiest method for simple plots is to dene 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. Dening a vector is straightforward. The MATLAB command x=a:b:c denes 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 % 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 x=-5:0.2:5;
25
20
15
10
5 5
1. INTRODUCTION TO MATLAB
Index
3. Plotting data It is very easy to read data in from a le, manipulate it and plot it. The command load is used to load data from an external le. For example read data from a le called datale, calculate the line of best t 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
40
30
20
This is text
10 0
4 x
Index 4. 3D Plotting
MATLAB has very good 3D graphics capability. The easiest method is do dene the domain using the meshgrid command, then dene 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=4x2-2y2 % 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 0 5 5 0 5
1. INTRODUCTION TO MATLAB
Index
5. Solving Linear Equations The original reason for developing MATLAB was to perform matrix calculations. Due to this it has many convenient ways of dealing with matrices. For 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 rst 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 2 3
d = 2 5 8 diary off
Index
1. INTRODUCTION TO MATLAB
Index
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 dont know the name of the function then use lookfor keyword This will nd all occurrences of functions or commands with the specied 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
10
1. INTRODUCTION TO MATLAB
Index
0.2000
0.4000
0.6000
0.8000
1.0000
3.1680
3.7040
4.6560
6.0720
8.0000
Index
11
2 f (x) =
x 0
et 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
1. INTRODUCTION TO MATLAB
Index
integral = 0.84270079342046
integral = 0.84270079427671 5 7 9 11 13 15 17 integral = 0.84270079342046 0.0000000000 0.0000000000 0.0000000000 0.2500000000 0.5000000000 0.5000000000 0.7500000000 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
13
>> 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
1. INTRODUCTION TO MATLAB
Index
There are 3 things you need to dene 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-le 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 rst 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.
15
dy = y 2 cost dt
subject to
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
1. INTRODUCTION TO MATLAB
Index
10
15
20 0
The same les 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
% function of 2 variables so need to do partial derivatives % define a function of x and y g=x*y-sin(x-y)+y3*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
g = x*y+sin(-x+y)+y3*cos(1/y) dgdx = y-cos(-x+y) dgdy = x+cos(-x+y)+3*y2*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
integralexample f = x2-sin(2*x)+cosh(3*x)-exp(-x)
intf = 1/3*x3+1/2*cos(2*x)+1/3*sinh(3*x)+exp(-x)
20
1. INTRODUCTION TO MATLAB
Index
plot(x,y)
plot y versus x
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
plot y vs x with a red dash line hold the plot in place add blue circles to the plot
t = 0:pi/50:2*pi; a=cos(t);
1 0.8
t is a vector 0 to 2 in steps of /50 Semicolon supressess the output. The plot is a parametric plot of a circle with green dots
b=sin(t); plot(a,b,g.)
100 50 0 50 100 10 5 0 5 5 0 5
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 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)
21
Comment dene a vector calculate the size of the vector y is the transpose of x
z=x*x z=x*y dot(x,y) cross(x,y) A=[1,2;3,4] eig(A) inv(A) det(A) A3 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
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 nd the eigenvalues of A 5.3723 -2.0000 1.0000 nd 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 rst 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 nd all references to the word sinh send all output to the le le.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 le xdata and store the results in a variable called xdata
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)} = The inverse transform is denoted: L 1 {F(s)} = f (t).
0
H(t-a)
The Heaviside Step function is like a switch. At time t = a it turns on with value 1. The Heviside function is dened in MATLAB both as a numerical function and as a symbolic function. Its usage is heaviside(x).
24
2. LAPLACE TRANSFORMS
Index
Example Plot f (x) = H(x 1)(x2 + 1) MATLAB code % heavisideexample.m % plot H(x-1)*(x2+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
25
force
f(t) A
t - /2
t + /2
time
The impulse delivered by f (t) is I=
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
2. LAPLACE TRANSFORMS
Index
0, , and
t = t0 t = t0 ,
f (t) dt = 1.
(t t0 ) = 0, t = t0,
b
(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.
If the function f (t) has n well dened derivatives then: L f (n) (t) = sn F(s) sn1 f (0) sn2 f (0) f (n1) (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.
1 The
Index
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
L
0
f ( ) d
1 = F(s) s
2.2.8 Division by t
A corresponding theorem for integration is L 1 f (t) = t
F(s)ds
s
f ( )g(t )d
28
2. LAPLACE TRANSFORMS
Index
results in
Fs = 3/((s-4)2+9)
Example Find the Laplace Transform of the derivative of f (t) = e3t sinh(5t) + t 2 , that is nd d L dt e3t sinh(5t) + t 2 MATLAB code % find the Laplace Transform of the derivative of a function % f(t)=d/dt (exp(-3*t)*sinh(5*t) +t2 ) % 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)+t2))
results in
Fs = -15/((s+3)2-25)+5*(s+3)/((s+3)2-25)+2/s2
Index
29
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 nd these rewrite the right hand side over a common denominator so that it resembles the left hand side and then equate the coefcients 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 coefcient of s gives A+B = 3 and the constant term (s to the zero power) gives A + 3B = 13
30
2. LAPLACE TRANSFORMS
Index
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 coefcients 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
31
0 = A+D 2 = 4A 2D + E 1 = 4A
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 + 4) s(s s s +4
hence enabling the solution: 2e3t + 5et . Example L 1 2s + 1 s(s 2)2 = L 1 1/4 1/4 5/2 + + (using partial fractions) s (s 2) (s 2)2 1 1 2t 5 2t e + e t (using shifting theorem) = 4 4 2
32
2. LAPLACE TRANSFORMS
Index
A Bs +C + 2 s s +4
1 = A(s2 + 4) + (Bs +C)s and by equating coefcients we get: O(s2 ) : O(s1 ) : O(s0 ) : 0 = A+B 0 = C 1 = 4A
L 1
1 s(s2 + 4)
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 simplies to (s2 + 4s + 3)Y = 3s + 13 giving Y = 3s + 13 (s + 3)(s + 1) 5 2 + (using partial fractions, see before) = s+3 s+1 L eat = 1/(s a) we can invert to obtain y(t) = 2e3t + 5et . This illustrates how a transformation simplies the solution to differential equations.
As
Index
33
34
2. LAPLACE TRANSFORMS
Index
Example For example nd the partial fraction representation of F(s) = MATLAB code
% % % % find the partial fraction representation of B(s) 3s-1 ---- = ------------A(s) s2 +2s -15
3s 1 s2 + 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
B = 3 A = 1 R = 2 1 P = -5 3 K = [] 2 -15 -1
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 coefcient of each of the poles, in the same order as they are listed in P so here 2 1 F(s) = + s+5 s3
Index Example
35
MATLAB can also deal with repeated roots but care must be taken when interpretting the output. For example nd the partial fraction representation of F(s) = MATLAB code
% % % % find the partial fraction representation of B(s) s2 + 4s -2 ---- = -------------A(s) s3 -3s -2
s2 + 4s 2 s3 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
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 coefcient 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
2. LAPLACE TRANSFORMS
Index
results in
Fs = s/(s2-2*s+1) ft = t*exp(t)+exp(t)
Index
37
More complicated expressions are also straightforward including ones that result in Heaviside functions. Example Find the inverse Laplace Transform of F(s) = MATLAB code % find the inverse Laplace Transform of % exp(-5*s) % F(s) = ---------% s3-3s+-2 syms s t % make s and t symbolic variables Fs=exp(-5*s)/(s3-3*s-2) ft=ilaplace(Fs) e5s s3 3s 2
results in
38
2. LAPLACE TRANSFORMS
Index
1 (s + 2)(s 1)
Note that MATLABs 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 s1 3 s+2 x1 (t) = x2 (t) = 1 t 1 2t e e 3 3 2 t 1 2t e + e 3 3
2 1 1 1 + 3 s1 3 s+2
39
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 = (sX1 x1 (0)) + (sX2 x2 (0)) 3X1 3X2 = Now apply the initial conditions and rearrange to get (2s 2)X1(s) + sX2(s) = (s 3)X1(s) + (s 3)X2(s) = 1 s 2 s 1 s 2 s
Now solve this to get X1 (s) and X2 (s). Rearrange the rst equation to get X2 =
1 s
(2s 2)X1 s
(s 3)X1(s) + (s 3) s
(s + 3) (s 3)(s 2)s
(2s 2) s
(s+3) (s3)(s2)s
40
2. LAPLACE TRANSFORMS
Index
Take the inverse Laplace Transform to get the solution to the original system as x1 (t) = 1 5 + e2t 2e3t 2 2 5 2t 8 3t 1 + e + e x2 (t) = 6 2 3
3.1 Introduction
An Initial Value Problem (IVP) is a differential equation of nth order where the function value and the rst n 1 derivatives are all specied at one given point known as the initial point. For examples y = 2y2 y + 3y + 7y 3y = 0 with y(1) = 3
y(2) = 0.5
Also it can be a system of rst 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
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
3.5
4.5
5.5
Index
NUMERICAL APPROACH
43
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 Eulers method.
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 + ix and fi = f (xi , yi ). Eulers method is equivalent to approximating the solution by using the rst two terms in the Taylor Series (the function value and the rst derivative) and ignoring the higher order terms. The rst 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
Index
3.3.1 Graphically
Index
EULERS 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
Exact solution and Eulers method with N = 10 and N = 100 applied to y = y with y(0) = 1
2.8 2.6 2.4 2.2 2 1.8 1.6 1.4 1.2 1 0 0.2 0.4 0.6 0.8 1 Euler Exact
2.8 2.6 2.4 2.2 2 1.8 1.6 1.4 1.2 1 0 0.2 0.4 0.6 0.8 1 Euler Exact
N = 10, x = 0.1
N = 100, x = 0.01
EULERS METHOD
47
1. For each step compare the result of using one step of size x with the result of using two steps of size x . Call the difference between the solutions y. If the method 2 you are using is a pth order method then the error from a single step is (x) p+1 so y = y(xi + x) y(xi + 2 k(x)
p+1
x ) 2
p+1
x k 2 k(x) p+1 2p 1 1 p 2
x +k 2
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 > 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 = mRxold where m is a tuning parameter usually taken to be a little less than one (eg m = 0.95 or thereabouts).
48
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
EULERS 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 attens as larger steps can be taken for the same error.
1.4 1.35 1.3 1.25 1.2 1.15 1.1 1.05 1 0 Adaptive Exact
0.2
0.4
0.6
0.8
1.2
1.4
50
Index
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 Eulers 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 Eulers method that is rst 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 efciency? That is, how will the extra function evaluations be offset by the improvement in the calculation as it is a higher order method?
51
with y(0) = 1
2 /2
0.2
0.4
0.6
0.8
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 ops .
A op 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 (ops) the Midpoint method is far superior to Eulers method. Just compare the errors for the Euler 120 ops calculation (N = 20, error 0.05278) with the Midpoint 120 ops calculation (N = 10, error 0.00257).
52
Index
MATLAB code % midpoint.m % % solve y=xy subject to y(0)=1 % find the result at x=1 % for Euler and Midpoint methods and count flops. % clear all; N=10; % number of points, a=0; b=1; % initial and end points dx=(b-a)/N; % step size x(1)=a; y(1)=1; % initial x, y(Euler) values z(1)=y(1); % initial z(Midpoint) value flops(0) for i=1:N % Euler method y(i+1)=y(i)+dx*feval(midf,x(i),y(i)); x(i+1)=x(i)+dx; end eulerflops=flops flops(0); for i=1:N % Midpoint method zhalf=z(i)+dx/2*feval(midf,x(i),z(i)); z(i+1)=z(i)+dx*feval(midf,x(i)+dx/2,zhalf); x(i+1)=x(i)+dx; end midptflops=flops plot(x,y,b-) hold on exact=exp(0.5.*x.2); plot(x,exact,go); plot(x,z,r-) legend(Euler,Exact,Midpoint,2) hold off print -depsc midptexample erroreuler=abs(exact(N+1)-y(N+1)) errormidpt=abs(exact(N+1)-z(N+1))
Index
53
Named after 2 German Mathematicians, Carl David Tolme Runge (18561927) and Wilhelm Kutta (18671944) 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 nding 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 yi+1 = yi + x 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
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
f f |i + fi x |i x y f f = yi + x a fi + b fi + b x |i + b fi x |i x y
yi + x a fi + b f (xi , yi ) + x
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 gives the midpoint method. These methods are second order. Because there is still a degree of freedom in the equations for specic 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 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); To solve the IVP b=1
= =
1 2
Use help ode23 to nd 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 nd the solution more accurately in a given region of the interval.
55
dy = ty with y(0) = 1 dt
2 /2
% 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 1.6 1.5 1.4 1.3 1.2 1.1 1 0 ode23 Exact
0.2
0.4
0.6
0.8
56
Index
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
Example Solve y = t 2 cos(t)y s.t. y(0) = 2 and y (0) = 3 MATLAB code % o45ex1.m % solve y=t2*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=t2*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)=t2.*y(1).*cos(t); f=f(:); % forces f to be a column vector return
30 y dy/dt 20
10 y and dy/dt
10
20
30 0
2 t
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 103 and the absolute error tolerance to 1 104 for the rst two components and 1 105 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 le func.m must return appropriate information. func.m is coded so that if ode45 passes it a ag with a value of events it determines the .m le that tells it information about the stopping criteria. If the ag is empty it determines the .m le that is actually used to calculate the integration (the DEs).
60
Index
dv dt d dt dx dt dy dt
= g sin
= v cos = v sin
Then just numerically integrate these equations forward in time form the known initial position, velocity and angle.
PROJECTILE EXAMPLE
61
% 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
>> 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
10
MATLAB can be set up to integrate the DE until a specic event occurs, for example here that might be when y = 0 (ie when the projectile hits the ground). You can then use fzero to nd the initial angle that gives a specic range (ie hits the target).
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 rst 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 2 x2 1 0 1 2 1
0.5
0.5
1
1
1.5
2.5
64
Index
MATLAB code
% ppexample.m % drawing phase plane for % x1=x2 % x2=-2x1-x2 % tend=10; tspan=[0 tend]; y0=[0 5]; [t,y]=ode45(ppexamplef,tspan,y0); plot(y(:,1),y(:,2)) xlabel(x_1); ylabel(x_2); print -depsc ppexample
MATLAB code
Index
INTRODUCTION
65
Or with multiple initial points put the whole thing into a for loop and update the initial points as you go through the loop. MATLAB code
% ppexample2.m % drawing phase plane for % x1=x2 % x2=-2x1-x2 % tend=10; tspan=[0 tend]; for s=-2:1:3 y0=[s 5]; [t,y]=ode45(ppexamplef,tspan,y0); plot(y(:,1),y(:,2)) xlabel(y_1); ylabel(y_2); hold on end hold off print -depsc ppexample2
5 4 3 2 1 y 0 1 2 3 4 2 1 0 1 2 3 4 5
2
y1
66
Index
INTRODUCTION
67
% 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 1 0 1 2 3 4 5 5 0 x 5 y
68
Index
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 J(y1 , y2 ) =
y1 y1 y2 y2
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?
PREDATOR-PREY MODEL
69
% 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 predator prey 250 250 300
200
200
150
150
100
100
50
50
0 0
20
40
60
80
100
120
0 0
50
100
150
200
250
300
Time plot
70
Index
(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 >> 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 For critical Jac = -0.2000 0 V = 1.0000 0 D = -0.2000 0 point (50.00 -0.1500 -0.0500 -0.7071 0.7071 0 -0.0500 0.00)
COMPETITION MODEL
71
0.00)
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
72
Index
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
73
This is (for obvious reasons) known as an SIR model. The total population is xed 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
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 rst 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
75
( SI + K S) ( SI I I)
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 nal state. So what is the nal 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
Index
MATLAB code
% sir.m % this M-file plots velocity vectors for the SIR model, plots % some trajectories, calculates evalues and evectors 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 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
77
78
Index
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 y(b) = 3
where
y(a) = 1
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
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
6 5.5 5 4.5 4 3.5 3 2.5
SHOOTING METHOD
81
c guess
20 18 16
10
Hence there is a solution that has y(2) = 3 somewhere between y (0) = 16 and y (0) = 18. How do you go about nding this value of y (0) that gives the correct value of y(2) ? Treat the problem as a zero nding problem. That is nd the value of y (0) = c that results in f (c) = y(2) 3 = 0. So use one of MATLABs methods for nding zeros of functions.
82
Index
Set it up so that there is a MATLAB function that takes as input the guess for the initial slope and returns the error in the right hand boundary condition. Then call this function from fzero to nd the correct slope that integrates to the correct right hand boundary condition. MATLAB code % shootingexamplemain.m % the main M-file for using the shooting method to solve the % DE defined in shootingexamplef.m % Uses fzero to solve the equation y(0)=3 where y(0) is % determined in shootingfunc.m by shooting forward using ode45 % myguess=4; % a guess at the value of y(0) actualvalue=fzero(shootingfunc,myguess) % use ode45 to get correct solution using actualvalue and plot tspan=[0 2]; y0=[1 actualvalue]; [t,y]=ode45(shootingexamplef,tspan,y0); plot(t,y(:,1)) print -deps shootingexampleplot MATLAB code function f=shootingfunc(c) % set up as a function, the input (c) is the initial slope % y(0) and the output (f) is the difference between the value % of y at x=2 and the number 3 (since y(2)=3 for the correct % solution. This function is called by MATLABs fzero % % shootingfunc.m % trying to solve y + 3y + 2y = 2x y(0)=1 y(2)=3 % using y(0)=1 and y(0)=c tspan=[0 2]; y0=[1 c]; [t,y]=ode45(shootingexamplef,tspan,y0); f=y(length(y),1)-3; % set function value to y(2)-3 return MATLAB code function f=shootingexamplef(t,y) % shootingexamplef.m % the DE y + 3y + 2y = 2x written as a system f(1)=y(2); f(2)=-3*y(2)-2*y(1)+2*t; f=f(:); return
Index
SHOOTING METHOD
83
84
Index
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 rst 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 dx
n
yn yn1 h
Both the forward and backward approximations are rst order accurate, that is the rst neglected term is order x. There is in fact a second order accurate approximation known as the central approximation given by dy yn+1 yn1 dx n 2h These can be used to nd approximations to the higher derivatives.
d 2y dx2
This is the central approximation to the second derivative and is second order accurate.
Index
RELAXATION METHODS
85
1. First divide up the region into N subintervals of width h where h= so xn = a + hn for n = 0 . . . N 2. Replace each derivative by its approximate value at each interior point. yn+1 yn1 yn+1 2yn + yn1 +3 + 2yn 2xn 2 h 2h for n = 1 . . . N 1 ba N
This results in N 1 equations in the N 1 unknowns y1 , y2 , . . . yN1 . 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 yn+1 yn1 yn+1 2yn + yn1 +3 + 2yn 2xn 2 h 2h for for n=1 n = 2 . . .N 2
for n = N 1
86
Index
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)yN3 + (2h2 2)yN2 + (1 + 3h/2)yN1 = 2h2 xN2 (1 3h/2)yN2 + (2h2 2)yN1 + (1 + 3h/2)yN = 2h2 xN1 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)yn1 + (2h2 2)yn + (1 + 3h/2)yn+1 = 2h2 xn for n = 2...N 2
(1 3h/2)yN2 + (2h2 2)yN1 = 2h2 xN1 (1 + 3h/2)yN 5. This is a system of N 1 equations in the N 1 unknowns y1 , y2 , . . . yN1 . 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 coefcients as above, Y is the vectors of unknowns Y = (y1 , y2 , . . . , yN1 )T and b is the known right hand side vector.
Index
RELAXATION METHODS
87
1 3h/2 0 0 A= . . . 0 0
2h2 2
0 0 1 + 3h/2 2h2 2 . . . 0 0
0 0 0 1 + 3h/2 . . . 1 3h/2 0
0 0
b=
2h2 x1 (1 3h/2)y0 2h2 x2 2h2 x3 2h2 x4 . . . 2h2 xN2 2h2 xN1 (1 + 3h/2)yN
0 0 . . . 1 + 3h/2 2h2 2
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
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
RELAXATION METHODS
89
% 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*h2-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*h2*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)
90
Index
RELAXATION METHODS
91
% 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*h2-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=h2*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
160 140 120 100
N = 10
400 350 300 250
80
200
60 40 20 0 20 0.5 1 1.5 2 2.5
150 100 50 0 0
0.5
1.5
2.5
N = 20
400 350 300 250 200 150 100 50 0 0 400 350 300 250 200 150 100 50 0.5 1 1.5 2 2.5 3 0 0
N = 100
0.5
1.5
2.5
92
Index
Index
RELAXATION METHODS
93
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= Example What does y + 3y + 2y = 5x2 y(0) = 2 y(2) = 3 become when it is transformed to have homogeneous boundary conditions 1 (yL yR ) xL xR b= 1 (xR yL + xL yR ) xL xR
94
Index
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 L y = 5x
2
where
d2 d L 2 + 3x2 + 2ex dx dx
y(xL ) = 0 y(xR ) = 0
The aim is to nd the coefcients 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
n
i=1
cii(x) r(x)
i=1
Index
RELAXATION METHODS
95
3. Establish a set of conditions that allow us to nd the coefcients ci . To do this choose a set of weight functions wi (x) for i = 1, . . . , n. Again these are as yet unspecied. These weight functions have the following properties (a) They are normalised so that
xR xL
w j (x)dx = 1
for
j = 1, . . . , n
w j (x)(x)dx = 0
for
j = 1, . . ., n
4. It is this last condition 3(b) that gives conditions that enable the coefcients ci to be found. By substituting the denition of (x) above into this expression we get for each j = 1, . . ., n
xR xL xR xL xR xL n n
w j (x)(x)dx = 0 dx = 0
w j (x)
i=1
w j (x) ci L i (x)dx
i=1
w j (x)r(x)dx = 0
xR xL
i=1
ci
xR xL
w j (x)L i (x)dx =
w j (x)r(x)dx
If the weight functions (w j (x)) and the expansion functions ( j (x)) are specied then the right hand side of the last expression is known. bj = and the part
xR xL xR xL
w j (x)r(x)dx
w j (x)L i (x)dx = M ji
i=1
ci M ji = b j
for
j = 1, . . . , n
This is just the matrix equation Mc = b So provided w j (x) and j (x) are specied then the coefcients 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 y(x) Y (x) = ci i (x)
i=1 n
96
Index
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 nite element method uses triangular hat functions for both the weight functions (wi (x)) and the expansion functions (i (x)).
1 h (x xi1 ) 1 h (xi+1 x)
for for
i (x) =
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 .
x0
x1
x2
x3
x4
x5
x6
The benet 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.
xR
F(x)
i i1
i1
xL
i F(x) dx
(zero for all other indices) (zero for all other indices) (zero for all other indices)
2h/3 h/6 1/2 2/h 1/h h ih2 (i2 + 1/6)h3 i(i2 + 1/2)h4
i i1
1 x x2 x3
Index
RELAXATION METHODS
97
y(0) = 0
y( /2) = 0
/2
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 . . . h/6 2h/3 h/6 0 0 . . . . . + . . . .. . . . . . . . . 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 = =
0
... ... .. .
0 0 . . .
0 0 . . .
/2
0
w j (x)r(x)dx
/2
j (x) 1 dx
n
b= h h
h h h . . .
98
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*h2)*diag(ones(1,N)); % main diagonal elements M=M+(1+h2/6)*diag(ones(1,N-1),-1); % one below main diagonal M=M+(1+h2/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
RELAXATION METHODS
99
function f=feexamplef(c,x) % feexamplef.m % function that calculates y(x)=sum(c(i)*phi_i(x)) % where the phi_i are the triangular hat functions centered around x % global a b N h=(b-a)/(N+1); xk=a+h:h:b-h; if (x <= xk(1)) % left most function f=c(1)*(x-a)/h; end for i=1:N-1 % all interior functions that have left and right parts if (x >= xk(i)) & (x <= xk(i+1)) right=c(i)*(xk(i+1)-x)/h; left =c(i+1)*(x-xk(i))/h; f=left+right; end end if (x >= xk(N)) % right most function f=c(N)*(b-x)/h; end return
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.2
0.4
0.6
0.8
1.2
1.4
1.6
100
Index
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) =
1 h (x xi1 ) 1 h (xi+1 x)
xf x0
F(x)
i i1
i1
x0
i F(x) dx
(zero for all other indices) (zero for all other indices) (zero for all other indices)
2h/3 h/6 1/2 2/h 1/h h ih2 (i2 + 1/6)h3 i(i2 + 1/2)h4
i i1
1 x x2 x3
Index
101
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 dont 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 nd. 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
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 nd 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 dened to within some arbitrary constant (An ). To specify a specic 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 xes 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.
103
function f=deig(omega0) % deig.m % % solving y + omega2 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
1.2
0.8
0.6
0.4
0.2
0 0
0.5
1.5
2.5
3.5
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 2.5 3 3.5
105
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 satised. How do you nd that particular value?
106
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 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
107
y(1)-4 = 0.15451914 y(1)-4 = 0.27958057 y(1)-4 = 0.02927189 y(1)-4 = 0.33132966 y(1)-4 = -0.02266307 y(1)-4 = 0.00004514 y(1)-4 = 0.00000001 y(1)-4 = 0.00000000 y(1)-4 = 0.00000000
4.5
3.5
2.5
2 0
0.2
0.4
0.6
0.8
108
Index
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 dene exactly what a partial differential equation (PDE) is and dene what is meant by a boundary condition and an initial condition.
u 2u = 2, t x
f = x
f y
c c c + + = c. x y z
(6.1)
In the rst 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
u 2u = 2, t x
are linear, whereas
f f f +x =z , x y z
4c 2c + +c = 0 x4 x2
(6.2)
u = t
are not.
u x
f = ef , x
4c + c2 = 0 x4
(6.3)
Index
111
F 2F =D 2 t x
2F 2F + =0 x2 y2
2 2F 2 F =c t2 x2
112
Index
F 2F =D 2 t x
(6.4)
where D is a positive constant and is called the diffusion coefcient. This partial differential equation occurs in studies of heat conduction and diffusion of mass (eg. pollutants or chemicals).
F 2F 2F =D + 2 t x2 y
F 2F 2F 2F =D + 2 + 2 t x2 y z
(6.5)
F = D2 F. t
Index
LAPLACES EQUATION
113
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).
114
Index
2F 2F = c2 2 t2 x
(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.
2F 2F 2F = c2 + 2 t2 x2 y
respectively. More generally, we write
2F 2F 2F 2F = c2 + 2 + 2 t2 x2 y z
(6.8)
2F = c2 2 F. t2
Index
POISSONS EQUATION
115
2F 1 F 1 2F 2F + F= 2 + + r r r r 2 2 z2
2
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
Index
F (10,t) = 100. x
The rst condition species the value of F, at the point x = 0, and the second species the value of the derivative of f at the point x = 10, for all time.
Index
117
Neumann The value of the normal derivative is given at the boundary. For example F (10,t) = 100 which can also be written F = 100 at x = 10 x x
118
Index
Cauchy Values of both the function and its normal derivative are given at the boundary. For example F = F at x = 10 x
Classify (with reason) each boundary condition of the following PDEs as Dirichlet, Neumann or Cauchy. (a)
d2T dy2
T (L, y) = sin y
dT dy (x, M) =
(b)
dT dt
d2 T dx2
with T (0,t) = 0
dT dx (L,t) =
0 T (x, 0) = f (x)
(c)
dT dt
d2T dx2
dT dx (L,t) =
T Ta
T (x, 0) = f (x)
Index
119
120
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 Newtons 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.
EXAMPLES
121
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 Newtons Law of Cooling, the sides are held at 20 C, write down the governing equation and boundary conditions.
122
Index
Plucked String Consider a string of length L that is held xed 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 Drum
EXAMPLES
123
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
Index
2u 2u 2u u u + 2B +C 2 = F(a, b, u, , ) 2 a a b b a b
where A, B and C are constants and F(a, b, u, u , u ) is an arbitrary function of those a b variables. The classication 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 = u . t
One Dimensional Wave equation Has A = 1, B = 0, C = 1 , a = x (the space variable), b = t (the time variable) and F = 0. c2
where yn = y(xn ) and x is the difference in x steps or just the space between x points. First derivative Backward Approximation dy dx yn yn1 x
Both the forward and backward approximations are rst order accurate, that is the rst neglected term is order x. First derivative Central Approximation given by dy dx yn+1 yn1 2x
126
Index
F 2F =D 2 t x
with boundary conditions F(a,t) = Fa (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 F(b,t) = Fb (t)
(7.1)
and let the variable i be the index that refers to the discrete x points. That is xi = a + ix 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 = jt j = 0, 1, . . ., Nt
This has now divided the (x,t) plane into a grid. The aim is to calculate the solution at each of the grid points.
Index
FTCS METHOD
127
t
j = 10 j=9 j=8 j=7 j=6 j=5 j=4 x j=3 j=2 j=1 t
t =0
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8
x=a
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
128
Index
j
Let the solution at the point (xi ,t j ) be denoted by Fi , that is Fi j = F(xi ,t j ) = F(a + ix, jt) Now approximate the diffusion equation
F 2F = 2 t x
around the point (xi ,t j ) by using a forward nite difference approximation for the time derivative j+1 j F j Fi Fi t i t and a centred nite difference approximation for the spatial second derivative
j i j j j
2F x2
Fi =D t
now make Fi
j+1
the subject Fi Fi
j+1
= tD
j
+ Fi
j+1
(7.2)
where
Dt (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
129
j+1 j
t =0
i1 i i+1
x=a
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
130
Index
T 2T =D 2 t x
where for steel D = 5 106 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 90 80 70 Temperature 60 50 40 30 20 10 0 0 0.5 1 x 1.5 2 numerical exact
FTCS METHOD
131
% 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 m2/s dt=3600 % time step in seconds (=60 minutes) tfinal=36000; % final time in seconds (=600 minutes) gamma=D*dt/(dx2) 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*(m2*pi2/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
132
Index
7.2.4 Stability
The stability of a time stepping numerical method is all to do with whether errors grow or decay as you step in time. Consider the forward time approximation to the time derivative at the point (xi ,t j )
F t
j i
Fi
j+1
Ti 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 where
j+1
=
that if
Dt (x)2
<
1 2 1 2
then the method is stable and errors do not grow then the method is unstable and errors grow
>
If > 1 then the method is totally useless! So it is essential that you keep track of the 2 size of and ensure that it stays below 1 at all times. This is particularly important if the 2 diffusion coefcient 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 106 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
Temperature in metal bar at steps of 120 minutes 250 200 150 Temperature 100 50 0 50 100 150 0 numerical exact
0.5
1 x
1.5
Disaster !
134
Index
F t
Fi
j+1
j i
1 =D 2
j+1
2F x2
j+1
j+1 i
2F + x2
j
j i j j
Fi D = t 2
j+1
Which rearranges to
Dt (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 nite-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
j+1
i1
i+1
Index
CRANK-NICOLSON METHOD
135
A=
0 1 + /2 /2 1 + /2 . . . . . . . . . 0 0 0 0 0 0 0 1 /2 /2 1 /2 . . . . . . . . . 0 0 0 0 0 0
... ... .. .
. . . 1 + /2 . . . /2 1 + 0 0 . . . 0 0 . . .
0 0 . . .
0 0 . . .
B=
j j+1 Fa + Fa
... ... .. .
. . . 1 /2 . . . /2 1 F1j+1 F2j+1 0 . . . 0
j+1 FNx 1
dj = 2
0 0 . . . 0 j j+1 Fb + Fb
j F =
F1 j F2 0 . . . 0
j FNx 1
j+1 F =
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 efcient 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.3.3 Example
Solve the heat diffusion problem of a steel bar of length 2m with one end held at 100C 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 106 m2 /s The boundary and initial conditions can be written T (0,t) = 100 T (2,t) = 0 T (x, 0) = 0
CRANK-NICOLSON METHOD
137
% 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 m2/s dt=144 % time step in seconds (plot every 100th) tfinal=144000; % final time in seconds (=4 hours) gamma=D*dt/(dx2) % 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
Index
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
For i = 1, 2, 3, . . ., N 1 we now have a system of (N 1) ODEs in the variables T1 , T2 , T3 , . . . , TN1 . For example dT1 = (T0 2T1 + T2 ) = ( fa 2T1 + T2 ) dt dT2 = (T1 2T2 + T3 ) dt dT3 = (T2 2T3 + T4 ) dt ... = ... dTN2 = (TN3 2TN2 + TN1 ) dt dTN1 = (TN2 2TN1 + TN ) = (TN2 2TN1 + fb ) dt where = D/(x)2 . With the initial condition giving us the initial values for each of T1 , T2 , T3 , . . ., TN1 . This system of ODEs is easily solved numerically using any of the ODE solvers mentioned previously (eg Eulers, 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.
METHOD OF LINES
139
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
MATLAB code function f=molf(t,y); % % molf.m % the system of equations global n Ta Tb beta f(1)=beta*(Ta-2*y(1)+y(2)); for i=2:n-2 f(i)=beta*(y(i-1)-2*y(i)+y(i+1)); end; f(n-1)=beta*(y(n-2)-2*y(n-1)+Tb); f=f(:); return
T 2T =D 2 t x
with T (0,t) = 100 T (2,t) = 50 T (x, 0) = 100 25x(x 1)
100
90 Temperature
80
70
60
50 0
0.5
1 x
1.5
Index
141
u r
known initial condition unknown points known boundary points Example The different types of boundary conditions can all be derived from Newtons Law of cooling. For example consider a steel bar where the end at x = 0 loses heat according to Newtons Law of cooling with a heat transfer coefcient 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 ow 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
142
Index
T x
i=0
T1 T1 2x
where T1 is a ctitious point. That is it is an articial point that is not in the domain of interest. So how does this help? Consider the case of an insulated boundary
T x
= 0.
T x
Using the above expression and the fact that the boundary is insulated so that get a relationship between T1 and T1 in this case this is simply that T1 = T1 But recall that we are dealing with the Diffusion Equation
= 0 we
T 2T =D 2 t x
using centred differencing on the second derivative around the point i = 0 results in
2T x2
i=0
T1 2T0 + T1 (x)2
2T x2
i=0
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 modied 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 = (T1 2T0 + T1 ) = (2T1 2T0 ) dt dT1 = (T0 2T1 + T2 ) dt dT2 = (T1 2T2 + T3 ) dt ... = ... dTN2 = (TN3 2TN2 + TN1 ) dt dTN1 = (TN2 2TN1 + TN ) = (TN2 2TN1 + fb ) dt
Index
143
T 2T =D 2 t x
with
T (2,t) = 50
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
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
100
90 Temperature
80
70
60
50 0
0.5
1 x
1.5
Index
145
T = h(T Ta ) x
the derivative of the function is given in terms of the unknown value at the boundary that must also be determined. After discretizing in the x-direction use centered differencing on the derivative term, for example centred differencing about i = 0 gives
T x
i=0
T1 T1 = h(T0 Ta ) 2x
where T1 is a ctitious point and Ta is the ambient temperature. This can be rearranged to get the ctitious point T1 in terms of the known points T0 and T1 . T1 = T1 2xh(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
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 T (x, 0) = 50 25 if x = 1 the hot spot elsewhere
Not that this equation can not be solved analytically! Use FTCS nite differences to get Ti, j+1 = (Ti+1, j + Ti1, j ) + (1 2 )Ti, j + t(Ti, j Ta )2
0.5
1 x
1.5
NONLINEAR EXAMPLE
147
% 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 m2/s dt=.001; % time step in seconds tfinal=1.48; % final time in seconds gamma=D*dt/(dx2) 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
2F 1 F 1 2F 2F + + + r 2 r r r 2 2 z2
The benet of using a coordinate system that matches the object is that often it is possible to reduce the dimension of the problem.
y
u = h(u ua ) at r = 0.5 r
u = D2 u t
which can be written in circular coordinates as
u 2F 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
149
around the disc. Hence there will not be any dependence and so any derivatives with respect to are zero. So the governing PDE becomes
u 2u 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 1 u appears to be r r undened at r = 0. In fact this term is not undened since at r = 0 u = 0 as well due to r the boundary condition. This difculty can be overcome by using LHopitals Rule.
r0
r0
lim g(r) = 0
then
u 2u u = D + r 2 t r r 2 u r2 = D + r2 1
= 2D
2u
2u r2
So in any numerical method this alternative PDE must be used if at the point r = 0.
150
Index
t
j = 10 j=9 j=8 j=7 j=6
u r
=0
j=5 j=4 r j=3 j=2 j=1 t
u r
= h(25 u)
t =0
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8
r=0
Initial condition known u(r, 0) = 100 unknown points
r = 0.5
151
u 2u 1 u =D + t r2 r r
so use Forward Time and Centred Space nite difference around the point (i, j) to approximate this by ui
j+1
ui =D t
Rearranging this to get the new time level as the subject results in ui
j+1
j j j ui+1 ui1 + ui 2i
Dt 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 ctitious point beyond the region of interest. The boundary condition is u r = h(u ua ) which on using central nite differences is
u r
j Nr
uNr +1 uNr 1 2r
= h(uNr ua )
uNr +1 = 2rh(uNr ua ) + uNr 1 This value is then used in the FTCS scheme above whenever uNr +1 is used.
j
152
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 u = 0 which on using central nite differences is r
u r
j j u1 u1 =0 2r 0 j j
which rearranges so that the ctitious point u1 can be written as u1 = u1 The PDE at r = 0 (which is i = 0) was found previously using LHopitals Rule to be
j j
u 2u = 2D 2 t r
so use Forward Time and Centred Space nite difference around the point (0, j) to approximate this by j j j j+1 j u1 2u0 + u1 u0 u0 = 2D t (r)2 Using the value for the ctitious point (u1 ) above and rearranging gives the governing FTCS equation for the r = 0 point as u0
j+1 j
= 4 u1 u0 + u0
j j j
153
% ftcs_cyl.m % Solve the heat diffusion equation on a steel disc radius 0.5m % with Newtons 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 = 5x106 m2/s h=10.0; % heat loss coefficient dt=30 % time step in seconds tfinal=30000; % final time in seconds gamma=D*dt/(dr2) % 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
0.1
0.2 radius
0.3
0.4
0.5
Elliptic equations (such as Laplaces 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.
20C
The governing PDE is Laplaces Equation in 2D
10
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
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 + ix and y j = 0 + jy. Turning to the governing PDE discretise the second derivative terms using centred differencing in the appropriate direction to get Ti+1, j 2Ti, j + Ti1, j Ti, j+1 2Ti, j + Ti, j1 + 0 (x)2 (y)2 That is the template used is a cross pattern
j+1 j j1
i1
Rearrange the equation to get
i+1
2 (y)2 + (x)2 Ti, j = (y)2 Ti+1, j + Ti1, j + (x)2 Ti, j+1 + Ti, j1 and so we get an equation for the central point in terms of its 4 neighbours as Ti, j = 1/2 (y)2 + (x)2 (y)2 Ti+1, j + Ti1, j + (x)2 Ti, j+1 + Ti, j1
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
n+1 Ti, j =
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.
157
% 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=dx2; dy2=dy2; x=0:dx:L; y=0:dy:M; V=20*ones(Nx+1,Ny+1); % initialise solution everywhere to 20 V(:,Ny+1)=2*x.*(10-x)+20; % boundary condition on y=5 Vnext=V; number_iter=0; denom=2*(dx2+dy2); % this is used often so calculate it outside loop % loop through until solution converges while delta > eps number_iter=number_iter+1; maxdiff=0; for i=2:Nx for j=2:Ny Vnext(i,j)=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(V(i,j-1)+V(i,j+1)))/den dV=V(i,j)-Vnext(i,j); % the difference between successive solut % keep track of the maximum difference if abs(dV) > maxdiff; maxdiff = abs(dV); end end end V=Vnext; delta=maxdiff; end number_iter % show how many iterations were used % plot the solution in 3D in 3 ways, surface plot, mesh plot, contour figure(1) surf(x,y,V) % surf plots the surface colorbar % put a color bar next to it showing what each color shading interp % use interpolation for the coloring (smooths the col xlabel(x); ylabel(y); zlabel(Temperature); title(Steady State Temperature); print -depsc laplace_direct_3D figure(2) mesh(x,y,V) % mesh plots the surface with the mesh stretched over colorbar % put a color bar next to it showing what each color xlabel(x); ylabel(y); zlabel(Temperature); title(Mesh plot of temperature) print -depsc laplace_direct_mesh figure(3) contour(x,y,V) % cont plots a contour plot axis image xlabel(x); ylabel(y); title(Contour plot of temperature) print -depsc laplace_direct_contour
158
Index
Index
159
160
Index
This is obtained by articially increasing the incremental change to the solution for i=2:Nx for j=2:Ny Vnext=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(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 specic value but is different for each problem. So how do you know what value to use? The best method is to nd 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 gure 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 Laplaces equation.
Iterations verses omega for SOR 120 110 100 number of iterations 90 80 70 60 50 40 30 1 Optimal value of omega
1.2
1.4 omega
1.6
1.8
162
Index
8.3.2 Example
Temperature in a house wall on a Canberra winter night Consider the problem of a long house wall 2m high and 25cm thick. The wall is at thermal equilibrium. The outside temperature is 5C the inside temperature 22C. The wall foundations are in the ground at 15C. The top of the wall is insulated by insulation in the ceiling of the house. The governing PDE is Laplaces Equation in 2D
2T 2T + 2 =0 x2 y
subject to the boundary conditions
x=0 x = 0.25
T = 0 on y
y=2
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 0.05 0.1 0.15 0.2 0.25 20 18 16 14 12 10 8 6 4 2
GAUSS-SEIDEL
163
% 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*(dx2+dy2); % 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=(dy2*(V(i+1,j)-2*dx*h*(V(i,j)-insidetemp)+V(i+1,j))... +dx2*(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=(dy2*(V(i+1,j)-2*dx*h*(V(i,j)-insidetemp)+V(i+1,j))... +dx2*(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=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(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=(dy2*(V(i-1,j)+V(i+1,j))+dx2*(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=(dy2*(V(i-1,j)+V(i-1,j)-2*dx*h*(V(i,j)-outsidetemp))... +dx2*(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=(dy2*(V(i-1,j)+V(i-1,j)-2*dx*h*(V(i,j)-outsidetemp))... +dx2*(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
164
Index
8.3.3 Example
A cylinder of height 2m and radius 1m has the bottom and sides held at 0C and the top satises a quadratic temperature distribution with maximum 1C at the centre and 0C at the edge. Write down the governing equation, boundary conditions and nd the steady state temperature distribution using MATLAB.
GAUSS-SEIDEL
165
% 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-r2 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*(dr2+dz2); % 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*dz2*(V(i+1,j))+dr2*(V(i,j-1)... +V(i,j+1)))/(2*dr2+4*dz2); V(i,j)=Vnext; for i=2:Nr Vnext=(dz2*(V(i-1,j)+V(i+1,j))+dr2*(V(i,j-1)+V(i,j+1))... +dz2/(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
0.8
0.6
0.4
0.2
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
F (x, 0) = V0 (x) t
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 nite differences to approximate the derivatives in the wave equation gives j j j j+1 j j1 Fi 2Fi + Fi 2 Fi+1 2Fi + Fi1 =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 Fi where
j+1
j1
2 =
That is the template used is a cross pattern
c2 (t)2 (x)2
j+1 j j1
i1
i+1
The signicant 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
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 F 1 Fi0 F = V0 i = i t i t Rearrange to get values at time level j = 1 as Fi1 = Fi0 +V0 i t Now the rst 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 xed at the ends. Initial the string is pulled to a parabolic shape between x = 25 and 33cm and then released. The governing differential equation is the one dimensional wave equation
2F 2F = c2 2 t2 x
subject to the boundary conditions F(0,t) = 0 and initial conditions F(x, 0) = given parabolic shape F(100,t) = 0
F (x, 0) = 0 t
EXAMPLE
169
% 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 y 1 1 y 1 1 y 1 1 y 1 1 y 1
t=0
1 y 1
t = 0.10328
1 y 1
t = 0.20656
t = 0.30984
1 y 1
t = 0.41312
1 y 1
t = 0.5164
t = 0.61968
1 y 1
t = 0.72296
1 y 1
t = 0.82624
t = 0.92952
1 y 1
t = 1.0328
1 y 1
t = 1.1361
t = 1.2394
1 y 1
t = 1.3426
1 y 1
t = 1.4459
170
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 reects from the rigid (xed) 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.
10 Concluding Remarks
172
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 Eulers method, 43 example, 45 explicit, 135 Fasto, 171 nite differences, 84, 125, 156, 167, 171 nite element, 93 nite 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 LHopitals rule, 149 Laplace transform, 23 convolution theorem, 27 denition, 23 derivative, 26, 27 rst shifting theorem, 26 integral, 27 inversion, 29 MATLAB, 33 second shifting theorem, 26 systems, 38 theorems, 27 Laplaces equation, 111, 113, 155 Laplacian, 113, 148 linear, 110 linear equations, 6 Lokta-Volterra, 68 lookfor, 8 M les, 1 running, 7 web site, 1 MATLAB ilaplace, 36
174
INDEX plotting, 3 3D, 5 data, 4 Poissons 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
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
saddle, 68, 75 seismic, 114 sending output to a le, 7 shooting, 102 example, 82 shooting method, 80, 82 Neumann boundary condition, 117, 141, 142 Simpsons 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 eld, 66 MATLAB, 33 velocity potential, 113 non linear factors, 31 wave equation, 111, 114, 167 repeated factors, 30 web site, i, 1 PDE, 109 classication, 124 denitions, 110 elliptic, 124, 155 examples, 109 hyperbolic, 124, 167 linear, 110 parabolic, 124, 125 separation of variables, 119 phase plane, 63