四次方程求解器的实现不起作用

发布于 2024-10-12 08:35:57 字数 434 浏览 3 评论 0原文

我的四次方程求解器的实现出了什么问题?
这是我的代码 GitHub 上。我按照以下步骤操作:http://www.1728.com/quartic2.htm
事实上,真正的实现从第 271 行开始,我在其中创建了单子多项式。

如果我尝试使用具有 4 个实根的多项式,则效果很好(例如 3x^4 + 6x^3 - 123x^2 - 126x + 1,080),否则给出错误的根。

谢谢,
鲁比克

​我将函数命名为 __quartic 因为它仍在开发中

What's going wrong with my implementation of a quartic equation solver?
Here is my code on GitHub. I followed this: http://www.1728.com/quartic2.htm
In fact the real implementation starts at line 271, where I create the monic poly.

If I try it with a polynomial with 4 real roots it works fine (for example with 3x^4 + 6x^3 - 123x^2 - 126x + 1,080), otherwise gives wrong roots.

Thanks,
rubik

P.S. I called the function __quartic because it is still in development

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞎闹 2024-10-19 08:35:57

如果这不是你的三次解算器的问题(正如phkahler建议的那样),那么我打赌这是这些行中int除法的情况:

f = float(c - (3*b**2 / 8))
g = float(d + (b**3/ 8) - (b*c / 2))
h = e - (3*b**4 / 256) + (b**2 * c / 16) - (b*d / 4)

如果你使用的Python版本低于3.0并且没有导入与 __future__ 相除,那么这里可能会遇到重大问题。例如,如果 b = 3,则 (3*b**2/8) 通过 int 除法等于 3,而不是正确的浮点除法值 3.375。纠正这个问题的最佳方法可能是在开始计算之前将 a、b、c、d 和 e 转换为浮点数。在进行此更正后,在对 f 和 g 进行所有计算后,您实际上没有必要将其转换为浮点型,而且它们现在对您并没有多大帮助。

有关此划分问题及其历史的更多信息,请参阅:

http://www.python。 org/dev/peps/pep-0238/

http://python-history.blogspot.com/2009/03/problem-with-integer-division.html

If it isn't a problem with your cubic solver (as phkahler suggested), then I'm betting that it is a case of int division in these lines:

f = float(c - (3*b**2 / 8))
g = float(d + (b**3/ 8) - (b*c / 2))
h = e - (3*b**4 / 256) + (b**2 * c / 16) - (b*d / 4)

If you are using a version of Python that is less than 3.0 and not importing division from __future__, then you can have major problems here. For instance, if b = 3, then (3*b**2/8) is equal to 3 by int division rather than the correct float division value of 3.375. Probably the best way to correct this is to convert a,b,c,d and e to floats before you start doing calculations. Your conversion to float after all of the calculations for f and g are really unnecessary after you've made this correction and they aren't really helping you much now.

For more information about this division issue and its history see:

http://www.python.org/dev/peps/pep-0238/

http://python-history.blogspot.com/2009/03/problem-with-integer-division.html

何以畏孤独 2024-10-19 08:35:57

你的立方根求解器有效吗?您应该将其减少为仅返回一个实根以在四次求解器中使用。首先在三次求解器上运行多个测试用例,包括同时具有 1 和 3 实根的方程。只有在验证之后,您才可以尝试调试四次求解器。

Does your cubic root solver work? You should have it reduced to only returning a single real root for use within the quartic solver. Run a number of test cases on the cubic solver first, including equations with both 1 and 3 real roots. Only after this is verified should you try to debug the quartic solver.

春夜浅 2024-10-19 08:35:57

也许我可以帮忙。我编写了一个程序和求解四次方程的过程

我猜你不是用 JavaScript 编写程序。
我愿意帮忙(如果可以的话)。

Perhaps I can help out. I wrote a program and the procedure for solving quartic equations.

I'm guessing you are not writing the program in JavaScript.
I'd be willing to help out (if I can).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文