RUNGE KUTTA METHOD 2ND ORDER

Solve The Given Differential  Equation By Using 2nd Order Runge Kutta Method 

Source code :-

//Solve by Runge Kutta 2nd order method dy/dx=(1+x^2)y where y(0)=1 and x=0(0.2)0.6        

#include<stdio.h>
#include<math.h>
float f(float x,float y)
{
return ((1+pow(x,2))*y);  // Replace With given Equation
}
int main()
{
float x,y,p,q,xf,h,d1,d2;
int i=0;
printf("Enter the initial value of x : ");
scanf("%f",&x);
printf("\nEnter the initial value of y : ");
scanf("%f",&y);
printf("\nEnter the final value of x : ");
scanf("%f",&xf);
printf("\nEnter the value of step length (h) : ");
scanf("%f",&h);
printf("\n\n Ite\t   Xi\t\t   Yi\t\t   D1\t\t   D2\t\t  Yi+1\n");
printf("----------------------------------------");
printf("----------------------------------------");
while(x<xf)
{
d1=h*f(x,y);
d2=h*f(x+h/2,y+d1/2);
p=x;
q=y;
x=x+h;
y=y+d2;
printf("\n%3d\t%f\t%f\t%f\t%f\t%f",i++,p,q,d1,d2,y);
}
printf("\n\n\n\t\t\t   The result of Y(%.3f) = %f\n",x,y);
return 1;
}

Input-Output :-

abhi@hp-15q-laptop:~$ gcc rk2.c
abhi@hp-15q-laptop:~$ ./a.out

Enter the initial value of x : 0

Enter the initial value of y : 1

Enter the final value of x : 0.6

Enter the value of step length (h) : 0.2


 Ite       Xi              Yi              D1              D2             Yi+1
--------------------------------------------------------------------------------
  0     0.000000        1.000000        0.200000        0.222200        1.222200        
  1     0.200000        1.222200        0.254218        0.294149        1.516349
  2     0.400000        1.516349        0.351793        0.423061        1.939411


                           The result of Y(0.600) = 1.939411