DSAS 4 Runge-Kutta Formula For Differential...
Transcript of DSAS 4 Runge-Kutta Formula For Differential...
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-1
4. Runge-Kutta Formula For Differential Equations
A. Euler Formula
B. Runge-Kutta Formula
C. An Example for Fourth-Order Runge-Kutta Formula
To solve the differential equations numerically, the most useful formula is called
Runge-Kutta formula which has been widely used in numerical analysis.
A. Euler Formula
For a dynamic system without input, it is generally expressed as the following
first-order differential equation:
( ) ( )( )txtftx ,=& , x(t0)=α (4-1)
where t=t0 is the initial time and x(t0)=α is the initial condition. The problem to solve
x(t) in (4-1) for t>t0 is called the initial value problem, or IVP in brief. For example,
the following equation
( ) ( ) ttxtx +−=& , x(0)=1 (4-2)
is an IVP and its solution can be obtained in closed form as below:
( ) 12 −+= − tetx t , t≥0 (4-3)
However, if (4-1) is given more complicated, such as
( ) ( ) ( )tsinttxtx ⋅+−= 2& , x(0)=1 (4-4)
then it is often difficult or impossible to find x(t) in closed form since most of the
differential equations do not have closed form solution. To determimne the solution,
numerical methods are required.
The simplest numerical method is called the Euler formula, which was propsed
by Euler in 1768. With the use of fixed grid size ∆t=h, the grid points along t are
orderly denoted as t0, t1, t2,… , where
hitt ⋅+= 0i , i=1,2,3,…… (4-5)
and the value x(t) at ti is defined as
( )ii txx = , i=0,1,2,3,…… (4-6)
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-2
where x0=x(t0)=α is given. According to the definition of derivative, we have
( ) ( ) ( )t
txttxlimtxt ∆
∆∆
−+=→0
& , (4-7)
which implies the derivative of x(t) at t=ti can be approximately expressed as
( ) ( ) ( ) ( ) ( )h
xx
h
txtx
t
txttxtx iiiiii
i
−=
−=
−+≈ ++ 11
∆∆
& (4-8)
Substituting (4-8) into (4-1) at t=ti leads to
( )iiii x,tf
h
xx≈
−+1 , x0=α (4-9)
and then
( )ii xtfhxx ,ii ⋅+=+1 , x0=α (4-10)
which is the well kown Euler formula. Clearly, in order to achieve a solution more
precisely, the grid size h should be as small as possible. However, reducing the grid
size h is inefficient since the cost of calculation time may increase tremendously.
Now, let’s introduce Taylor’s expansion to explain the error caused by Euler
formula (4-10). According to Taylor’s expansion, a function y(t) continuous at t=ti can
be expressed as
( ) ( ) ( ) ( ) LL +−++−+−+= ninii ttattattaatx 2
210 (4-11)
whose higher order derivatives are
( ) ( ) ( ) ( ) LL& +−++−+−+= −12321 32 n
inii ttnattattaatx (4-12)
( ) ( ) ( )
( ) ( ) LL
L&&
+−−++
+−⋅+−⋅+=−2
2432
1
34232n
in
ii
ttann
ttattaatx (4-13)
( ) ( ) ( )
( )( ) ( ) LL
L&&&
+−−−++
+−⋅⋅+−⋅⋅+⋅=−3
2543
21
34523423n
in
ii
ttannn
ttattaatx (4-14)
Viewing from the above equations, we have ( )itxa =0 and ( ) ( )ik
k txk
a!
1= for
k=1,2,…,∞, and thus, (4-11) can be rewritten as
( ) ( ) ( )( ) ( ) ( )( ) ( ) ( ) LL
&&& +−++−+−+= n
ii
n
ii
iii ttn
txtt
txtttxtxtx
!!2
2 (4-15)
Let t=ti+1, we have
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-3
( ) ( ) ( )( ) ( ) ( )
( ) ( ) ( ) LL
L&&
&
+−++
+−+−+=
+
+++
nii
in
iii
iiiii
ttn
tx
tttx
tttxtxtx
1
2111
2
!
! (4-16)
i.e.,
( ) ( ) ( ) ( )
( ) ( )2
12
1
2
hOhxtfx
hn
xtfh
xtfhxtfxx
iii
niin
iiiiii
++=
+++′
++=−
+
,!
,
!
,, LL
(4-17)
where
( ) ( ) ( ) ( )LL +++
′=
−nii
nii h
n
xtfh
xtfhO
!
,
!
, 122
2 (4-18)
Comparing (4-17) with (4-10), we know that the term ( )2hO is the error of Euler
formula, which is proportional to h2.
B. Runge-Kutta Formula
In order to reduce the error to h3, further modify Euler form (4-10) as the
following form:
( ) ( )hxhtfxtfh
xx δγββ +++=−+
iiiiii , , 10
1 , (4-19)
where β0, β1, γ and δ are variables to be determined and a new term ( )hxhtf δγ ++ ii ,
is included. Since ( )( )txtf , depends on x(t) and t, its Taylor’s expansion at x(t)=xi
and t=ti is given as the following form
( )( ) ( ) ( )( ) ( ) ( )( )( )
( )( ) ( ) ( ) ( )( )iittxitttixx
iitxittixit
xtxttattaxtxa
ttxtxattaxtxattaaty,tf
−−+−+−+
−−+−+−+−+=232
20
( ) ( )( ) ( )( ) L+−+−−+ 32 ixxxiitxx xtxaxtxtta (4-20)
where all the coefficients a0, at, ax, att, atx … are constant. Its partial derivatives are
( )( ) ( )( )
( ) ( )( ) ( )( ) ( )( ) ( )( ) L+−+−−+
−+−+−+=∂
∂=
2
2
2
32
itxxiittx
itttitxittt
t
xtxaxtxtta
ttaxtxattaa
t
tx,tftx,tf
(4-21)
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-4
( )( ) ( )( )( )
( ) ( )( ) ( )( ) ( )( ) ( )( ) L+−+−−+
−+−+−+=
∂∂=
2
2
32
2
ixxxiitxx
ittxixxitxx
x
xtxaxtxtta
ttaxtxattaa
tx
txtftxtf
,,
(4-22)
( )( ) ( )( ) ( )( )
( ) ( )( ) L+−+−⋅+=∂
∂=
∂∂=
ittxittttt
ttt
xtxattaat
txtf
t
txtftytf
2232
2
2 ,,,
(4-23)
( )( ) ( )( )
( )( )( )
( ) ( )( ) L+−+−+=∂
∂=
∂∂∂=
itxxittxtx
xtx
xtxattaa
t
txtf
ttx
txtftytf
22
2 ,,,
(4-24)
( )( ) ( )( )
( )( )( )( )
( )( ) ( )( ) L+−⋅+−+=
∂∂
=∂
∂=
ixxxitxxxx
xxx
xtxattaa
tx
txtf
tx
txtftxtf
2322
2
2 ,,,
(4-25)
Clearly, all the coefficients can be derived from the above partial derivatives and
expressed as ( )ii xtfa ,=0 , ( )iitt xtfa ,= , ( )iixx xtfa ,= , ( )iitttt xtfa ,!2
1= ,
( )iitxtx xtfa ,= , ( )iixxxx xtfa ,!2
1= , …. Substituting them into (4-20) yields
( )( ) ( ) ( )( ) ( ) ( )( ) ( )( )2
2
1iiittiiixiiitii ttx,tf
!xtxx,tfttx,tfx,tftx,tf −+−+−+=
( )( ) ( )( ) ( ) ( )( )
( )( ) ( ) ( )( ) L+−+−+
−+−−+
33
2
3
1
3
1
2
1
iiixxxiiittt
iiixxiiiitx
xtxx,tf!
ttx,tf!
xtxx,tf!
xtxttx,tf (4-26)
Let t=ti+γh and x(t)=xi+δh, then
( )( ) ( ) ( ) ( )
( ) ( ) ( )3222
22
,2
1 ,
,2
1 , , ,
hOxtfhxtfh
xtfhxtfhxtfhxtf
hxhtf
xxtx
ttxt
+++
+++=
++
iiii
iiiiiiii
ii ,
δγδ
γδγ
δγ
(4-27)
Clearly, (4-19) can be rewritten as
( ) ( )[ ] ( ) ( )[ ]
( ) ( ) ( )[ ]( )4
211
21
3
11
2
101
,3,6,33
,2,22
,
hO
xtfxtfxtf!
h
xtfxtf!
hxtfhxx
iixxiitxiitt
iixiitiiii
+
+++
++++=+
δβγδβγβ
δβγβββ
(4-28)
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-5
If we want to take the precision to h2, from (4-17) and (4-28) we have
( ) ( ) ( )( ) ( )[ ]
( ) ( )[ ] ( )311
2
10
321
,2,22
,
2
hOxtfxtf!
h
xtfhx
hOh!
x,tfhx,tfxx
iixiit
iii
iiiiii
+++
++≈
+′
++≈+
δβγβ
ββ (4-29)
Since ( ) ( )iii xtftx ,=& , the term ( )ii x,tf ′ can be changed into the following form:
( ) ( ) ( ) ( )
( ) ( ) ( )iiiixiit
iiixiitii
xtfxtfxtf
txxtfxtfxtf
,,,
,,,
+=+=′ &
(4-30)
As a result, from (4-29) and (4-30) we obtain
( ) ( ) ( ) ( )( )
( ) ( )[ ] ( ) ( )[ ]iixiitii
iiiixiitii
xtfxtf!
hxtfh
xtfxtfxtf!
hhx,tf
,2,22
,
,,,2
11
2
10
2
δβγβββ +++=
++ (4-31)
which leads to
110 =+ ββ (4-32)
( ) ( ) ( ) ( ) ( )iiiiiiiiii xtfxtfxtfxtfxtf xtxt , , , ,2,2 11 +=+ δβγβ (4-33)
i.e., 12 1 =γβ (4-34)
( )ii xtf ,2 1 =δβ (4-35)
Since there are four variables β0, β1, γ and δ in three equations (4-32), (4-34) and
(4-35), the choice of these variables is not unique. The most common one is
( )ii xtf , ,1 ,2
110 ==== δγββ (4-36)
i.e., from (4-28) we have the approximate ssolution as below:
( ) ( )( )ii xthfxhtfh
xtfh
xx ,2
,21 ++++=+ iiiiii , , (4-37)
which is called the second-order Runge-Kutta formula. For the convenience of
programming, the formula is often rearranged as below:
( )101 2
1kkxx ++=+ ii (4-38)
where
( )( )
++⋅=⋅=
01
0
,
kx,htfhk
xtfhk
ii
ii (4-39)
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-6
In case that a higher precision is required, we oftem employ the higher order
Runge-Kutta formula. For example, if a precision of h4 is needed, then the
fourth-order Runge-Kutta formula must be used, which is given as
( )32101 226
1kkkkxx ++++=+ ii (4-40)
where
( )
( )
+⋅=
++⋅=
++⋅=⋅=
231
2
010
,22
22 ,
kx,tfhk
kx,
htfhk
kx,
htfhkx,tfhk
iiii
iiii
(4-41)
This formula has been widely applied to a lot of applications in engineering due to its
simplicity and acceptable accuracy.
C. An Example for Fourth-Order Runge-Kutta Formula
Next, let’s use an example to show the programming of the fourth-order
Runge-Kutta formula in MATLAB. Consider the following equation:
( ) ( )( ) ( ) 1+−== txttxftx ,& , x(0)=0.5 (4-42)
and find the solution x(t) for t≥0. The closed form solution can be obtained as
( ) 150 +−= −tetx . (4-43)
Now let’s apply the fourth-order Runge-Kutta formula to solve (4-42) numerically. If
the step size is set as h=0.01, then the precision will be in the order of h4=10−8.
According to (4-40), we have
( )32101 226
1kkkkxx ++++=+ ii (4-44)
where
( ) ( ) ( )
( ) ( )( ) ( )11
12
1222
12
1222
11,
2223
1112
0001
0
−+−=++−=+⋅=
−+−=
+
+−=
++⋅=
−+−=
+
+−=
++⋅=
−−=+−=⋅=
kxhkxh,kx,tfhk
kxh
kxh
kx,
htfhk
kxh
kxh
kx,
htfhk
xhxhxtfhk
iiii
iiii
iiii
iiii
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-7
The programming in MATLAB is given as below:
=================================================== >> % Fourth order Runge-Kutta method >> x0=0.5; % initial condition x(0)=0.5 >> h=0.01; % step size 0.01sec >> k0=-h*(x0-1); k1=-h*(x0+k0/2-1); >> k2=-h*(x0+k1/2-1); k3=-h*(x0+k2-1); >> x(1)=x0+(k0+2*k1+2*k2+k3)/6; >> e(1)=x(1)-(-0.5*exp(-h)+1); % numerical error at t=h >> t(1)=h; >> for n=1:599 % total simulation time 6 sec >> k0=-h*(x(n)-1); k1=-h*(x(n)+k0/2-1); >> k2=-h*(x(n)+k1/2-1); k3=-h*(x(n)+k2-1); >> x(n+1)=x(n)+(k0+2*k1+2*k2+k3)/6; >> t(n+1)=(n+1)*h; % t-axis >> e(n+1)=x(n+1)-(-0.5*exp(-t(n+1))+1); % numerical error >> end >> plot(t,x); xlabel(‘t’); ylabel(‘x(t)’)
0 1 2 3 4 5 60.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1
t
x(t)
>> plot(t,e); xlabel(‘t’); ylabel(‘e(4-t)’)
0 1 2 3 4 5 6-1.6
-1.4
-1.2
-1
-0.8
-0.6
-0.4
-0.2
0x 10
-11
t
e(t)
===========================================================
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-8
From the above numerical results of e(t), it is truth that the precision is less than
10−10 which is indeed around the order of h4 (10−8). In MATLAB, some instructions are
provided to solve the differential equations, such as ode23 and ode45. In fact, these
instructions are also implemented by the concept of Runge-Kutta method. Now, let’s
use the instruction ode23 to solve the system (4-42) and use the instruction ode45 to
solve the system described below:
( ) ( )( ) ( ) ( )tsinsintxtxtftx 1050., +−==& , x(0)=0.5 (4-45)
whose solution can not be expressed by closed form. The programming in MATLAB
is given as below:
========================================= Create m-file: first1.m function dx=first1(t,x) dx=-x+1; Create m-file: first2.m function dx=first2(t,x) dx=-x+0.5*sin(sin(10*t));
>> % key in the following instructions >> [t,x]=ode23(@first1,[0:0.01:6],0.5) >> plot(t,x); xlabel(‘t’); ylabel(‘x(t)’)
0 1 2 3 4 5 60.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1
t
x(t)
>> % key in the following instructions >> [t,x]=ode45(@first2,[0:0.01:6],0.5) >> plot(t,x); xlabel(‘t’); ylabel(‘x(t)’)
NCTU Department of Electrical and Computer Engineering Senior Course <Dynamic System Analysis and Simulation>
By Prof. Yon-Ping Chen
4-9
0 1 2 3 4 5 6-0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
t
x(t)
=====================================================
Problems
P.4-1 Derive the following third-order Runge-Kutta formula:
( )2101 46
1kkkxx ii +++=+
where
( )
( )102
01
0
222
,
kkx,htfhk
kx,
htfhk
xtfhk
ii
ii
ii
+−+⋅=
++⋅=
⋅=
P.4-2 Solve the following system:
( ) ( )( ) ( ) ( )tsinetxtxtftx t 52 10., −+−==& , x(0)=1