| euler.c |
/* euler.c */
#include <stdio.h>
#include <math.h>
int main()
{
int i, N;
double a = 0.0, b = 1.0;
double x0;
double t, x, h;
double f(double, double);
printf(" x0="); scanf("%lf", &x0);
printf(" N="); scanf("%d", &N);
h = (b - a) / N;
t = a; x = x0;
printf("%g %g\n", t, x);
for (i = 0; i < N; i++) {
x = x + h * f(t,x);
t = t + h;
printf("%g %g\n", t, x);
}
printf("%g %20.15e\n", t, x);
return 0;
}
double f(double t, double x)
{
return x;
}
|
| 実行結果 |
mathpc00% ./euler x0=1 N=100 0 1 0.01 1.01 0.02 1.0201 0.03 1.0303 0.04 1.0406 0.05 1.05101 0.06 1.06152 0.07 1.07214 0.08 1.08286 0.09 1.09369 中略 0.91 2.47312 0.92 2.49785 0.93 2.52283 0.94 2.54806 0.95 2.57354 0.96 2.59927 0.97 2.62527 0.98 2.65152 0.99 2.67803 1 2.70481 1 2.704813829421526e+00 mathpc00% |