2017-05-11 7 views
0

なぜ私のコードが機能しないのか、私が書いている関数は、テイラー級数を使ってPiの推定値を計算します。プログラムを実行します。C++でテイラー級数を使用してPiを計算する関数

  1. から項を計算するPIのすべての変数
  2. セットの推定値を定義します。ここ

    は私のコードは、コードの背後にあるロジックは以下の通りです

    #include <iostream> 
    #include <math.h> 
    #include <stdlib.h> 
    using namespace std; 
    
    double get_pi(double accuracy) 
    { 
    double estimate_of_pi, latest_term, estimated_error; 
    int sign = -1; 
    int n; 
    
    estimate_of_pi = 0; 
    n = 0; 
    
    do 
    { 
        sign = -sign; 
        estimated_error = 4 * abs(1.0/(2*n + 1.0)); //equation for error 
        latest_term = 4 * (1.0 *(2.0 * n + 1.0));  //calculation for latest term in series 
        estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi 
        n = n + 1;          //changing value of n for next run of the loop 
    } 
    while(abs(latest_term)< estimated_error); 
    
    return get_pi(accuracy); 
    
    } 
    
    int main() 
    { 
        cout << get_pi(100); 
    } 
    

    ですテイラー級数を計算し、誤差を計算する

  3. パイの推定値の最新の用語は
  4. プログラムは、それにシリーズの次の用語やエラーを仕事とwhile文の条件が満たされるまで、パイの推定値に追加する必要があります

助けてくれてありがとう

答えて

1

機能にいくつかのエラーがあります。コメントは "// NOTE:"で始まる行で見てください。

double get_pi(double accuracy) 
{ 
    double estimate_of_pi, latest_term, estimated_error; 
    int sign = -1; 
    int n; 

    estimate_of_pi = 0; 
    n = 0; 

    do 
    { 
     sign = -sign; 

     //NOTE: This is an unnecessary line. 
     estimated_error = 4 * abs(1.0/(2*n + 1.0)); //equation for error 

     //NOTE: You have encoded the formula incorrectly. 
     // The RHS needs to be "sign*4 * (1.0 /(2.0 * n + 1.0))" 
     //      ^^^^  ^
     latest_term = 4 * (1.0 *(2.0 * n + 1.0));  //calculation for latest term in series 
     estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi 
     n = n + 1;          //changing value of n for next run of the loop 
    } 

    //NOTE: The comparison is wrong. 
    // The conditional needs to be "fabs(latest_term) > estimated_error" 
    //        ^^^^    ^^^ 
    while(abs(latest_term)< estimated_error); 

    //NOTE: You are calling the function again. 
    // This leads to infinite recursion. 
    // It needs to be "return estimate_of_pi;" 
    return get_pi(accuracy);  
} 

また、mainの関数呼び出しは間違っています。

get_pi(0.001) 

これは、用語の絶対値が0.001より小さい場合、関数が戻ることができることを示します。

私のために機能する最新バージョンの関数です。

double get_pi(double accuracy) 
{ 
    double estimate_of_pi, latest_term; 
    int sign = -1; 
    int n; 

    estimate_of_pi = 0; 
    n = 0; 

    do 
    { 
     sign = -sign; 
     latest_term = sign * 4 * (1.0 /(2.0 * n + 1.0)); //calculation for latest term in series 
     estimate_of_pi += latest_term;     //adding latest term to estimate of pi 
     ++n;            //changing value of n for next run of the loop 
    } 
    while(fabs(latest_term) > accuracy); 

    return estimate_of_pi; 
} 
+0

あなたがチームメイトありがとう、私が掲示した後、私はあなたを歓迎している、おかげで再び –

+0

@AmanSood、今鉱山を修正し、意図したとおり、それが動作している、あなたが言及したいくつかのことに気づきました。私は喜んで助けてくれました。 –

0

あなたのリターンステートメントが原因かもしれません。

get_pi(精度)ではなく "estimate_of_pi"を返すようにしてください。

0

あなたのブレーク条件が

2*n + 1 < 1/(2*n + 1)  => (2*n + 1)^2 < 1 

のように書き換えることができ、これは任意の正nためtrueになることはありません。したがって、あなたのループは決して終わらないでしょう。これを修正した後、あなたはあなたが現在(あなたが停止条件を一定と仮定して)終了せずに再帰関数を呼び出している

return estimated_error; 

にreturn文を変更する必要があります。

さらに、計算にはまったく使用しないsignとパラメータaccuracyがあります。

このような繰り返しのための私のアドバイスは、常にいくつかの最大反復回数を打破することです。この場合、収束していることがわかります(数学を修正すると仮定します)。しかし、一般的に、反復が収束することは決して確認できません。

関連する問題