INTRODUCTION TO

Cho, Hyoung Kyu

Department of Nuclear Engineering Seoul National University 10.

10.1 Background 10.11 Local Truncation Error in Second‐Order 10.2 Euler's Methods Range‐Kutta Method 10.3 Modified Euler's Method 10.12 Step Size for Desired Accuracy 10.4 10.13 Stability 10.5 Runge‐Kutta Methods 10.14 Stiff Ordinary Differential Equations 10.6 Multistep Methods 10.7 Predictor‐Corrector Methods 10.8 System of First‐Order Ordinary Differential Equations 10.9 Solving a Higher‐Order Initial Value Problem 10.10 Use of MATLAB Built‐In Functions for Solving Initial‐Value Problems 10.1 Background  Ordinary differential equation  A differential equation that has one independent variable  A first‐order ODE . The first derivative of the dependent variable with respect to the independent variable

 Example  Rates of water inflow and outflow

 The time rate of change of the mass in the tank

 Equation for the rate of height change 10.1 Background  Time dependent problem

 Independent variable: time  Dependent variable: water level

 To obtain a specific solution, a first‐order ODE must have an initial condition or constraint that specifies the value of the dependent variable at a particular value of the independent variable.

 In typical time‐dependent problems . Initial condition . Initial value problem (IVP) 10.1 Background  First order ODE statement  General form

 Ex)

. Flow lines

 Analytical solution  In many situations an analytical solution is not possible!

 Numerical solution of a first‐order ODE  A set of discrete points that approximate the function y(x)  Domain of the solution: ,  N subintervals 10.1 Background  Overview of numerical methods used/or solving a first‐order ODE  Start from the initial value  Then, estimate the value at a second nearby point  third point  …

 Single‐step and multistep approach

. Single step approach:  . Multistep approach: ⋯, ,, 

 Explicit and implicit approach . Right hand side in explicit method: known values

. Right hand side in implicit method: unknown value

– In general, non‐linear equation  non‐linear equation solution method !

. Implicit methods provide improved accuracy over explicit methods, but require more effort at each step. 10.1 Background  Errors in numerical solution of ODEs  Round‐off errors  Truncation errors . Numerical solution of a differential equation  calculated in increments (steps) . Local truncation error: in a single step . Propagated, or accumulated, truncation error – Accumulation of local truncation errors from previous steps

 Single‐step explicit methods

 Euler’s explicit method: slope at ,  Modified Euler’s explicit method: average slope at , and ,  Midpoint method: slope at /2  Runge‐Kutta methods . A weighted average of estimates of the slope of at several points 10.2 Euler’s Method  Euler’s method step size is exaggerated !  Simplest technique for solving a first‐order ODE . Explicit or implicit

 Euler's Explicit Method

 The error in this method depends on the value of and is smaller for smaller h.

 Derivation  Numerical integration or finite difference approximation of the derivative

(rectangle method)

(forward ) 10.2 Euler’s Method  Example 10‐1: Solving a first‐order ODE using Euler's explicit method. 10.2 Euler’s Method  Example 10‐1: Solving a first‐order ODE using Euler's explicit method.

% Solving Example 8-1 clear all a=0; b=2.5; h=0.1; yINI = 3; [x, y] = odeEULER(@Chap8Exmp1ODE,a,b,h,yINI); xp=a:0.1:b; yp=70/9*exp(-0.3*xp)-43/9*exp(-1.2*xp); plot(x,y,'--b',xp,yp) xlabel('x'); ylabel('y')

function dydx = Chap8Exmp1ODE(x,y) dydx = -1.2*y + 7*exp(-0.3*x);

function [x, y] = odeEULER(ODE,a,b,h,yINI)

x(1) = a; y(1) = yINI; N = (b-a)/h; for i = 1:N x(i+1) = x(i) + h; y(i+1) = y(i) + ODE(x(i),y(i))*h; end 10.2 Euler’s Method  Analysis of truncation error in Euler's explicit method  Local truncation error  Global truncation error

 Taylor series expansion at position 1

 Numerical solution

 Local truncation error

 Total error . The difference between the numerical solution and the true solution. 10.2 Euler’s Method  Analysis of truncation error in Euler's explicit method  Global truncation error . Truncation error is propagated or accumulated !

Mean value theorem 10.2 Euler’s Method  Analysis of truncation error in Euler's explicit method  Global truncation error

. Suppose

. Propagation of error – At the first point  to the second point  to the third point

– At the fourth point

– At the point

. Suppose 10.2 Euler’s Method  Analysis of truncation error in Euler's explicit method  Global truncation error . Suppose

– Difficult to determine the order of magnitude directly – Possible to determine the bound

∵ 10.2 Euler’s Method  Euler's implicit method

 In general, this equation is non‐linear!  Must be solved with a numerical solution method

 In the derivation . Backward difference formula for the derivative .

 The local and global truncation errors . Same as those in the explicit method 10.2 Euler’s Method  Example 10‐2: Solving a first‐order ODE using Euler's implicit method

To solve the non‐linear equation using Newton method Iteration function 10.2 Euler’s Method  Example 10‐2: Solving a first‐order ODE using Euler's implicit method

% Solving First Order ODE with Euler's implicit Method. clear all a = 0; b = 0.5; h = 0.002; N = (b - a)/h; n(1) = 2000; t(1) = a; for i=1:N t(i+1) = t(i) + h; x = n(i); % Newton's method starts. for j = 1:20 num = x +0.800*x^(3/2)*h - 10.0*n(1)... *(1-exp(-3*t(i+1)))*h - n(i); denom = 1 + 0.800*1.5*x^(1/2)*h; xnew = x - num/denom; if abs((xnew - x)/x) < 0.0001 break else x = xnew; end end if j == 20 fprintf('Numerical solution could no be calculated at t = %g s', t(i)) break end % Newton's method ends. n(i+1) = xnew; end plot(t,n) axis([0 0.5 0 2000]), xlabel('t (s)'), ylabel('n') 10.3 Modified Euler’s Method  Modified Euler’s Method  Main assumption in Explicit method . Constant derivative (slope) between , and , main source of error . Equal to the derivative at point ,  Modified Euler method . To include the effect of slope changes within the subinterval

. Uses the average of the slope at points , and ,

 Slope at the beginning: at point ,

 Using Euler’s explicit method

 Estimating slope at the end of interval: at point , 10.3 Modified Euler’s Method  Modified Euler’s Method

 Better estimation of with new slope

. Also derived by integrating ODE using the trapezoidal method  Algorithm 1. 2. 3. 4. 10.3 Modified Euler’s Method  Example 10‐3: Solving a first‐order ODE using the modified Euler method.

function [x, y] = odeModEuler(ODE,a,b,h,yINI)

x(1) = a; y(1) = yINI; N = (b-a)/h; for i = 1:N x(i+1) = x(i) + h; SlopeEu = ODE(x(i),y(i)); yEu = y(i) + SlopeEu*h; SlopeEnd = ODE(x(i+1),yEu); y(i+1) = y(i) + (SlopeEu+SlopeEnd)*h/2; end

Explicit Euler Modified Euler 10.4 Midpoint Method  Midpoint method  Another modification of Euler’s explicit method  Slope at the middle point of the interval  Two steps

1. Calculate

2. Estimate slope at the midpoint ,

3. Calculate numerical solution : 10.5 Runge‐Kutta Methods  Runge‐Kutta Methods  A family of single‐step, explicit, numerical techniques for a first‐order ODE

. Slope: obtained by considering the slope at several points within the subinterval . Different orders of Runge‐Kutta method  the number of points within the subinterval – Second‐order RK: two points – Third‐order RK: three points – Fourth order RK (classical RK): four points . Global truncation error – Second‐order RK: second‐order accurate globally; third order accurate locally

 More accurate than simple Euler’s explicit method

 However, require several evaluations of function for the derivative 10.5 Runge‐Kutta Methods  Second‐order Runge‐Kutta Methods  General form

. . The values of these constants vary with the specific second‐order method. . Modified Euler method and the midpoint method – Two versions of a second‐order RK method

. Modified Euler method: , , 1, 1

. Midpoint method: 0, 1, , 10.5 Runge‐Kutta Methods  Second‐order Runge‐Kutta Methods  Heun’s method . , , ,

 Truncation error in second‐order RK methods . Local truncation error: . Global truncation error: . A larger step size can be used for the same accuracy !

 However, the function , is calculated twice. 10.5 Runge‐Kutta Methods  Second‐order Runge‐Kutta Methods and Talyor series expansion

 First and second derivatives: given by the differential equation

 Then,

 General form of RK 10.5 Runge‐Kutta Methods  Second‐order Runge‐Kutta Methods and Talyor series expansion

 Two different equations for

Three equations with four unknowns

Modified Euler: , , 1, 1 Midpoint method: 0, 1, , Heun’s: , , , 10.5 Runge‐Kutta Methods  Example 10‐4: Solving by hand a first‐order ODE using the second‐order RungeKutta method 10.5 Runge‐Kutta Methods  Third‐order RK methods  General form

. ,,,,,,, . Four term Talyor series expansion

 Classical third‐order RK method . , , , , 1, , 1, 2 10.5 Runge‐Kutta Methods  Third‐order RK methods  Truncation error . Local truncation error: . Global truncation error:

 Other third‐order RK method 10.5 Runge‐Kutta Methods  Fourth‐order RK methods  General form

. 13 constants: ,,,,,,,,,,,,

 Classical fourth‐order RK method 10.5 Runge‐Kutta Methods  Fourth‐order RK methods 10.5 Runge‐Kutta Methods  Fourth‐order RK methods  Truncation error . Local truncation error: . Global truncation error: 10.5 Runge‐Kutta Methods  Example 10‐5: Solving by hand a first‐order ODE using the fourth‐order Runge‐Kutta method

 Example 10‐6: A user‐defined function for solving a first‐order ODE using the fourth‐ order Runge‐Kutta method. 10.5 Runge‐Kutta Methods  Example 10‐6: A user‐defined function for solving a first‐order ODE using the fourth‐ order Runge‐Kutta method.

clear all function [x, y]… a=0; b=2.5; = odeRK4(ODE,a,b,h,yIni) h=0.5; yIni=3; [x,y] = odeRK4(@Chap8Exmp6ODE,a,b,h,yIni) x(1) = a; y(1) = yIni; xp=a:0.1:b; n = (b-a)/h; yp=70/9*exp(-0.3*xp)-43/9*exp(-1.2*xp); for i = 1:n plot(x,y,'*r',xp,yp) x(i+1) = x(i) + h; yExact=70/9*exp(-0.3*x)-43/9*exp(-1.2*x ) K1 = ODE(x(i),y(i)); error=yExact-y xhalf = x(i) + h/2; yK1 = y(i) + K1*h/2; K2 = ODE(xhalf,yK1); yK2 = y(i) + K2*h/2; K3 = ODE(xhalf,yK2); yK3 = y(i) + K3*h; K4 = ODE(x(i+1),yK3); y(i+1) = y(i) + ... (K1 + 2*K2 + 2*K3 + K4)*h/6; end

function dydx = Chap8Exmp6ODE(x,y) dydx = -1.2*y + 7*exp(-0.3*x); 10.6 Multistep Methods  Single‐step method

 Use only the value of and at the previous point

 Multi‐step method  Two or more previous points  Explicit and implicit methods . Explicit: – The first few points can be determined by single‐step methods or by multistep methods that use fewer prior points.

. Implicit method: appears on both sides.  non‐linear equation solution method!

 Adams‐Bashforth Method  Adams‐Moulton Method 10.6 Multistep Methods  Adams‐Bashforth Method  Explicit multistep method for solving a first‐order ODE  Second‐order formula uses

. , and ,  Third‐order formula uses

. ,, ,, ,

 Integration of ODE

. Integration is carried out with a polynomial that interpolates the value of , at , and at previous points. 10.6 Multistep Methods  Adams‐Bashforth Method  Second‐order AB method . First order polynomial

. Integration

Euler’s explicit method

 Third‐order AB method

 Fourth‐order AB method 10.6 Multistep Methods  Adams‐Moulton Method  Implicit multistep method for solving first‐order ODEs

 Second‐order formula

. Implicit form of the modified Euler method

 Third‐order and fourth formulas

 Two ways to use AM method . If they are used by themselves, they have to be solved numerically. . Usually, however, they are used in conjunction with other equations in methods that are called predictor‐corrector methods. 10.7 Predictor‐Corrector Methods  Predictor‐corrector methods  Solving ODEs using two formulas; predictor and corrector formulas

 Predictor step . Explicit formula

. Used first to determine an estimate of the solution . is calculated from the known solution at the previous point ,. . Single‐step method or multistep methods

 Corrector step

. Uses the estimated value of on the right‐hand . The corrector equation, which is usually an implicit equation, is being used in an explicit manner. . This scheme utilizes the benefits of the implicit formula while avoiding the difficulties associated with solving an implicit equation directly. . Furthermore, the application of the corrector can be repeated several times such that the new value of is substituted back on the right‐hand side of the corrector formula to obtain a more refined value for . 10.7 Predictor‐Corrector Methods  Predictor‐corrector methods  Algorithm

1. Calculate using an explicit method 2. Substitute from Step 1, as well as any required values from the already known solution at previous points, in the right‐hand side of an implicit formula to obtain a refined value for . 3. Repeat Step 2 by substituting the refined value of back in the implicit formula, to obtain an even more refined value for . 4. Step 2 can be repeated as many times as necessary to produce the desired level of accuracy, that is, until further repetitions do not change the answer for to a specified number of decimal places.

 The simplest example of a predictor‐corrector method: modified Euler method 10.7 Predictor‐Corrector Methods  Predictor‐corrector methods  Modified Euler predictor‐corrector method 1. Calculate a first estimate for using Euler's explicit method as a predictor:

2. Calculate better estimates for repetitively as a corrector:

3. Stop the iterations when 10.7 Predictor‐Corrector Methods  Predictor‐corrector methods  Adams‐Bashforth and Adams‐Moulton predictor‐corrector methods . AB method: explicit method . AM method: implicit method

. Can be used together in a predictor‐corrector method!

. Predictor equation with the third‐order formula

. Corrector equations 10.8 System of First‐order ODE  System of coupled first‐order ODEs  In many of these instances there is a need to solve a system of coupled first‐order ODEs.  Initial value problems involving ODEs of second and higher orders . Solved by converting the equation into a system of first‐order equations

 Predator‐prey problem

 Suppose a community consists of lions (predators) and gazelles (prey).

 In chemical reactions 10.8 System of First‐order ODE  General form of a system of first‐order ODEs

 With a single‐step explicit method

 Euler's explicit method  The modified Euler method  The fourth‐order Runge‐Kutta method 10.8 System of First‐order ODE  General form of a system of first‐order ODEs  Euler's explicit method

. Initial conditions: and z

 The modified Euler method 10.8 System of First‐order ODE  Example 10‐7: Solving a system of two first‐order ODEs using Euler's explicit method and second‐order Runge‐Kutta method. 10.8 System of First‐order ODE  Example 10‐7: Solving a system of two first‐order ODEs using Euler's explicit method and second‐order Runge‐Kutta method.

clear a = 0; b = 3; yINI = 3; zINI = 0.2; h = 0.1; [x, y, z] = Sys2ODEsRK2(@odeExample7dydx,@odeExample7dzdx,a,b,h,yINI,zINI); % Data from part (a) xa = [0 0.25 0.5 0.75]; ya = [3 1.472 1.374 1.427]; za = [0.2 0.94 1.087 1.135]; % Data from part (b) xb = [0 0.25 0.5]; yb = [3 2.187 1.903]; zb = [0.2 0.6436 0.9230]; plot(x,y,'-k',x,z,'-r',xa,ya,'*k',xa,za,'*r',xb,yb,'ok',xb,zb,'or') axis([0 3 0 4])

function dzdx = odeExample7dzdx(x,y,z) dzdx = y-z^2;

function dydx = odeExample7dydx(x,y,z) dydx = (-y+z)*exp(1-x)+0.5*y; 10.8 System of First‐order ODE  Example 10‐7: Solving a system of two first‐order ODEs using Euler's explicit method and second‐order Runge‐Kutta method.

function [x, y, z] = Sys2ODEsRK2(ODE1,ODE2,a,b,h,yINI,zINI)

x(1) = a; y(1) = yINI; z(1) = zINI; N = (b - a)/h; for i = 1:N x(i+1) = x(i) + h; Ky1 = ODE1(x(i),y(i),z(i)); Kz1 = ODE2(x(i),y(i),z(i)); Ky2 = ODE1(x(i+1),y(i)+Ky1*h,z(i)+Kz1*h); Kz2 = ODE2(x(i+1),y(i)+Ky1*h,z(i)+Kz1*h); y(i+1) = y(i) + (Ky1 + Ky2)*h/2; z(i+1) = z(i) + (Kz1 + Kz2)*h/2; end 10.8 System of First‐order ODE  Solving a System of First‐Order ODEs Using the Classical Fourth‐Order RK Method  Same as the second‐order method except the number of constants

. 13 constants: ,,,,,,,,,,,,

 Initial conditions: , z , 10.8 System of First‐order ODE  Solving a System of First‐Order ODEs Using the Classical Fourth‐Order RK Method