Second Order Runge-Kutta Method
Second Order Runge-Kutta Method
method
1.) : Given y = y^2 − x^2, where y(0) = 2. Find y(0.1) and y(0.2) by
second-order Runge-Kutta method.
Solution. Here h = 0.1, x0 = 0, y0 = 2, f(x, y) = y2 − x2.
Then
K1 = hf(x0, y0)=0.1(22 − 02)
=0.4000.
k2 = hf(x0 + h, y0 + k1)=0.1 × f(0 + 0.1, 2+0.4000)
= 0.1 × (2.42 − 0.12)=0.5750.
Therefore, y1 = y0+1/2 (k1+k2)
= 2+1/2 (0.4000+0.5750)
= 2.4875, i.e., y(0.1) = 2.4875.
To determine y2 = y(0.2),
let x1 = 0.1 and y1 = 2.4875.
k1 = hf(x1, y1)
=0.1 × f(0.1, 2.4875)
= 0.1 × (2.48752 − 0.12)
= 0.6178.
k2 = hf(x1 + h, y1 + k1)
=0.1 × f(0.2, 2.4875 + 0.6178)
= 0.1 × f(0.2, 3.1053)
= 0.1 × (3.10532 − 0.22)
=0.9603.
Therefore,
y2 = y1 + 1 2 (k1 + k2)
=2.4875 + 1 2 (0.6178 + 0.9603)
= 3.2766. Hence, y(0.2) = 3.2766
Algorithm RK2
Input function f(x, y);
Read x0, xn, y0, h; //initial and final value of x, initial value of y and step size.//
Set y = y0;
for x = x0 to xn step h do
Compute k1 = h ∗ f(x, y);
Compute k2 = h ∗ f(x + h/2, y + k1/2);
Compute y = y0 +1/2[ k1 + k2] ;
Print x, y;
function dy=f(x,y) //RK2 METHOD
dy=y^2-x^2
endfunction Scilab Program And Output
xo=input("Enter initial value of xo: ")
yo=input("Enter the value of yo: ")
h=input("Enter value of h: ")
x(1) =xo
y(1)=yo
xn=input("Enter Final value of xn: ")
n=(xn-xo)/h //iterations
for i=1:n
k1=h*f(x(i),y(i))
k2=h*f(x(i)+h,y(i)+k1)
y(i+1)=y(i)+(1/2)*(k1+k2)
x(i+1)=x(i)+h
end
disp([x y])
plot2d(x,y)
title("Graph for RK2 Method")
xlabel("values of x")
GRAPH SCREEN
Four order Runge-Kutta Method
2.)Given y = x^2 + y^2 with x = 0, y = 1. Find y(0.1) by fourth-order
Runge-Kutta method.
Solution. Here h = 0.1, x0 = 0, y0 = 1, f(x, y) = x^2 + y^2.
k1 = hf(x0, y0)
=0.1 × (02 + 12)
=0.1000.
k2 = hf(x0 + h/2, y0 + k1/2)
= 0.1 × f(0.05, 1.05)
= 0.1 × (0.052 + 1.052)
=0.1105.
k3 = hf(x0 + h/2, y0 + k2/2)
= 0.1 × f(0.05, 1.0553)
= 0.1 × (0.052 + 1.05532)
=0.1116.
k4 = hf(x0 + h, y0 + k3)
=0.1 × f(0.1, 1.1116)
= 0.1 × (0.12 + 1.11162)
=0.1246.
Therefore,
y1 = y0 +1/6(k1 + 2k2 + 2k3 + k4)
=1+1/6(0.1000 + 2 × 0.1105 + 2 × 0.1116 + 0.1246)
= 1.0932.
Algorithm RK4
Input function f(x, y);
Read x0, xn, y0, h; //initial and final value of x, initial value of y and step size.//
Set y = y0;
for x = x0 to xn step h do
Compute k1 = h ∗ f(x, y);
Compute k2 = h ∗ f(x + h/2, y + k1/2);
Compute k3 = h ∗ f(x + h/2, y + k2/2);
Compute k4 = h ∗ f(x + h, y + k3);
Compute y = y + [k1 + 2(k2 + k3) + k4]/6;
Print x, y;
endfor;
end RK4
clc;
function dy=f(x,y) //RK4 METHOD
dy=x^2+y^2
Scilab Program And
endfunction OUTPUT
x0=input("Enter the initial value of x“)
y0=input("Enter the initial value of y")
xn=input("Enter the final value of x")
h=0.1 //input("Enter step size")
n=(xn-x0)/h //iterations
x(1)=x0
y(1)=y0
for i=1:n
k1=h*f(x(i),y(i))
k2=h*f(x(i)+(h/2),y(i)+(k1/2))
k3=h*f(x(i)+(h/2),y(i)+(k2/2))
k4=h*f(x(i)+h,y(i)+k3)
k=(1/6)*(k1+2*k2+2*k3+k4)
y(i+1)=y(i)+k
x(i+1)=x(i)+1
end
disp([x y])
plot2d(x,y)
title("Graph for RK4 Method")
xlabel("values of x")
GRAPH
SCREEN
ADVANTAGE: The Runge-Kutta
methods give more accurate result.
One advantage of this method is it
requires only the value of the
function at some selected points on
the subinterval and it is stable, and
easy to program.