Maths Explore

Maths · sample calcsheet · 4 pages

Solve a cubic equation with root()

Finding every real root of a cubic f(x) = a₃x³ + a₂x² + a₁x + a₀. A cubic has at least one real root and at most three. This sheet finds them numerically with the built-in root(f, a, b), which needs only a bracket [a, b] where f changes sign, then confirms them two independent ways: Vieta's relations between the roots and the coefficients, and the trigonometric closed form that applies when all three roots are real. The coefficients are the only inputs — change them and the whole sheet re-solves.

First page of the Solve a cubic equation with root() sample calcsheet in Maths Explore
The first page of the calcsheet as it appears in Maths Explore.

What this calculation covers

Coefficients

  • Cubic coefficient (not zero)
  • Quadratic coefficient
  • Linear coefficient
  • Constant term

The function and where it changes sign

root(f, a, b) returns one root inside the bracket [a, b] and insists that f(a) and f(b) have opposite signs — Brent's method then cannot miss. Tabulating f at a few integers shows where the sign flips: between 0 and 1, between 1 and 2, and between 3 and 4 (not between 2 and 3, where f stays negative). Three sign changes, three real roots, one bracket each.

  • The cubic to solve, f(x) = 0
  • f at x = 0
  • f at x = 1
  • f at x = 2
  • f at x = 3
  • f at x = 4

Numerical roots with root()

The first argument is the function itself (f, not f(x)); the other two are the bracket ends. A bracket may contain only one root — a wide one such as [0, 4] still returns a single root, whichever the iteration lands on.

  • First root, bracketed in [0, 1]
  • Second root, bracketed in [1, 2]
  • Third root, bracketed in [3, 4]

Checks: Vieta's relations

For a₃(x − x₁)(x − x₂)(x − x₃) expanded, the roots' sum, pairwise products and product must equal −a₂/a₃, a₁/a₃ and −a₀/a₃. Three identities the three numerical roots have to satisfy together.

  • Sum of the roots
  • Sum of the pairwise products
  • Product of the roots

The closed form for three real roots

Substituting x = t + x_sh with x_sh = −a₂/(3a₃) removes the square term and leaves the depressed cubic t³ + p·t + q = 0. Its discriminant Δ = (q/2)² + (p/3)³ says how many real roots there are: Δ < 0 means three distinct real roots. In that case every root is a cosine — Viète's trigonometric form — which sidesteps the complex cube roots Cardano's formula would need (the casus irreducibilis).

  • Linear coefficient of the depressed cubic
  • Constant term of the depressed cubic
  • Discriminant of the depressed cubic
  • Three distinct real roots
  • Shift from t back to x
  • Angle in the trigonometric form

Every root as a cosine

  • Root number k (0, 1 or 2) as a cosine
  • Closed-form root for k = 0
  • Closed-form root for k = 1
  • Closed-form root for k = 2

Checks: closed form against root()

  • Matches the third numerical root
  • Matches the second numerical root
  • Matches the first numerical root

Results summary

  • The cubic x³ − 6x² + 9x − 3 has three real roots: 0.4679, 1.6527 and 3.8794. root() found each from a one-unit bracket, Vieta's three relations close exactly (sum 6, pairwise products 9, product 3), and the trigonometric closed form reproduces all three — the angle here is exactly 60°, so the roots are 2 + 2cos 20°, 2 + 2cos 140° and 2 + 2cos 260°. The numerical route is the general one: it needs no formula, only a bracket with a sign change, and works unchanged for any f(x), polynomial or not.

Try changing…

  • Set a₀ := 10 — Δ turns positive and one real root is left, near −0.7: every bracket above now reports "needs a sign change"; move x₁'s to [−1, 0] to catch it.
  • Give x₂ the bracket [2, 3] — f(2) and f(3) are both negative, so root() refuses with "needs a sign change" rather than guess.
  • Double every coefficient — nothing moves: the roots, p, q and Δ all depend only on the coefficients' ratios.

All samples