c program for Runga-kutta method of 4th order
#include<stdio.h>
#include<math.h>
float f(float x,float y)
{
float f;
f=((1/(x+y)));
return (f);
}
int main()
{
float x0,y0,h,xn,k,k1,k2,k3,k4;
printf("\n Enter the initial value that is x0 and y0\n");
scanf("%f%f",&x0,&y0);
printf("\n Enter the value of h\n");
scanf("%f",&h);
printf("\n Enter the value of x for which we need to find the value of y\n");
scanf("%f",&xn);
do
{
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);
k=(k1+2*k2+2*k3+k4)/6;
y0=y0+k;
x0=x0+h;
printf("\n%f %f",x0,y0);
}
while(x0<xn);
printf("\n value of y=%f",y0);
}
#include<math.h>
float f(float x,float y)
{
float f;
f=((1/(x+y)));
return (f);
}
int main()
{
float x0,y0,h,xn,k,k1,k2,k3,k4;
printf("\n Enter the initial value that is x0 and y0\n");
scanf("%f%f",&x0,&y0);
printf("\n Enter the value of h\n");
scanf("%f",&h);
printf("\n Enter the value of x for which we need to find the value of y\n");
scanf("%f",&xn);
do
{
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);
k=(k1+2*k2+2*k3+k4)/6;
y0=y0+k;
x0=x0+h;
printf("\n%f %f",x0,y0);
}
while(x0<xn);
printf("\n value of y=%f",y0);
}
output:
Enter the initial value that is x0 and y0
0
1
Enter the value of h
.1
Enter the value of x for which we need to find the value of y
.1
0.100000 1.091389
value of y=1.091389
Comments
Post a Comment