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

为什么MATLAB不能给出四次方程的正确结果?,matlab,equation-solving,Matlab,Equation Solving,虽然MATLAB拥有内置的求解四次方程的solve(),但在某些情况下,solve()函数会提供一些错误信息。因此,我编写了一个名为solveQuartic()的用户定义函数来求解四次方程的根 我找到的参考资料是 我的审判 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 +

虽然MATLAB拥有内置的求解四次方程的
solve()
,但在某些情况下,
solve()
函数会提供一些错误信息。因此,我编写了一个名为
solveQuartic()
的用户定义函数来求解四次方程的根

我找到的参考资料是

我的审判

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
试验


但是,当我在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

问题:

  • 有人知道原因吗?THX很多:)

    • 你在计算
      p
      时有一个输入错误:你写
      8^a^2*d
      ,而它应该是
      8*a^2*d

      你得到结果了吗

      res =
      
        -1.2878 - 0.8579i
        -1.2878 + 0.8579i
         0.2878 - 1.4161i
         0.2878 + 1.4161i
      

      根据需要。

      你在计算
      p
      时有一个输入错误:你写
      8^a^2*d
      ,而它应该是
      8*a^2*d

      你得到结果了吗

      res =
      
        -1.2878 - 0.8579i
        -1.2878 + 0.8579i
         0.2878 - 1.4161i
         0.2878 + 1.4161i
      

      根据需要。

      如果使用MATLAB内置
      solve
      ,您会得到什么?如果使用MATLAB内置
      solve
      ,您会得到什么?亲爱的hbaderts THX很多:-)亲爱的hbaderts THX很多:-)
      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