C语言实现亚当姆斯方法

#include
#include
double fx(double x, double y)
{
return(y - 2 * x / y);
}
int main() {

double x1, K1, K2, K3, K4, y1;
double x0, y0, h,yx1,yx0,y2,y3,y4,x2,x3,x4,yp;
int N;
printf("请输入x0,y0,h,N:");
scanf_s("%lf%lf%lf%d", &x0, &y0, &h, &N);
yx0 = y0;
//printf("%lf%lf%lf%d", x0, y0, h, N);
for (int i = 0; i < 3; i++) {
	x1 = x0 + h;
	K1 = fx(x0, y0);
	K2 = fx(x0 + h / 2, y0 + h * K1 / 2);
	K3 = fx(x0 + h / 2, y0 + h * K2 / 2);
	K4 = fx(x1, y0 + h * K3);
	y1 = y0 + h * (K1 + 2 * K2 + 2 * K3 + K4) / 6;
	if (i ==0)
		yx1 = y1;
	if (i ==1)
		y2 = y1;
	if (i ==2)
		y3 = y1;
	printf("%lf\t%lf\n", x1, y1);
	x0 = x1;
	y0 = y1;
}
x0 = x0 - 3 * h;
x1 = x0 + h;
x2 = x0  + 2 * h;
x3 = x0  + 3 * h;
for (int n = 4; n <= N; n++)
{
	x4 = x3 + h;
	yp = y3 + h * (55 * fx(x3, y3) - 59 * fx(x2, y2) + 37 * fx(x1, yx1) - 9 * fx(x0, yx0)) / 24;
	y4 = y3 + h * (9 * fx(x4,yp) + 19 * fx(x3, y3) - 5 * fx(x2, y2) + fx(x1, yx1)) / 24;
	printf("%lf\t%lf\n", x4, y4);		
	x0 = x1, yx0 = yx1;
	x1 = x2, yx1 = y2;
	x2 = x3, y2 = y3;
	x3 = x4, y3 = y4;	
}
system("pause");
return 0;

}// 为4阶龙格库塔修改得 极为复杂仅供参考

你可能感兴趣的:(C语言)