/* *   台形則による定積分 */ #include #include int main(void) { int j; int ntrial; double x0 = 0.0, x, y; double s = 0.0; double h; double sreal, serror; printf("Input: Number of trials:\n"); scanf("%u", &ntrial); h=1.0/ntrial; j = 0; x = x0; y = exp(-x); s = s + 0.5*y; for (j = 1; j <= ntrial-1; j ++) { x = x + h; y = exp(-x); s = s + y; } j = j +1; x = x + h; y = exp(-x); s = s + 0.5*y; s = s / ntrial; sreal=1.0-exp(-1.0); serror=s-sreal; printf("s = %1.10f \n", s); printf("sreal = %1.10f \n", sreal); printf("serror = %1.10f \n", serror); }