【问题标题】:gnu scientific library (GSL) cubic polynomial discrepancy in resultsgnu 科学库 (GSL) 结果中的三次多项式差异
【发布时间】:2015-09-09 06:02:56
【问题描述】:

GSL polynomial solver 给了我一个不正确的结果。我尝试使用 GSL 三次多项式求解器来求解以下多项式:

x^3-1.96848e20 x^2+9.07605e28 x+9.07605e28 = 0

Wolframalpha 上,results 是:

  • -1
  • 4.61069e+8
  • 1.96848e+20

我的程序中,结果是:

  • 2.30531e+08
  • 2.30531e+08
  • 1.96848e+20

我使用的代码

   #include <iostream>
    #include <gsl/gsl_poly.h>

    using namespace std;

    int main() {
      double result[3]={0,0,0};
      gsl_poly_solve_cubic(-1.96848e20,9.07605e28,9.07605e28, &result[0], &result[1], &result[2]);
      cout << result[0] << endl;
      cout << result[1] << endl;
      cout << result[2] << endl;
      return 0;
    }

可能出了什么问题?

编辑:实际上,Wolframalpha 也给出了奇怪的答案

更新:Wolframalpha 都错了(更接近正确答案),我的代码也错了。

这是 Python 中的相同解决方案(Numpy 库)

In [11]: np.roots(p)
Out[11]: array([  1.96848000e+20,   4.61068948e+08,  -9.99999998e-01])

【问题讨论】:

  • 似乎 jenkins-traub 多项式根算法给出了更准确的结果,而 GSL 三次多项式根给出了四舍五入的答案。在这种情况下,答案的顺序在 -1 ~ 2e20 左右,这可能会暗示 GSL 算法失败的原因。在这种情况下,Jenkins-Traub 求解器似乎给出了更准确的答案。
  • 不,GSL 例程中不涉及四舍五入,只有一种数值不稳定的案例检测方法会错误地得出双根的结论。避免该分支会导致正确答案。

标签: c++ polynomial-math gsl wolframalpha polynomial-approximations


【解决方案1】:

在这种情况下,GSL 中使用的数值方法不够准确。

使用例如Jenkins-Traub算法(代码here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多