中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

一重積分的C++實現

發布時間:2020-06-21 13:40:27 來源:網絡 閱讀:1250 作者:Dussssss 欄目:編程語言
#include<iostream>  
#include<iomanip>  
#include<cmath>  

using namespace std;  

//求積:梯形公式  
double Func_Integral_Trapezoid  
(double lo, double hi, double(*Func)(double), int n = 1000)  
{//lo:下限;hi:上限;Func:函數;n:等分數  
   if (n <= 0) n = 100;  
      double x;  
    double step = (hi - lo) / n;  

    double result = 0.0;  
    x = lo;  
    for (int i = 1; i < n; i++) {  
        x += step;  
        result += Func(x);  
    }  
    result += (Func(lo) + Func(hi)) / 2;  
    result *= step;  

    return result;  
}  

//求積:Simpson公式  
double Func_Integral_Simpson  
(double lo, double hi, double(*Func)(double), int n = 1000)  
{//lo:下限;hi:上限;Func:函數;n:等分數  

    if (n <= 0) n = 100;  

    double x;  
    double step = (hi - lo) / n;  

    double result1 = 0.0;  
    x = lo;  
    for (int i = 1; i < n; i++) {  
        x += step;  
        result1 += Func(x);  
    }  
    result1 *= 2;  

    double result2 = 0.0;  
    x = lo + step / 2;  
    for (int i = 0; i < n; i++) {  
        result2 += Func(x);  
        x += step;  
    }  
    result2 *= 4;  

    double result = result1 + result2 + Func(lo) + Func(hi);  
    result *= step / 6;  

    return result;  
}  

//求積:Cotes公式  
double Func_Integral_Cotes  
(double lo, double hi, double(*Func)(double), int n = 1000)  
{//lo:下限;hi:上限;Func:函數;n:等分數  

    if (n <= 0) n = 100;  

    double x;  
    double step = (hi - lo) / n;  

    double result1 = 0.0;  
    x = lo;  
    for (int i = 1; i < n; i++) {  
        x += step;  
        result1 += Func(x);  
    }  
    result1 *= 14;  

    double result2 = 0.0;  
    x = lo + step / 2;  

    double result3 = 0.0;  
    double x1 = lo + step / 4;  
    double x2 = lo + step / 4 * 3;  

    for (int i = 0; i < n; i++) {  
        result2 += Func(x);  
        result3 += Func(x1) + Func(x2);  
        x += step;  
        x1 += step;  
        x2 += step;  
    }  
    result2 *= 12;  
    result3 *= 32;  

    double result4 = (Func(lo) + Func(hi)) * 7;  

    double result = result1 + result2 + result3 + result4;  
    result *= step / 90;  

    return result;  
}  

//求積:Romberg公式  
double Func_Integral_Romberg  
(double lo, double hi, double(*Func)(double), int k = 4)  
{//lo:下限;hi:上限;Func:函數;k:等分指數(區間等分成2^k份)  
    int size = k + 1;  
    double *matrix = new double[size*size];  
    for (int i = 0; i < size*size; i++) matrix[i] = 0.0;  
    double step = hi - lo;  
    matrix[0] = Func_Integral_Trapezoid(lo, hi, Func, 1);  
    for (int i = 1; i < size; i++) {  
        int n = 1 << (i - 1);  
        for (int k = 0; k < n; k++) {  
            matrix[i*size + 0] += Func(lo + (k + 0.5)*step);  
        }  
        matrix[i*size + 0] *= step;  
        matrix[i*size + 0] += matrix[(i - 1)*size];  
        matrix[i*size + 0] /= 2.0;  
        step /= 2.0;  
    }  

    double temp = 1.0;  
    double factor1, factor2;  
    for (int j = 1; j < size; j++) {  
        temp *= 4.0;  
        factor1 = temp / (temp - 1);  
        factor2 = 1 / (temp - 1);  
        for (int i = j; i < size; i++) {  
            matrix[i*size + j] = factor1*matrix[i*size + j - 1]  
                - factor2*matrix[(i - 1)*size + j - 1];  
        }  
    }  

    double result = matrix[k*size + k];  
    delete[] matrix;  

    return result;  
}  

//測試用的被積函數,0到1積分為PI  
double Func_test1(double x)  
{  
    return 4 / (1 + x*x);  
}  

int main() {  
    cout << "Trapezoid Numerical Integration" << endl;  
    cout << setprecision(15) << Func_Integral_Trapezoid(0, 1, Func_test1, 1024) << endl << endl;  
    cout << "Simpson Numerical Integration" << endl;  
    cout << setprecision(15) << Func_Integral_Simpson(0, 1, Func_test1, 1024) << endl << endl;  
    cout << "Cotes Numerical Integration" << endl;  
    cout << setprecision(15) << Func_Integral_Cotes(0, 1, Func_test1, 1024) << endl << endl;  
    cout << "Romberg Numerical Integraion" << endl;  
    cout << setprecision(15) << Func_Integral_Romberg(0, 1, Func_test1, 10) << endl << endl;  
    getchar();  
    return 0;  
}  
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

深圳市| 临沂市| 郑州市| 中卫市| 霍山县| 班戈县| 兰坪| 彰化县| 肥乡县| 大余县| 常宁市| 泰宁县| 肥东县| 安徽省| 乌鲁木齐县| 滨海县| 白河县| 扶沟县| 青海省| 项城市| 仪征市| 当雄县| 昌邑市| 屏东县| 沈阳市| 绵阳市| 太谷县| 临沂市| 榆林市| 报价| 建昌县| 奉化市| 扬中市| 平和县| 汉寿县| 始兴县| 修水县| 云霄县| 句容市| 木里| 丹巴县|