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

拈花ヽ惹草 提交于 2019-12-24 14:00:32

问题


Although the MATLAB owns the built-in solve() to solve the quartic equation, the solve() function gives some error information in some cases. So I write a user-defined function called solveQuartic() to solve the roots of quartic equations.

The reference that I find is here

MY TRIAL

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

TEST

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

However, when I implement the formula in Mathematica as below:

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

In addition, I can use the Solve[] to varify my answer

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

QUESTION

  • Could someone know the reason? THX a lot:)

回答1:


You have a typo in the calculation of p: You write 8^a^2*d, while it should be 8*a^2*d.

With

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

you get the result

res =

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

as desired.



来源:https://stackoverflow.com/questions/32734330/why-matlab-cannot-give-the-correct-result-of-the-quartic-equation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!