0% found this document useful (0 votes)
101 views

Department of Chemical Engineering

The document describes the Runge-Kutta method for solving ordinary differential equations numerically. It provides an overview of the Runge-Kutta method, its derivation and order. It then presents an example of using the Runge-Kutta method in MATLAB to solve the initial value problem y'(t) = 1 – t*y(t), y(0.5) = 2.5, and displays the code and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Department of Chemical Engineering

The document describes the Runge-Kutta method for solving ordinary differential equations numerically. It provides an overview of the Runge-Kutta method, its derivation and order. It then presents an example of using the Runge-Kutta method in MATLAB to solve the initial value problem y'(t) = 1 – t*y(t), y(0.5) = 2.5, and displays the code and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Chemical Engineering

Assignment No. 02

Humayun Afzal CHEN18111004

Muhammad Mubashar Wahab CHEN18111007

Tahseen Ahmad CHEN18111009

Asad Khurshid CHEN18111048

Submitted to

Engr. Basit Shafique

for

Numerical Methods for Engineers Lab

Khwaja Fareed University of Engineering and

Information Technology, Rahim Yar Khan

25th February, 2021


Page 1 of 8
Runge-Kutta Method in MATLAB

Runge-Kutta Method

Runge-Kutta method is a popular iteration method of approximating solution of ordinary


differential equations. Developed around 1900 by German mathematicians. Also known as RK
method, the Runge-Kutta method is based on solution procedure of initial value problem in which
the initial conditions are known. Based on the order of differential equation, there are different
Runge-Kutta methods which are commonly referred to as: RK2, RK3, and RK4 methods.
Runge-Kutta Method Derivation
Let’s consider an initial value problem given as:

Y' = f( t, y), y(t0) = y0

where,
 y is an unknown function of time t, which may be scalar or vector quantity
 y' is the rate of change of y with respect to t
 t0 is the initial value of t
 y0 is the value of y at time t0
Let ‘h’ be the step size such that h > 0. Now, the generation term of the series can be defined as:

yn+1 = yn + h/6 * ( k1 + 2k2 + 2k3 + k4)


tn+1 = tn + h
for n = 1, 2 , 3, 4, …. such that:
 k1 = f( tn, yn)
 k2 = f( tn + h/2 , yn + h/2 k1 )
 k3 = f( tn + h/2, yn + h/2k2)
 k4 = f( tn + h, yn + hk3)
 yn+1 is the Runge-Kutta method approximation of y(tn+1)
 k1 is the increment which depends on the gradient of starting interval as in Euler’s
method
 k2 is the increment which relies on the slope at the midpoint of the interval, k 2 = y+ h/2
* k1
 k3 is also an increment based on the gradient at midpoint, k3=y+h/2*k2
 k4 is again an increment whose value depends on end of the end of interval k 4 = y + hk3

Page 2 of 8
Solving an example of Runge-Kutta method in MATLAB:

Let’s analyze and solve an initial value problem using Runge-Kutta method. We have to
approximate y(1.5) using RK2 method in this problem:

y'(t) = 1 – t*y(t) ; y(0.5) = 2.5

In the following, is given the MATALB code for solving this problem:

function a = RungeKutta(df)
% asking initial conditions
df=@(t,y)1-t*y

x0 = input('Enter initial value of x : ');


y0 = input ('Enter initial value of y : ');
x1 = input( 'Enter value of x at which y is to be calculated : ');
tol = input( 'Enter desired level of accuracy in the final result : ');

%choose the order of Runge-Kutta method

r = menu ( ' Which order of Runge Kutta u want to use', ...


' 2nd order ' , ' 3rd order ' , ' 4th order ');

switch r
case 1
% calculating the value of h
n =ceil( (x1-x0)/sqrt(tol));
h = ( x1 - x0)/n;
for i = 1 : n
X(1,1) = x0; Y (1,1) = y0;
k1 = h*feval( df , X(1,i), Y(1,i));
k2 = h*feval( df , X(1,i) + h , Y(1,i) + k1);
k = 1/2* ( k1+ k2);
X( 1, i+1) = X(1,i) + h;
Y( 1 ,i+1) = Y(1,i) + k;
end

case 2
% calculating the value of h
n =ceil( (x1-x0)/nthroot(tol,3));
h = ( x1 - x0)/n;
for i = 1 : n
X(1,1) = x0; Y (1,1) = y0;
k1 = h*feval( df , X(1,i), Y(1,i));
k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1);
k3 = h*feval( df , X(1,i) + h, Y(1,i) + k2);
k = 1/6* ( k1+ 4*k2 + k3);
X( 1, i+1) = X(1,i) + h;
Y( 1 ,i+1) = Y(1,i) + k;
end

case 3

Page 3 of 8
% calculating the value of h
n =ceil( (x1-x0)/nthroot(tol,3));
h = ( x1 - x0)/n;
for i = 1 : n
X(1,1) = x0; Y (1,1) = y0;
k1 = h*feval( df , X(1,i), Y(1,i));
k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1);
k3 = h*feval( df , X(1,i) + h/2, Y(1,i) + k2);
k4 = h*feval( df , X(1,i) + h, Y(1,i) + k3);
k = 1/6* ( k1+ 2*k2 + 2*k3 + k4);
X( 1, i+1) = X(1,i) + h;
Y( 1 ,i+1) = Y(1,i) + k;
end

end

%displaying results

fprintf( 'for x = %g \n y = %g \n' , x1,Y(1,n+1))

%displaying graph

x = 1:n+1;
y = Y(1,n+1)*ones(1,n+1) - Y(1,:);
plot(x,y,'r')
xlabel = (' no of interval ');
ylabel = ( ' Error ');

Following are the screenshots of the code written and executed in MATLAB along with graph:

Page 4 of 8
Figure 1

Figure 2

Page 5 of 8
Figure 3

Figure 4

Page 6 of 8
Output:

Figure 5

Figure 6

Page 7 of 8
Graph:

Figure 7

Page 8 of 8

You might also like