19
Chapter 19 ODEs : Adaptive Methods and Stiff Systems Gab-Byung Chae 2007 12 Z 4 5{ 9

Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

Chapter 19

ODEs : Adaptive Methods and Stiff Systems

Gab-Byung Chae

2007�̧� 12�Z4 5{9�

Page 2: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

2

Chapter Objectives

• Understanding how the Runge-kutta Fehlberg methods use RK methods of

different orders to provide error estimates that sre used to adjust the step

size.

• Learning how to solve ODEs using MATLAB.

• Understanding the difference between one-step and multistep methods for

solving ODEs.

• Understanding what is meant by stiffness and its implications for solving

ODEs.

19.1 ADAPTIVERUNGE-KUTTA METHODS

See Fig. 19.1, for most of the range, the solution change gradually. A fairly

large step size could be used. But for a region from t = 1.75 to 2.25, the solution

undergoes an abrupt change. A very small step size would be required.

Adaptive step-size control : automatically adjust the step size - an estimate of

the local truncation error at each step is required.

Page 3: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

3

The first approach :

1. Step halving involves taking each step twice, once as a full step and then as

two half steps.

2. The different in the two results represents an estimate of the local truncation

error.

3. The step size can then be adjusted based on this error estimate.

The second approach(Embedded RK method or RK-Fehlberg methods) : The

local truncation error is estimated as the difference between two predictions using

different-order RK methods. These are currently the methods of choice because

they are more efficient than step halving.

19.1.1 MATLAB Functions for Nonstiff Systems

Fehlberg methods : several versions are available as built-in functions in MAT-

LAB.

ode23 :

The ode23 function uses the BS23 algorithm(Bogacki and Shampine, 1989;

Shampine, 1994) which simultaneously uses second- and third-order RK formulas

to solve ODE and make error estimates for step-size adjustment.

yi+1 = yi +19(2k1 + 3k2 + 4k3)h (1)

where

k1 = f(ti, yi) (2)

k2 = f

(ti +

12h, yi +

12k1h

)(3)

k3 = f

(ti +

34h, yi +

34k2h

)(4)

The error is estimated as

Ei+1 =172

(−5ki + 6k2 + 8k3 − 9k4)h (5)

Page 4: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

4

where

k4 = f(ti+1, yi+1) (6)

After each step, the error is checked to determine whether it is within a desired

tolerance.

If it is, the value of yi+1 is accepted, and k4 becomes k1 for the next step.

If the error is too large, the step is repeated with reduced step sizes until

estimated error satisfies

E ≤ max(RelTol × |y|, AbsTol) (7)

where RelTol is the relative tolerance(default = 10−3) and AbsTol is the absolute

tolerance(default = 10−6).

ode45 :

The ode45 function uses an algorithm developed by Dormand and Prince(1990),

which simultaneously uses fourth- and fifth-order RK formulas to solve ODE and

make error estimates for step-size adjustment. MATLAB recommends that ode45

is the best function to apply as a ”first try” for most problems.

ode113 :

The ode113 function uses a variable-order Adams-Bashforth-Moulton solver.

Useful for stringent error tolerances or computationally intensive ODE func-

tions.

A multistep method as we will describe subsequently in Section 19.2

[t, y] = ode45(odefun, tspan, y0)

where y is the solution array where each column is one of the dependent variables

and each row corresponds to a time in the column vector t, odefun is the name of

the function returning a column vector of the right-hand-sides of the differential

equations, tspan specifies the integration interval, and y0 = a vector containing

the initial values.

tspan = [ti tf];

from ti to tf .

Page 5: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

5

tspan = [t0 t1 ... tn];

at specific times t0, t1, . . . tn

¨ Example 0.1 Employ ode45 to solve the following set of nonlinear ODEs

from t = 0 to 20:

dy1

dt= 1.2y1 − 0.6y1y2

dy2

dt= −0.8y2 + 0.3y1y2

where y1 = 2 and y2 = 1 at t = 0. Such equations are referred to as predator-

prey equations.

Solution Create a M-file as (predpry.m)

function yp = predprey(t,y)

yp = [1.2*y(1) -0.6*y(1)*y(2); -0.8*y(2)+0.3*y(1)*y(2)];

Integration range and the initial conditions :

>> tspan = [0 20];

>> y0 = [2, 1];

The solver can then be invoked by

>> [t, y] = ode45(@predprey, tspan, y0);

Plotting (Fig 19.2)

Page 6: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

6

plot(t, y)

state-space plot : a plot of the dependent variables versus each other (Fig 19.3)

plot(y(:,1), y(:,2))

Additional arguments

[t, y] = ode45( odefun, tspan, y0, options, p1, p2, ....);

where options is a data structure that is created with the odeset function to control

features of the solution, and p1, p2, ... are parameters that you want to pass into

odefun. The odeset function has the general syntax

options = odeset(′par′1, val1,′ par′2, val2, ...)

where the parameter pari has the value vali.

A complete listing of all the possible parameters can be obtained by merely

entering odeset at the command prompt. Some of them are

’RelTol’ : Allows you to adjust the relative tolerance.

’AbsTol’ : Allows you to adjust the absolute tolerance.

Page 7: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

7

’InitialStep’ : The solver automatically determines the initial step. This option

allows you to set your own.

’MaxStep’ : The maximum step defaults to one-tenth of the tspan interval. This

option allows you to override this default.

¨ Example 0.2 Using odeset to Control Integration Options

Use ode23 to solve the following ODE from t = 0 to 4 :

dy

dt= 10e−(t−2)2/[2(0.075)2] − 0.6y

where y(0) = 0.5. Obtain solutions for the default (10−3) and for more stringent

(10−4) relative error tolerance.

Solution

function yp = dydt(t, y)

yp = 10*exp(-(t-2)*(t-2)/(2*0.075^2)) - 0.6*y;

Then the relative error (10−3) :

>> ode23(@dydt, [0 4], 0.5);

Automatically creates a plot of the results displaying circles at the values it has

computed.(See Fig. 19.4a)

Then the relative error (10−4) :

Page 8: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

8

>>options =odeset(’RelTol’, 1e-4);

>> ode23(@dydt, [0 4], 0.5, options);

See Fig 19.4b.

19.2 MULTISTEP METHODSThe one-step methods described in the previous sections utilize information at

a single point ti to predict a value of the dependent variable yi+1 at a future point

ti+1(Fig 19.5a).

The multistep methods (Fig. 19.5b), are based on the insight that, once the

computation has begun, valuable information from previous points is at our com-

mand.

19.2.1 The Non-Self-Starting Heun Method

A predictor in Heun’s approach

y0i+1 = yi + f(ti, yi)h : (Euler′s Method) (8)

and trapezoidal rule as a corrector

yi+1 = yi +f(ti, yi) + f(ti+1, y

0i+1)

2h (9)

The predictor and the corrector have local truncation errors of O(h2) and O(h3).

To improve the predictor’s local truncation error to O(h3) use

y0i+1 = yi−1 + f(ti, yi)2h truncation error to O(h3) (10)

Page 9: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

9

But yi−1 is not available at the starting, so Eq. (9) and (10) are called the

non-self-starting Heun method.

See Fig. 19.6, the derivative estimate in Eq. (10) is now located at the midpoint

rather than at the beginning of the interval over which the prediction is made. This

centering improves the local error of the predictor to O(h3).

The non-self-starting Heun method can be summarized as

y0i+1 = yi−1 + f(ti, yi)2h (11)

yji+1 = yi +

f(ti, yi) + f(ti+1, yj−1i+1 )

2h (12)

where j is j−th iteration. The iterations are terminated based on an estimate of

the approximate error,

|εa| =∣∣∣∣∣yj

i+1 − yj−1i+1

yji+1

∣∣∣∣∣× 100% (13)

Page 10: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

10

¨ Example 0.3 Non-Self-Starting Heun’s Method

Use the non-self-starting Heun method to perform the same computations as

were performed previously in Example 18.2 using Heun’s method. That is, inte-

grate y′ = 4e0.8t − 0.5y from t = 0 to 4 with a step size 1. As with Example

18.2, the initial condition at t = 0 is y = 2. However, because we are now dealing

with a multistep method, we require the additional information that y is euqal

to −0.3929953 at t = −1.

Solution The predictor is used to extrapolate linearly from t = −1 to 1 :

y0i+1 = −0.3929953 + [4e0.8(0) − 0.5(2)]2 = 5.607005

The corrector is used to compute the value :

y11 = 2 +

4e0.8(0) − 0.5(2) + 4e0.8(1) − 0.5(5.607005)2

1 = 6.549331

which represents a true percent relative error of −5.73% (true value = 6.194631).

Now Eq. (12) can be applied iteratively to improve the solution:(4e0.8(0)−0.5(2) =

3)

y21 = 2 +

3 + 4e0.8(1) − 0.5(6.549331)2

1 = 6.313749

which represents a percent relative error of −1.92%. An approximate estimate of

the error can be determined using Eq. (13):

|εa| =∣∣∣∣6.313749− 6.549331

6.313749

∣∣∣∣× 100% = 3.7

Because the initial predictor value is more accurate, the multistep method converge

at a somewhat faster rate.

19.2.2 Error Estimates

The non-self-starting Heun can be used to estimate the local truncation error.

The predictor is equivalent to the midpoint rule. Hence its truncation error is

(Table 16.4)

Ep =13h3y(3)(ξp) =

13h3f ′′(ξp) (14)

where the subscript p designates that this is the error of the predictor.

Page 11: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

11

This error estimate can be combined with the estimate of yi+1 from the pre-

dictor step to yield

True value = y0i+1 +

13h3y(3)(ξp) (15)

The corrector is equivalent to the trapezoidal rule, a similar estimate of the

local truncation error for the corrector is (Table 16.2)

Ec = − 112

h3y(3)(ξc) = − 112

h3f ′′(ξc) (16)

This error estimate can be combined with the corrector result yi+1 to give

True value = ymi+1 −

112

h3y(3)(ξc) (17)

where ymi+1 is the converging value of iterations.

Eq. (15) can be subtracted from Eq. (17) to yield

0 = ymi+1 − y0

i+1 −512

h3y(3)(ξ) (18)

where ξ is now between ti−1 and ti. Or

y0i+1 − ym

i+1

5= − 1

12h3y(3)(ξ) (19)

If the third derivative does not vary appreciably over the interval in ques-

tion(that is, ξp ≈ ξ), Eq. (16) and Eq. (19) give us

Ec =y0

i+1 − ymi+1

5(20)

This makes possible to estimate the per-step truncation error.

¨ Example 0.4 Use Eq. (20) to estimate the per-step truncation error of Ex-

ample 19.3 above. Note that the true values at t = 1 and 2 are 6.194631 and

14.84392, respectively.

Solution At ti+1 = 1, the predictor give 5.607005 and the corrector yields

6.360865. These values can be substituted into Eq. (20) to give

Ec =5.607005− 6.360865

5= −0.150722

which compares well with the exact error,

Et = 6.194631− 6.360865 = −0.1662341

Page 12: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

12

At ti+1 = 2, the predictor give 13.44346 and the corrector yields 15.30224,

therefore

Ec =13.44346− 15.30224

5= −0.37176

which compares well with the exact error,

Et = 14.84392− 15.30224 = −0.45831

19.3 STIFFNESSStiffness is a special problem that can arise in the solution of ordinary different

equation. A stiff system is one involving rapidly changing components together

with slowly changing ones. An example of a single stiff ODE is

dy

dt= −1000y + 3000− 2000e−t (21)

If y(0) = 0, the analytical solution can be developed as

y = 3− 0.998e−1000t − 2.002e−t (22)

As in Fig. 19.7, the solution is initially dominated by the fast exponential term

(e−1000t). After a short period (t < 0.005), this transient dies out and the solution

becomes governed by the slow exponential (e−t).

Finding step size : (the explicit approaches)

Page 13: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

13

Examining the homogeneous part of Eq. (21) :

dy

dt= −ay (23)

If y(0) = y0, the solution

y = y0e−at

The solution starts at y0 and asymptotically approaches zero.

By Euler’s method

yi+1 = yi +dyi

dth

Substituting Eq. (23) gives

yi+1 = yi − ayih

or

yi+1 = yi(1− ah) (24)

The stability of this formula depends on the step size h. So

|1− ah| < 1

If

h > 2/a,

then |yi| → ∞ as i →∞.

The step size to maintain stability must be < 2/1000 = 0.002.

Alternative Remedy : Backward, or implicit, Euler’s method. Use derivative

at the future time

yi+1 = yi +dyi+1

dth

Substituting Eq. (23) yields

yi+1 = yi − ayi+1h

which can be solved for

yi+1 =yi

1 + ah(25)

For this case, regardless of the size of the step, |yi| → 0 as i →∞. Hence, the

approach is called unconditionally stable.

Page 14: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

14

¨ Example 0.5 Use both the explicit and implicit Euler methods to solve

Eq. (21), where y(0) = 0.

(a) Use explicit Euler with step sizes of 0.0005 and 0.0015 to solve for y between

t = 0 and 0.006.

(b) Use the implicit Euler with a step size of 0.05 to solve for y between 0 and

0.4.

Solution

(a) For this problem, the explicit Euler’s method is

yi+1 = yi + (−1000yi + 3000− 2000e−ti)h

from yi+1 = yi + dyi

dt h.

The result is displayed in Fig. 19.8a for h = 0.0005.

(b) The implicit Euler’s method is

yi+1 = yi + (−1000yi+1 + 3000− 2000e−ti+1)h

from yi+1 = yi + dyi+1dt h. or

yi+1 =yi + 3000h− 2000he−ti+1

1 + 1000h

The result for h = 0.05 is displayed in Fig 19.8b along with the analytical

solution.

Systems of ODEs can also be stiff.

An example is :dy1

dt= −5y1 + 3y2 (26)

dy2

dt= 100y1 − 301y2 (27)

For y1(0) = 52.29 and y2(0) = 83.82, the exact solution is

y1 = 52.96e−3.9899t − 0.67e−302.0101t (28)

y2 = 17.83e−3.9899t + 65.99e−302.0101t (29)

An implicit Euler’s method

y1,i+1 = y1,i + (−5y1,i+1 + 3y2,i+1)h (30)

Page 15: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

15

y2,i+1 = y2,i + (100y1,i+1 − 301y2,i+1)h (31)

or

(1 + 5h)y1,i+1 − 3y2,i+1 = y1,i (32)

−100y1,i+1 + (1 + 301h)y2,i+1 = y2,i (33)

Thus, we can see that the problem consists of solving a set of simultaneous equa-

tions for each time step.

19.3.1 MATLAB Functions for Stiff Systems

Built-in functions for solving stiff systems of ODEs.

ode15s multistep solver that optionally uses the Gear backward differentiation

formulas. For stiff problems of low to medium accuracy.

ode23s Based on a modified Rosenbrock formula of order 2. one-step solver,

more efficient than ode15s at crude tolerances.

ode23t For moderately stiff problems with low accuracy.

Page 16: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

16

ode23tb use implicit Runge-Kutta formula with a first stage that is trapezoidal

rule and a second stage that is a backward differentiation formula of order 2, more

efficient than ode15s at crude tolerances.

¨ Example 0.6 MATLAB for Stiff ODEs The Van der Pol equation is a

model of an electronic circuit that arose back in the days of vacuum tubes.

d2y1

dt2− µ(1− y2

1)dy1

dt+ y1 = 0 (34)

The solution to this equation becomes progressively stiffer as µ gets large.

Given the initial conditions, y1(0) = dy1/dt = 1, use MATLAB to solve the

following two cases:

(a) for µ = 1, use ode45 to solve from t = 0 to 20; and

(b) for µ = 1000, use ode23s to solve t = 0 to 6000.

Solution

(a) The first step is to convert the second-order ODE into a pair of first-order

ODEs by definingdy1

dt= y2

Using this equation, Eq (34) can be written as

dy2

dt= µ(1− y2

1)y2 − y1

M-file for this ODEs

function yp = vanderpol(t,y, mu)

yp = [y(2) ; mu*(1-y(1)^2)*y(2) -y(1)];

>> [t, y] = ode45(@vanderpol,[0 20],[1,1],[],1);

>> plot(t,y(:,1), ’-’,t,y(:2), ’--’)

>> legend(’y1’, ’y2’);

The smooth nature of the plot (Fig. 19.9a) suggests that the van der Pol

equation with µ = 1 is not a stiff system.

(b) ode45 will fail.

Page 17: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

17

>> [t, y] = ode23s(@vanderpol,[0 6000],[1,1],[],1000);

>> plot(t,y(:,1))

We have only displayed the y1 component because the result for y2 has a much

larger scale. Notice how the solution (Fig. 19.9b) has much sharper edges than is

the case in Fig (Fig. 19.9a).

19.4 MATLAB APPLICATION: BUNGEE JUMPERWITH CORD

Bungee Jumper again

dx

dt= v (35)

When cord is slack, the only forces are gravity(+) and drag(±).

dv

dt= g − sign(v)

cd

mv2 (36)

When the cord is stretched.

dv

dt= g − sign(v)

cd

mv2 − k

m(x− L)− γ

mv (37)

¨ Example 0.7 MATLAB for Stiff ODEs Determine the position and veloc-

ity of a bungee jumper with the following parameters: L = 30 m, g = 9.81 m/s2,

Page 18: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

18

m = 68.1 kg, cd = 0.25 kg/m, k = 40 N/m, and γ = 8 N · s/m. Perform

the computation from t = 0 to 50 s and assume that the initial conditions are

x(0) = v(0) = 0.

ÕªaË> 1: Figure 19.10

Solution

M-file

function dydt = bungee(t,y,L,cd,m,k,gamma)

g = 9.81;

cord = 0;

if y(1) > L %determine if the cord exerts a force

cord = k/m*(y(1)-L)+gamma/m*y(2);

end

dydt = [y(2); g - sign(y(2))*cd/m*y(2)^2 -cord];

The derivatives are returned as a column vector.

Because these equations are not stiff, we can use ode45 to obtain the solutions

and display them on a plot :

Page 19: Chapter 19 ODEs : Adaptive Methods and Stifi Systemsrg.wonkwang.ac.kr/teaching/numeanaly2006-2/pdf/chap19.pdf · † Learning how to solve ODEs using MATLAB. † Understanding the

19

>> [t,y] = ode45(@bungee,[0,50],[0,0],[],30,0.25,68.1,40,8);

>> plot(t, -y(:,1),’-’,t,y(:,2),’:’)

>>legend(’x (m)’,’v (m/s)’)

Negative distance is in the downward direction.