2018年12月31日 星期一

範例EX4-1 利用梯行法計算 e^x , x=0 to 1 的積分值

梯形數值積分法

================================
若 f(x) 在 [a,b] 中連續,則可積。 梯形數值積分法 (trapezoidal rule) 是求定積分之近似值的一種方法, 其公式如下
\int_a^b f(x)\,dx \approx h \cdot \Bigl[\frac{f(x_0)}2 +
\frac{f(x_n)}2 + \sum_{k=1}^{n-1} f(x_k)\Bigr]
其中 n 表示將 [a,b] 等分成 n 段,令 
h = \frac{b-a}n
而且 x0 = a、 xn = b,一般而言 xk = a + k h

================================


計算 
plot | e^x | x = 0 to 1





#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.                                                                                                                   
                             

沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...