• 검색 결과가 없습니다.

Numerical solution of a first‐order ODE  A set of discrete points that approximate the function y(x

N/A
N/A
Protected

Academic year: 2022

Share "Numerical solution of a first‐order ODE  A set of discrete points that approximate the function y(x"

Copied!
50
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

Cho, Hyoung Kyu

Department of Nuclear Engineering Seoul National University

Cho, Hyoung Kyu

Department of Nuclear Engineering Seoul National University

INTRODUCTION TO 

NUMERICAL ANALYSIS

(2)

10. NUMERICAL INTEGRATION

10.1 Background

10.2 Euler's Methods

10.3 Modified Euler's Method 10.4 Midpoint Method

10.5 Runge‐Kutta Methods 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.11 Local Truncation Error in Second‐Order  Range‐Kutta Method

10.12 Step Size for Desired Accuracy 10.13 Stability

10.14 Stiff Ordinary Differential Equations

(3)

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

(4)

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)

(5)

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

(6)

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.

(7)

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

(8)

10.2 Euler’s Method

 Euler’s method

 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

step size is exaggerated !

(rectangle method)

(forward Euler method)

(9)

10.2 Euler’s Method

 Example 10‐1: Solving a first‐order ODE using Euler's explicit method.

(10)

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 [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

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

(11)

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.

(12)

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

(13)

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 

(14)

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

(15)

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

(16)

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

(17)

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')

(18)

10.3 Modified Euler’s Method

 Modified Euler’s Method

 Main assumption in Explicit method

Constant derivative (slope) between  , and  ,

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  ,

main source of error

(19)

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.

(20)

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

(21)

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 

(22)

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 

(23)

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, ,

(24)

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.

(25)

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

(26)

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: , , ,

(27)

10.5 Runge‐Kutta Methods

 Example 10‐4: Solving by hand a first‐order ODE 

using the second‐order RungeKutta method

(28)

10.5 Runge‐Kutta Methods

 Third‐order RK methods

 General form

, , , , , , ,

Four  term Talyor series expansion

 Classical third‐order RK method

, , , , 1, , 1, 2

(29)

10.5 Runge‐Kutta Methods

 Third‐order RK methods

 Truncation error

Local truncation error: 

Global truncation error: 

 Other third‐order RK method

(30)

10.5 Runge‐Kutta Methods

 Fourth‐order RK methods

 General form

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

 Classical fourth‐order RK method

(31)

10.5 Runge‐Kutta Methods

 Fourth‐order RK methods

(32)

10.5 Runge‐Kutta Methods

 Fourth‐order RK methods

 Truncation error

Local truncation error: 

Global truncation error: 

(33)

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.

(34)

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 a=0; b=2.5;

h=0.5; yIni=3;

[x,y] = odeRK4(@Chap8Exmp6ODE,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,'*r',xp,yp)

yExact=70/9*exp(-0.3*x)-43/9*exp(-1.2*x ) error=yExact-y

function [x, y]…

= odeRK4(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;

K1 = ODE(x(i),y(i));

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);

(35)

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

(36)

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.

(37)

10.6 Multistep Methods

 Adams‐Bashforth Method

 Second‐order AB method

First order polynomial

Integration

 Third‐order AB method

 Fourth‐order AB method

Euler’s explicit method

(38)

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.

(39)

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  .

(40)

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

(41)

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

(42)

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

(43)

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

(44)

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

(45)

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

(46)

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.

(47)

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;

(48)

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

(49)

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 , 

(50)

10.8 System of First‐order ODE

 Solving a System of First‐Order ODEs Using the Classical Fourth‐Order RK Method

참조

관련 문서

• When a word w appears in a text, its context is the set of words that appear nearby (within a fixed-size window). • Use the many contexts of w to build up a

(1.1 points) According to the passage, which of the following is NOT mentioned as the nature of pain.. ① Pain is not tied to any referent in

(1.1 points) Which of the following is the best example of the great damage in (a) The damage they do is particularly great when they are relied upon as guide in the process

1 John Owen, Justification by Faith Alone, in The Works of John Owen, ed. John Bolt, trans. Scott Clark, &#34;Do This and Live: Christ's Active Obedience as the

For the analytic solution, define the subdomain vector in the same way, namely the summation of the fluxes obtained at the same discretized points as the numerical solution.. Do

Step 2 The subsidiary equation is solved by purely algebraic manipulations3. Step 3 The solution in Step 2 is transformed back, resulting in the solution of

Step 2: Beginning from the perception that there is a need to protect the environment of historic buildings throughout the 1960s and 70s, This is not a preservation centered

à For each subentity, create a table that includes the attributes of that entity set plus the primary key of the higher level