Thursday, August 19, 2010

C Program Of NA Using Runga-Kutta Method

/* Runge-kutta method of 2nd order */
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t m1 \t m2\t Y\n");
printf("-------------------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+h,y0+m1);
y1=y0+(.5*(m1+m2));
printf("%2d\t%5.6f\t%5.6f\t%5.6f\t %5.6f\n",c,i,m1,m2,y1);
y0=y1;
}
getch();
}
float f(float x,float y)
{
return (y-x);
}
OUTPUT
Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 2
Enter Value of X at which Y is required : .1
Enter Interval ,h : .1
No. X m1 m2 Y
--------------------------------------------------------------------
1 0.000000 0.200000 0.210000 2.205000
2 0.100000 0.210500 0.221550 2.421025

No comments:

Post a Comment