As we know, Taylor’s series is a numerical method used for solving differential equations and is limited by the work to be done in finding the derivatives of the higher order. To overcome this, we can use a new category of numerical methods called Runge-Kutta methods to solve differential equations. These will give us higher accuracy without performing more calculations. These methods coordinate with the solution of Taylor’s series up to the term in hr, where r varies from method to method, representing the order of that method. One of the most significant advantages of Runge-Kutta formulae is that it requires the function’s values at some specified points.
Before learning about the Runge-Kutta RK4 method, let’s have a look at the formulas of the first, second and third-order Runge-Kutta methods.
Consider an ordinary differential equation of the form dy/dx = f(x, y) with initial condition y(x0) = y0. For this, we can define the formulas for Runge-Kutta methods as follows.
1st Order Runge-Kutta method
y1 = y0 + hf(x0, y0) = y0 + hy’0 {since y’ = f(x, y)}
This formula is same as the Euler’s method
2nd Order Runge-Kutta method
y1 = y0 + (½) (k1 + k2)
Here,
k1 = hf(x0, y0)
k2 = hf(x0 + h, y0 + k1)
3rd Order Runge-Kutta method
y1 = y0 + (⅙) (k1 + 4k2 + k3)
Here,
k1 = hf(x0, y0)
k2 = hf[x0 + (½)h, y0 + (½)k1]
k3 = hf(x0 + h, y0 + k1) such that k1 = hf(x0 + h, y0 + k1)
Fourth Order Runge-Kutta Method
The most commonly used Runge Kutta method to find the solution of a differential equation is the RK4 method, i.e., the fourth-order Runge-Kutta method. The Runge-Kutta method provides the approximate value of y for a given point x. Only the first order ODEs can be solved using the Runge Kutta RK4 method.
Runge-Kutta (Fourth Order) Method Formula
The formula for the fourth-order Runge-Kutta method is given by:
y1 = y0 + (⅙) (k1 + 2k2 + 2k3 + k4)
Here,
k1 = hf(x0, y0)
k2 = hf[x0 + (½)h, y0 + (½)k1]
k3 = hf[x0 + (½)h, y0 + (½)k2]
k4 = hf(x0 + h, y0 + k3)
Runge-Kutta Method Solved Example
Consider an ordinary differential equation dy/dx = x2 + y2, y(1) = 1.2. Find y(1.05) using the fourth order Runge-Kutta method.
Solution:
Given,
dy/dx = x2 + y2, y(1) = 1.2
So, f(x, y) = x2 + y2
x0 = 1 and y0 = 1.2
Also, h = 0.05
Let us calculate the values of k1, k2, k3 and k4.
k1 = hf(x0, y0)
= (0.05) [x02 + y02]
= (0.05) [(1)2 + (1.2)2]
= (0.05) (1 + 1.44)
= (0.05)(2.44)
= 0.122
k2 = hf[x0 + (½)h, y0 + (½)k1]
= (0.05) [f(1 + 0.025, 1.2 + 0.061)] {since h/2 = 0.05/2 = 0.025 and k1/2 = 0.122/2 = 0.061}
= (0.05) [f(1.025, 1.261)]
= (0.05) [(1.025)2 + (1.261)2]
= (0.05) (1.051 + 1.590)
= (0.05)(2.641)
= 0.1320
k3 = hf[x0 + (½)h, y0 + (½)k2]
= (0.05) [f(1 + 0.025, 1.2 + 0.066)] {since h/2 = 0.05/2 = 0.025 and k2/2 = 0.132/2 = 0.066}
= (0.05) [f(1.025, 1.266)]
= (0.05) [(1.025)2 + (1.266)2]
= (0.05) (1.051 + 1.602)
= (0.05)(2.653)
= 0.1326
k4 = hf(x0 + h, y0 + k3)
= (0.05) [f(1 + 0.05, 1.2 + 0.1326)]
= (0.05) [f(1.05, 1.3326)]
= (0.05) [(1.05)2 + (1.3326)2]
= (0.05) (1.1025 + 1.7758)
= (0.05)(2.8783)
= 0.1439
By RK4 method, we have;
y1 = y0 + (⅙) (k1 + 2k2 + 2k3 + k4)
y1 = y(1.05) = y0 + (⅙) (k1 + 2k2 + 2k3 + k4)
By substituting the values of y0, k1, k2, k3 and k4, we get;
y(1.05) = 1.2 + (⅙) [0.122 + 2(0.1320) + 2(0.1326) + 0.1439]
= 1.2 + (⅙) (0.122 + 0.264 + 0.2652 + 0.1439)
= 1.2 + (⅙) (0.7951)
= 1.2 + 0.1325
= 1.3325
C Program
//Runge-Kutta Method
/*This code is written by Debmitra Chatterjee
& Modified by Souvik Ghosh*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//define your function here
float f(float x,float y)
{
return x*x+y*y;
}
//main function
void main ()
{
//declare variables
float x0,y0,h,k,k1,k2,k3,k4,x,y,yn,xf;
int count=1;
//inputs
printf("Please enter the value of x0: ");
scanf("%f",&x0);
printf("Please enter the value of y0: ");
scanf("%f",&y0);
printf("Please enter the value of h: ");
scanf("%f",&h);
printf("please enter the x value where we need to find y: ");
scanf("%f",&xf);
//for printing the table
printf("\niteration\tk1\t\tk2\t\tk3\t\tk4\t\tyn\n");
//Runge-Kutta Method
while(x0<xf){
k1=h*f(x0,y0);
k2=h*f((x0+h/2),(y0+k1/2));
k3=h*f((x0+h/2),(y0+k2/2));
k4=h*f((x0+h),(y0+k3));
yn=(k1+(2*k2)+(2*k3)+k4)*1/6+y0;
printf("%d\t\t%f\t%f\t%f\t%f\t%f\n",count,k1,k2,k3,k4,yn);
x0=x0+h;
y0=yn;
count++;
}
//output
printf("The value of y is: %f",yn);
}
Output Terminal

MATLAB Program
%{
Runge-Kutta Method
This code is written by Debmitra Chatterjee
& Modified by Souvik Ghosh
%}
count=1;
%inputs
prompt="Please enter the value of x0:";
x0=input(prompt);
prompt="Please enter the value of y0:";
y0=input(prompt);
prompt="Please enter the value of h:";
h=input(prompt);
prompt="please enter the value of where we need to find y:";
xf=input(prompt);
%Runge-Kutta Method
while(x0<xf)
k1=h*f(x0,y0);
k2=h*f((x0+h/2),(y0+k1/2));
k3=h*f((x0+h/2),(y0+k2/2));
k4=h*f((x0+h),(y0+k3));
yn=(k1+(2*k2)+(2*k3)+k4)*1/6+y0;
x0=x0+h;
y0=yn;
count=count+1;
end
%output
yn
%define your function here
function func= f(x,y)
func = x*x+y*y;
end
Output
