This Java Script calculator finds the roots of a cubic equation. To operate the calculator, enter the coefficients of the cubic equation and press the Compute button to obtain the solution. On invalid entries, a popup window will display an error message.
Return to Contents
The Java Script source code for this program can be viewed by using the View|Source command of your web browser.
You may use or modify this source code in any way you find useful, provided that you agree that the author has no warranty, obligations or liability. You must determine the suitability of this source code for your use.
Return to Contents
It is well known that a quadratic polynomial with the form:
has the exact solution:x2 + A·x + B = 0
A cubic equation of the form:x = (-A ± (A2 + 4·B)1/2)/2
also has an exact solution. To obtain it, first let:x3 + A·x2 + B·x + C = 0
Then a polynomial discriminant can be defined as:Q = (3·B - A2)/9 R = (9·A·B - 27·C - 2·A3)/54
Now, if D > 0 then one root is real and two are complex conjugates; or if D = 0, all roots are real and at least two are equal. Let,D = Q3 + R2
then the roots are given by:S = (R + D1/2)1/3 T = (R - D1/2)1/3
where j = (-1)1/2. Otherwise, D < 0 and all roots are real and unequal. In this case, let:x1 = -A/3 + (S + T) x2 = -A/3 - (S + T)/2 + j·31/2·(S - T)/2 x3 = -A/3 - (S + T)/2 - j·31/2·(S - T)/2
then the three distinct roots are given by:θ = cos-1(R/(-Q3)1/2)
x1 = 2·(-Q)1/2·cos(θ/3) - A/3 x2 = 2·(-Q)1/2·cos((θ + 2·π)/3) - A/3 x3 = 2·(-Q)1/2·cos((θ + 4·π)/3) - A/3
References
Eric W. Weisstein; Cubic Formula; from MathWorld
Return to Contents