1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語で学ぶ台形公式による積分の近似

Last updated at Posted at 2017-12-24

台形公式とは

下の絵の青い線がが積分したい関数のグラフ、aからbまでを積分したい範囲(左の茶色の線から右茶色の線)だと思ってください。台形公式とは緑と黄色の部分の面積を積分した値だと考えることです。その値は

 \frac{(f(a) + f(b)) (b - a)}{2} 

になりますね
68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3130383731362f66613335663735312d356335352d663361642d313138322d6632366236306266373732332e706e67.png

しかし、図で見て分かる通り緑の部分が明らかに余計です。なので、$ a $と$ b $の間に$ c = \frac{a + b}{2} $という点を設けましょう。緑色と黄色の部分の面積の合計は

\frac{(f(a) + f(c)) (c - a) + (f(c) + f(b)) (b - c)}{ 2 }

になりますね。するとついさっきの図より緑色の部分が減っているのがわかります。これが台形公式です。$ a $と$ b $の間に点があればあるほど、その値は正確になります。
68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f3130383731362f33303762303434352d636133352d306663622d386137362d3030663866383337303136392e706e67.png! Untitled.png

ソースコード

integral.c
#include <stdio.h>
#include <math.h>

#define EPS (1.0e-6)
#define f(x) ((x)*(x))

int main(){
    double a = 0, b = 2, sum = 0;
    for(double x = a; x < b; x += EPS){
        sum += (f(x) + f(x+EPS));
    }
    sum *= (EPS / 2);

    printf("%f\n", sum);
}
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?