求解一个三次方程 [英] Solving a cubic equation

查看:164
本文介绍了求解一个三次方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个程序我写的一部分,我需要解决一个三次方程的准确位置(而不是使用数值求根):

As part of a program I'm writing, I need to solve a cubic equation exactly (rather than using a numerical root finder):

a*x**3 + b*x**2 + c*x + d = 0.

我想这里使用来自方程。然而,考虑以下code(这是Python的,但它是pretty的通用code):

I'm trying to use the equations from here. However, consider the following code (this is Python but it's pretty generic code):

a =  1.0
b =  0.0
c =  0.2 - 1.0
d = -0.7 * 0.2

q = (3*a*c - b**2) / (9 * a**2)
r = (9*a*b*c - 27*a**2*d - 2*b**3) / (54*a**3)

print "q = ",q
print "r = ",r

delta = q**3 + r**2

print "delta = ",delta

# here delta is less than zero so we use the second set of equations from the article:

rho = (-q**3)**0.5

# For x1 the imaginary part is unimportant since it cancels out
s_real = rho**(1./3.)
t_real = rho**(1./3.)

print "s [real] = ",s_real
print "t [real] = ",t_real

x1 = s_real + t_real - b / (3. * a)

print "x1 = ", x1

print "should be zero: ",a*x1**3+b*x1**2+c*x1+d

但输出是:

q =  -0.266666666667
r =  0.07
delta =  -0.014062962963
s [real] =  0.516397779494
t [real] =  0.516397779494
x1 =  1.03279555899
should be zero:  0.135412149064

所以输出不为零,所以x1为不实际的解决方案。有维基百科的文章中的错误?

so the output is not zero, and so x1 isn't actually a solution. Is there a mistake in the Wikipedia article?

PS:我知道numpy.roots会解决这类方程,但我必须这样做以百万计的方程,所以我需要实现这个工作对系数数组

ps: I know that numpy.roots will solve this kind of equation but I need to do this for millions of equations and so I need to implement this to work on arrays of coefficients.

推荐答案

维基百科的形式(RHO ^(1/3),THETA / 3)不等于 RHO ^(1/3)的实部和 THETA / 3 是虚部。相反,这是极坐标。因此,如果你想真正的一部分,你会采取 RHO ^(1/3)* COS(THETA / 3)

Wikipedia's notation (rho^(1/3), theta/3) does not mean that rho^(1/3) is the real part and theta/3 is the imaginary part. Rather, this is in polar coordinates. Thus, if you want the real part, you would take rho^(1/3) * cos(theta/3).

我做了这些改变到code和它的工作对我来说:

I made these changes to your code and it worked for me:

theta = arccos(r/rho)
s_real = rho**(1./3.) * cos( theta/3)
t_real = rho**(1./3.) * cos(-theta/3)

(当然, s_real = t_real 在这里,因为 COS 是偶数。)

(Of course, s_real = t_real here because cos is even.)

这篇关于求解一个三次方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆