Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Runge-Kutta Method (R-K Method):

• Euler’s method is less efficient in practical problems since it requires ℎ to be


small for obtaining reasonable accuracy.

• The Runge-Kutta methods for the numerical solution of an ordinary


differential equation gives a greater accuracy and avoid the disadvantage of
Taylor’s series method which demands the higher order total derivative of
𝑦(𝑥).

• The advantage of this method is that the actual computational formulae


require only the functional values at some selected points.

• The method is well suited for commuters because it needs no special starting
procedure, makes light demand on storage, and repeatedly uses the same
straightforward computational procedure.
We will mainly use 2nd and 4th order R-K method.

Second order R-K Method:


𝑑𝑦
Let’s consider = 𝑓(𝑥, 𝑦) with 𝑦(𝑥0 ) = 𝑦0 .
𝑑𝑥

Then the second order R-K method formula is


1
𝑦𝑛 = 𝑦𝑛−1 + (𝐾1 + 𝐾2 ), 𝑛 ≥ 1
2

where 𝐾1 = ℎ 𝑓(𝑥𝑛−1 , 𝑦𝑛−1 )


𝐾2 = ℎ 𝑓(𝑥𝑛−1 + ℎ, 𝑦𝑛−1 + 𝐾1 )
𝒅𝒚
Example: Using R-K method of second order find the solution of = 𝒚 − 𝒙,
𝒅𝒙
where 𝒚(𝟎) = 𝟐 at 𝒙 = 𝟎. 𝟒 with step size 𝒉 = 𝟎. 𝟏 correct up to 3-decimal
places.
𝑑𝑦
Solution: We are given that = 𝑦 − 𝑥, where 𝑦(0) = 2.
𝑑𝑥

Here 𝑓(𝑥, 𝑦) = 𝑦 − 𝑥, 𝑥0 = 0, 𝑦0 = 2 and ℎ = 0.1.


Then
𝑥1 = 𝑥0 + ℎ = 0 + 0.1 = 0.1
𝑥2 = 𝑥0 + 2ℎ = 0 + 2(0.1) = 0.2
𝑥3 = 𝑥0 + 3ℎ = 0 + 3(0.1) = 0.3
𝑥4 = 𝑥0 + 2ℎ = 0 + 2(0.1) = 0.4
Now, by R-K method of second order, we have
1
𝑦𝑛 = 𝑦𝑛−1 + (𝐾1 + 𝐾2 ), 𝑛 ≥ 1
2

where 𝐾1 = ℎ 𝑓(𝑥𝑛−1 , 𝑦𝑛−1 )


𝐾2 = ℎ 𝑓(𝑥𝑛−1 + ℎ, 𝑦𝑛−1 + 𝐾1 )

For 𝑛 = 1,
1
𝑦1 = 𝑦(𝑥1 ) = 𝑦0 + (𝐾1 + 𝐾2 )
2
Here 𝐾1 = 0.1 𝑓(𝑥0 , 𝑦0 ) = 0.1 𝑓(0,2)
= 0.1 (2 − 0) = 0.2
And 𝐾2 = 0.1 𝑓(𝑥0 + ℎ, 𝑦0 + 𝐾1 ) = 0.1 𝑓(0.1, 2 + 0.2)
= 0.1 𝑓(0.1,2.2)
= 0.1 (2.2 − 0.1)
= 0.21
Therefore,
1
𝑦1 = 𝑦(0.1) = 2 + (0.2 + 0.21)
2
∴ 𝑦1 = 2.205
For 𝑛 = 2,
1
𝑦2 = 𝑦(𝑥2 ) = 𝑦1 + (𝐾1 + 𝐾2 )
2
Here 𝐾1 = 0.1 𝑓(𝑥1 , 𝑦1 ) = 0.1 𝑓(0.1,2.205)
= 0.1 (2.205 − 0.1) = 0.2105
And 𝐾2 = 0.1 𝑓(𝑥1 + ℎ, 𝑦1 + 𝐾2 ) = 0.1 𝑓(0.2, 2.205 + 0.2105)
= 0.1 𝑓(0.2,2.4155)
= 0.1 (2.4155 − 0.2)
= 0.2216
Therefore,
1
𝑦2 = 𝑦(0.2) = 2.205 + (0.2105 + 0.2216)
2
∴ 𝑦2 = 2.4211

For 𝑛 = 3,
1
𝑦3 = 𝑦(𝑥3 ) = 𝑦2 + (𝐾1 + 𝐾2 )
2
Here 𝐾1 = 0.1 𝑓(𝑥2 , 𝑦2 ) = 0.1 𝑓(0.2,2.4211)
= 0.1 (2.4211 − 0.2) = 0.2221
And 𝐾2 = 0.1 𝑓(𝑥2 + ℎ, 𝑦2 + 𝐾1 ) = 0.1 𝑓(0.3, 2.4211 + 0.2221)
= 0.1 𝑓(0.3,2.6432)
= 0.1 (2.6432 − 0.3)
= 0.2343
Therefore,
1
𝑦3 = 𝑦(0.3) = 2.4211 + (0.2221 + 0.2343)
2
∴ 𝑦3 = 2.6493
For 𝑛 = 4,
1
𝑦4 = 𝑦(𝑥4 ) = 𝑦3 + (𝐾1 + 𝐾2 )
2
Here 𝐾1 = 0.1 𝑓(𝑥3 , 𝑦3 ) = 0.1 𝑓(0.3,2.6493)
= 0.1 (2.6493 − 0.3) = 0.2349
And 𝐾2 = 0.1 𝑓(𝑥3 + ℎ, 𝑦3 + 𝐾1 ) = 0.1 𝑓(0.4, 2.6493 + 0.2349)
= 0.1 𝑓(0.4,2.8842)
= 0.1 (2.8842 − 0.4)
= 0.2484
Therefore,
1
𝑦4 = 𝑦(0.4) = 2.6493 + (0.2349 + 0.2484)
2
∴ 𝑦3 = 2.8909
Hence the required approximation correct up to 3-decimal places is 2.891.

Example: Use second order Runge -Kutta method to solve the initial value
problem 𝒚′ = 𝟑𝒙 + 𝒚𝟐 with 𝒚(𝟏) = 𝟏.2 and find the value of 𝒚(𝟏. 𝟏) with 𝒉 =
𝟎. 𝟏.
Solution:
𝑑𝑦
We are given that = 3𝑥 + 𝑦 2 , where 𝑦(1) = 1.2.
𝑑𝑥

Here 𝑓(𝑥, 𝑦) = 3𝑥 + 𝑦2 , 𝑥0 = 1, 𝑦0 = 1.2 and ℎ = 0.1.


Then
𝑥1 = 𝑥0 + ℎ = 1 + 0.1 = 1.1
Now, by R-K method of second order, we have
1
𝑦𝑛 = 𝑦𝑛−1 + (𝐾1 + 𝐾2 ), 𝑛 ≥ 1
2

Where 𝐾1 = ℎ 𝑓(𝑥𝑛−1 , 𝑦𝑛−1 )


𝐾2 = ℎ 𝑓(𝑥𝑛−1 + ℎ, 𝑦𝑛−1 + 𝐾1 )

For 𝑛 = 1,
1
𝑦1 = 𝑦(𝑥1 ) = 𝑦0 + (𝐾1 + 𝐾2 )
2
Here 𝐾1 = 0.1 𝑓(𝑥0 , 𝑦0 ) = 0.1 𝑓(1,1.2)
= 0.1 (3(1) + 1.22 ) = 0.444
And 𝐾2 = 0.1 𝑓(𝑥0 + ℎ, 𝑦0 + 𝐾1 ) = 0.1 𝑓(1.1, 1.2 + 0.444)
= 0.1 𝑓(1.1,1.644)
= 0.1 (3(1.1) + 1.6442 )
= 0.60027

Therefore,
1
𝑦1 = 𝑦(1.1) = 1.2 + (0.444 + 0.60027)
2
∴ 𝑦1 = 1.72214
Fourth order R-K Method:
𝑑𝑦
Let’s consider = 𝑓(𝑥, 𝑦) with 𝑦(𝑥0 ) = 𝑦0 .
𝑑𝑥

Then the fourth order R-K method formula is


1
𝑦𝑛 = 𝑦𝑛−1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 ), 𝑛 ≥ 1
6

Where 𝐾1 = ℎ 𝑓(𝑥𝑛−1 , 𝑦𝑛−1 )


ℎ 𝐾1
𝐾2 = ℎ 𝑓 (𝑥𝑛−1 + , 𝑦𝑛−1 + )
2 2
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑥𝑛−1 + , 𝑦𝑛−1 + )
2 2

𝐾4 = ℎ 𝑓(𝑥𝑛−1 + ℎ, 𝑦𝑛−1 + 𝐾3 )

Example: Use RK-4 method to solve the initial value problem 𝒚′ = 𝒕 − 𝒚 with
𝒚(𝟎) = 𝟏 and find the value of 𝒚(𝟎. 𝟐) correct to five decimal places with 𝒉 =
𝟎. 𝟏.
𝑑𝑦
Solution: We are given that = 𝑡 − 𝑦 with 𝑦(0) = 1.
𝑑𝑡

Here 𝑓(𝑡, 𝑦) = 𝑡 − 𝑦, 𝑡0 = 0, 𝑦0 = 1 and ℎ = 0.1.


Then
𝑡1 = 𝑡0 + ℎ = 0 + 0.1 = 0.1
𝑡2 = 𝑡0 + 2ℎ = 0 + 2(0.1) = 0.2

Now, by R-K method of fourth order, we have


1
𝑦𝑛 = 𝑦𝑛−1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 ), 𝑛 ≥ 1
6

Where 𝐾1 = ℎ 𝑓(𝑡𝑛−1 , 𝑦𝑛−1 )


ℎ 𝐾1
𝐾2 = ℎ 𝑓 (𝑡𝑛−1 + , 𝑦𝑛−1 + )
2 2
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑡𝑛−1 + , 𝑦𝑛−1 + )
2 2
𝐾4 = ℎ 𝑓(𝑡𝑛−1 + ℎ, 𝑦𝑛−1 + 𝐾3 )
For 𝑛 = 1,
1
𝑦1 = 𝑦(𝑡1 ) = 𝑦0 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 )
6
Here
𝐾1 = 0.1 𝑓(𝑡0 , 𝑦0 )
= 0.1 𝑓(0,1)
= 0.1 (0 − 1)
= −0.1
ℎ 𝐾1
Now, 𝐾2 = ℎ 𝑓 (𝑡0 + , 𝑦0 + )
2 2
0.1 − 0.1
= 0.1 𝑓 (0 + ,1 + )
2 2

= 0.1 𝑓(0.05, 0.95)


= 0.1 (0.05 − 0.95)
= −0.09
Also,
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑡0 + , 𝑦0 + )
2 2
− 0.09
= 0.1 𝑓 (0.05, 1 + )
2
− 0.09
= 0.1 𝑓 (0.05, 1 + )
2

= 0.1 𝑓(0.05, 0.955)


= 0.1 (0.05 − 0.955)
= −0.0905
And
𝐾4 = ℎ 𝑓(𝑡0 + ℎ, 𝑦0 + 𝐾3 )
= 0.1 𝑓(0 + 0.1, 1 − 0.0905)
= 0.1 𝑓(0.1,0.9095)
= 0.1 (0.1 − 0.9095)
= −0.08095
Therefore,
1
𝑦1 = 𝑦(0.1) = 1 + (−0.1 + 2(−0.09) + 2(−0.0905) − 0.08095)
6
= 0.909675

Now, for 𝑛 = 2,
1
𝑦2 = 𝑦(𝑡2 ) = 𝑦1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 )
6
Here
𝐾1 = 0.1 𝑓(𝑡1 , 𝑦1 )
= 0.1 𝑓(0.1,0.909675)
= 0.1 (0.1 − 0.909675)
= −0.080968
ℎ 𝐾1
Now, 𝐾2 = ℎ 𝑓 (𝑡1 + , 𝑦1 + )
2 2
0.1 − 0.080968
= 0.1 𝑓 (0.1 + , 0.909675 + )
2 2

= 0.1 𝑓(0.15, 0.869191)


= 0.1 (0.15 − 0.86919125)
= −0.0719191
Also,
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑡1 + , 𝑦1 + )
2 2
− 0.0719191
= 0.1 𝑓 (0.15, 0.909675 + )
2

= 0.1 𝑓(0.15, 0.873715)


= 0.1 (0.15 − 0.873715)
= −0.072372
And
𝐾4 = ℎ 𝑓(𝑡1 + ℎ, 𝑦1 + 𝐾3 )
= 0.1 𝑓(0.1 + 0.1, 0.909675 − 0.072372)
= 0.1 𝑓(0.2,0.837303)
= 0.1 (0.2 − 0.837303)
= −0.063730
Therefore,
𝑦2 = 𝑦(0.2) = 0.909675
1
+ (−0.080968 + 2(−0.0719191) + 2(−0.072372) − 0.063730)
6
= 0.837462
Hence the required approximation corrects up to 5-decimal places is 𝑦(0.2) =
0.83746.
Example-2: Use RK-4 method to solve the initial value problem
𝒚′ = −𝟐𝒚 + 𝒙 + 𝟒 with 𝒚(𝟎) = 𝟏 and find the value of 𝒚(𝟎. 𝟐) correct to five
decimal places with 𝒉 = 𝟎. 𝟏.
Solution:
𝑑𝑦
We are given that = −2𝑦 + 𝑥 + 4 with 𝑦(0) = 1.
𝑑𝑥

Here 𝑓(𝑥, 𝑦) = −2𝑦 + 𝑥 + 4, 𝑥0 = 0, 𝑦0 = 1 and ℎ = 0.1.


Then
𝑥1 = 𝑥0 + ℎ = 0 + 0.1 = 0.1
𝑥2 = 𝑥0 + 2ℎ = 0 + 2(0.1) = 0.2

Now, by R-K method of fourth order, we have


1
𝑦𝑛 = 𝑦𝑛−1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 ), 𝑛 ≥ 1
6

Where 𝐾1 = ℎ 𝑓(𝑥𝑛−1 , 𝑦𝑛−1 )


ℎ 𝐾1
𝐾2 = ℎ 𝑓 (𝑥𝑛−1 + , 𝑦𝑛−1 + )
2 2
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑥𝑛−1 + , 𝑦𝑛−1 + )
2 2

𝐾4 = ℎ 𝑓(𝑥𝑛−1 + ℎ, 𝑦𝑛−1 + 𝐾3 )
For 𝑛 = 1,
1
𝑦1 = 𝑦(𝑥1 ) = 𝑦0 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 )
6
Here
𝐾1 = ℎ 𝑓(𝑥0 , 𝑦0 )
= 0.1 𝑓(0,1)
= 0.1 (−2(1) + 0 + 4)
= 0.2
ℎ 𝐾1
Now, 𝐾2 = ℎ 𝑓 (𝑥0 + , 𝑦0 + )
2 2
0.1 0.2
= 0.1 𝑓 (0 + ,1 + )
2 2

= 0.1 𝑓(0.05, 1.1)


= 0.1 (−2(1.1) + 0.05 + 4)
= 0.185
Also,
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑥0 + , 𝑦0 + )
2 2
0.185
= 0.1 𝑓 (0.05, 1 + )
2

= 0.1 𝑓(0.05, 1.0925)


= 0.1 (−2(1.0925) + 0.05 + 4)
= 0.1865
And
𝐾4 = ℎ 𝑓(𝑥0 + ℎ, 𝑦0 + 𝐾3 )
= 0.1 𝑓(0 + 0.1, 1 + 0.1865)
= 0.1 𝑓(0.1,1.1865)
= 0.1 (−2(1.1865) + 0.1 + 4)
= 0.1727
Therefore,
1
𝑦1 = 𝑦(0.1) = 1 + (0.2 + 2(0.185) + 2(0.1865) + 0.1727)
6
= 1.18595

Now, for 𝑛 = 2,
1
𝑦2 = 𝑦(𝑥2 ) = 𝑦1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 )
6
Here
𝐾1 = ℎ 𝑓(𝑥1 , 𝑦1 )
= 0.1 𝑓(0.1,1.18595)
= 0.1 (−2(1.18595) + 0.1 + 4)
= 0.17281
ℎ 𝐾1
Now, 𝐾2 = ℎ 𝑓 (𝑥1 + , 𝑦1 + )
2 2
0.1 0.17281
= 0.1 𝑓 (0.1 + , 1.18595 + )
2 2

= 0.1 𝑓(0.15, 1.27235)


= 0.1 (−2(1.27235) + 0.15 + 4)
= 0.16053

Also,
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑥1 + , 𝑦1 + )
2 2
0.16053
= 0.1 𝑓 (0.15, 1.18595 + )
2

= 0.1 𝑓(0.15, 1.26622)


= 0.1 (−2(1.26622) + 0.15 + 4)
= 0.16176
And
𝐾4 = ℎ 𝑓(𝑥1 + ℎ, 𝑦1 + 𝐾3 )
= 0.1 𝑓(0.1 + 0.1, 1.18595 + 0.16176)
= 0.1 𝑓(0.2,1.34771)
= 0.1 (−2(1.34771) + 0.2 + 4)
= 0.15046
Therefore,
1
𝑦2 = 𝑦(0.2) = 1.18595 + (0.17281 + 2(0.16053) + 2(0.16176) + 0.15046)
6
= 1.34726
Hence the required approximation corrects up to 5-decimal places is 𝑦(0.2) =
1.34726.
Practice Examples

1. Using R-K (4th order) method, find the solution of the differential equation
(IVP)
𝑥𝑦′ = 𝑥 − 𝑦, 𝑦(2) = 2 at 2.3 correct up to five decimal places with ℎ =
0.1.
Ans: 2.01956

2. Using R-K (4th order) method, find the solution of the differential equation
𝑦
(IVP) 𝑦 ′ = 3𝑥 + , 𝑦(0) = 1 at 0.2 correct to four decimal places with
2
ℎ = 0.1.
Ans: 1.1668

3. Find the value of y at x=0.2, using fourth order Runge-Kutta method to


y2 − 2x
given that y  = such that y ( 0 ) = 1 , h=0.1.
y2 + x
Ans: 1.168
Application:
Example:
The concentration of salt 𝒙 in a homemade soap maker is given as a function
𝒅𝒙
of time by = 𝟑𝟕. 𝟓 − 𝟑. 𝟓𝒙. At the initial time, 𝒕 = 𝟎, the salt concentration
𝒅𝒕
in the tank is 50 g/L. Using Runge-Kutta 4th order method and a step size of
𝒉 = 𝟏. 𝟓 min, what is the salt concentration after 3 minutes?
Solution:
𝒅𝒙
We are given that by = 𝟑𝟕. 𝟓 − 𝟑. 𝟓𝒙 with 𝒙(𝟎) = 𝟓𝟎.
𝒅𝒕
Here 𝑓(𝑥, 𝑡) = 37.5 − 3.5𝑥, 𝑡0 = 0, 𝑥0 = 50 and ℎ = 1.5.
Then
𝑡1 = 𝑡0 + ℎ = 0 + 1.5 = 1.5
𝑡2 = 𝑡0 + 2ℎ = 0 + 2(1.5) = 3.

Now, by R-K method of fourth order, we have


1
𝑥𝑛 = 𝑦𝑛−1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 ), 𝑛 ≥ 1
6

Where 𝐾1 = ℎ 𝑓(𝑡𝑛−1 , 𝑥𝑛−1 )


ℎ 𝐾1
𝐾2 = ℎ 𝑓 (𝑡𝑛−1 + , 𝑥𝑛−1 + )
2 2
ℎ 𝐾2
𝐾3 = ℎ 𝑓 (𝑡𝑛−1 + , 𝑥𝑛−1 + )
2 2

𝐾4 = ℎ 𝑓(𝑡𝑛−1 + ℎ, 𝑥𝑛−1 + 𝐾3 )

For 𝑛 = 1,
1
𝑥1 = 𝑥(𝑡1 ) = 𝑥0 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 ) = 681.24
6

For 𝑛 = 2,
1
𝑥2 = 𝑥(𝑡2 ) = 𝑥1 + (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4 ) = 11455
6
The exact solution of the ordinary differential equation is given by
x(t ) = 10.714 + 39.286e −3.5t

The solution to this nonlinear equation at t = 3 min is


x(3) = 10.715 g/L

Figure 1 compares the exact solution with the numerical solution using Runge-
Kutta 4th order method using different step sizes.

Figure 1 Comparison of Runge-Kutta 4th order method with exact


solution for different step sizes.

Table 1 and Figure 2 show the effect of step size on the value of the calculated
temperature at t = 3 min.
Table 1 Value of concentration of salt at 3 minutes for different step sizes.

Step size, h x(3) Et |t | %

3 14120 14109 131680


1.5 11455 11444 106800
0.75 25.559 14.843 138.53
0.375 10.717 0.0014969 0.013969
0.1875 10.715 0.00031657 0.0029544

Figure 2 Effect of step size in Runge-Kutta 4th order method.


In Figure 3, we are comparing the exact results with Euler’s method (Runge-Kutta
1st order method), Heun’s method (Runge-Kutta 2nd order method) and Runge-
Kutta 4th order method.

Figure 3 Comparison of Runge-Kutta methods of 1st, 2nd, and 4th order.

Example: A ball at 1200 K is allowed to cool down in air at an ambient


temperature of 300 K. Assuming heat is lost only due to radiation, the
differential equation for the temperature of the ball is given by
𝒅𝜽
= −𝟐. 𝟐𝟎𝟔𝟕 × 𝟏𝟎𝟏𝟐 (𝜽𝟒 − 𝟖𝟏 × 𝟏𝟎𝟖 ), 𝜽(𝟎) = 𝟏𝟐𝟎𝟎𝒌
𝒅𝒕
Find 𝜽(𝟒𝟖𝟎) with h=240k.

You might also like