Introduction to Numerical Analysis
Total Page:16
File Type:pdf, Size:1020Kb
INTRODUCTION TO NUMERICAL ANALYSIS Cho, Hyoung Kyu Department of Nuclear Engineering Seoul National University 10. NUMERICAL INTEGRATION 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 Midpoint Method 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 Euler method) 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 . backward Euler method 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.