# The Cubic Formula
The cubic formula is the formula for computing the three roots of any cubic from its coefficients. It is rather complicated which is why it is useful to break it down into several steps described below.
For any general form cubic where $ax^3 + bx^2 + cx + d=0$:
$
\begin{align}
x &= \sqrt[3] {\frac {-b^3} {27a^3} + \frac {bc} {6a^2} - \frac d {2a} + \sqrt {\left( \frac {-b^3} {27a^3} + \frac {bc} {6a^2} - \frac d {2a} \right)^2 + \left( \frac c {3a} - \frac {b^2} {9a^2} \right)^3}} \\
&+ \sqrt[3] {\frac {-b^3} {27a^3} + \frac {bc} {6a^2} - \frac d {2a} - \sqrt {\left( \frac {-b^3} {27a^3} + \frac {bc} {6a^2} - \frac d {2a} \right)^2 + \left( \frac c {3a} - \frac {b^2} {9a^2} \right)^3}} \\
&- \frac {b} {3a}
\end{align}
$
Notice that as per [[The Fundamental Theorem of Algebra]], the cubic has three solutions. The formula above yields all three, when you consider all the possible complex roots (any complex number has $n$ $n$-th roots).
Deriving this formula leads to an algorithm for applying it that makes it easier to work with.
## Deriving the Cubic Formula
Notice that we can rewrite the perfect cube (as per the [[Binomial Theorem]]) as follows:
$
\begin{align}
(w+z)^3 &= w^3 + 3w^2z+3wz^2+z^3 \\
(w+z)^3 &= w^3+z^3+3wz(w+z) \\
(w+z)^3 &-3wz(w+z) - (w^3+z^3) = 0
\end{align}
$
This basically tells us that we can solve any $x^3 + cx+ d$ cubic if we can come up with two numbers $w$ and $z$ such that $c = -3wz$ and $d = - (w^3 + z^3)$. The solution would then be the sum of $w$ and $z$. ^co8yut
However, we're aiming to solve any cubic, and not just the so-called depressed (second coefficient is 0) monic (first coefficient is 1) cubic. Luckily, we can divide by $a$ to actually solve a different cubic that has the same roots, and if shift the whole cubic by the mean of its roots, we will turn it into a depressed cubic.
### Step 1: Depressing The Cubic
We can depress any polynomial by shifting its graph by the mean of its roots. As per [[Vieta's Formulas]], the mean is known: $\frac {-b} {3a}$.
When we solve new cubic, we can get the solutions of the original one by undoing the shift (which explains the last term of the cubic formula). We shall construct the new polynomial as follows:
$
\begin{align}
y_1 &= a \left(x-\frac b {3a} \right)^3 + b\left(x-\frac b {3a} \right)^2 + c\left(x-\frac b {3a} \right)+d\\
y_1&= a\left( x^3 -3x^2\frac b {3a} + 3x \left( \frac b {3a} \right)^2 - \left ( \frac {b} {3a} \right)^3 \right) \\
&+ b\left( x^2 - 2x\frac b {3a} + \left ( \frac b {3a} \right)^2 \right) \\
&+ cx - \frac {bc} {3a} + d \\
y_1 &= ax^3 - x^2b + x\frac {b^2} {3a} - \frac {b^3} {27a^2} \\
&+ bx^2 -2x \frac {b^2} {3a} + \frac {b^3} {9a^2} \\
&+ cx - \frac {bc} {3a} + d \\
y_1 &= ax^3 + (-b+b)x^2 + \left(\frac {b^2} {3a} - \frac {2b^2} {3a} + c \right)x - \frac {b^3} {27a^2} + \frac {b^3} {9a^2} - \frac {bc} {3a} + d \\
y_1 &= ax^3 + \left( c -\frac {b^2} {3a} \right)x+\frac {2b^3} {27a^2} - \frac {bc} {3a} + d
\end{align}
$
$y_1$ is the depressed version of $y$ that is shifted by the mean of $y$ roots. We further construct a monic version of $y_1$ by dividing all coefficients by $a$. This will change the polynomial but the resulting polynomial will have the same roots.
$y_2 = x^3 + \left( \frac c a -\frac {b^2} {3a^2} \right)x+\frac {2b^3} {27a^3} - \frac {bc} {3a^2} + \frac d a$
These new coefficients are commonly referred to as $p$ and $q$.
$p =\frac c a -\frac {b^2} {3a^2}$
$q = \frac {2b^3} {27a^3} - \frac {bc} {3a^2} + \frac d a$
### Step 2: Solve the Depressed Monic Cubic
Our analysis of the perfect cube formula led us to the following conclusion: ![[The Cubic Formula#^co8yut]]
The equations above we now substitute $p$ and $q$:
$
\begin{gather}
p = -3wz \\
q = -(w^3+z^3)
\end{gather}
$
We can manipulate these a bit to get the following:
$
\begin{gather}
p^3 = -27w^3z^3 \\
w^3z^3 = -\frac {p^3} {27} \\
w^3 + z^3 = -q
\end{gather}
$
^9l97u6
The last two equalities are telling us that we're looking for two numbers $w^3$ and $z^3$, given their sub and their product. This is a [[Quadratics|quadratic equation]] in disguise, whose solutions are precisely $w^3$ and $z^3$.
### Step 3: Computing the Actual Solutions
The last step left us with $w^3$ and $z^3$. The solutions of the cubic we're looking for are $w+z$ (plus the offset from step 1). The three complex third roots of $w^3$ and $z^3$ (if dealing with a negative discriminant), will yield the three solutions.
We can easily compute the third roots by converting any complex numbers into polar form and then following the rule that when complex numbers are multiplied their magnitudes are multiplied and their angles added. This boils down to computing the cube root of the magnitude, dividing the angle by 3, and computing the real part of the complex solution (cosine of angle times magnitude). That will give us $w$, but because $w$ and $z$ are conjugates we can simply multiply the real part by 2 and we're done.
## Example
$x^3-5x^2+2x+8=0$
### Step 1
$p = \frac c a -\frac {b^2} {3a^2} = 2 - \frac {25} 3 = - \frac {19} 3$
$q = \frac {2b^3} {27a^3} - \frac {bc} {3a^2} + \frac d a = -\frac {250} {27} + \frac {10} {3} + 8 = \frac {56} {27}$
New cubic is
$x^3-\frac {19} 3x + \frac {56}{27} = 0$
with an offset of $-\frac 5 3$.
### Step 2
As per ![[The Cubic Formula#^9l97u6]]
to solve for $w^3$ and $z^3$ we need to solve the following quadratic:
$
\begin{gather}
x^2 - qx -\frac {p^3} {27} = 0 \\
x^2 + \frac {56} {27}x + \frac {6859} {729} = 0 \\
m = - \frac {28} {27} \\
w^3, z^3 = -\frac {28} {27} \pm \sqrt {\frac {784}{729} - \frac {6859} {729}} = -\frac {28}{27} \pm \frac 5 3 \sqrt 3 i
\end{gather}
$
### Step 3
We need to compute cubes of $w^3$ and $z^3$. To do that we'll first convert $w^3$ into polar form. The angle can be calculated using $\arctan$:
$\tan \theta=-\frac{\frac 5 3 \sqrt 3}{\frac {28} {27}} = -\frac {45\sqrt 3} {28}$
$
\begin{gather}
w^3 = \frac {19 \sqrt {19}} {27} e^{\left(\pi -\arctan \left(\frac {45\sqrt 3} {28}\right)\right)i} \\
w_1 = \frac 1 3 \sqrt {19} \; e^{\left(\pi -\arctan \left(\frac {45\sqrt 3} {28}\right)\right)\frac 1 3i} = \frac 7 6 + ?i \\
w_2 = \frac 1 3 \sqrt {19} \; e^{\left(3\pi -\arctan \left(\frac {45\sqrt 3} {28}\right)\right)\frac 1 3i} = -\frac 4 3 +?i \\
w_3 = \frac 1 3 \sqrt {19} \; e^{\left(-\pi -\arctan \left(\frac {45\sqrt 3} {28}\right)\right)\frac 1 3i} = \frac 1 6 + ?i
\end{gather}
$
$z$ values are conjugates of $w$ values (because our discriminant is negative, we have three real solutions). We can therefore completely ignore the imaginary parts of solutions, as they will cancel out when we add $w$ and $z$.
Consequently, the three solutions of our cubic are:
$
\begin{gather}
x_1 = w_1+ z_1 = 2\cdot \frac 7 6 + \frac 5 3 = 4\\
x_2 = w_2+ z_2 = 2 \cdot - \frac 4 3 + \frac 5 3 = -1 \\
x_3 = w_3+ z_3 = 2\cdot \frac 1 6 + \frac 5 3 = 2\\
\end{gather}
$