RUNGE-KUTTA 4th ORDER METHOD.

//program to implement runge kutta method of 4th order
#include<stdio.h>
#include<math.h>
double F (double x,double y)
{
  return x+y*y;//change this function to given question
}

int main ()
{
double x0,y0,y1,n,h,f,k1,k2,k3,k4;

printf("Enter x0,y0,h,xn values \n");
scanf("%lf%lf%lf%lf",&x0,&y0,&h,&n);

for(;x0<n;x0+=h)
{
f=F(x0,y0);
k1=h*f;
f=F(x0+h/2,y0+k1/2);
k2=h*f;
f=F(x0+h/2,y0+k2/2);
k3=h*f;
f=F(x0+h,y0+k3);
k4=h*f;
y1=y0+(k1+k2*2+2*k3+k4)/6;

printf("\nk1=%.5lf\nk2=%.5lf\nk3=%.5lf\nk4=%.5lf\n",k1,k2,k3,k4);
printf("y(%.5lf) = %.3lf",x0+h,y1);

y0=y1;
}
}

Comments