#include <stdio.h>
double f(double x, double y)
{
double result = x*x + y;
return result;
}
int main()
{
double a = 0;
double b = 1;
double h = 0.2;
double n0 = (b-a)/h + 1;
int n = n0;
printf("n = %d\n", n);
double y0[n], x[n], y[n];
y0[n-1] = 1;
y[n-1] = 1;
for (int i = 0; i < n; i++)//initialize x[n]
{
x[i] = a + h * i;
}
for (int i = n-1; i > 0; i--)//calculate y[n]
{
y0[i-1] = y0[i] - h*f(x[i], y0[i]);
y[i-1] = y[i] - h/2*(f(x[i-1], y0[i-1]) + f(x[i], y[i]));
}
for (int i = 0; i < n; i++)//print
{
printf("y(%lf) = %lf\n", x[i], y[i]);
}
return 0;
}
改进Euler法的C语言实现
最新推荐文章于 2024-08-19 22:10:14 发布