範例EX4-2 利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.3 to 3.0 的積分值
#include <stdio.h>
#include <math.h>
/* 示範梯形數值積分法 (trapeziodal.c) */
#define STEP 10
double f(double);
main()
{
int n, k, i;
double h, T, a, b, x;
T = 0;
a = -3.5;
b = -3.0;
for (n=1, i=0; i < STEP; ++i) {
n = 2*n;
h = (b-a)/n;
x = a;
T = f(x)/2;
for (k=1; k<n ; ++k) {
x = x + h;
T = T + f(x);
}
T = T + f(b)/2;
T = h*T;
printf("T_%-5d = %17.14f.\n", n, T);
}
}
double f(double x) {
return ( (exp(-pow(x,2)/2)) / sqrt(2*3.14159));
}
T_8 = 0.00112060204348.
T_16 = 0.00111810278396.
T_32 = 0.00111747777712.
T_64 = 0.00111732151342.
T_128 = 0.00111728244675.
T_256 = 0.00111727268003.
T_512 = 0.00111727023835.
T_1024 = 0.00111726962793.
...Program finished with exit code 0
Press ENTER to exit console.
沒有留言:
張貼留言