Numerical
Differentiation
Runge Kutta
Nur Laila Hamidah
Runge-Kutta 2 nd Order Method
Runge Kutta 2nd order method is given by
( a k a k ) h
y
y
i+1=
i+
1 1+
2 2where
(
xi yi)
f k1 =
,
(
x p h y q k h)
f
k2 = i + 1 , i + 11 1
For
f ( x , y ), y ( 0 ) y
0dx
dy = =
Heun’s Method
x y
xi xi+1
yi+1, predicted
yi
Figure 1 Runge-Kutta 2nd order method (Heun’s method) (x h y k h)
f
Slope= i + , i + 1
( ) ( )
f xi h yi k h f xi yi
Slope
Average , ,
2 1
1 +
+ +
=
(xi yi)
f Slope= ,
Heun’s method
2 1
1 = a
1 =1 p
11
= 1 q
resulting in
h k k
y
yi i
+
+
+1 = 1 2
2 1 2
1
where
(
xi yi)
f k1 = ,
(x h y k h)
f
k2 = i + , i + 1
Here a2=1/2 is chosen
Midpoint Method
Here a2 =1 is chosen, giving
1 =
0
a2 1
1 = p
2 1
11 = q
resulting in h k y
yi+1 = i + 2
where
(
xi yi)
f k1 = ,
+ +
= f x h y k h
k2 i i 1
2 , 1
2 1
Ralston’s Method
Here
3 2
2 =
a is chosen, giving
3 1
1 = a
4 3
1 = p
4 3
11 = q
resulting in
h k k
y
yi i
+
+
+1 = 1 2
3 2 3
1
where
(
xi yi)
f k1 = ,
+ +
= f x h y k h
k2 i i 1
4 , 3
4 3
How to write Ordinary Differential Equation
Example
( )
0 5 ,3 . 1
2 = =
+ y e− y dx
dy x
is rewritten as
( )
0 5, 2 3
.
1 − =
= e− y y dx
dy x
In this case
( )
x y e yf , =1.3 −x −2
How does one write a first order differential equation in the form of
( )
x ydx f
dy = ,
Example
A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by
( ) ( )
Kdt
d = −2.206710−12 4 −81108 , 0 =1200
Find the temperature at t = 480 seconds using Heun’s method. Assume a step size of
=
240
h seconds.
(
4 8)
12 81 10
10 2067 .
2 −
−
= −
dt d
( )
t, =−2.206710−12(
4 −81108)
f
h k
i k
i
+
+
+1 = 1 2
2 1 2
1
Solution
Step 1:
i 0,t 0, (0) 1200K0
0 = = =
=
( )
( )
( )
5579 .
4
10 81 1200
10 2067 .
2
1200 ,
0 ,
8 4
12 0
1
−
=
−
−
=
=
=
−
f t f
k o
( )
( )
( )
( )
( )
017595 .
0
10 81 09
. 106 10
2067 .
2
09 . 106 , 240
240 5579 .
4 1200
, 240 0
,
8 4
12 1 0 0
2
=
−
−
=
=
− + +
=
+ +
=
−
f f
h k h
t f
k
( ) ( )
( )
K
h k k
16 . 655
240 2702 .
2 1200
240 017595
. 2 0 5579 1 .
2 4 1200 1
2 1 2
1
2 1
0 1
=
− +
=
− +
+
=
+
+
=
Solution Cont
Step 2: i =1,t1 = t0 +h = 0+ 240= 240,1 = 655.16K
( )
( )
( )
38869 .
0
10 81 16
. 655 10
2067 .
2
16 . 655 , 240
,
8 4
12 1
1 1
−
=
−
−
=
=
=
−
f t f
k
( )
( )
( )
( )
( )
20206 .
0
10 81 87
. 561 10
2067 .
2
87 . 561 , 480
240 38869 .
0 16
. 655 , 240 240
,
8 4
12 1 1 1
2
−
=
−
−
=
=
− + +
=
+ +
=
−
f f
h k h
t f
k
( ) ( )
( )
K
h k k
27 . 584
240 29538 .
0 16
. 655
240 20206
. 2 0
38869 1 .
2 0 16 1
. 655
2 1 2
1
2 1
1 2
=
− +
=
− + −
+
=
+
+
=
Solution Cont
The exact solution of the ordinary differential equation is given by the solution of a non-linear equation as
(
0.0033333)
0.22067 10 2.9282 tan8519 .
300 1 ln 300
92593 .
0 − 1 = − 3 −
+
− − −
t
The solution to this nonlinear equation at t=480 seconds is K
57 . 647 )
480
( =
Comparison with exact results
Figure 2. Heun’s method results for different step sizes
-400 0 400 800 1200
0 100 200 300 400 500
Time, t(sec)
Temperature,θ(K)
Exact h=120
h=240 h=480
Effect of step size
Table 1. Temperature at 480 seconds as a function of step size, h
Step size, h
(480) Et |єt|%480 240 120 60 30
−393.87 584.27 651.35 649.91 648.21
1041.4 63.304
−3.7762
−2.3406
−0.63219
160.82 9.7756 0.58313 0.36145 0.097625 K
57 . 647 )
480
( =
(exact)Effects of step size on Heun’s Method
Figure 3. Effect of step size in Heun’s method
-400 -200 0 200 400 600 800
0 100 200 300 400 500
Step size, h
Temperature,θ(480)
Step size, h
(480)
Euler Heun Midpoint Ralston 480
240 120 60 30
−987.84 110.32 546.77 614.97 632.77
−393.87 584.27 651.35 649.91 648.21
1208.4 976.87 690.20 654.85 649.02
449.78 690.01 667.71 652.25 648.61
Comparison of Euler and Runge- Kutta 2 nd Order Methods
Table 2. Comparison of Euler and the Runge-Kutta methods
K 57 . 647 )
480
( =
(exact)Comparison of Euler and Runge- Kutta 2 nd Order Methods
Table 2. Comparison of Euler and the Runge-Kutta methods
Step size,
h Euler Heun Midpoint Ralston
480 240 120 60 30
252.54 82.964 15.566 5.0352 2.2864
160.82 9.7756 0.58313 0.36145 0.097625
86.612 50.851 6.5823 1.1239 0.22353
30.544 6.5537 3.1092 0.72299 0.15940
K
57 . 647 )
480
(
=
(exact)t %
Comparison of Euler and
Runge-Kutta 2 nd Order Methods
Figure 4. Comparison of Euler and Runge Kutta 2nd order methods with exact results.
500 600 700 800 900 1000 1100 1200
0 100 200 300 400 500 600
Time, t (sec)
Temperature,
Analytical
Ralston Midpoint
Euler
Heun
θ(K)
Runge-Kutta 4 th Order Method
where
(
k k k k)
hy
yi 1 i 1 2 2 2 3 4 6
1 + + +
+
+ =
( x
iy
i)
f k
1= ,
+ +
= f x h y k h
k2 i i 1
2 , 1
2 1
+ +
= f x h y k h
k3 i i 2
2 , 1
2 1
( x h y k h )
f
k
4=
i+ ,
i+
3For
f ( x , y ), y ( 0 ) y
0dx
dy = =
Runge Kutta 4th order method is given by
Example
A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by
( ) ( )
Kdt
d = −2.206710−12 4 −81108 , 0 =1200
Find the temperature at t = 480 seconds using Runge-Kutta 4th order method.
=
240
h seconds.
(
4 8)
12 81 10
10 2067 .
2 −
−
= −
dt d
( )
t, =−2.206710−12(
4 −81108)
f
Assume a step size of
(
k k k k)
hi
i 1 1 2 2 2 3 4
6
1 + + +
+
+ =
Solution
Step 1:
0, 0, (0) 12000
0 = = =
= t
i
(
0,) (
0,1200)
2.2067 10 12(
12004 81 108)
4.55791 = f t = f =− − − =−
k o
( ) ( )
(
120,653.05)
2.2067 10(
653.05 81 10)
0.38347240 5579 .
2 4 1200 1 ,
2 240 0 1
2 , 1
2 1
8 4
12 1
0 0
2
−
=
−
−
=
=
+ + −
=
+ +
= f −
f h k h
t f
k
( ) ( )
(
120,1154.0)
2.2067 10(
1154.0 81 10)
3.8954240 38347
. 2 0
1200 1 ,
2 240 0 1
2 , 1
2 1
8 4
12 2
0 0
3
−
=
−
=
=
+ + −
=
+ +
= f −
f h
k h
t f
k
( ) ( ( ) ( ) )
( 240
0, 265 ,
0. 10 )
32 . 2067 0 10 240
12( , 1200 265 . 10
43 81 . 984 10 240
8) 0 . 0069750
4
=
−
=
=
− + +
= +
+
= f −
f h
k h
t f
k
Solution Cont
1 is the approximate temperature at 240240
0 0
1 = + = + =
= t t h t
( ) 240
1 =675 . 65
K
( )
( ) ( ) ( )
( )
( )
K
h k k
k k
65 . 675
240 1848
. 6 2
1200 1
240 069750
. 0 8954
. 3 2 38347
. 0 2 5579 .
6 4 1200 1
2 6 2
1
4 3
2 1
0 1
=
− +
=
+
− +
− +
− +
=
+ +
+ +
=
Solution Cont
Step 2: i =1,t1 = 240,1 = 675.65K
(
1, 1) (
240,675.65)
2.2067 10 12(
675.654 81 108)
0.441991 = f t = f =− − − =−
k
( ) ( )
(
360,622.61)
2.2067 10(
622.61 81 10)
0.31372240 44199 .
2 0 65 1 . 675 , 2 240 240 1
2 , 1
2 1
8 4
12 1
1 1
2
−
=
−
−
=
=
+ + −
=
+ +
= f −
f h k h
t f
k
( ) ( )
(
360,638.00)
2.2067 10(
638.00 81 10)
0.34775240 31372 .
2 0 65 1 . 675 , 2 240 240 1
2 , 1
2 1
8 4
12 2
1 1
3
−
=
−
=
=
+ + −
=
+ +
= f −
f h
k h
t f
k
( ) ( ( ) ( ) )
(
4801 ,592, 1.19)
3 2.20672401024012(
592,675.19.654 81 010.347758)
0240.253514
−
=
−
=
=
− + +
= +
+
= f −
f h k h
t f
k
Solution Cont
2 is the approximate temperature at 480
240
1 240
2 = t + h = + =
t
( )
480 2 = 594.91K
( )
( ) ( ) ( )
( )
( )
K
h k k
k k
91 . 594
240 0184
. 6 2
65 1 . 675
240 25351
. 0 34775
. 0 2 31372
. 0 2 44199 .
6 0 65 1 . 675
2 6 2
1
4 3
2 1
1 2
=
− +
=
− +
− +
− +
− +
=
+ +
+ +
=
Solution Cont
The exact solution of the ordinary differential equation is given by the solution of a non-linear equation as
(
0.00333)
0.22067 10 2.9282tan 8519 .
300 1 ln 300
92593 .
0 − 1 = − 3 −
+
−
− −
t
The solution to this nonlinear equation at t=480 seconds is K
57 . 647 )
480
( =
Comparison with exact results
Figure 1. Comparison of Runge-Kutta 4th order method with exact solution
-400 0 400 800 1200 1600
0 200 400 600
Time,t(sec)
Temperature, h=120
Exact
h=240
h=480
θ(K)
Step size, h (480) E
t|є
t|%
480 240 120 60 30
−90.278 594.91 646.16 647.54 647.57
737.85 52.660 1.4122 0.033626 0.00086900
113.94 8.1319 0.21807 0.0051926 0.00013419
Effect of step size
Table 1. Temperature at 480 seconds as a function of step size, h
K 57 . 647 )
480
( =
(exact)Effects of step size on Runge- Kutta 4 th Order Method
Figure 2. Effect of step size in Runge-Kutta 4th order method
-200 0 200 400 600 800
0 100 200 300 400 500
Step size, h
Temperature,θ(480)
Comparison of Euler and Runge-Kutta Methods
Figure 3. Comparison of Runge-Kutta methods of 1st, 2nd, and 4th order.
0 200 400 600 800 1000 1200 1400
0 100 200 300 400 500
Time, t(sec)
Temperature,θ(K)
Exact 4th order
Heun Euler