14) Ordinary Differential Equation (Ode) - Runge-Kutta Methods (Compatibility Mode)
14) Ordinary Differential Equation (Ode) - Runge-Kutta Methods (Compatibility Mode)
Equation
quat o (O
(ODE)/
)/
Runge-Kutta Methods
By
Dr Ali Jawarneh
Dr Ali Jawarneh 1
Ch.25: Ordinary Differential Equations/
Runge-Kutta Methods
25 1 Euler
25.1- Euler's
s Method
25.2- Improvements of Euler's Method
25.3- Runge-Kutta Methods
25.4- Systems of Equations
Ch.27: Boundary-Value and Eigen-Value
Problems
27.1- General Methods for Boundary-Value Problems
and Derivative Boundary Conditions.
Dr Ali Jawarneh 2
This chapter is developed
to solve ODE on the form:
dy
= g( x, y )
dx
Dr Ali Jawarneh 3
Euler’s
Euler s Methods
y
It is based on the low-order Taylor series:
f ( x i +1 ) = f ( x i ) + f ' ( x i )( x i +1 − x i )
y i +1 = y i + f ( x i , y i )h h
Φ
x
where f ( x i , y i ) = Φ = y' ( x i , y i ) = slope
xi x i +1
h = x i +1 − x i = step size
Dr Ali Jawarneh 4
Example: Solve the following initial-value problem
analytically and numerically using EulerEuler’s
s method
over the interval from x=0 to 2 with step size of h=0.5,
given the initial condition y(0)=1.
dy
− yx 2 + 1.1y = 0
dx
Solution:
Analytically:
dy
dy dy
∫ y ∫ − 1.1)dx
= 2
dy = 2
− = 2
− 1.1 ( x
− yx + 1.1y = 0 dx
2 yx 1 . 1y x
dx y
x3 x3 x3
Ln( y ) = − 1.1x + c y=e 3
− 1.1 x + c
= c 1e 3
− 1. 1 x
3
From the initial condition y(0)=1 c1 = 1
x3
−1.1 x
y=e 3
Dr Ali Jawarneh 5
Numerically:
y i +1 = y i + f ( x i , y i )h
dy
= yx 2 − 1.1y
dx
Initial condition y(0)=1 0 0.5 1 1.5 2
y (0.5) = y (0) + f (0,1)(0.5) = 1 + ( −1.1)(0.5) = 0.45
f (0,1) = 1(0) 2 − 1.1(1) = −1.1
y (1) = y (0.5) + f (0.5,0.45)(0.5) = 0.45 + ( −0.3825)(0.5) = 0.25875
f (0.5,0.45) = 0.45(0.5) 2 − 1.1(0.45) = −0.3825
y (1.5) = y (1) + f (1,0.25875)(0.5) = 0.25875 + ( −0.02588)(0.5) = 0.245813
Dr Ali Jawarneh 7
Improvements of Euler’s Methods
(1)Heun’s Method
Dr Ali Jawarneh 8
Heun’s Method slope = f ( x i +1 , y o i +1 )
f ( x i , y i ) + f ( x i +1 , y o i +1 )
y i +1 = y i + h
2 xi x i +1
Corrector Equation
Dr Ali Jawarneh 9
Example: Solve the following equation using Heun’s
method for the interval from x=0
x 0 to x
x=4
4 with h
h=1.
1. Initial
condition y(0)=2. y' = 4e − 0.5y
0.8 x
Solution:
f ( x i , y i ) + f ( x i +1 , y o i +1 )
y o
i +1 = y i + f ( x i , y i )h y i +1 = yi + h
2 0 1 2 3 4
y o (1) = y ( 0) + f (0,2)1 = 2 + 3 * 1 = 5 f ( 0, 2 ) = 3
2 16.31978
3 37.19924
4 83.3377
Dr Ali Jawarneh 11
The Midpoint Method
( Improved Polygon Method))
(or
This is another simple slope = f ( x 1 ,y 1 )
i+ i+
2 2
modification
difi ti off Euler’s
E l ’ method.
th d
Technique: use Euler’s method
to predict a value of y at the
midpoint of the interval
h xi x
y 1 = y i + f (xi , y i ) i+
1
2
i+
2
2 slope = f ( x ,y )
1 1
i+ i+
2 2
y i +1 = y i + f ( x 1 ,y 1 )h
i+ i+
2 2
xi x i +1
Dr Ali Jawarneh 12
Example: Solve the following equation using the
midpoint method with a step size of 0.5
0 5 and x=[0 2].
2]
dy
− yx 2 + 1.1y = 0 y (0) = 1
d
dx
Solution:
h y i +1 = y i + f ( x ,y )h 0 0.5 1 1.5 2
y 1 = y i + f (xi , y i ) i+
1
2
i+
1
2
i+ 2
2
0.5 0 .5
y (0.25) = y (0) + f (0,1) = 1 + ( −1.1) = 0.725 f (0,1) = −1.1
2 2
y (0.5) = y (0) + f (0.25,0.725)0.5 = 1 + ( −0.75219)0.5 = 0.623906
f (0.25,0.725) = −0.75219
0. 5
y (0.75) = y ( 0.5) + f ( 0.5,0.623906 ) = 0.491326 Dr Ali Jawarneh
2
y (1) = y (0.5) + f (0.75,0.491326)0.5 = 0.491862
13
x y f(xi,yi) yi+1/2 f(xi+1/2,yi+1/2)
15
1.5 0 602762
0.602762 0 693176 0.776056
0.693176 0 776056 1 52301
1.52301
Dr Ali Jawarneh 14
Runge-Kutta
g Methods
Runge‐Kutta (RK) methods achieve the accuracy of a Taylor series without
requiring the calculation of higher derivatives
The general form: y i +1 = y i + Φ( x i , y i , h )h
Φ( x i , y i , h ) =increment function which represent the slope over the interval
The general form of Φ : Φ = a1k 1 + a 2k 2 + ... + a n k n
k 1 = f (xi , y i )
k 2 = f ( x i + p1h, y i + q11k 1h )
k 3 = f ( x i + p 2h, y i + q 21k 1h + q 22k 2h )
.
.
.
n=1:
=1 1st order
d RKRK, O(h)
n=2: 2nd order RK,, O(h
( 2)
n=3: 3rd order RK, O(h3)
n=4: 4th order RK,
RK O(h4)
Dr Ali Jawarneh 16
1st order RK Method
1st order RK is Euler’s method:
y i + 1 = y i + a 1k 1h
a1 = 1
y i + 1 = y i + k 1h
k 1 = f (xi , y i )
Dr Ali Jawarneh 17
2nd order RK Methods
y i +1 = y i + (a1k 1 + a 2k 2 )h (1)
k 1 = f (xi , y i )
k 2 = f ( x i + p1h, y i + q11k 1h )
Values of a1, a2, p1, and q11 are evaluated by setting Eq.(1) equal to a Taylor
series expansion to the 2nd order term and use the Chain rule:
a1 + a 2 = 1, a 2p1 = 0.5, a 2q11 = 0.5
Because we have 3 equations with 4 unknown, we must assume a value
of one of the unknowns. Suppose that we specify a2. Then
0.5 0.5
a1 = 1 − a 2 , p 1 = , q11 =
a2 a2
S we can choose
So, h an infinite
i fi it number
b off values
l for
f a2, then
th there
th are an
infinite number of 2nd order RK methods
Dr Ali Jawarneh 18
3 common versions of 2nd order RK
(1) Heun Method (a2=1/2)
1/2)
a1 = 0.5, p1 = q11 = 1
1 1
y i +1 = y i + ( k 1 + k 2 )h k 1 = f ( x i , y i ) k 2 = f ( x i + h, y i + k 1h )
2 2
((2)) The Midpoint
p Method ((a2=1))
a1 = 0, p1 = q11 = 0.5
y i + 1 = y i + k 2h k 1 = f ( x i , y i ) k = f ( x + 1 h , y + 1 k h )
2 i i 1
2 2
(3) Ralston’s Method (a2=2/3)
a1 = 1 / 3, p1 = q11 = 3 / 4
1 2 3 3
y i +1 = y i + ( k 1 + k 2 )h k 1 = f ( x i , y i ) k 2 = f ( x i + h, y i + k 1h )
3 3 4 4
Dr Ali Jawarneh 19
We have been showing Heun
and Midpoint Methods in
Euler and 2nd order RK.
RK
Is there any
diff
difference??
Dr Ali Jawarneh 20
Example: solve the following equation using Heun,
Midpoint, and Ralson
Ralson’s s methods. Integrate from x
x=0
0 to
x=4 with a step size of 0.5
dy
+ 2x 3 − 12x 2 + 20x − 8.5 = 0
dx
Initial condition: y ( 0) = 1
S l ti
Solution
(1) Heun Method 0 0 .5 1 1 .5 2 2 .5 3 3 .5 4
1 1 1 1
y i +1 = y i + ( k 1 + k 2 )h y (0.5) = y (0) + ( k 1 + k 2 )0.5
2 2
2 2
k 1 = f (xi , y i ) k 1 = f (0,1) = y' (0,1) = 8.5
k 2 = f ( x i + h, y i + k 1h )
k 2 = f (0 + 0.5,1 + (8.5 * 0.5)) = f (0.5,5.25) = y' (0.5,5.25) = 1.25
1 1
Dr Ali Jawarneh y (0.5) = 1 + ( 8.5 + 1.25)0.5 = 3.4375
2 2 21
1 1
y (1) = y (0.5) + ( k 1 + k 2 )0.5 x yHuen
2 2
k 1 = f (0.5,3.4375)
0 1
k 2 = f (0.5 + 0.5,3.4375 + k 1 0.5)
0.5 3.4375
1 3.375
and SO ON…
15
1.5 2 6875
2.6875
2 2.5
2.5 3.1875
3 4.375
3.5 4.9375
4 3
Dr Ali Jawarneh 22
(2) Midpoint Method
1 1
y i + 1 = y i + k 2h k 1 = f ( x i , y i ) k 2 = f ( x i + h , y i + k 1h )
2 2
y (0.5) = y (0) + 0.5k 2
x yMidpoint
k 1 = f (0,1) = 8.5
0 1
11 1 1
k 2 = f (0 + ,1 + 8.5 ) = f (0.25,2.125) = 4.21875 05
0.5 3 109375
3.109375
22 2 2
y (0.5) = y (0) + 0.5k 2 = 1 + (0.5 * 4.21875) = 3.109375
1 2.8125
1.5 1.984375
y (1) = y (0.5) + 0.5k 2 2 1.75
k 1 = f (0.5,3.109375 ) 2.5 2.484375
11 1 1 3 3.8125
k 2 = f (0.5 + ,3.109375 + k 1 )
22 2 2 3.5 4.609375
and SO ON… 4 3
Dr Ali Jawarneh 23
(3) Ralston’s Method
1 2 = 3 3
y i +1 = y i + ( k 1 + k 2 )h 1
k f ( x i , y i ) k 2 = f ( x i + h , y i + k 1h )
3 3 4 4
1 2 1
y (0.5) = y (0) + ( k 1 + k 2 ) x yRalston
3 3 2
0 1
k 1 = f (0,1) = 8.5
05
0.5 3 277344
3.277344
31 3 1
k 2 = f (0 + ,1 + * 8.5 * ) = f (0.375,3.1875) = 2.58203 1 3.101563
42 4 2
1.5 2.347656
1 2 1
y ( 0 .5 ) = y ( 0 ) + ( k 1 + k 2 ) = 2 2.140625
3 3 2
1 2 1 2.5 2.855469
1 + ( 8.5 + 2.58203) = 3.277344
3 3 2
3 4.117188
3.5 4.800781
and SO ON… 4 3.03125
Dr Ali Jawarneh 24
3rd order RK Methods
Infinite number of versions, the most common version is:
1
y i +1 = y i + (k 1 + 4k 2 + k 3 )h
6
k 1 = f (xi , y i )
1 1
k 2 = f ( x i + h , y i + k 1h )
2 2
k 3 = f ( x i + h, y i − k 1h + 2k 2h )
Note: if the derivative is a function of x only, this 3rd order method reduces to
Simpson’s
Simpson s 1/3 rule
Dr Ali Jawarneh 25
4th order RK Methods
Infinite number of versions, the most common version is called
“ Classical Fourth-Order RK Method”:
1
y i +1 = y i + (k 1 + 2k 2 + 2k 3 + k 4 )h
6
k 1 = f (xi , y i )
1 1
k 2 = f ( x i + h , y i + k 1h )
2 2
1 1
k 3 = f ( x i + h, y i + k 2h )
2 2
k 4 = f ( x i + h, y i + k 3h )
y this 4th order method is similar to
Note: if the derivative is a function of x only,
Simpson’s 1/3 rule
Dr Ali Jawarneh 26
Example: Use 4th order RK method with h=0.5 for
interval x=
x [0 2] to solve the initial problem:
dy y ( 0) = 1
− yx 2 + 1.1y = 0
dx
Solution
1
i +1 = y i + (k 1 + 2k 2 + 2k 3 + k 4 )h
y i+ 0 0.5 1 1.5 2
6
k 1 = f (xi , y i )
1 1
k 2 = f ( x i + h , y i + k 1h )
2 2
1 1
k 3 = f ( x i + h , y i + k 2h )
2 2
k 4 = f ( x i + h , y i + k 3h )
Dr Ali Jawarneh 27
k 1 = f (xi , y i ) k 1 = f (0,1) = −1.1
1 1 1 1
k 2 = f ( x i + h, y i + k 1h ) k 2 = f (0 + 0.5,1 + ( −1.1)0.5) = f (0.25,0.725) = −0.75219
2 2 2 2
1 1
k 3 = f ( x i + h , y i + k 2h )
2 2
1 1
k 3 = f (0 + 0.5,1 + ( −0.75219)0.5) = f (0.25,0.811953) = −0.8424
2 2
k 4 = f ( x i + h , y i + k 3h )
k 4 = f (0 + 0.5,1 + ( −0.8424)0.5) = f (0.5,0.5788) = −0.49198
1
y i +1 = y i + (k 1 + 2k 2 + 2k 3 + k 4 )h
6
1
y (0.5) = 1 + (( −1.1) + 2( −0.75219) + 2( −0.8424) + ( −0.49198))0.5 = 0.60157
6
Dr Ali Jawarneh 28
x y k1 k2 k3 k4
0 1 ‐1.1 ‐0.75219 ‐0.8424 ‐0.49198
1 0 464524 ‐0.04645
0.464524 0 04645 0.209471
0 209471 0.239062
0 239062 0.671663
0 671663
15
1.5 0 59138
0.59138 0 680087 1.494252
0.680087 1 494252 1.893701
1 893701 4.460869
4 460869
Dr Ali Jawarneh 29
System
y of Equations
q
dy 1
= f1 ( x, y 1 , y 2 , y 3 ,..., y n )
dx
d
dy 2
= f 2 ( x, y 1 , y 2 , y 3 ,..., y n )
dx
dy n
= f n ( x, y 1 , y 2 , y 3 ,..., y n )
dx
Dr Ali Jawarneh 30
Example: Split the following 2nd order ODE to system of
equations
q of order 1.
mx"+ cx'+ kx = cos( ωt ) x(0) = 3 x' (0) = 4
Solution
d2x dx
m 2 +c + kx = cos(ωt )
dt dt
dx x( 0 ) = 3
=z (1)
dt
dz
m + cz + kx = cos(ωt )
dt
dz c k dx
= − z − x + cos(( ωt ) ( 2) z ( 0) = ( 0) = 4
dt m m dt
Dr Ali Jawarneh 31
Example: Split the following 3rd order ODE to system
of equations
q of order 1.
d3y d2y
3
− 2 + 2xy = 0 y (0) = 1 y ' ( 0 ) = 2 y" ( 0 ) = 3
dx dx
Solution
dy
=z (1) y ( 0) = 1
dx
d 2 z dz
2
− + 2xy = 0
dx dx
( 2) z ( 0) = y' ( 0) = 2
dz
=w
dx
dw
− w + 2xy = 0
dx
2
dz d dy d y
dw
= w − 2xy ( 3) w ( 0 ) = = = 2
( 0) = 3
dx dx dx dx dx
Dr Ali Jawarneh 32
Euler’s Method
Example: Use Euler’s method to solve the following
equation from t=0 to 4 using h=0.1
d2y
2
− 0.5t + y = 0 y ( 0) = 2 y' ( 0) = 0
dt
Solution:
dy y ( 0) = 2
=z
dt
dz dy
= 0.5t − y z(0) = ( 0) = 0
dt dt
Dr Ali Jawarneh 33
Single: y i +1 = y i + f ( x i , y i )h
Euler’s method
for single
g can be
y i +1 = y i + f1 (t i , y i , z i )h
extended to the
system: z i +1 = z i + f 2 (t i , y i , z i )h
y (0.1) = y (0) + f1 (0,2,0)0.1 = 2 + 0(0.1) = 2
z(0.1) = z(0) + f 2 (0,2,0)0.1 = 0 + ( −2)(0.1) = −0.2
y(0.2) = y(0.1) + f1 (0.1,2,−0.2)0.1 = 2 + ( −0.2)(0.1) = 1.98
z(0.2) = z(0.1) + f 2 (0.1,2,−0.2)0.1 = −0.2 + ( −1.95)(0.1) = −0.395
A d so on…
And
Dr Ali Jawarneh 34
t y z f1 f2
0 2 0 0 ‐2
0.1 2 ‐0.2 ‐0.2 ‐1.95
0.2 1.98 ‐0.395 ‐0.395 ‐1.88
0.3 1.9405 ‐0.583 ‐0.583 ‐1.7905
0.4 1.8822 ‐0.76205 ‐0.76205 ‐1.6822
0.5 1.805995 ‐0.93027 ‐0.93027 ‐1.556
.
.
.
4
Dr Ali Jawarneh 35
RK Methods
RK method for single can be extended to the system
x= [0 5], h=0.5
Solution:
dy
=z y ( 0) = 4
dx
dz
= −0.6z − 8y z(0) = 0
dx
Dr Ali Jawarneh 36
RK method 1
k 1 = f (xi , y i )
y i +1 = y i + (k 1 + 2k 2 + 2k 3 + k 4 )h
for single 6
1 1 1 1
k 2 = f ( x i + h , y i + k 1h ) k 3 = f ( x i + h , y i + k 2 h ) k 4 = f ( x i + h , y i + k 3h )
2 2 2 2
RK method for system:
1 1
y i +1 = y i + (k 1 + 2k 2 + 2k 3 + k 4 )h z i +1 = z i + (l 1 + 2l 2 + 2l 3 + l 4 )h
6 6
k 1 = f1 ( x i , y i , z i ) l1 = f 2 (xi , y i , z i )
1 1 1 1 1 1
k 2 = f1 ( x i + h, y i + k 1h, z i + l 1h ) l 2 = f 2 ( x i + h , y i + k 1h , z i + l 1h )
2 2 2 2 2 2
1 1 1 1 1 1
k 3 = f1 ( x i + h , y i + k 2 h , z i + l 2 h ) l 3 = f 2 ( x i + h, y i + k 2h, z i + l 2h )
2 2 2 2 2 2
k 4 = f1 ( x i + h , y i + k 3h , z i + l 3h ) l 4 = f 2 ( x i + h , y i + k 3h , z i + l 3h )
Dr Ali Jawarneh 37
k 1 = f1 (0,4,0) = 0
l 1 = f 2 (0,4,0) = −32
1 1 1
k 2 = f1 (0 + 0.5,4 + (0)0.5,0 + ( −32)0.5) = f1 (0.25,4,−8) = −8
2 2 2
l 2 = f 2 (0.25,4,−8) = −27.2
1 1 1
k 3 = f1 (0 + 0.5,4 + ( −8)0.5,0 + ( −27.2)0.5) = f1 (0.25,2,−6.8) = −6.8
2 2 2
l 3 = f 2 (0.25,2,−6.8) = −11.92
l 4 = f 2 (0.5,0.6,−5.96) = −1.22
1
y (0.5) = 4 + [0 + 2( −8) + 2( −6.8) + ( −5.96)]0.5 = 1.0367
6
1
z(0.5) = 0 + [( −32) + 2( −27.2) + 2( −11.92) + ( −1.22)]0.5 = −9.2887
6
Dr Ali Jawarneh 38
x y z k1 l1 k2 l2 k3 l3 k4 l4
0 4 0 0 ‐32 ‐8 ‐27.2 ‐6.8 ‐11.92 ‐5.96 ‐1.22
0.5 1.0367 ‐9.2887 ‐9.29 ‐2.72 ‐9.97 16.27 ‐5.22 14.78 ‐1.9 13.74
1 ‐2.4276 ‐3.1969 ‐3.2 21.34 2.14 24.53 2.94 13.38 3.49 5.58
1.5 ‐1.5571
1.5571 5.3654 5.37 9.24 7.67 ‐2.88
2.88 4.65 ‐5.68
5.68 2.53 ‐7.64
7.64
Dr Ali Jawarneh 39
Example: Given the following equation. The initial
velocity is zero,
zero and the initial displacement is 1 m.
m
Using 4RK method with a time step of 1 sec, find the
displacement and velocity at t=1 sec.
mx"+ cx'+ kx = 0
m = 20 kg, c = 40 N .s / m , k = 20 N / m
Solution
d2x dx
20x"+40x'+20x = 0 2
+2 +x=0
dt dt
dx
= v x( 0 ) = 1
dt
dv dv
+ 2v + x = 0 = −2 v − x v( 0) = 0
dt dt
Dr Ali Jawarneh 40
1 1
x i +1 = x i + (k 1 + 2k 2 + 2k 3 + k 4 )h v i +1 = v i + (l 1 + 2l 2 + 2l 3 + l 4 )h
6 6
k 1 = f1 (0,1,0) = 0 k 1 = f1 ( x i , y i , z i )
l 1 = f 2 (0,1,0) = −1 1 1 1
k 2 = f1 ( x i + h, y i + k 1h, z i + l 1h )
k 2 = f1 (0.5,1,−0.5) = −0.5 2 2 2
l 2 = f 2 (0.5,1,−0.5) = 0 1 1 1
k 3 = f1 ( x i + h , y i + k 2 h , z i + l 2 h )
k 3 = f1 (0.5,0.75,0) = 0 2 2 2
l 3 = f 2 (0.5,0.75,0) = −0.75 k 4 = f1 ( x i + h , y i + k 3h , z i + l 3h )
k 4 = f1 (1,1,−0.75) = −0.75
l 4 = f 2 (1,1,−0.75) = 0.5