Post on 13-Mar-2021
P t 6Part 6Chapter 20Chapter 20
I iti l V l P blInitial-Value Problems
All images copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
PowerPoints organized by Dr. Michael R. Gustafson II, Duke UniversityRevised by Prof. Jang, CAU
Chapter ObjectivesChapter ObjectivesUnderstanding the meaning of local and global truncation• Understanding the meaning of local and global truncation errors and their relationship to step size for one-step methods for solving ODEs.K i h t i l t th f ll i R K tt (RK)• Knowing how to implement the following Runge-Kutta (RK) methods for a single ODE:– Euler
H– Heun– Midpoint– Fourth-Order RK
K i h i h f H ’ h d• Knowing how to iterate the corrector of Heun’s method.• Knowing how to implement the following Runge-Kutta
methods for systems of ODEs:y– Euler– Fourth-order RK
Ordinary Differential EquationsOrdinary Differential Equations• Conservation laws are presented in the form of differential
equationsequations. • Nth order differential equation can be reduced to first-order
differential equation.Methods described here are for sol ing differential eq ations of• Methods described here are for solving differential equations of the form: dy
dt f t, y
• The methods in this chapter are all one-step methods and have the general format:
dt
y y h
where is called an increment function, and is used to extrapolate from an old value y to a new value y h is the time step
yi1 yi h
from an old value yi to a new value yi+1. h is the time step.• One-step methods compute a future prediction yi+1 based only on
information at a single point yi
M l i h d f di i b d• Multi-step methods compute a future prediction yi+1 based on several previous points.
Euler’s MethodEuler s Method• The first derivativeThe first derivative
provides a direct estimate of the slope at ti: dy
dt ti
f ti , yi
and the Euler method uses that estimate as
ti
the increment
function: f ti , yi y y f t y hyi1 yi f ti , yi h
Example 20.1 (1/2)p ( / )Q. Use Euler Method to integrate y' = 4e0.8t – 0.5y =f from t = 0 to
4 ith t i f 1 Th i iti l diti t t 0 i 2 Th4 with a step size of 1. The initial condition at t = 0 is y = 2. The exact solution can be determined by
4 ttt eeey 5.05.08.0 2)(3.1
4
)1)(2,0()0()1( fyy
3)2(5.04)2,0( 0 ef 5)1(32)1( y
Euler’s method
19463.62)(3.1
4 )1(5.0)1(5.0)1(8.0 eeey
%28.19%10019463.6
519463.6
t
)1)(51()1()2( f
40216.11)1)](5(5.04[5
)1)(5,1()1()2()1(8.0
e
fyy
Example 20.1 (2/2)Example 20.1 (2/2)t ytrue yEuler (%)t
012
2.000006.1946314.84392
2.000005.0000011.40216
19.2823.19
34
33.6771775.33896
25.5132156.84931
24.2424.54
Error Analysis for Euler’s MethodError Analysis for Euler s Method• The numerical solution of ODEs involves twoThe numerical solution of ODEs involves two
types of error:– Truncation errors, caused by the nature of the
i ti t h i l dapproximation techniques employed– Roundoff errors, caused by the limited numbers of
significant digits that can be retaineds g ca t d g ts t at ca be eta ed• The total, or global truncation error can be
further split into:– local truncation error that results from an
application method in question over a single step, andand
– propagated truncation error that results from the approximations produced during previous steps.
Error Analysis for Euler’s MethodError Analysis for Euler s Method• The local truncation error for Euler’s method is
O(h2) and proportional to the derivative of f(t,y) while the global truncation error is O(h).
)1(
)(!
),(!2
),(),( 1
)1(2
1
nniin
iiiiii hOh
nytf
hytf
hytfyy
hytfyy iiii ),(1 )(),( 12
nii hOh
ytfE
• This means:– The global error can be reduced by decreasing the
)(!2
t hOhE
The global error can be reduced by decreasing the step size, and
– Euler’s method will provide error-free predictions if the nderl ing f nction is linear ( > secondthe underlying function is linear (-> second derivative is zero).
• Euler’s method is conditionally stable, depending u e s et od s co d t o a y stab e, depe d gon the size of h.
Stability of Euler Methody-
0 0 (0) atdy ay with y y y y edt
dt
1 1 (1 )ii i i i
dyy y h y y ahdt
Amplification factor : g = 1 ahAmplification factor : g = 1-ahIf |g|>1 (that is h > 2/a), the solution will grow in anunbound fashion.unbound fashion. -> Euler method is conditionally stable.
There are certain ODEs where errors always growregardless of the method -> ill-conditioned ODE
A fundamental source of error in Euler’s method is thatthe derivative at the beginning of the interval is assumedg gto apply across the entire interval.
MATLAB Code for Euler’s MethodMATLAB Code for Euler s Method
Heun’s MethodHeun s Method• One method to improve Euler’s method is to determine
derivatives at the beginning and predicted ending of the interval g g p gand average them:
0
),( iii ytfy
hytfyy iiii ),(01
),( 0111 iii ytfy
Predictor Eq.
hi li ki di i f h l f
hytfytf
yy iiiiii 2
),(),( 011
1
Corrector Eq.
• This process relies on making a prediction of the new value of y, then correcting it based on the slope calculated at that new value.
• This predictor-corrector approach can be iterated to convergence:s p ed cto co ecto app oac ca be te ated to co e ge ce:
hytfyy iimii ),(0
1
),(),( 111 h
ytfytf jii
miimj
),,2,1 (
2
111
mj
hyy iiiimi
ji
Example 20.2 (1/3)p ( / )Q. Use Heun’s method with iteration to integrate
y' = 4e0.8t – 0.5y from t = 0 to 4 with a step size of 1 The initial condition at t = 0 is y = 2size of 1. The initial condition at t = 0 is y = 2. The stopping criterion is 0.00001%.
Example 20 2 (2/3)Example 20.2 (2/3)Sol.) 2,0:IC 5.04 8.0 ytyey t
(t0, y0):
P di t
3)2(5.04 00 ey
Predictor: 19.28%: percent relative error (Euler)5)1(3201 y
402164.6)5(5.04),( )1(8.00111 eyxfy
701082.42402164.63
y
Corrector: -8.18%: percent relative
error
701082.6)1(701082.4211 y
Approximate error : %39.25%100701082.6
5701082.6
a
ff )()( 0)()( 111 ytfytf j
imiij
hytfytf
yy iiiiii 2
),(),( 011
1
),,2,1 (
2
),(),( 111
mj
hytfytf
yy iiiimi
ji
Example 20.2 (3/3)p ( / ) Corrector:
A i
275811.612
)701082.6(5.0432)1(8.0
21
ey
70108262758116Approximate error : %776.6%100275811.6
701082.6275811.6
a
0.8(1)3 4 0 5(6 275811)e Corrector:
Approximate error :
31
3 4 0.5(6.275811)2 1 6.3821292
ey
%666.1%100275811.6382129.6
%666.1%100
382129.6a
With t it ti With it tiWithout iteration With iteration
t ytrue yEuler (%) yHeun (%) yHeun (%)
0 2 00000 2 00000 2 00000 2 00000t t t
012
2.000006.19463
14.84392
2.000005.00000
11.4021619.2823.19
2.000006.70108
16.319788.189.94
2.000006.36087
15.302242.683.09
34
33.6771775.33896
25.5132156.84931
24.2424.51
37.1992583.33777
10.4610.62
34.7432877.73510
3.173.18
Midpoint MethodMidpoint Method• Another improvement to Euler’s method is similar
to Heun’s method but predicts the slope at theto Heun s method, but predicts the slope at the midpoint of an interval rather than at the end:
h2
),(2/1hytfyy iiii
)( 2/12/12/1 iii ytfy ),( 2/12/12/1 iii ytfy
hytfyy iiii ),( 2/12/11
• The midpoint method is superior to Euler’s method in the same way that centered difference is better approximation rather than either forward or backward versions. This method has a local t ti f O(h3) d l b l f O(h2)truncation error of O(h3) and global error of O(h2).
Runge-Kutta MethodsRunge Kutta Methods• Runge-Kutta (RK) methods achieve the accuracy of a Taylor
series approach without requiring the calculation of higher pp q g gderivatives.
• For RK methods, the increment function can be generally written as: a k a k a k
(1) 1 hyy ii
written as:
where the a’s are constants and the k’s are
a1k1 a2k2 ankn
ytfk ii1 , hkqhkqyhptfk
hkqyhptfkytfk
ii
ii
ii
22212123
11112
1
, ,
,
where the p’s and q’s are constants. hkqhkqhkqyhptfk nnnnninin 11,122,111,11 ,
• First order RK method with n=1: Euler’s method• Once n is chosen, values for the a’s, p’s, and q’s are evaluated
by setting Eq (1) equal to terms in Taylor series expansionby setting Eq. (1) equal to terms in Taylor series expansion.
Second order Runge KuttaSecond-order Runge-KuttaThe second order version of Eq. (1) is
hkakayy ii )( 22111
),(1 ii ytfk
)( hkhfk ),( 11112 hkqyhptfk ii
The values for a1, a2, p1 and q11 are evaluated by setting E (1) l d d T l i
21 !2
),(),( h
ytfhytfyy ii
iiii
dtdy
yytf
tytfytf ii
),(),(),(
Eq. (1) equal to a second order Taylor series.
!2
!2),(
2
1h
dtdy
yf
tfhytfyy iiii
dtyt
y
hkakayy ii )( 22111
Second order Runge KuttaSecond-order Runge-Kutta
gsgrytgsyrtg )()( hkakayy )(
y
st
rytgsyrtg ),(),( hkakayy ii )( 22111
),( 11112 hkqyhptfk ii
)(),(),( 211111111 hO
yfhkq
tfhpytfhkqyhptf iiii
)(),(),(),( 32112
212211 hO
yfytfhqa
tfhpaythfaythfayy iiiiiiii
)(),(),(),( 3211212211 hOh
yfytfqa
tfpahytfaytfayy iiiiiiii
1
2/12/11
12
21
qapa
aaFour unknownsThree eq.
21 1 aa 2
111 21a
qp
There are an infinite number of second-2/1112 qaorder RK methods.
Second order Runge KuttaSecond-order Runge-Kutta
• Heun method without iteration– a2 = 1/2 a1 = 1/2 and p1 = q11 = 1a2 / a1 / a d p1 q11
hkkyy ii
211 2
121 ),(1 ii ytfk
)( hkyhtfk
• Midpont method
22 ),( 12 hkyhtfk ii
– a2 = 1 a1 = 0 and p1 = q11 = 1/2
hkyy )(fkhkyy ii 21 ),(1 ii ytfk )2/,2/( 12 hkyhtfk ii
Classical Fourth order RKClassical Fourth-order RK• Classical Fourth-order RK
– The most popular RK method
hkkkkyy ii 43211 2261
6
),(1 ii ytfk
hkyhtfk ii 12 2
1,21
22
hkyhtfk ii 23 2
1,21
– The fourth-order RK method is i il t th H th d i
hkyhtfk ii 34 ,
similar to the Heun method in that multiple estimates of the slop are used.
Example 20.3 (1/2)p ( / )Q. Use the classical fourth-order RK to integrate the following
f ti f t 0 t 1 ith t i f 1 d i iti l ditifunction from t = 0 to 1 with a step size of 1 and initial condition is y(0) = 2.
Sol ) ytfyey t ,5.04 8.0
Sol.)the slope at the beginning3)2(5.04)2,0( )0(8.0
1 efk53)50(3250 10 hky
the slope at the midpoint
5.3)5.0(325.0 10 hky217299.4)5.3(5.04)5.3,5.0( )5.0(8.0
2 efk
1086494)50(2172994250 hky
another slope at the midpoint
108649.4)5.0(217299.425.0 20 hky
912974.3)108649.4(5.04)108649.4,5.0( )5.0(8.03 efk
another slope at the midpoint
Example 20 3 (2/2)Example 20.3 (2/2)912974.5)0.1(912974.3230 hky
the slope at the end
)(30y0.8(0.5)
4 (1.0, 5.912974) 4 0.5 (5.912974) 5.945677k f e
the average slope
201037.4945677.5)912974.3(2)217299.4(2361
g p
final prediction(1.0) 2 4.201037 (1.0) 6.201037y
p
= 0 103% where the exact solution is 6 194631t = 0.103% where the exact solution is 6.194631.
Systems of EquationsSystems of Equations• Many practical problems require the solution y p p q
of a system of equations:dy1
d f1 t, y1, y2 ,, yn
dtf1 , y1, y2 , , yn
dy2
dt f2 t, y1, y2 ,, yn
dt
dyn
dt fn t, y1, y2 ,, yn
• The solution of such a system requires that ninitial conditions be known at the starting
dt n 1 2 n
initial conditions be known at the starting value of t.
Solution MethodsSolution Methods• Single-equation methods can be used to g q
solve systems of ODE’s as well; for example Euler’s method can be used onexample, Euler s method can be used on systems of equations - the one-step method is applied for every equation atmethod is applied for every equation at each step before proceeding to the next step.
• Fourth-order Runge-Kutta methods canFourth order Runge Kutta methods can also be used, but care must be taken in calculating the k’scalculating the k s.
Example 20.4 (1/3)p ( / )Q. Solve for the velocity and position of the free falling
b j i E l th d Th i iti l ditibungee jumper using Euler method. The initial condition for t = 0, x = v = 0. The step size is 2s and integrate up to t = 10s g= 9 81 m/s2 m= 68 1 kg cd= 0 25 kg/mto t = 10s. g= 9.81 m/s , m= 68.1 kg, cd= 0.25 kg/m.
The exact solution for v is
tgcgmtv dtanh)(The exact solution for v is
The exact solution for x is
mcd
)(
tgcmtx dcoshln)(The exact solution for x is
Compute the true relative errors of the results
tmc
tx d
d
coshln)(
Compute the true relative errors of the results.
Example 20.4 (2/3)Example 20.4 (2/3)Sol.)
Slope at t 0: dxSlope at t = 0: 0 vdtdx
81.9)0(25.081.9 22 vc
gdv d
Euler method ( t = 2 s) Analytic sol. Percent relative errors.x(2) = 19 16629 100%
81.9)0(1.68
81.9vm
gdt
0)2(00 x x(2) 19.16629 100%v(2) = 18.72919 4.756%
Euler method ( t = 4 s)
0)2(00 x62.19)2(81.90 v
( )
24.39)2(62.190 x
20.2519 62 9 81 (19 62) (2)v 19.62 9.81 (19.62) (2)68.1
36.41368
v
Example 20.4 (3/3)p ( / )
t xtrue vtrue xEuler vEuler t(x) t(v)
02
019.1663
018.7292
00
019.6200 100.00 4.76
468
10
71.9304147.9462237.5104334 1782
33.111842.076246.957549 4214
39.2400112.0674204.6640305 0244
36.413746.298350.180251 3123
45.4524.2513.838 72
9.9710.036.863 8310 334.1782 49.4214 305.0244 51.3123 8.72 3.83
Example 20.4 (1/4)p ( / )Q. Use the fourth-order RK method to solve for the same
problem as in Ex. 20.4. h=2.
Sol.) The differential equation to be integrated is
vvxtfdtdx
),,(1
cdv
Slope at the beginning of the interval.
22 ),,( v
mc
gvxtfdtdv d
p g g
0)0,0,0(11,1 fk
819)0(25.0819)000( 2 fk 81.9)0(1.68
81.9)0,0,0(22,1 fk
Example 20.4 (2/4)Example 20.4 (2/4)
2h 02200
2)0()1( 1,1
hkxx
81.92281.90
2)0()1( 21
hkvv
Slope at the midpoint of the interval.
22)()( 2,1
8100.9)81.9,0,1(11,2 fk
4567.9)81.9,0,1(22,2 fk
810092810090)0()1( hkxx 8100.9
28100.90
2)0()1( 1,2 kxx
4567.9224567.90
2)0()1( 2,2
hkvv22
Example 20.4 (3/4)Example 20.4 (3/4) Another slope at the midpoint of the interval.
4 69)4 69810091(fk 4567.9)4567.9,8100.9,1(11,3 fk
4817.9)4567.9,8100.9,1(22,3 fk
913418)2(456790)0()2( hk 9134.18)2(4567.90)0()2( 1,3 hkxx
9634.18)2(4817.90)0()2( 2,3 hkvv
Slope at the end point of the interval.
963418)9634189134182( fk 9634.18)9634.18,9134.18,2(11,4 fk
4898.8)9634.18,9134.18,2(22,4 fk
Example 20.4 (4/4)p ( / ) Average slope
1 1656.192]9634.18)4567.98100.9(20[610)2( x
7256.182]4898.8)4817.94567.9(28100.9[610)2( v ])([6
)(
t xtrue vtrue xRK4 vRK4 t(x) t(v)
0246
019.166371.9304
147.9642
018.729233.111842.0762
019.165671.9311
147.9521
018.725633.099542.0547
0.0040.0010.004
0.0190.0370.0516
810
147.9642237.5104334.1782
42.076246.957549.4214
147.9521237.5104334.1626
42.054746.934549.4207
0.0040.0000.005
0.0510.0490.038
MATLAB RK4 CodeMATLAB RK4 Code