I have a question about solving quartic equation in MATLAB, please see here
The MATLAB function shown as below:
MATLAB
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
solveQuartic(-65, -312, -582, -488, -153)
(*
-1.400000000000000 - 0.627571632442189i
-1.000000000000000 + 0.000000000000000i
-1.400000000000000 + 0.627571632442189i
-1.000000000000000 + 0.000000000000000i
*)
Obviously, the quartic equation $$-65x^4-312x^3-582x^2-488x-153=0$$ owns two real roots
Now I would like to exact the real roots
of the quartic equation, my trial as below
function [res] = solveQuarticReals(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];
%exact the real roots
j = 0;
for i = 1 : length(res)
if imag(res(i)) == 0
j = j + 1;
mid(j) = res(i);
end
end
res = mid;
end
However, it failed.
QUESTION
- How exact the real root in MATLAB?
- Or is there a built-in like
Chop
in Mathematica?