MENU

数値積分

台形則による定積分

関数 f(x) の定積分 \int_{a}^{b}f(x)dx を台形則により求めてみます。

関数 f(x) の定積分を微小区間に分割して近似値として求める方法を数値積分といいます。

f:id:Manao55:20200814022308j:plain

図に示すように, a, b 区間m 個の台形に分割し, 各台形の面積を合計すると,

{ \displaystyle
\int_{a}^{b}f(x)dx = \frac{h}{2}(f(a)+f(a+h)) + \frac{h}{2}(f(a+h)+f(a+2h)) 
}
{ \displaystyle
+ \frac{h}{2}(f(a+2h)+f(a+3h)) + ... + \frac{h}{2}(f(a+(n-1)h)+f(b))
}
{ \displaystyle
= h{\frac{1}{2}(f(a)+f(b))+f(a+h)+f(a+2h)+...+f(a+(n-1)h)}
}

となります。

プログラム

#include <iostream>
#include <math.h>
using namespace std;

#define f(x) (sqrt(4-(x)*(x))) /* 被積分関数 */

int main(void){
    int k;
    double a,b,n,h,x,s,sum;

    cout << "Integration interval A,B ? " << endl;
    cin >> a >> b ;

    n = 1000;
    h=(b-a)/n;
    x=a;
    s=0;
    for(k=1;k<=n-1;k++){
        x=x+h;
        s=s+f(x);
    }
    sum = h*((f(a)+f(b))/2+s);
    cout << "    /" << b << endl;
    cout << "    | sqrt(4-x*x) dx = " << sum << endl;
    cout << "    /" << a << endl;

    return 0;
}

実行結果

Integration interval A,B ?
0 2
    /2
    | sqrt(4-x*x) dx = 3.14156
    /0