C 言語で級数展開を用いた e の近似計算

CBeginner
オンラインで実践に進む

はじめに

この実験では、C 言語を用いて級数展開を用いて数学定数 e を近似する方法を学びます。e の級数は階乗の逆数の和として定義されます:e = 1 + 1/1! + 1/2! + 1/3! + ... まず、この級数近似を計算する C プログラムを作成し、次に、所望の精度に達するまで項を合計するようにプログラムを修正します。これにより、計算の精度を制御し、より正確な e の近似値を得ることができます。

e = ∑(1/n!) を用いる

このステップでは、C 言語を用いて級数展開を用いて数学定数 e を近似する方法を学びます。e の級数は階乗の逆数の和として定義されます:e = 1 + 1/1! + 1/2! + 1/3! + ...

まず、この級数近似を計算する C プログラムを作成します。プロジェクトディレクトリに新しいファイルを作成します。

cd ~/project
nano e_approximation.c

次に、以下の C コードを入力します。

#include <stdio.h>

double factorial(int n) {
    if (n == 0 || n == 1) return 1;
    double result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

double approximate_e(int terms) {
    double e = 0;
    for (int n = 0; n < terms; n++) {
        e += 1.0 / factorial(n);
    }
    return e;
}

int main() {
    int num_terms = 10;
    double e_approximation = approximate_e(num_terms);
    printf("Approximation of e with %d terms: %f\n", num_terms, e_approximation);
    return 0;
}

コンパイルしてプログラムを実行します。

gcc e_approximation.c -o e_approximation
./e_approximation

実行結果の例:

Approximation of e with 10 terms: 2.718282

所望の精度まで項を合計する

このステップでは、所望の精度に達するまで項を合計することで、e の近似値を計算するプログラムを修正します。このアプローチにより、計算の精度を制御できます。

以前の C ファイルの更新を行います。

cd ~/project
nano e_approximation.c

以前の内容を以下の改良されたコードに置き換えます。

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

double factorial(int n) {
    if (n == 0 || n == 1) return 1;
    double result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

double approximate_e_with_accuracy(double desired_accuracy) {
    double e = 0;
    double term = 1;
    int n = 0;

    while (fabs(term) > desired_accuracy) {
        e += term;
        n++;
        term = 1.0 / factorial(n);
    }

    return e;
}

int main() {
    double accuracy = 1e-6;  // 所望の精度レベル
    double e_approximation = approximate_e_with_accuracy(accuracy);

    printf("Approximation of e with accuracy %e: %f\n", accuracy, e_approximation);
    printf("標準の math.h の e の値:%f\n", M_E);

    return 0;
}

更新されたプログラムをコンパイルして実行します。

gcc e_approximation.c -o e_approximation -lm
./e_approximation

実行結果の例:

Approximation of e with accuracy 1.000000e-06: 2.718282
Standard math.h e value: 2.718282

このプログラムのバージョンでは、次の項が指定された精度しきい値より小さくなるまで、級数に項を追加し続けます。fabs() 関数は、項の絶対値を使用して計算を行うことを保証します。

重要な点:

  • 所望の精度レベルとして 1e-6 を使用しています。
  • プログラムは、必要な項数を動的に決定します。
  • 標準の math ライブラリ関数による e の値と近似値を比較しています。

近似値の出力

この最終ステップでは、さまざまな精度レベルで e の近似値を出力し、より詳細な結果を提供するようにプログラムを強化します。

C ファイルのさらに更新を行います。

cd ~/project
nano e_approximation.c

以前の内容を以下の包括的なコードに置き換えます。

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

double factorial(int n) {
    if (n == 0 || n == 1) return 1;
    double result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

void print_e_approximations() {
    double accuracies[] = {1e-1, 1e-3, 1e-6, 1e-9};
    int num_accuracies = sizeof(accuracies) / sizeof(accuracies[0]);

    printf("e の近似値(異なる精度レベル):\n");
    printf("---------------------------------------------\n");

    for (int i = 0; i < num_accuracies; i++) {
        double e = 0;
        double term = 1;
        int n = 0;
        int terms_used = 0;

        while (fabs(term) > accuracies[i]) {
            e += term;
            n++;
            term = 1.0 / factorial(n);
            terms_used++;
        }

        printf("精度:%e\n", accuracies[i]);
        printf("近似値:%.10f\n", e);
        printf("使用された項の数:%d\n", terms_used);
        printf("math.h の e との差:%.10f\n\n", fabs(e - M_E));
    }
}

int main() {
    print_e_approximations();
    return 0;
}

プログラムをコンパイルして実行します。

gcc e_approximation.c -o e_approximation -lm
./e_approximation

実行結果の例:

eの近似値(異なる精度レベル):
---------------------------------------------
精度: 1.000000e-01
近似値: 2.7000000000
使用された項の数: 4
math.hのeとの差: 0.0182818284

精度: 1.000000e-03
近似値: 2.7182500000
使用された項の数: 8
math.hのeとの差: 0.0000318284

精度: 1.000000e-06
近似値: 2.7182818000
使用された項の数: 14
math.hのeとの差: 0.0000000284

精度: 1.000000e-09
近似値: 2.7182818285
使用された項の数: 21
math.hのeとの差: 0.0000000000

このバージョンの主な改善点:

  • 複数の精度レベルでの近似値を出力
  • 各近似値で使用する項数を表示
  • 標準の math ライブラリ関数による e の値との差を計算して表示

まとめ

この実験では、C 言語を用いて級数展開を用いて数学定数 e の近似値を計算する方法を学びました。最初に、数値の階乗を計算する関数を実装し、次に、所望の項数まで級数の項を合計することで e の近似値を計算しました。その後、所望の精度に達するまで項を合計し続けるようにプログラムを修正し、計算の精度を制御できるようにしました。最終的なプログラムは、所望の精度で e の近似値を出力します。