#include <stdio.h>
#include <math.h>
double f(double x, double y)
{
// 此处为待求解的方程,需要根据实际问题进行修改
return x * y - x * y * y;
}
void euler(double x0, double y0, double h, double xf)
{
double x = x0;
double y = y0;
double y_pred, y_corr;
while (x <= xf)
{
printf("%.4lf %.4lf\n", x, y);
y_pred = y + h * f(x, y);
y_corr = y + h * (f(x, y) + f(x + h, y_pred)) / 2.0;
y = y_corr;
x += h;
}
}
int main()
{
double x0, y0, xf, h;
printf("Enter the initial value of x: ");
scanf("%lf", &x0);
printf("Enter the initial value of y: ");
scanf("%lf", &y0);
printf("Enter the final value of x: ");
scanf("%lf", &xf);
printf("Enter the step size h: ");
scanf("%lf", &h);
euler(x0, y0, h, xf);
return 0;
}
需要修改的部分是函数f,该函数表示待求解的方程。根据不同的问题,需要修改函数中的表达式。例如,对于欧拉预估-校正公式的示例问题,可以将f函数修改为:
double f(double x, double y)
{
return x * y;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:主函数代码:
#include <stdio.h>
#include <stdlib.h>
int MinCommonMultiple(int a, int b);
int MaxCommonFactor(int a, int b);
int main(void)
{
int a, b, x,y;
printf("Input two positive integers,a,b:\n");
scanf("%d,%d", &a, &b);
if(a<=0||b<=0)
{
printf("Input error!\n");
}
else
{
int MaxCommonFactors = MaxCommonFactor(a,b);
int MinCommonMultiples = (a*b)/MaxCommonFactor(a,b);
printf("MinCommonMultiple = %d\n",MinCommonMultiples);
printf("MaxCommonFactor = %d\n",MaxCommonFactors);
}
return 0;
}