Short of the long of it:
I need a function that gives the exact roots of a cubic function. I was using
this to get exact values, however some tests have shown obvious failures in the algorithm. Given:
f(x) = a x<sup>3</sup> + b x<sup>2</sup> + c x + d = a (x - x<sub>1</sub>) (x - x<sub>2</sub>) (x - x<sub>3</sub>) ~ the algorithm is:
Code:
q = ( (3*a*c) - (b^2) ) / (9 * (a^2) );
r = ( (9*a*b*c) - (27*(a^2)*d ) - 2*(b^3)) / (54*(a^3));
s = (r + sqrt(q^3 + r^2))^(1/3);
t = (r - sqrt(q^3 + r^2))^(1/3);
x1 = s + t - (b / (3*a));
x2 = -(1/2)*(s+t) - (b / (3*a)) + (sqrt(3)/2)*(s-t)*j;
x3 = -(1/2)*(s+t) - (b / (3*a)) - (sqrt(3)/2)*(s-t)*j;
Long of it:
I am modeling data that requires all roots of a cubic function, even imaginary if they occur. I liked the one outlined in the link provided however as it doesn't work with all numbers I throw at it.
I throw
a = 1, b = -2, c = 1, d = 0, I get answers like this:
Code:
x1 = 0.5000 - 0.2887i
x2 = 0.5000 - 0.2887i
x3 = 1.0000 + 0.5774i
The answers should be 0, 1, 1. If you remember your algebra II class, you know that every cubic function with real coefficients will always have one real root. It has worked for almost all other functions I throw at it... Here it doesn't work.
Can someone throw a new exact algorithm at me? One that doesn't use loops to approximate?