为什么MATLAB不能给出正确的四次方程结果?

Why MATLAB cannot give the correct result of the quartic equation?

尽管 MATLAB 拥有 内置 solve() 来求解四次方程,solve()函数在某些情况下会给出一些错误信息。所以我编写了一个名为 solveQuartic() 的用户定义函数来求解四次方程的根。

我找到的参考是here

我的试用版

function [res] = solveQuartic(a, b, c, d, e)
 p = (8*a*c - 3*b^2)/(8*a^2);
 q = (b^3 - 4*a*b*c + 8^a^2*d)/(8*a^3);

 delta0 = c^2-3*b*d + 12*a*e;
 delta1 = 2*c^3 - 9*b*c*d + 27*b^2*e + 27*a*d^2 - 72*a*c*e;

 Q = ((delta1 + sqrt(delta1^2 - 4*delta0^3))*0.5)^(1/3);
 S = 0.5*sqrt(-2/3*p + (Q + delta0/Q)/(3*a));

 x1 = -b/(4*a) - S - 0.5*sqrt(-4*S^2-2*p + q/S);
 x2 = -b/(4*a) - S + 0.5*sqrt(-4*S^2-2*p + q/S);
 x3 = -b/(4*a) + S - 0.5*sqrt(-4*S^2-2*p - q/S);
 x4 = -b/(4*a) + S + 0.5*sqrt(-4*S^2-2*p - q/S);

  res = [x1; x2; x3; x4];
end

测试

res = solveQuartic(1,2,3,4,5)
-4.1425 - 0.0000i
 1.5669 + 0.0000i
 0.2878 + 3.3001i
 0.2878 - 3.3001i

但是,当我在 Mathematica 中实现如下公式时:

solveQuartic[a_, b_, c_, d_, e_] :=
 Module[{p, q, delta0, delta1, Q, S, x1, x2, x3, x4},
  p = (8*a*c - 3*b^2)/(8*a^2);
  q = (b^3 - 4*a*b*c + 8^a^2*d)/(8*a^3);
  delta0 = c^2 - 3*b*d + 12*a*e; 
  delta1 = 2*c^3 - 9*b*c*d + 27*b^2*e + 27*a*d^2 - 72*a*c*e;
  Q = ((delta1 + Sqrt[delta1^2 - 4*delta0^3])*0.5)^(1/3); 
  S = 0.5*Sqrt[-2/3*p + (Q + delta0/Q)/(3*a)];

  x1 = -b/(4*a) - S - 0.5*Sqrt[-4*S^2 - 2*p + q/S]; 
  x2 = -b/(4*a) - S + 0.5*Sqrt[-4*S^2 - 2*p + q/S]; 
  x3 = -b/(4*a) + S - 0.5*Sqrt[-4*S^2 - 2*p - q/S]; 
  x4 = -b/(4*a) + S + 0.5*Sqrt[-4*S^2 - 2*p - q/S];
  {x1, x2, x3, x4}
]

solveQuartic[1, 2, 3, 4, 5] // MatrixForm

此外,我可以使用 Solve[] 来验证我的答案

Solve[{1, 2, 3, 4, 5}.Table[x^i, {i, 4, 0, -1}] == 0, x] // N // TableForm

问题

你在p的计算中有错别字:你写了8^a^2*d,而它应该是8*a^2*d

q = (b^3 - 4*a*b*c + 8*a^2*d)/(8*a^3);

你得到结果

res =

  -1.2878 - 0.8579i
  -1.2878 + 0.8579i
   0.2878 - 1.4161i
   0.2878 + 1.4161i

随心所欲。