Chapter 21 Chapter 21 ODEs: Adaptive ODEs: Adaptive Methods and Stiff Methods and Stiff Systems Systems
Dec 20, 2015
Chapter 21Chapter 21
ODEs: Adaptive Methods ODEs: Adaptive Methods and Stiff Systemsand Stiff Systems
Adaptive Runge Kutta MethodAdaptive Runge Kutta Method
Use small step size in high gradient region
(abrupt change)
automatic step size adjustment
Adaptive Runge Kutta MethodAdaptive Runge Kutta Method
First approach: Step halvingEstimate local truncation error using two different
step sizes Solve each step twice, once as a full step and then
as two half steps
Second approach: Embedded RK methods (also called RK-Fehlberg methods)Estimate local truncation error between two
predictions using two different-order RK methods
Step Halving MethodStep Halving Method
Step Halving (Adaptive RK) method
Compute the solutions at each step twice using 4th-order classic RK method
Once as a full step h and independently as two half steps (h/2)
y1 – one full step; y2 – two half steps
15yy
yy
22
12
Error estimateError estimate
Correction – 5Correction – 5thth-order-order
Adaptive 4Adaptive 4thth-order RK Method-order RK Method
One full-step with h
Two half-steps with h/2
Therefore,
)()( 651 hOhyhxy
)()( 65
2 hO2
h2yhxy
15y
15
yyy
2
h2yy
yyh15
16 h
16
15
32
h2hyy
212
2
5
2
1255
55
12
)(
Two half-steps
Embedded Runge-Kutta MethodEmbedded Runge-Kutta Method
MATLAB function: ODE23MATLAB function: ODE23 BS23 algorithm (Bogacki and Shampine, 1989; Shampine, 1994)
Use 3rd-order & 4th-order RK methods simultaneously to solve the ODE and estimate the error for step-size adjustment
Error estimate (Note: k1 is the same as k4 from previous step)
),(
),(
),(
)(
hk4
3yh
4
3xfk
hk2
1yh
2
1tfk
ytfk
;hk4k3k29
1yy
2ii3
1ii2
ii1
321i1i
),()( 1i1i443211i ytfk ;hk9k8k6k572
1E
Embedded RK Method: ODE23Embedded RK Method: ODE23
Uses only three function evaluations (k1, k2, k3)
After each step, the error is checked to determine whether it is within desired tolerance. If it is, yi+1 is accepted and k4 becomes k1 for the next time step
If the error is too large, the step is repeated with a reduced step sizes until the estimate error is acceptable
RelTol: relative tolerance (default = 103)
AbsTol: relative tolerance (default = 106)
),max( AbsTolyRelTolE
Adaptive RK Method – ode23Adaptive RK Method – ode23
Example 21.2: Use ode23 to solve the following ODE from t = 0 to 4:
500y e10y60dt
dy 22 075022t .)(;. ]).(/[)(
function yp = ex21_2(t, y)
% Example 21.2 in the text book
yp = 10*exp(-(t-2)*(t-2)/(2*0.075^2)) - 0.6*y;
Example 20.2: ode23Example 20.2: ode23>> options = odeset('RelTol',1.e-4);
>> ode23('ex21_2', [0 4], 0.5, options);
>> options = odeset('RelTol',1.e-3);
>> ode23('ex21_2', [0 4], 0.5, options);
(a) RelTol = 103 (b) RelTol = 104
Other MATLAB FunctionsOther MATLAB Functions
MATLAB Function: ode45 Dormand and Prince (1990)
Solve fourth- and fifth-order RK formulas simultaneously
Make error estimates for step-size adjustment
MATLAB Function: ode113
Adams-Bashforth-Moulton solver (order 1-12)
Predictor-corrector method
Runge-Kutta Fehlberg MethodRunge-Kutta Fehlberg Method
Fourth-orderFourth-order
Fifth-order Fifth-order
1
2 1
3 1 2
4 1 2 3
5 1 2 3 4
6 1 2 3
( , )
1 1( , )
5 53 3 9
( , )10 40 40
3 3 9 6( , )
5 10 10 511 5 70 35
( , )54 2 27 27
7 1631 175 575 44275( ,
8 55296 512 13824 11
i i
i i
i i
i i
i i
i i
k f x y
k f x h y k h
k f x h y k h k h
k f x h y k h k h k h
k f x h y k h k h k h k h
k f x h y k h k h k h
4 5
253)
0592 4096k h k h
hk1771
512k
594
125k
621
250k
378
37yy 6431i1i )(
hk4
1k
14336
277k
55296
13525k
48384
18575k
27648
2825yy 65431i1i )(
These coefficients were developed by Cash and Karp (1990). Also called Cash-Karp RK Method
Runge-Kutta Fehlberg MethodRunge-Kutta Fehlberg Method
Identical coefficients (k1, k2, k3, k4, k5, k6) for both the
fourth and fifth order Runge-Kutta-Fehlberg methodsSave CPU time
Error estimate – use two RK methods of different order to estimate the local truncation error
hk1771
512
4
1hk 0
14336
277hk
594
125
55296
13525
hk 621
250
48384
18575hk 00hk
378
37
27648
2825Error
654
321
Runge-Kutta Fehlberg MethodRunge-Kutta Fehlberg Method
First, calculate yi+1 using 4th-order Runge-Kutta
Felberg method (y1)4th
Then, calculate yi+1 using 5th-order Runge-Kutta
Felberg method (y2)5th
Calculate Error Ea = (y2)5th - (y1)4th
Adjust step size according to error estimate
Step Size ControlStep Size Control
Use step-halving or Runge-Kutta Fehlberg to estimate the local truncation error
Adjust the step size according to error estimate Increase the step size if the error is too small and
decrease it if the error is too largeFor fourth-order schemes
present
present
0.2 if ;
0.25 if
Desired error value
newnewnew present
newpresent
new
h h
Step Size AdjustmentStep Size Adjustment
For step size increases (RK4, n = 4)
For step size decreases, h is implicit in new
h
h
present
new
1n
present
new
20
present
newpresent
1n
1
present
newpresentnew hhh
.
For nth-order RK Method
250
present
newpresent
n
1
present
newpresentnew hhh
.
Reduce hnew also
reduce new
ExampleExample21.221.2
Forcing function
solution
0.5y(0) e10y60dt
dy 22 075022t ;. ]).(/[)(
small step size around t = 2
Runge-Kutta-Fehlberg method
with adaptive step size control
t
t
Predator-Prey EquationPredator-Prey Equation
A simple predator-prey relationship is described by the Lotka-Volterra model, which we write in terms of a fox population f(t), with birth rate bf and death rate df and a geese population g(t) with birth rate bg and death rate dg
)(
)(
)()(
)()(
1gg22
f2f11
gg
ff
ydbydt
dy
dybydt
dy
or
tfdbtgdt
dg
dtgbtfdt
df
Predator-Prey EquationPredator-Prey Equation
Example: Given bf = 0.3, df = 0.8, bg = 1.2, dg = 0.6, find the populations of predators and preys as a function of time (t = 0 to 20) using ode45.
20y yy60y21dt
dy
10y y80yy30dt
dy
12122
21211
)(;..
)(;.. Predator
Prey
function yp = predprey(t,y)% Fox (Predator) population y1(t), birth rate bf, death rate df% Geese (prey) poupulation y2(t), birth rate bg, death rate dgbf = 0.3; df = 0.8; bg = 1.2; dg = 0.6;yp = [y(1)*(bf*y(2)-df); y(2)*(bg-dg*y(1))];
>> tspan=[0 20]; y0 = [1, 2];>> [t,y] = ode45('predprey',tspan,y0);>> out = [t y]out = 0 1.00000000000000 2.00000000000000 0.08372954771699 0.98466335925883 2.10387606351447 0.16745909543397 0.97217906418030 2.21469691968449 0.25118864315096 0.96261463455261 2.33264806768044 0.33491819086794 0.95606000043195 2.45787517860514 0.62870201024789 0.95940830414765 2.95257534365817 0.92248582962784 1.00915715534212 3.53554512325941 1.21626964900779 1.11944001342999 4.17743733245175 1.51005346838774 1.31420023093313 4.80288705430266 1.70029730481714 1.49877617603179 5.14418160032910 1.89054114124654 1.73932623590652 5.37426899115905 2.08078497767594 2.03724630610217 5.44284022577897 2.27102881410534 2.38123550265509 5.31619117469119 2.46127265053474 2.74867929767025 4.98122010458805 2.65151648696414 3.09373931102751 4.48531246964154 2.84176032339354 3.37201992118282 3.89965751560944 3.03200415982294 3.55414367377695 3.29772307544844 3.21873982743099 3.62508046596480 2.75627545562992 3.40547549503903 3.59592495497541 2.29786856084044 3.59221116264708 3.48499741488926 1.93369109247072 3.77894683025512 3.31638942318525 1.65488750920928 3.94714350340851 3.13529220765148 1.46223282653043 4.11534017656189 2.93901369774356 1.31685686503153 4.28353684971528 2.73779832900552 1.20974621108398 4.45173352286867 2.53877488937042 1.13408335792525 4.65875081398499 2.30372716989349 1.07647855767312 4.86576810510130 2.08503830477357 1.05104469525303 5.07278539621762 1.88590370110986 1.05298367705029 ... ... ... 19.13258672508553 1.39621015109817 1.21506308763510 19.36078504002272 1.26894875046392 1.33168491347240 19.58898335495991 1.16392668931057 1.48284270918163 19.69173751621993 1.12358612912238 1.56325743063018 19.79449167747995 1.08747228241032 1.65192298666833 19.89724583873998 1.05554028549720 1.74928308667197 20.00000000000000 1.02776980338246 1.85579178601841
Adaptive RK method : ode45Adaptive RK method : ode45
Predator-Prey ModelPredator-Prey Model
Time history of predator (fox) and prey (geese) populations
Predator-Prey ModelPredator-Prey Model
State-space plot
Multistep MethodsMultistep Methods
Runge-Kutta methods -- one-step method
-- use intermediate values between ti and ti+1
-- several evaluations of slope per step
Multistep methods -- use values at ti , ti-1 , ti-2 etc
-- only one evaluation of derivative per step
One-Step and Multistep MethodsOne-Step and Multistep Methods
One-step Multistep
- explicit (b0 = 0) & implicit methods. - # of the previous steps.
• Non-self start Huen method*
•Adams-Bashforth Methods (explicit methods b0 = 0)
•Admas-Moulton Methods (implicit methods)
•Predictor-Corrector Methods
] ...),( ),(
),([..
1121
1101211
iiii
iiiii
yxfbyxfb
yxfbhyayay
Multi-step methods use several previous points (yi-1 , yi-2 ,…) in addition to the present point yi
Start with second-order method
Look at Heun’s method (Self-Starting)
Euler predictor - O(h2)
Trapezoid corrector - O(h3)
hytfyy iii0
1i ,
h
2
ytfytfyy
01i1iii
i1i
,,
Heun’s Method Heun’s Method (Review)(Review)
To improve predictor, use 3rd-order Euler method
along with the same old corrector
iterated until converged
)))(,( 3mii
m1i
01i O(h ;h2ytfyy
)(),(),(
m,1,2,j ;h2
ytfytfyy
1j1i1i
miim
ij
1i
sj1i
1j1i
j1i
a %100y
yy
Non-Self-Starting Heun’s MethodNon-Self-Starting Heun’s Method
mi
m1i yy ,Note: are the final results of the corrector iterations
at the previous time step
Non-Self Starting Heun’s MethodNon-Self Starting Heun’s Method
(a) 3(a) 3rdrd-order predictor (b) 3-order predictor (b) 3rdrd-order corrector-order corrector
Non-self starting, need yi-1 and yi (two initial values)
Heun’s MethodHeun’s Method
(a) 2(a) 2ndnd-order predictor (b) 3-order predictor (b) 3rdrd-order corrector-order corrector
Self-starting, need yi only
Truncation ErrorsTruncation Errors
Non-self-starting Heun methodNon-self-starting Heun method
Predictor truncation errorPredictor truncation error
Corrector truncation errorCorrector truncation error )(
)(
C3m
1i
P30
1i
yh12
1y Value True
yh3
1y Value True
)()(
,)()(
yh12
5yh
3
1
12
1yy
then yy If
33m1i
01i
CP
ModifiersModifiers Corrector Modifier
Predictor Modifier (not used in textbook)
5
yyyy
5
yyyh
12
1E
01i
m1im
1im
1i
01i
m1i3
C
)(
)(
)()(
0i
mi
01i
01i
0i
mi
3P
yy5
4yy
yy5
4yh
3
1E
Error in textbook
So the sequence is
Predict
Adjust prediction*
Correct
Converged?
If not, correct again
Non-Self Starting Heun’s MethodNon-Self Starting Heun’s Method
Hand CalculationsHand Calculations Non-self-starting Heun’s method
Need two initial conditions
Example: 21.3 (p523- 524)
0.80.5 4 ( , )
(0) 2, y(-1.0) 0.3929953
Find (1.0) to (4.0) using h 1.0
tdyy e f t y
dty
y y
Exact solution: y = 3.076923e 0.8t – 1.076923 e –0.5 t
Hand Calculations – First StepHand Calculations – First Step
First Step (i = 0) : need two initial conditions
Predictor:
h2
ytfytfyy
h2ytfyy1j
1i1imiim
ij
1i
mii
m1i
01i
),(),(
))(,(
Predictor:
Corrector:
1 0 1
1 0
1.0, 0, 1.0;
0.3929953, 2.0;m m
t t t
y y
0 0.8*00 0 0
01 1 0 0
( , ) 0.5 0.5(2) 4 3
( , )(2 ) 0.3929953 (3)(2)(1) 5.06070
tm m
m m
f t y y e e
y y f t y h
h2ytfyy m00
m1
01 ))(,(
Hand Calculations – First StepHand Calculations – First Step Corrector:
1
1
0.80 0 0.8*11 1 1
11
0.81 1 0.81 1 1
21
( , ) 0.5 4 0.5(5.607005) 4 6.098661
3 6.0986612 (1) 6.549331
2
( , ) 0.5 4 0.5(6.549331) 4 5.627498
3 5.6274982 (1) 6.313749
2
t
t
f t y y e e
y
f t y y e e
y
h2
ytfytfyy
1j11
m00m
0j
1
),(),(
1Exact 6.194631 , 1.9%, 3.7%et ey
10.82 2 0.81 1 1
31
1
( , ) 0.5 4 0.5(6.313749) 4 5.745289
3 5.7452892 (1) 6.372645, 0.9%,
2
After many iterations, 6.36087
t
e
m
f t y y e e
y
y
Hand Calculations – Second StepHand Calculations – Second Step
Second Step (i = 1) :
Predictor:
0 1 2
0 1
0.0, 1.0, 2.0
2, 6.36087m m
t t t
y y
10.8 0.81 1 1
02 0 1 1
( , ) 0.5 4 0.5(6.36087) 4 5.72173
( , )(2 ) 2 (5.72173)(2)(1) 13.44346
tm m
m m
f x y y e e
y y f x y h
h2yxfyy m11
m0
02 ))(,(
Hand Calculations – First StepHand Calculations – First Step Corrector:
2
1
0.80 0 0.8*22 2 2
12
0.82 1 1.61 1 2
22
( , ) 0.5 4 0.5(13.44346) 4 13.09040
5.72173 13.090406.36087 (1) 15.76693
2
( , ) 0.5 4 0.5(15.76693) 4 11.92866
5.72173 11.928666.36087 (1) 15.18607
2
t
t
f t y y e e
y
f t y y e e
y
11 1 2 2
2 1
( , ) ( , )
2
m jj m f t y f t y
y y h
1Exact 14.84392 , ey
StiffnessStiffness A stiff system involves rapidly changing components (usually
die away quickly) together with slowly ones Long-time solution is dominated by slow varying components
tt1000exact
t
e0022e99803y
00y
e20003000y1000dt
dy
..
)(
Numerical StabilityNumerical Stability
Amplification or decay of numerical errors A numerical method is stable if error incurred at
one stage of the process do not tend to magnify at later stages
Ill-conditioned differential equation
-- numerical errors will be magnified regardless of numerical method Stiff differential equation
-- require extremely small step size to achieve accurate results
StabilityStability
Example problem eyy
y0y
aydt
dyat
0
0
)(
)(
/
)(,
0E
aEdtdE y(x)(x)yE(x) Error let
eyy then yy(0) if at00
stablelllyexponentia decay error :0a
stable neutrally :0a
unstable llyexponentia grow error :0a
eE at
Euler Explicit MethodEuler Explicit Method
Stability criterion
Region of absolute stability
iiii
i1i
0
yah1hayyhdt
yd yy
y0y ayyxfdt
yd
)()(
)(;),(
1ah1 ro 1y
y
i
1i
2ah0 1ah11
Amplification factor
Euler Implicit MethodEuler Implicit Method
Unconditionally stable !
ah1
yy
aydt
yd ;h
dt
yd yy
y0y ayyxfdt
yd
i1i
1i1i1i
i1i
0
)(;),(
h all orf 1ah1
1
y
y
i
1i
Forward,Forward,Backward,Backward,or Centeredor CenteredScheme?Scheme?
StabilityStability Explicit Euler method
Second-order Adams-Moulton
Implicit methods are in general more stable than explicit methods.
2ah0
ah0 Unconditionally stable
If a = 1000, then h 0.002 to ensure stability
Example Example 21.521.5
Euler Explicit
Euler Implicit
tt1000exact
t
e0022e99803y
00y ;e20003000y1000dt
dy
..
)(
MATLAB Functions for Stiff SystemsMATLAB Functions for Stiff Systems
ode15s – based on Gear backward differentiation
for stiff problems of low to medium accuracyode23s – based on modified Rosenbrock formulaode23t – trapezoidal rule with a free interpolant
for moderately stiff problems without
numerical dampingode23tb – implicit Runge-Kutta formula
for more explanation please see page 529
Stiff ODE – van del Pol equationStiff ODE – van del Pol equation
Van der Pol equation for electronic circuit
Convert to two first-order ODEs
1dt
0dy
10y sCI 0y
dt
dyy1
dt
yd1
1
112
121
2
)(
)(..;
10y
10y sCI
yyy1dt
dy
ydt
dy
2
1
1221
2
21
)(
)(..
)(
Stiff ODE – van del Pol equationStiff ODE – van del Pol equation
M-file for van del Pol equation
function yp = vanderpol(t,y,mu)
% van der Pol equation for electronic circuit
% d^2y1/dt^2 - mu*(1-y1^2) * dy1/dt + y1 = 0
% initial conditions, y1(0) = dy1/dt = 1
% convert to two first-order ODEs
% The solution becomes progressively stiffer
% as mu gets large
yp = [y(2); mu*(1-y(1)^2)*y(2)-y(1)];
Nonstiff ODE – van del Pol equationNonstiff ODE – van del Pol equation
= 1
>> [t,y]=ode45(@vanderpol,[0 20],[1 1],[], 1);
>> H=plot(t,y(:,1),t,y(:,2),'m--');
>> legend('y1','y2',2);
ode45
Stiff ODE – van del Pol equationStiff ODE – van del Pol equation
= 1000
>> [t,y]=ode23s(@vanderpol,[0 6000],[1 1],[], 1000);
>> H=plot(t,y(:,1));
>> set(H,'LineWidth',3)
ode23s
Bungee Jumper – Coupled ODEsBungee Jumper – Coupled ODEs
Vertical Dynamics of a jumper connected to a stationary platform with a bungee cord (Ex21.4)
Air resistance depending on whether the cord is slack or stretched – use sign(v) for drag force
Spring constant k (N/m) and damping coefficient (N·s/m)
Need to solve two simultaneous ODEs for x and v
vm
Lxm
kv
m
cvsigng
dt
dv 2d )()(
vm
Lxm
kv
m
cvsigng
dt
dv
vdt
dx
2d )()(
k = = 0 if x L
Non-stiff ODEs: Bungee JumperNon-stiff ODEs: Bungee Jumper
Use ode45 for non-stiff ODEs to solve for the distance and velocity of a bungee jumper
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];
>> [t,y] = ode45(@bungee,[0 50],[0 0],[],30,0.25,68.1,40,8);
>> h1 = plot(t,-y(:,1),t,y(:,2),'m--');
>> h2 = legend('x (m)','v(m/s)');
>> set(h1,'LineWidth',3); set(h2,'FontSize',12);
Bungee jumper distance
Bungee jumper velocity
CVEN 302-501CVEN 302-501Homework No. 14Homework No. 14
Chapter 21Prob. 21.3 (30) (Hand Calculation) 21.5 (30)
Prob. 21.8 (40) (using MATLAB program except for part a))
Due 12/02/08 before 5:00 pmDue 12/02/08 before 5:00 pm