Wednesday, 9 April 2014

Runge-Kutta 4th order method

#include<stdio.h>
#include<conio.h>
#include<math.h>

float f(float x,float y)
{
    return((1+0.3*x*x*x+0.4*y*y*y)/(1+0.3*x*x+0.4*y*y));
}

void main()
{
    float x0,y0,h,x,y1,k1,k2,k3,k4;
    clrscr();
    printf("\nEnter the initial value of x:");
    scanf("%f",&x0);
    printf("\nEnter the initial value of y:");
    scanf("%f",&y0);
    printf("\nEnter the value to be determined:");
    scanf("%f",&x);
    printf("\nEnter the step length:");
    scanf("%f",&h);
    while(x0<x)
    {
      k1=h*f(x0,y0);
      k2=h*f(x0+h/2,y0+k1/2);
      k3=h*f(x0+h/2,y0+k2/2);
      k4=h*f(x0+h,y0+k3);
      x0+=h;
      y1=y0+(k1+2*k2+2*k3+k4)/6;
      printf("\n\t\t y(%0.1f)=%0.5f",x0,y1);
     y0=y1;
   }
    printf("\n\n\n  Hence the required value of y(%0.1f)=%0.5f(correct to 5D)",x0,y1);
   getch();
}

No comments:

Post a Comment