(これは授業ではカットするかな。そういうわけで工事中。)
/*
* example1.c -- 多項式の積分
*/
#include <stdio.h>
#include <math.h>
#include "nc.c"
int degree = 0; // 0 or 1 or 2 or 3 or 4
double f(double x)
{
switch (degree) {
case 0: return 1;
case 1: return 1 + 2 * x;
case 2: return 1 + 2 * x + 3 * x * x;
case 3: return 1 + 2 * x + 3 * x * x + 4 * x * x * x;
case 4: return 1 + 2 * x + 3 * x * x + 4 * x * x * x + 5 * x * x * x * x;
default:
return 1;
}
return 1;
// return 1 + 2 * x;
//return 1 + 2 * x + 3 * x * x;
}
int main(void)
{
int N;
double a, b, I[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
double M, T, S;
a = 0.0; b = 1.0;
printf("次数="); scanf("%d", °ree);
printf("N="); scanf("%d", &N);
M = midpoint(f, a, b, N);
T = trapezoidal(f, a, b, N);
S = simpson(f, a, b, N);
printf("中点公式 M=%20.15f, 誤差=%e\n", M, I[degree] - M);
printf("台形公式 T=%20.15f, 誤差=%e\n", T, I[degree] - T);
printf("Simpson公式 S=%20.15f, 誤差=%e\n", S, I[degree] - S);
return 0;
}
|
$ cc -o example1 example1.c $ ./example1 次数=2 N=10 中点公式 M= 2.997500000000000, 誤差=2.500000e-03 台形公式 T= 3.005000000000001, 誤差=-5.000000e-03 Simpson公式 S= 3.000000000000000, 誤差=0.000000e+00 $ |
桂田 祐史