Contents

Solving Cubic Equations

Robert Eisele

Consider a cubic equation in its standard form:

\[ ax^3 + bx^2 + cx + d = 0 \]

where \(a, b, c, d \in \mathbb{R}\). There are several methods to find the roots of this equation, ranging from numerical algorithms to exact algebraic solutions. According to the Fundamental Theorem of Algebra, a polynomial of degree 3 has exactly three roots \(x_1, x_2, x_3\), which allows us to express the cubic equation as:

\[ (x - x_1)(x - x_2)(x - x_3) = 0 \]

Expanding this product and dividing by \(a\) yields:

\[ x^3 + \underbrace{(-x_1 - x_2 - x_3)}_{= \frac{b}{a}} x^2 + \underbrace{(x_1 x_2 + x_1 x_3 + x_2 x_3)}_{= \frac{c}{a}} x + \underbrace{(-x_1 x_2 x_3)}_{= \frac{d}{a}} = 0 \]

Finding the roots \(x_1, x_2,\) and \(x_3\) given the coefficients \(a, b, c,\) and \(d\) is a non-trivial problem due to the complexity of their relationships.

Cubic Equation Calculator

\[ ax^3 + bx^2 + cx + d = 0 \]

\(a\)\(b\)\(c\)\(d\)
Calculate

First Observations

  1. When \(a = 0\): The cubic equation reduces to a quadratic equation:

\[ bx^2 + cx + d = 0 \]

  1. When \(d = 0\): The equation simplifies to:

\[ ax^3 + bx^2 + cx = 0 \]

Factoring out \(x\), we get:

\[ x(ax^2 + bx + c) = 0 \]

This yields one root at \(x = 0\), and the remaining roots are given by solving the quadratic equation:

\[ ax^2 + bx + c = 0 \]

Now, this idea can be generalized. If we have a polynomial \( p(x) \) of degree \( n \geq 1 \), defined as:

\[ p(x) = a_nx^n + a_{n-1}x^{n-1} + \ldots + a_1x + a_0 \]

and we know a root \(x_0\) such that \( p(x_0) = 0 \), then there always exists another polynomial \( q(x) \) of degree \( n - 1 \) according to the Factor Theorem such that:

\[ p(x) = (x - x_0) \cdot q(x) \]

This means we can find \( q(x) \) using polynomial division / synthetic division, which in our case again is a quadratic. The initial root \(x_0\) can be determined through an educated guess, graphical analysis, or a numerical method such as Newton's Method. This approach is feasible because every cubic equation is guaranteed to have at least one real root.

Newton's Method

Newton's Method provides a recursive formula for refining an initial guess \(x_0\) to approximate a root. The general update rule is given by:

\[ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \]

Don't get confused here by the notation, \(x_n\) in the context of Newton's method is the nth iteration, not the nth root. For a cubic function \( f(x) = ax^3 + bx^2 + cx + d \), the derivative is \(f'(x) = 3ax^2 + 2bx + c\). Substituting \( f(x) \) and \( f'(x)\) into the update formula yields:

\[ x_{n+1} = x_n - \frac{ax_n^3 + bx_n^2 + cx_n + d}{3ax_n^2 + 2bx_n + c} = \frac{2 a x_n^3 + b x_n^2 - d}{3 a x_n^2 + 2 b x_n + c} \]

This iteration converges to a real root when starting with an appropriately chosen initial guess.

Other Numerical Methods

Alternative numerical methods for root finding include:

While numerical methods are practical for approximations, they can suffer from rounding errors, especially in the case of multiple or closely spaced roots. Despite their usefulness, these methods might not be reliable for finding exact solutions. Hence, when precise results are required, an analytical approach to solving cubic equations is preferable.

Derivation of Cardano’s Method

Cardano’s method is an analytical approach for solving cubic equations, reducing the original equation to a simpler form known as the depressed cubic, which is then solved directly.

1. Standard Form of a Cubic Equation

The general cubic equation as before is:

\[ ax^3 + bx^2 + cx + d = 0 \]

To simplify, we first normalize the equation by dividing through by \( a \):

\[ x^3 + \frac{b}{a}x^2 + \frac{c}{a}x + \frac{d}{a} = 0 \]

Introducing new coefficients, we obtain:

\[ x^3 + Bx^2 + Cx + D = 0 \quad \text{where} \quad B = \frac{b}{a}, \; C = \frac{c}{a}, \; D = \frac{d}{a} \]

2. Reducing to a Depressed Cubic

In general, a cubic equation is half-turn symmetric about its inflection point. Typically the inflection point is shifted on the x-axis. The inflection point is exactly that point where the second derivative of the cubic gets zero. Therefore

\[f(x)=x^3 + \frac{b}{a}x^2 + \frac{c}{a}x + \frac{d}{a}\]

\[f'(x)=3x^2+2\frac{b}{a}x+\frac{c}{a}\]

\[f''(x)=6x+2\frac{b}{a}\overset{!}{=}0\]

Solving for \(x\) leads to \(6x+2\frac{b}{a} = 0\Leftrightarrow x = -\frac{b}{3a} = - \frac{B}{3}\).

To eliminate the quadratic term \( Bx^2 \), we use the inflection point and perform the substitution:

\[ x = t - \frac{b}{3a} \]

Substituting this into the equation yields a cubic in \(t\):

\[ \left(t - \frac{b}{3a}\right)^3+\frac{b}{a}\left(t - \frac{b}{3a}\right)^2 + \frac{c}{a}\left(t - \frac{b}{3a}\right)+\frac{d}{a} = 0 \]

After expanding and simplifying, the equation reduces to the depressed cubic form:

\[ \begin{array}{rrl} &\left(t - \frac{b}{3a}\right)^3+\frac{b}{a}\left(t - \frac{b}{3a}\right)^2 + \frac{c}{a}\left(t - \frac{b}{3a}\right)+\frac{d}{a} = 0\\ \Leftrightarrow&t^3+\left(\frac{c}{a}-\frac{b^2}{3a^2}\right)t+\left(\frac{2b^3}{27a^3}-\frac{bc}{3a^2}+\frac{d}{a}\right) = 0\\ \Leftrightarrow&t^3 + pt+q=0 \end{array} \]

where:

\[ p = \frac{3ac-b^2}{3a^2}= C - \frac{B^2}{3} \]

\[ q = \frac{27 a^2 d - 9 a b c + 2 b^3}{27 a^3} = \frac{2B^3}{27} - \frac{BC}{3}+D \]

This transformation moves the inflection point onto the y-axis and simplifies the original cubic equation by removing the quadratic term, making it easier to solve using Cardano's formula. Besides this, we also have \(p\) and \(q\) interpretable as the y-intercept and slope at the inflection point:

3. Calculating the Roots of the Depressed Cubic

To solve the depressed cubic equation

\[ t^3 + pt + q = 0, \]

we start with a well known cubic identity and re-write it a bit:

\[\begin{array}{rl} (u+v)^3 &= u^3+3u^2v+3uv^2+v^3\\ &= 3uv(u+v)+u^3+v^3 \end{array}\]

Which when solved for zero and written next to the depressed cubic allows some substitions:

\[\begin{array}{rl} {\underbrace{(u+v)}_{t}}^3 \underbrace{-3uv}_{p}\underbrace{(u+v)}_{t}\underbrace{-(u^3+v^3)}_{q}&=0 \end{array}\]

Therefore we can read of the equivalences

\[\begin{array}{rl} t&= u+v\\ p&=-3uv\\ q&=-(u^3+v^3) \end{array}\]

Which therefore gives

\[\begin{array}{rl} uv&=-\frac{p}{3}\\ u^3+v^3&=-q \end{array}\]

Solving for \( u^3 \) and \( v^3 \)

Next, we solve for \( u^3 \) and \( v^3 \). Using the fact that \( u^3 + v^3 = -q \) and \( uv = -\frac{p}{3} \), we can find \( u^3 \) and \( v^3 \) directly by solving a quadratic equation. When cubing \( uv = -\frac{p}{3} \) we get \( u^3v^3 = -\left(\frac{p}{3}\right)^3 \) and multiplying \( u^3 + v^3 = -q \) with \(v^3\), we get \( u^3v^3 + (v^3)^2+qv^3 =0 \) and since both expressions share \(u^3v^3\), we come up with

\[ \left(v^3\right)^2+qv^3-\left(\frac{p}{3}\right)^3 = 0 \]

The solutions to this quadratic equation are:

\[ u^3, v^3 = -\frac{q}{2} \pm \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3} \]

These expressions ensure that \( u^3 \) and \( v^3 \) satisfy both \( u^3 + v^3 = -q \) and \( uv = -\frac{p}{3} \). We then take the cube roots of \( u^3 \) and \( v^3 \) to obtain \( u \) and \( v \), which give us the roots of the depressed cubic.

\[ t = \sqrt[3]{-\frac{q}{2} + \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3}} + \sqrt[3]{-\frac{q}{2} - \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3}} \]

4. Understanding the Discriminant

The discriminant \(\Delta\) of the depressed cubic equation can be read off as

\[ \Delta = \left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3, \]

and is crucial for understanding the nature of the roots:

Understanding the discriminant helps in choosing appropriate methods to find the roots of the cubic equation and provides insight into the behavior of the function.

5. Efficient Solution to Cubic Equations

When looking again at the solution we found so far

\[ t = \sqrt[3]{-\frac{q}{2} + \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3}} + \sqrt[3]{-\frac{q}{2} - \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3}} ,\]

we recognize that there are also cases where the square root can produce Complex Numbers. When dealing with the square roots directly in the complex space, we see that \(u^3\) and \(v^3\) are complex conjugates of eachother, mirrored on the real axis. When plotting them we get basically something like

To find all 3 roots in \(t\) we rotate \(u\) and \(v\) by 120°, or in complex terms we multiply by the complex number \(\cos\left(\frac{2\pi}{3}\right) + \mathbf{i} \sin\left(\frac{2\pi}{3}\right) \) which is the cube root of unity \(\omega = e^{2\pi i / 3} = -\frac{1}{2} + i\frac{\sqrt{3}}{2}\) with the properties \(\omega^2=\overline{\omega}\) and \(\omega^3 = 1\).

The roots of the cubic equation can then be represented as:

\[\begin{array}{rl} t_1 &= u + v\\ t_2 &= \omega u + \omega^2 v\\ t_3 &= \omega^2 u + \omega v \end{array}\]

This form leverages the rotational symmetry in the complex plane. Here’s why this approach works:

  1. Complex Rotation and Symmetry: The multiplication by \(\omega\) rotates any complex number by \(120^\circ\) counterclockwise, while multiplication by \(\omega^2\) results in a \(240^\circ\) counterclockwise rotation. These rotations correspond to the angles that divide the complex plane into three equal parts, perfectly aligning with the nature of cubic roots.

  2. Pairing Roots with Their Conjugates: Since \(u\) and \(v\) are conjugate pairs when the discriminant \(\Delta\) is negative, each root combination \(t_2\) and \(t_3\) incorporates both \(u\) and \(v\) in a way that their real parts align and imaginary parts cancel out. This ensures that all three roots are real, preserving the symmetry of the cubic function in this case.

  3. General Applicability: This formulation holds regardless of whether the discriminant \(\Delta\) is positive, negative, or zero. It ensures that all three roots—whether real or complex—are derived from the basic components \(u\) and \(v\) through these rotations.

6. Transforming Back to \( x_k \)

After finding the roots \(t_1\), \(t_2\), and \(t_3\) of the depressed cubic equation, we transform them back to the original variable \(x\) using the substitution \( x = t + \frac{B}{3} \):

\[ x_k = t_k - \frac{B}{3}, \quad \text{for } k = 1, 2, 3. \]

This final step ensures the roots \( x_1 \), \( x_2 \), and \( x_3 \) are solutions to the original cubic equation in its standard form:

\[ ax^3 + bx^2 + cx + d = 0. \]

By using the cube roots of unity and transforming back to \( x_k \), Cardano’s method systematically finds all roots, covering all cases uniformly.

Cubic Roots JavaScript Implementation

When implementing Cardano's method in JavaScript using the Complex.js library, we can achieve a concise and effective solution to find the roots of a cubic equation. Below is a sample implementation that leverages the power of complex numbers to handle all cases, including real and complex roots.

function cubicRoots(a,b,c,d) {

    // Make a depressed cubic of the form x^3 + px + q = 0
    let p = (3 * a * c - b * b) / (3 * a * a);
    let q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);

    // Calculate cube roots of complex numbers
    let D = Complex(Math.pow(q / 2, 2) + Math.pow(p / 3, 3));
    let sqrtD = D.sqrt();
    let u = Complex(-q / 2).add(sqrtD).pow(1 / 3);
    let v = Complex(-q / 2).sub(sqrtD).pow(1 / 3);

    // Calculate the roots in t
    let omega = Complex(-0.5, Math.sqrt(3) / 2);  // Principal cube root of unity
    let t1 = u.add(v);
    let t2 = v.mul(omega.conjugate()).add(u.mul(omega));
    let t3 = u.mul(omega.conjugate()).add(v.mul(omega));

    // Transform back to the original variable x
    let shift = -b / (3 * a);
    return [t1.add(shift), t2.add(shift), t3.add(shift)];
}

References