【问题标题】:Function for calculating Pi using taylor series in c++在 c++ 中使用泰勒级数计算 Pi 的函数
【发布时间】:2017-10-10 20:29:02
【问题描述】:

所以我不知道为什么我的代码不起作用,基本上我正在编写的函数使用泰勒级数计算 Pi 的估计值,每当我尝试运行程序时它就会崩溃。

这是我的代码

#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);
 }

代码背后的逻辑如下:

  1. 定义所有变量
  2. 将 pi 的估计值设置为 0
  3. 计算泰勒级数中的一项并计算误差 这个词
  4. 然后它将最新项添加到 pi 的估计中
  5. 然后程序应计算出系列中的下一项和其中的误差,并将其添加到 pi 的估计值中,直到满足 while 语句中的条件

感谢您的帮助

【问题讨论】:

    标签: c++ loops do-while pi taylor-series


    【解决方案1】:

    您的函数中有几个错误。查看我的 cmets 以“//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;
    }
    

    【讨论】:

    • 谢谢你的朋友,我注意到你在我发布后提到的一些事情,现在已经修改了我的,它可以正常工作,再次感谢
    【解决方案2】:

    您的退货声明可能是原因。

    尝试返回“estimate_of_pi”而不是 get_pi(accuracy)。

    【讨论】:

      【解决方案3】:

      你的中断条件可以改写为

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

      对于任何积极的n,这永远不会是true。因此,您的循环将永远不会结束。修复此问题后,您应该将 return 语句更改为

      return estimated_error;
      

      您当前正在递归调用该函数而没有结束(假设您修复了停止条件)。

      此外,您还有一个sign 和参数accuracy,您在计算中根本不使用它们。

      我对此类迭代的建议是始终在某个最大迭代次数上中断。在这种情况下,您知道它会收敛(假设您修复了数学问题),但通常您永远无法确定您的迭代是否会收敛。

      【讨论】:

        猜你喜欢
        • 2017-09-27
        • 2015-05-15
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 2017-03-20
        • 2020-03-01
        • 1970-01-01
        • 2014-07-11
        相关资源
        最近更新 更多