Ordinary Differential Equations   

(Runge - Kutta Method) 

                  Click Here to Download the Exercise 9                           

Objective : -             To write C++ Program to solve Ordinary Differential Equations using Runge – Kutta Method

Theory :

The Taylor’s series method of solving differential equations numerically restricted by the labour involved in finding the higher order derivatives. However these are a class of methods which do not require the calculations of higher order derivatives. These methods agree with Taylor’s series solution upto the terms in hr, where r differs from method to method and it is called the order of the method. 

Euler’s Method, Modified Euler’s Method & Runge’s Methods are the Runge-Kutta Methods of the first, second and third order respectively. The Fourth Order Runge-Kutta Method is most commonly used and is often referred as ‘Runge-Kutta Method’ only. 

Program:

#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<iomanip.h>


float fun(float x, float y)

{

return (1+pow(y,2));

}


int main()

{

clrscr();

int n,i,j;

float h;

//Entering the Number of Iterations

cout<<"Enter the number of Iterations "<<endl;

cin>>n;

//Entering the width of step size

cout<<"Enter The Value of Step Size "<<endl;

cin>>h;

long double y[25],x[25],k[25][25];

//Entering the initial values of x & y

cout<<"Enter the value of x0 "<<endl;

cin>>x[0];

cout<<"Enter The Value of y0 "<<endl;

cin>>y[0];


//Calculating the value of x

for(i=1;i<=n;i++)

 {

  x[i]=x[i-1]+h;

 }

cout<<"Solution of the given differential equation by Fourth Order Runga Kutta Method is "<<endl;

//Calculating & Printing the values of k1, k2, k3, k4 & y

for(j=1;j<=n;j++)

 {

  k[1][j]=h*fun(x[j-1],y[j-1]);

  cout<<"K[1] = "<<k[1][j]<<"\t";

  k[2][j]=h*fun(x[j-1]+(h/2),y[j-1]+(k[1][j]/2));

  cout<<"K[2] = "<<k[2][j]<<"\t";

  k[3][j]=h*fun(x[j-1]+(h/2),y[j-1]+(k[2][j]/2));

  cout<<"K[3] = "<<k[3][j]<<"\t";

  k[4][j]=h*fun(x[j-1]+h,y[j-1]+k[3][j]);

  cout<<"K[4] = "<<k[4][j]<<"\n";

  y[j]=y[j-1]+((k[1][j]+2*k[2][j]+2*k[3][j]+k[4][j])/6);

  cout<<"y["<<h*j<<"] = "<<setprecision(5)<<y[j]<<endl;

 }

getch();

}


Output: