#include
#include
double fxy(double xi,double yi) /*定义函数fxy*/
{
double y;
y=yi-2*xi/yi;
return(y);
}
void main()
{
double x0,y0,h,xi,yi,yi_1,xk2,yk2,xk3,yk3,xk4,yk4,k1,k2,k3,k4;
int i;
x0=0; /*赋初始值*/ y0=1;
h=0.1;
xi=x0;
yi=y0;
for(i=1;i
{
k1=h*fxy(xi,yi); /*求解k1值*/
xk2=xi+0.5*h; /*求解k2的值*/ yk2=yi+0.5*k1;
k2=h*fxy(xk2,yk2);
xk3=xi+0.5*h; /*求解k3的值*/ yk3=yi+0.5*k2; k3=h*fxy(xk3,yk3); xk4=xi+h; /*求解k4的值*/ yk4=yi+k3; k4=h*fxy(xk4,yk4);
yi_1=yi+(k1+2*k2+2*k3+k4)/6; /*求解yi+1的值*/ yi=yi_1;
if(i==1)
{
printf("输出函数yi 的近似值:\n"); /*输出所有的yi 值*/ printf("y0 = %.10f ",y0);
}
printf("y%d = %.10f ",i,yi);
if((i+1)%3==0) /*每行显示三个数值*/ printf("\n");
}
printf("\n");
}
#include
#include
double fxy(double xi,double yi) /*定义函数fxy*/
{
double y;
y=yi-2*xi/yi;
return(y);
}
void main()
{
double x0,y0,h,xi,yi,yi_1,xk2,yk2,xk3,yk3,xk4,yk4,k1,k2,k3,k4;
int i;
x0=0; /*赋初始值*/ y0=1;
h=0.1;
xi=x0;
yi=y0;
for(i=1;i
{
k1=h*fxy(xi,yi); /*求解k1值*/
xk2=xi+0.5*h; /*求解k2的值*/ yk2=yi+0.5*k1;
k2=h*fxy(xk2,yk2);
xk3=xi+0.5*h; /*求解k3的值*/ yk3=yi+0.5*k2; k3=h*fxy(xk3,yk3); xk4=xi+h; /*求解k4的值*/ yk4=yi+k3; k4=h*fxy(xk4,yk4);
yi_1=yi+(k1+2*k2+2*k3+k4)/6; /*求解yi+1的值*/ yi=yi_1;
if(i==1)
{
printf("输出函数yi 的近似值:\n"); /*输出所有的yi 值*/ printf("y0 = %.10f ",y0);
}
printf("y%d = %.10f ",i,yi);
if((i+1)%3==0) /*每行显示三个数值*/ printf("\n");
}
printf("\n");
}