梯形數值積分法
================================
若 f(x) 在 [a,b] 中連續,則可積。 梯形數值積分法 (trapezoidal rule) 是求定積分之近似值的一種方法, 其公式如下================================
計算
#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 = 0;
b = 1;
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(x);
}
T_2 = 1.75393109246483.
T_4 = 1.72722190455752.
T_8 = 1.72051859216430.
T_16 = 1.71884112857999.
T_32 = 1.71842166031633.
T_64 = 1.71831678685009.
T_128 = 1.71829056808348.
T_256 = 1.71828401336682.
T_512 = 1.71828237468609.
T_1024 = 1.71828196501581.
...Program finished with exit code 0
Press ENTER to exit console.
沒有留言:
張貼留言