Education and Science: Linear, quadratic and cubic equations in one variable.
This text explains the mathematical background needed to solve equations in one variable. It also includes some hints for the case, you intend to
write a computer program (using Free Pascal) to do this for you. Simple command line implementations of programs solving linear, quadratic and cubic
equations in 1 variable may be found in the Free Pascal Console Programs section of this site.
Solving linear equations.
Linear equations (or 1st degree equations) are equations of the form: ax + b = 0 (a, b: real numbers). Three possible cases:
a ≠ 0 | 1 root | x = -b / a |
a = 0 and b ≠ 0 | the equation has no solution | |
a = 0 and b = 0 | the equation has an infinity of solutions |
Solving quadratic equations.
Quadratic equations (or 2nd degree equations) are equations of the form: ax^{2} + bx + c = 0 (a, b, c: real numbers; a ≠ 0). The usual way to solve such equations, is to calculate the so called discriminant:
Δ = b^{2} - 4ac |
The solutions of the equation are then given by: x = [-b ± √Δ] / 2a. This is called the quadratic formula.
It is obvious, that if Δ > 0 there are 2 solutions and only one, if Δ = 0. If Δ < 0, you get the square root of a negative number, which is not defined, if we consider real numbers only. Thus, there are three cases, depending on the sign of the discriminant:
Δ > 0 | 2 distinct roots | x_{1} = (-b + √Δ) / 2a x_{2} = (-b - √Δ) / 2a |
Δ = 0 | 1 (double) root | x = -b / 2a |
Δ < 0 | the equation has no real roots |
If we now consider complex numbers and knowing that i is defined such as i^{2} = -1, we can write for the case, where Δ < 0:
√Δ = √[-1·(-Δ)] = √[i^{2}·(-Δ)] = √(i^{2}) · √(-Δ) = √(-Δ)i (with -Δ > 0 and √(-Δ) well existing).
Thus, the solutions of the quadratic equation are finally given by:
Δ > 0 | 2 distinct real roots | x_{1} = (-b + √Δ) / 2a x_{2} = (-b - √Δ) / 2a |
Δ = 0 | 1 (double) real root | x = -b / 2a |
Δ < 0 | 2 conjugate complex roots | x_{1} = (-b / 2a) + [√(-Δ) / 2a]i x_{2} = (-b / 2a) - [√(-Δ) / 2a]i |
Notes:
- If in the quadratic equation a = 0, you get the case of a linear equation.
- Complex numbers are a mathematical extension of our every-day real numbers, defined by z = a + bi, a being called the real part and b the imaginary part of the complex. The number i, as said above, is defined by i = √(-1).
Solving cubic equations.
Cubic equations (or 3rd degree equations) are equations of the form: ax^{3} + bx^{2} + cx + d = 0 (a, b, c, d: real numbers; a ≠ 0).
In practice, it's rather rare that there is a need to solve cubic equations. In school, exercises are mostly special cases, such as d being 0. The equation may then be written as
x[ax^{2} + bx + c] = 0.
This product is equal to 0, if either of its operands is 0, what gives- the first root being 0.
- the two others being determined by solving the quadratic equation ax^{2} + bx + c = 0, getting 2 (or 1 double) real resp. 2 conjugate complex roots.
Just a special case, but the type of the roots will be similar for all 3rd degree equations:
1. All cubic equations have (at least) 1 real root. |
2. The other 2 roots are either both real (eventually equal and possibly also equal to the first root), or both complex (conjugates). |
There are several more or less suitable methods to solve cubic equations. The one described here is based on the cubic formula (Cardano's formula), that allows a similar approach as the one we saw for quadratic equations: determination of a discriminant and, continuation of the calculations, depending on the discriminant's sign.
Finding an equivalent quadratic equation.
After a lots of trials and considerable algebraic labor, the mathematicians found a way to transform the cubic equation into a quadratic one and solving this one using the discriminant method. Here what you need to do the calculations manually or by program, algebraic details being mostly omitted.
Setting
x = y - b/3a |
it is possible to reduce the cubic equation ax^{3} + bx^{2} + cx + d = 0 to this simpler one: y^{3} + py + q = 0
where
p = -b^{2}/3a^{2} + c/a | |
q = 2b^{3}/27a^{3} - bc/3a^{2} + d/a |
Now setting
y = z - p/3z |
it is possible to obtain the equation z^{6} + qz^{3} - p^{3}/27
And this equation is a quadratic equation in z^{3}!
Cubic formula and discriminant.
The equation in z may be solved using the quadratic formula shown above.
z^{3} = -q/2 ± √Δ |
The discriminant Δ being given by:
Δ = q^{2}/4 + p^{3}/27 |
For the roots z, we get:
z = ³√[-q/2 ± √Δ] |
Together with y = z - p/3z and x = y - b/3a, this is called the cubic formula.
Of course, when solving the equation in z, you will get 6 roots. However, when you substitute these in the equation for y, at most three different y values will result, and you will get at most three distinct roots for x.
Determining the roots when Δ ≥ 0.
Obviously, a distinction must be made depending on Δ ≥ 0 or Δ < 0.
For Δ ≥ 0, you can select one of the two real square roots ±√Δ (for example √Δ) and then determine the 3 cubic roots for z^{3} = -q/2 + √Δ. The first of these roots z_{0} is the real cube root, the 2 others are given by z_{1} = ωz_{0} and z_{2} = ω^{2}z_{0}, where ω and ω^{2} are the complex cube roots of 1:
ω = -1/2 + [√3/2]i and ω^{2} = -1/2 - [√3/2]i |
Knowing the 3 values of z, you can calculate those of y and finally the 3 roots x of the original cubic equation.
In the case where Δ = 0, the second and third roots are 2 equal real numbers (that may or may not be equal to the first root). Thus: for Δ > 0, a cubic equation has 1 real and 2 conjugate complex roots; for Δ = 0, it has 3 real roots, 2 at least being equal.
Determining the roots when Δ < 0.
When Δ < 0, then √Δ is imaginary, and you have to find the cube roots z of the complex number -q/2 + [√(-Δ)]i.
Without giving the details, concerning the algebraic calculations to do, here what you need, to be able to calculate the roots. Setting
r = √(|p|/3) | |
θ = arccos[3√3·q / [2p√(-p))]] |
you'll get as solution for the equation in y (mentionned above and allowing to calcuate the 3 roots x of the original equation):
y = [r - (p/3r)]·cosψ |
where ψ = θ/3, θ/3 + 120°, θ/3 + 240°.
Roots of a cubic equation (summary).
Δ > 0 | 1 real root and 2 conjugate complex roots |
Δ = 0 | 3 real roots (at least 2 being equal) |
Δ < 0 | 3 distinct real roots |
In the calculations described, there is however a special case to consider. In order to find a quadratic equation, we have set y = z - p/3z. Thus, if it turns out that z = 0, it will not be possible to calculate y (and x)! This case must be considered apart (think of it, if you intend to write a computer program to solve cubic equations); calculations are easy: if z = 0, then you have Δ = (q/2)^{2}, hence p = 0. The equation to solve will be y^{3} + q = 0, giving 3 equal real roots y = ³√(-q).
Note:
If in the equation ax^{3} + bx^{2} + cx + d = 0, a = 0, then you get a quadratic equation (if b ≠ 0), or a linear equation (if b = 0).
Linear, quadratic and cubic functions.
If we consider the mathematical functions y = ax + b, y = ax^{2} + bx + c and y = ax^{3} + bx^{2} + cx + d and plot them on a graph, than the roots of the linear, quadratic and cubic equations described are the x value of the intersection points of the curve and the x-axis (the values, for those the curve cuts or "touches" the x-axis).
- y = ax + b is the equation of a line. Three possibilities: 1. the line and the x-axis have 1 intersection point (the line cuts the x-axis once); 2. the line and the x-axis have no intersection point (the line is parallel to and distinct from the x-axis; y = b, with b ≠ 0); 3. the line and the x-axis have an infinity of intersection points (they are parallel and equal; y = 0).
- y = ax^{2} + bx + c is the equation of a parabola. Three possibilities: 1. the parabola and the x-axis have 2 intersection points (the parabola cuts the x-axis 2 times); 2. the parabola and the x-axis have 1 intersection point (the parabola "touches" the x-axis at one single point); 3. the parabola and the x-axis have no intersection point (the curve is entirely located above or below the x-axis).
- y = ax^{3} + bx^{2} + cx + d is a cubic function, which are more complex. But again there are 3 possibilities, as show us the resolution of cubic equations: the curve and the x-axis have 1, 2 or 3 intersection points (the curve cuts/"touches" the x-axis 1, 2 or 3 times (what also means, that a cubic function always cuts/"touches" the x-axis).
Solving equations, using a computer program.
If you have some elementary programming skills, it should not be a big deal to write a computer program to solve linear, quadratic and cubic equations. Just ask the user for the equation coefficients and find the roots, doing the calculations described above. Some hints for Free Pascal users:
- The core of Free Pascal only includes a limited number of mathematical functions. To use functions as "power" or "arccos", you will have to find them elsewhere. Free Pascal includes a standard Math unit; just include it in your "uses" statement and you'll have plenty mathematical functions ready to be used.
- The "Math" unit does not include any functions to handle complex numbers. One possibility would be to write your own functions (for example, declaring a new type "Complex" as a record with two reals, representing the real and imaginary part of the complex). A simpler way is to use the UComplex unit. It contains the code for overloading the basic operators +, -, * and / (thus you can simple write Z1 + Z2, with Z1 and Z2 being of the type "Complex", as declared in that unit), as well as a certain number of complex functions. UComplex is freely downloadable from GitHub.
- The "Math" unit does not include any functions to calculate roots (the square root function "sqrt" is part of the Free Pascal
core) and in order to solve cubic equations, we do need to calculate cubic roots. This may be done, using the "power" function of the "Math" unit; in fact,
we have: ³√x = x^{1/3}. However, this only works with x > 0! The workaround consists in writing a simple cubic root function,
based on "power", but with special handling of the cases x = 0 and x < 0. Considering that ³√0 = 0 and that ³√(-x) = -³√x, here the code of the "cubrt"
function, that I use in my "equcub" command line program:
function Cubrt(X: Real): Real;
var
Root: Real;
begin
if X = 0 then
Root := 0
else if X > 0 then
Root := power(X, 1 / 3)
else
Root := -power(-X, 1 / 3);
Cubrt := Root;
end;
[The cubic formula text on this side is based on a document, that I found on the Internet. Title: Cubic and Quadratic formulas, author: James T. Smith, San Francisco State University]