首页 文章

用c中的泰勒级数计算Pi的函数

提问于
浏览
0

所以我不知道为什么我的代码不能正常工作,基本上我正在编写的函数使用泰勒系列计算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);
 }

代码背后的逻辑如下:

  • 定义所有变量

  • 将pi的估计值设为0

  • 从泰勒系列中计算一个项并计算该项中的误差

  • 然后将最新术语添加到pi的估计值

  • 程序应该计算出系列中的下一个术语及其中的错误并将其添加到pi的估计值,直到满足while语句中的条件为止

感谢您的帮助

3 回答

  • 0

    您的功能有几个错误 . 用“// 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;
    }
    
  • 1

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

    尝试返回“estimate_of_pi”而不是get_pi(准确度) .

  • 0

    您的休息条件可以改写为

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

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

    return estimated_error;
    

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

    无论你有一个 sign 和参数 accuracy ,你在计算中根本不使用它 .

    我对这种迭代的建议是总是打破一些最大迭代次数 . 在这种情况下,你知道它会收敛(假设你修复了数学),但一般来说你永远无法确定你的迭代是否收敛 .

相关问题