write a program to find solution of ordinary differential equation using Runge-Kutta forth order method in c++.

write a program to find solution of ordinary differential equation using Runge-Kutta forth order method in c++.


CODE:

#include<iostream.h>
#include<conio.h>
float f(float x,float y)
{
return (x+y);
}
void main()
{
clrscr();
float x,y,xn,h,k,k1,k2,k3,k4;
cout<<"Enter value of x:";
cin>>x;
cout<<"Enter value of y:";
cin>>y;
cout<<"Enter value of xn:";
cin>>xn;
cout<<"Enter size of interval:";
cin>>h;
cout<<"Runge-kutta fourth order method:\n";
cout<<"x \t y"<<endl;
cout<<x<<" \t "<<y<<endl;
int i=1;
while(x<xn)
{
 k1=h*f(x,y);
 k2=h*f(x+(h/2),y+(k1/2));
 k3=h*f(x+(h/2),y+(k2/2));
 k4=h*f(x+h,y+k3);
 k=(k1+(2*k2)+(2*k3)+k4)/6;
 y=y+k;
 x=x+h;
 i++;
cout<<x<<" \t "<<y<<endl;
}
getch();
}

OUTPUT:

Comments