Python 无法计算第七个多项式方程

问题描述 投票:0回答:3

我必须计算一个方程来翻译两个不同字段中的坐标。通过 Geogebra,我得出了这个方程,我检查过并且是正确的:

https://i.stack.imgur.com/StSCs.jpg<-- Link to the equation

该方程的输入和输出如下:

输入 输出
0 6
1 7
2 8
3 11
4 12
5 13
6 16
7 17
8 18

如前所述,方程是正确的。

为了测试这个方程,我将其实现到 python 中


import math
import colorful

def polynom(x):
    return 0.0036 * pow(x,7) - 0.1 * pow(x,6) + 1.1 * pow(x,5) - 6 * pow(x,4) + 16.7583 * pow(x,3) - 21.9 * pow(x,2) + 11.1381 * x + 6


# To test the equation with the input and the expected output
for i in range (0,9):
    result = polynom(i)
    if result == exp[i]:
        print(colorful.green(str(i) + " : " + str(result)))
    else:
        print(colorful.red(str(i) + " : " + str(result)))

奇怪的是,这个脚本给了我以下输出:

0 : 6.0
1 : 7.0
2 : 8.003399999999996
3 : 11.061599999999942
4 : 12.465999999999973
5 : 15.228000000000364
6 : 23.99099999999936
7 : 40.51839999999771
8 : 77.90159999999871

只有两个是正确的,而其余的给出了越来越奇怪的解决方案。

现在我的问题:

  • 这是python本身引擎的问题吗?
  • 是否有错别字或思维错误
  • 如果我用Java实现这个会出现类似的错误吗?
python java polynomials
3个回答
1
投票

python 给你的结果是正确的。问题出在你的输入和预期输出表上。

我在 Wolfram 网站中检查了以下结果:

0.0036 * xˆ7 - 0.1 * xˆ6 + 1.1 * xˆ5 - 6 * xˆ4 + 16.7583 * xˆ3 - 21.9 * xˆ2 + 11.1381 * x + 6
:P(x)=8的方程结果链接,并且给我的结果与你的 python 测试相同。

我认为你需要重新计算你的产出预期。


0
投票

在 Excel 中运行方程式后,结果看起来是正确的。 这是 x = 3 的情况

所以,是的,看起来您得到的结果是预期的。


0
投票

我认为您已经从 GeoGebra 复制了方程,而没有增加舍入设置。尝试使用 15 位有效数字

© www.soinside.com 2019 - 2024. All rights reserved.