2018年12月31日 星期一

範例EX4-2 利用梯行法計算(exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5.3 to 3.0 的積分值

範例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.    

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...