C语言实现4阶龙格库塔公式

#include<stdio.h>
#include<stdlib.h>
double fx(double x, double y)
{
return(y - 2 * x / y);//书上的公式
}
int main() {

double x1, K1, K2,K3,K4, y1;
double x0, y0, h;
int N;
printf("请输入x0,y0,h,N:");
scanf_s("%lf%lf%lf%d", &x0, &y0, &h, &N);
//printf("%lf%lf%lf%d", x0, y0, h, N);
for (int i = 0; i < N; 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;
	printf("%lf\t%lf\n", x1, y1);
	x0 = x1;
	y0 = y1;
}
system("pause");
return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_41281130/article/details/88894304