Runge-Kutta Method
Program for Runge-Kutta 4th Order Method
Prepared By – Prof.Prashant Goad
Prof.Prashant Goad
Application
• Runge kutta RK4 is 4th order differential integration method is a
complex integration
• It uses in physics.
• It calculate the estimation in 4 steps
Example : -
• A rocket that is flying through the Earth’s atmosphere. First of all we
have the ODE to calculate the acceleration:
• Acceleration = (rocket force + force drag) / mass
• We know that acceleration is the derivative of velocity (as mentioned
earlier) so using RK4 to calculate this should be relatively straight
forward
Prof.Prashant Goad
Formulae for R.K Method
• 𝐾1 = ℎ𝑓(𝑥, 𝑦)
• 𝐾2 = ℎ𝑓(𝑥 +
1
2
ℎ , 𝑦 +
1
2
𝐾1)
• 𝐾 = 𝐾2
Second order R.K Method
Prof.Prashant Goad
Formulae for R.K Method
• 𝐾1 = ℎ𝑓(𝑥, 𝑦)
• 𝐾2 = ℎ𝑓(𝑥 +
1
2
ℎ , 𝑦 +
1
2
𝐾1)
• 𝐾3 = ℎ𝑓(𝑥 + ℎ, 𝑦 + 2𝐾2 − 𝐾1)
• 𝐾 =
1
6
(𝐾1 + 4𝐾2 + 𝐾3)
Third order R.K Method
Prof.Prashant Goad
Formulae for R.K Method
• 𝐾1 = ℎ𝑓(𝑥, 𝑦)
• 𝐾2 = ℎ𝑓(𝑥 +
1
2
ℎ , 𝑦 +
1
2
𝐾1)
• 𝐾3 = ℎ𝑓(𝑥 +
1
2
ℎ, 𝑦 +
1
2
𝐾2)
• 𝐾4 = ℎ𝑓(𝑥 + ℎ, 𝑦 + 𝐾3)
• 𝐾 =
1
6
(𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4)
Fourth order R.K Method
Prof.Prashant Goad
Apply the Fourth Order Runge Kutta method to find y(0.2)
given that y’ =x+y ,y(0)=1 for initial condition x=y=0 with h=0.2
• Solution : -
• Here y’ =x+y
• So f(x,y) = x+y
• Here Initial condition is
• 𝑥0 = 0 𝑎𝑛𝑑 𝑦0 = 1 with h = 0.2
• We have to ask to calculate y(0.2)
i.e. calculate the value of Y for x = 0.2
Apply the Fourth Order Runge Kutta method to find y(0.2) given that
y’ =x+y ,y0=1 for initial condition x0=0 with h=0.2
Prof.Prashant Goad
• By Fourth order Ranga kutta method
• 𝐾1 = ℎ𝑓(𝑥, 𝑦)
• 𝐾1 = 0.2 (𝑥0 + 𝑦0)
• 𝐾1 = 0.2 0 + 1 = 0.2
Solution
Prof.Prashant Goad
𝐾2 = ℎ𝑓(𝑥 +
1
2
ℎ , 𝑦 +
1
2
𝐾1)
𝐾2 = 0.2𝑓 𝑥0 +
1
2
ℎ , 𝑦0 +
1
2
𝐾1
𝐾2 = 0.2𝑓(0 +
1
2
0.2 , 1 +
1
2
0.2)
𝐾2 = 0.2𝑓(0.1 , 1.1)
𝐾2 = 0.2(0.1 + 1.1)
𝐾2 = 0.24
Solution
Prof.Prashant Goad
• 𝐾3 = ℎ𝑓(𝑥0 +
1
2
ℎ, 𝑦0 +
1
2
𝐾2)
• 𝐾3 = 0.2𝑓(0 +
1
2
0.2 , 1 +
1
2
0.24)
• 𝐾3 = 0.2𝑓(0.1 , 1 + 0.12)
• 𝐾3 = 0.2𝑓(0.1 , 1.12)
• 𝐾3 = 0.2 0.1 + 1.12 = 0.2(1.22)
• 𝐾3 = 0.244
Solution
Prof.Prashant Goad
• 𝐾4 = ℎ𝑓(𝑥0 + ℎ, 𝑦0 + 𝐾3)
• 𝐾4 = 0.2 𝑓(0 +
1
2
0.2 , 1 + 0.244)
• 𝐾4 = 0.2 𝑓(0.1 , 1.244)
• 𝐾4 = 0.2 (0.1 + 1.244)
• 𝐾4 = 0.2 (1.344)
• 𝐾4 = 0.2688
Solution
Prof.Prashant Goad
• 𝐾 =
1
6
(𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4)
• 𝐾 =
1
6
(0.2 + 2 × 0.24 + 2 × 0.244 + 0.2888)
• 𝐾 =
1
6
(0.2 + 0.48 + +0.488 + 0.2888)
• 𝐾 = 0.2428
Solution
Prof.Prashant Goad
• So here we will find
• 𝑥1= 𝑥0 + ℎ
• 𝑥1= 0 + 0.2 = 0.2
• Here we have to get x1 = 0.2
• So we will find y(0.2) by using
• y(0.2) = yo + K = 1+0.2448
= 1.2448 -------------------------- Answer
Solution
Prof.Prashant Goad
Prof.Prashant Goad
Here ,we have to ask to find y for x=0.2
Formulae
By using all formulae's we
calculated value of “K” with
Initial Term X0 and Yo
Prof.Prashant Goad
• By using all terms we will calculate X1 & Y1
• Here ,We need to check weather value of x now becomes 0.2 that’s
we have to ask .
• if “ X is not equal to 0.2” then we need to calculate ones again all
values of K. i.e. K1,K2,K3,K4 & K.
Formulae
Prof.Prashant Goad
• Then ones again we will check X2 & Y2 ,by using following formulae
• X2 = X1 + h
if Here X =0.2
Then Calculate the value of Y by using Y2 = Y1 + h
Formulae
Prof.Prashant Goad
# Python program to implement Runge Kutta
method :
# A sample differential equation "dy / dx = (x + y^2)"
def dydx(x, y):
return (x + y*y)
# Finds value of y for a given x using step size h
# and initial value y0 at x0.
def rungeKutta(x0, y0, x, h):
Prof.Prashant Goad
# Count number of iterations using step size or
# step height h
n = (int)((x - x0)/h)
# Iterate for number of iterations
y = y0
for i in range(1, n + 1):
"Apply Runge Kutta Formulas to find next value of y"
# Python program to implement Runge Kutta
method :
Prof.Prashant Goad
k1 = h * dydx(x0, y)
k2 = h * dydx(x0 + 0.5 * h, y + 0.5 * k1)
k3 = h * dydx(x0 + 0.5 * h, y + 0.5 * k2)
k4 = h * dydx(x0 + h, y + k3)
# Python program to implement Runge Kutta
method :
Prof.Prashant Goad
# Update next value of y
K = (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4)
y = y + k
# Update next value of x
x0 = x0 + h
# Python program to implement Runge Kutta
method :
Prof.Prashant Goad
return y
# Driver code
x0 = 0
y = 1
x = 0.2
h = 0.2
print (“The value of y at x is:”, rungeKutta(x0, y, x, h) )
• Output:
• The value of y at x is : 1.2735
# Python program to implement Runge Kutta
method :
Prof.Prashant Goad
Prof.Prashant Goad

Runge kutta method -by Prof.Prashant Goad(R.C.Patel Institute of Technology,Shirpur)

  • 1.
    Runge-Kutta Method Program forRunge-Kutta 4th Order Method Prepared By – Prof.Prashant Goad Prof.Prashant Goad
  • 2.
    Application • Runge kuttaRK4 is 4th order differential integration method is a complex integration • It uses in physics. • It calculate the estimation in 4 steps Example : - • A rocket that is flying through the Earth’s atmosphere. First of all we have the ODE to calculate the acceleration: • Acceleration = (rocket force + force drag) / mass • We know that acceleration is the derivative of velocity (as mentioned earlier) so using RK4 to calculate this should be relatively straight forward Prof.Prashant Goad
  • 3.
    Formulae for R.KMethod • 𝐾1 = ℎ𝑓(𝑥, 𝑦) • 𝐾2 = ℎ𝑓(𝑥 + 1 2 ℎ , 𝑦 + 1 2 𝐾1) • 𝐾 = 𝐾2 Second order R.K Method Prof.Prashant Goad
  • 4.
    Formulae for R.KMethod • 𝐾1 = ℎ𝑓(𝑥, 𝑦) • 𝐾2 = ℎ𝑓(𝑥 + 1 2 ℎ , 𝑦 + 1 2 𝐾1) • 𝐾3 = ℎ𝑓(𝑥 + ℎ, 𝑦 + 2𝐾2 − 𝐾1) • 𝐾 = 1 6 (𝐾1 + 4𝐾2 + 𝐾3) Third order R.K Method Prof.Prashant Goad
  • 5.
    Formulae for R.KMethod • 𝐾1 = ℎ𝑓(𝑥, 𝑦) • 𝐾2 = ℎ𝑓(𝑥 + 1 2 ℎ , 𝑦 + 1 2 𝐾1) • 𝐾3 = ℎ𝑓(𝑥 + 1 2 ℎ, 𝑦 + 1 2 𝐾2) • 𝐾4 = ℎ𝑓(𝑥 + ℎ, 𝑦 + 𝐾3) • 𝐾 = 1 6 (𝐾1 + 2𝐾2 + 2𝐾3 + 𝐾4) Fourth order R.K Method Prof.Prashant Goad
  • 6.
    Apply the FourthOrder Runge Kutta method to find y(0.2) given that y’ =x+y ,y(0)=1 for initial condition x=y=0 with h=0.2 • Solution : - • Here y’ =x+y • So f(x,y) = x+y • Here Initial condition is • 𝑥0 = 0 𝑎𝑛𝑑 𝑦0 = 1 with h = 0.2 • We have to ask to calculate y(0.2) i.e. calculate the value of Y for x = 0.2 Apply the Fourth Order Runge Kutta method to find y(0.2) given that y’ =x+y ,y0=1 for initial condition x0=0 with h=0.2 Prof.Prashant Goad
  • 7.
    • By Fourthorder Ranga kutta method • 𝐾1 = ℎ𝑓(𝑥, 𝑦) • 𝐾1 = 0.2 (𝑥0 + 𝑦0) • 𝐾1 = 0.2 0 + 1 = 0.2 Solution Prof.Prashant Goad
  • 8.
    𝐾2 = ℎ𝑓(𝑥+ 1 2 ℎ , 𝑦 + 1 2 𝐾1) 𝐾2 = 0.2𝑓 𝑥0 + 1 2 ℎ , 𝑦0 + 1 2 𝐾1 𝐾2 = 0.2𝑓(0 + 1 2 0.2 , 1 + 1 2 0.2) 𝐾2 = 0.2𝑓(0.1 , 1.1) 𝐾2 = 0.2(0.1 + 1.1) 𝐾2 = 0.24 Solution Prof.Prashant Goad
  • 9.
    • 𝐾3 =ℎ𝑓(𝑥0 + 1 2 ℎ, 𝑦0 + 1 2 𝐾2) • 𝐾3 = 0.2𝑓(0 + 1 2 0.2 , 1 + 1 2 0.24) • 𝐾3 = 0.2𝑓(0.1 , 1 + 0.12) • 𝐾3 = 0.2𝑓(0.1 , 1.12) • 𝐾3 = 0.2 0.1 + 1.12 = 0.2(1.22) • 𝐾3 = 0.244 Solution Prof.Prashant Goad
  • 10.
    • 𝐾4 =ℎ𝑓(𝑥0 + ℎ, 𝑦0 + 𝐾3) • 𝐾4 = 0.2 𝑓(0 + 1 2 0.2 , 1 + 0.244) • 𝐾4 = 0.2 𝑓(0.1 , 1.244) • 𝐾4 = 0.2 (0.1 + 1.244) • 𝐾4 = 0.2 (1.344) • 𝐾4 = 0.2688 Solution Prof.Prashant Goad
  • 11.
    • 𝐾 = 1 6 (𝐾1+ 2𝐾2 + 2𝐾3 + 𝐾4) • 𝐾 = 1 6 (0.2 + 2 × 0.24 + 2 × 0.244 + 0.2888) • 𝐾 = 1 6 (0.2 + 0.48 + +0.488 + 0.2888) • 𝐾 = 0.2428 Solution Prof.Prashant Goad
  • 12.
    • So herewe will find • 𝑥1= 𝑥0 + ℎ • 𝑥1= 0 + 0.2 = 0.2 • Here we have to get x1 = 0.2 • So we will find y(0.2) by using • y(0.2) = yo + K = 1+0.2448 = 1.2448 -------------------------- Answer Solution Prof.Prashant Goad
  • 13.
    Prof.Prashant Goad Here ,wehave to ask to find y for x=0.2
  • 14.
    Formulae By using allformulae's we calculated value of “K” with Initial Term X0 and Yo Prof.Prashant Goad
  • 15.
    • By usingall terms we will calculate X1 & Y1 • Here ,We need to check weather value of x now becomes 0.2 that’s we have to ask . • if “ X is not equal to 0.2” then we need to calculate ones again all values of K. i.e. K1,K2,K3,K4 & K. Formulae Prof.Prashant Goad
  • 16.
    • Then onesagain we will check X2 & Y2 ,by using following formulae • X2 = X1 + h if Here X =0.2 Then Calculate the value of Y by using Y2 = Y1 + h Formulae Prof.Prashant Goad
  • 17.
    # Python programto implement Runge Kutta method : # A sample differential equation "dy / dx = (x + y^2)" def dydx(x, y): return (x + y*y) # Finds value of y for a given x using step size h # and initial value y0 at x0. def rungeKutta(x0, y0, x, h): Prof.Prashant Goad
  • 18.
    # Count numberof iterations using step size or # step height h n = (int)((x - x0)/h) # Iterate for number of iterations y = y0 for i in range(1, n + 1): "Apply Runge Kutta Formulas to find next value of y" # Python program to implement Runge Kutta method : Prof.Prashant Goad
  • 19.
    k1 = h* dydx(x0, y) k2 = h * dydx(x0 + 0.5 * h, y + 0.5 * k1) k3 = h * dydx(x0 + 0.5 * h, y + 0.5 * k2) k4 = h * dydx(x0 + h, y + k3) # Python program to implement Runge Kutta method : Prof.Prashant Goad
  • 20.
    # Update nextvalue of y K = (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4) y = y + k # Update next value of x x0 = x0 + h # Python program to implement Runge Kutta method : Prof.Prashant Goad
  • 21.
    return y # Drivercode x0 = 0 y = 1 x = 0.2 h = 0.2 print (“The value of y at x is:”, rungeKutta(x0, y, x, h) ) • Output: • The value of y at x is : 1.2735 # Python program to implement Runge Kutta method : Prof.Prashant Goad
  • 22.