THE RUNGE KUTTA-4 ALGORITHM

In the present world and technology, differential equations are used almost everywhere. Ordinary differential are most commonly used for population growth and over-population, over-use of natural resources leading to extinction of animal populations and the depletion of natural resources, genocide, and the spread of diseases.

Finding solutions for these differential equations has been a difficult and interesting task. Around 1900, two mathematicians named C Runge and M W Kutta developed different techniques to find out the approximate solutions of ordinary differential equations. These Techniques are thus named after them as Runge-Kutta methods. Most commonly used method for finding solutions of an ordinary differential equation is RK4 (Runge-Kutta 4th order).

Real world Applications :

Runge-kutta methods are used widely in many researches mainly in fluid dynamics and mechanics for better solutions of the fluids. Other real life application of Runge-Kutta method is simulation and games. In all present games, we find the motion of object relatively and vary the position of different objects according to it. If there is only one force acting on the body, or only one acceleration such as gravity then we can apply simply laws of motion. But in reality, there are lot of constraints that change with time or velocity or any another physical quantity. In this type of situations we need to numerically integrate the equations we have to get the approximation of the moving object.

Therory :

RK4 is a iterative method to find out the approximate solution of ODE(Ordinary Differential Equation). Starting with an initial given condition we calculate forward step by step using RK4 algorithm. The differential equation of the form

and with an initial condition

are given. We need to find solution for some .

Here is the number of iterations. Now we define the step size .

With the above values, the RK4 method is as follows :

Here is a unknown function of x for which we have to find approximate solution. We are told that , the rate at which changes, is a function of and itself. The initial approximation of the value is . The function and are given initially.

Now, for we have

Where

fornumber of iterations

Here is nothing but the RK4 approximation of

We can see that is calculated by adding to the weighted average of the four increments.

We can use an example for a small demonstration of how this method actually works.

Suppose and Find .

Let us take the step-size as 0.5, so that it takes few steps for the result.

Here ,

For (1st iteration)

Now we have

Now 2nd iteration)

 

The above example of 1st degree differential equation itself takes a lot of time to find out the four increments and then the weighted averaged and then the required result. What if we need to calculate the result after 10 iterations ?or 20 ? or even 100 ? Or what if instead of 1st degree equation, a 2nd or 3rd degree equation ? It takes days to find the approximate result just by hand. In this case we need to develop an algorithm for RK4 method and code it, so that it can save a lot of time.

In my case, I got to find the solution of a three degree differential equation

With initial conditions This kind of problem takes a lot of time to do manually. The solution can be found but the main drawback is time. So I wrote an algorithm for this problem to solve in RK4 method to save time and reduce errors.

Algorithm :

  • For any given differential equation, find out the function of the highest derivative.

For my problem it is

As 3 is the highest degree in the given equation.

  • As the degree is 3, define 3 different functions as following
  • Suppose we need to find the solution of y at 100th iteration with a step-size 0.1, we do the RK4 method to find .
  • Apply RK4 method for all 3 functions separately,
  • After every iteration print the data with respective value of or save all the data in a text file.
  • Plot graph of the solution required with the data we have.

 

C++ CODE :

#include<iostream>

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<fstream>

using namespace std;

double fun1(double t, double u1, double v1, double w1)

{

return (v1);

}

double fun2(double t, double u1, double v1, double w1)

{

return (w1);

}

double fun3(double t, double u1, double v1,double w1)

{

int m=2, l=-1;

return (((-1-m)+(v1*v1)-(2*u1*w1)+(l*w1*w1)+(m*v1))/(1-(2*l*v1)));

}

int main()

{

ofstreammyfile;

myfile.open(“rk4.txt”);

double h=0.1,t=0,k[4][3],u[3],t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;

u[0]=0;

u[2]=0.367032;

u[1]= 2*u[2];

inti,j;

cout<<“R.K. 4th order method to solve a 2nd order diff eqn”<<endl<<endl;

cout<<“t \t\tu1 \t\tu2 \t\t”<<endl<<endl;

for(i=0;i<81;i++)

{

myfile<<t<<“\t\t”<<u[0]<<“\t\t”<<u[1]<<“\t\t”<<u[2]<<endl;

k[0][0]=h*fun1(t,u[0],u[1],u[2]);

k[0][1]=h*fun2(t,u[0],u[1],u[2]);

k[0][2]=h*fun3(t,u[0],u[1],u[2]);

t1=t+(h/2);

t2=u[0]+((k[0][0])/2);

t3=u[1]+((k[0][1])/2);

t4=u[2]+((k[0][2])/2);

k[1][0]=h*fun1(t1,t2,t3,t4);

k[1][1]=h*fun2(t1,t2,t3,t4);

k[1][2]=h*fun3(t1,t2,t3,t4);

t5=u[0]+((k[1][0])/2);

t6=u[1]+((k[1][1])/2);

t7=u[2]+((k[1][2])/2);

k[2][0]=h*fun1(t1,t5,t6,t7);

k[2][1]=h*fun2(t1,t5,t6,t7);

k[2][2]=h*fun3(t1,t5,t6,t7);

t8=t+h;

t9=u[0]+(k[2][0]);

t10=u[1]+(k[2][1]);

t11=u[2]+(k[2][2]);

k[3][0]=h*fun1(t8,t9,t10,t11);

k[3][1]=h*fun2(t8,t9,t10,t11);

k[3][2]=h*fun3(t8,t9,t10,t11);

for(j=0;j<=3;j++)

{

u[j]=u[j]+((k[0][j]+(2*(k[1][j]))+(2*(k[2][j]))+k[3][j])/6);

}

t=t+h;

}

myfile.close();

getch();

return 0;
}

 

With the help of this code we can save data on a text file named as RK4.txt automatically in the same folder where the program is being compiled. From that data we can plot graphs if necessary.

 

  • Ravi TejaVarma
THE RUNGE KUTTA-4 ALGORITHM

Leave a Reply

Your email address will not be published. Required fields are marked *