You are on page 1of 4

KATHMANDU UNIVERSITY

SCHOOL OF ENGINEERING
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING

RUNGE-KUTTA SECOND ORDER METHOD

Submitted By:
Prabhiv Adhikary
Roll number - 22004
Group - EE Communication
Year 2, Semester 2

Submitted To:
Dr. Saraswati Acharya
Department of Mathematics
KU

Date of submission : May 26, 2023


1. INTRODUCTION TO RK-2 METHOD
Runge-Kutta method is an effective technique to solve the initial value problems of
differential equations. The main formula for RK-2 method is given by :

yn+1 = yn + ½ * [k1 + k2]

where,
k1 = h*f(xn,yn)
k2 = h*f(xn +h, yn +k1)

Derivation:

Consider a differential equation : y’ = f(x,y) ; y(x0) = y0 ; y(xn) = ?


We know the general formula for modified Euler’s method after nth step is given by,

yn+1* = yn + h*f(xn,yn)........................................................(1)
yn+1 = yn + h/2 [f(xn,yn) + f(xn+1,yn+1*)]...............................(2)

Substituting the value of yn+1* (equation 1) into equation 2, we get,

yn+1 = yn +h/2 [ f(xn,yn) + f(xn+1, yn + h*f(xn,yn))]

multiplying by h inside the brackets,

yn+1 = yn +½ [ h*f(xn,yn) + h*f(xn+1, yn + h*f(xn,yn))]

If we now set k1 = h*f(xn,yn) and xn+1 = xn+h (since h is the equidistant gap between
the x-coordinates), we have,

yn+1 = yn +½ [k1 + h*f( xn+h, yn+k1)]

again set k2 = h*f(xn+h,yn+k1). Then;

yn+1 = yn + ½ * [k1 + k2]

which is the required derivation of the RK-2 method formula.

1
2. MATLAB Code

Explanation:

● In line 2, a function named RK2 is created which takes the function itself (f), the
initial point coordinates (x1,y1), the last x coordinate(xn) and the interval gap(h) as
its input arguments. By doing this, the input values are easily passed and the
output values are returned. In line 3, the input arguments are stored in the local
variables x and y.
● Line 7 shows the value that the variable x takes from x1 to xn, with intervals of h.
Similarly, line 8 shows a variable ‘n’ which counts the number of values ‘x’ takes.
It is basically the number of data points.
● A ‘for’ loop starts at line 9, where the variable ‘i’ runs from 1 to the number of
data points (n) -1. This is done because if the loop runs from 1 to n, then an extra
value of y is printed which lies outside the interval domain (not needed). Hence,
the loop runs from 1 to n-1.
● As explained in the derivation earlier, the value of k1 and k2 is also calculated at
lines 10 and 11 respectively, which updates everytime after the loop repeats. The y
coordinate value for the next interval is calculated by the formula at line 12.
● Line 13 uses the display function. It helps the user to see which y coordinate value
is being printed. The result is finally printed in line 14 and the code ends.

2
3. OUTPUT
The output obtained by the execution of this code is shown below :

Explanation:

● Here, the function ‘f’ is declared as an inline function since inline functions
provide a convenient way to define short and simple functions like in the example
above.
● Then, the function RK2 is called and the values are passed. For this example, the
values taken are : x1 = 0, y1 = 1, xn = 1, h = 0.25
● By following the ‘for’ loop, the required values of y2,y3,y4 and y5 are then
calculated and displayed, y5 being the last point coordinate. The solution is then
complete by following the RK2 method.

You might also like