Understanding the cubic equation solver — concept and principle
A cubic equation has the form ax³ + bx² + cx + d = 0 with a ≠ 0. Every cubic with real coefficients has at least one real root, because its graph runs from −∞ to +∞; it has either three real roots or one real root and a pair of complex conjugate roots. Cubics appear in volume problems, equations of state and interpolation curves. This solver returns all three roots in closed form.
How it is calculated
- Divide by a: x³ + Bx² + Cx + D = 0 with B = b/a, C = c/a, D = d/a.
- Substitute x = t − B/3 to remove the squared term, giving the depressed cubic t³ + Pt + Q = 0 with P = C − B²/3 and Q = 2B³/27 − BC/3 + D.
- Compute Δ = Q²/4 + P³/27.
- Δ > 0: one real root. Cardano's formula: u = ∛(−Q/2 + √Δ), v = ∛(−Q/2 − √Δ), t = u + v; the other two roots are complex.
- Δ = 0: repeated roots, t = 2u and −u with u = ∛(−Q/2).
- Δ < 0: three distinct real roots, found with the trigonometric form tₖ = 2√(−P/3)·cos(θ − 2πk/3), k = 0, 1, 2, where θ = ⅓·arccos((3Q/2P)·√(−3/P)).
- Shift back: x = t − B/3. Real roots are sorted from smallest to largest.
The principle
The substitution x = t − B/3 centres the cubic on its point of inflection, which removes the x² term. Cardano's idea is to write t = u + v; then t³ = u³ + v³ + 3uv(u + v), and choosing 3uv = −P turns the cubic into a quadratic in u³. Δ is the discriminant of that quadratic, which is why its sign decides the root pattern. When Δ < 0 Cardano's square root is imaginary even though all roots are real (the "casus irreducibilis"), so the solver uses the identity cos 3θ = 4cos³θ − 3cosθ instead.
Worked example
- Inputs: a = 1, b = −6, c = 11, d = −6.
- B = −6, C = 11, D = −6; shift −B/3 = 2.
- P = 11 − 36/3 = −1 and Q = 2(−216)/27 − (−66)/3 − 6 = −16 + 22 − 6 = 0.
- Δ = 0 + (−1)/27 ≈ −0.037 < 0, so there are three real roots.
- 2√(1/3) ≈ 1.1547 and θ = ⅓·arccos(0) = π/6, so t = 1.1547·cos 30° = 1, 1.1547·cos(−90°) = 0 and 1.1547·cos(−210°) = −1.
- x = t + 2 gives 3, 2, 1. Result: 1, 2, 3. Check: (x − 1)(x − 2)(x − 3) = x³ − 6x² + 11x − 6.
How to read the result
Vieta's formulas let you check any answer: the roots add up to −b/a (here 6), their pairwise products add up to c/a (11) and their product is −d/a (6).
Common mistakes
- Entering coefficients in the wrong order or forgetting a zero coefficient (x³ − 1 is 1, 0, 0, −1).
- Treating a tiny imaginary part from rounding as a genuinely complex root.
Frequently asked questions
Does every cubic have a real root?
Yes, if the coefficients are real. The graph must cross the x-axis at least once.
Can I factor a cubic instead?
If it has a rational root p/q, p divides d and q divides a (rational root theorem). Divide that factor out and solve the remaining quadratic.
Who discovered the cubic formula?
Scipione del Ferro and Niccolò Tartaglia solved it in the early 16th century; Gerolamo Cardano published it in Ars Magna (1545).
Sources: M. Abramowitz and I. A. Stegun, Handbook of Mathematical Functions, NBS Applied Mathematics Series 55, §3.8.2 · W. H. Press et al., Numerical Recipes, Cambridge University Press, §5.6 Quadratic and Cubic Equations