Completing the Square
Completing the square rewrites as a perfect square minus a correction: . With it you can solve any quadratic equation, put a quadratic into vertex form to read off its highest or lowest point, and understand where the quadratic formula comes from. It is also the algebra behind finding the minimum of a cost function in one variable.
The key identity
From : if the x-coefficient is , then , and
In words: take half the x-coefficient, square it, add it and subtract it.
The added term makes a perfect square trinomial (Special Products); subtracting it again keeps the value unchanged.
Solving an equation
It works just as well when the roots are irrational:
When
Divide by first (or factor it out of the x terms):
Doing these steps with general , , gives the quadratic formula, line by line; the full derivation is in The Quadratic Formula.
Vertex form
Applied to an expression rather than an equation, completing the square gives the vertex form of a quadratic:
Since , the value of is at and moves away from everywhere else. So is the vertex of the parabola: the minimum if , the maximum if (more in Equations of the Parabola).
Tip: When you factor out of the x terms, remember that the subtracted square is multiplied by too: , and .
In Python
A function that returns , , with exact fractions, checked against SymPy:
from fractions import Fraction as F
import sympy as sp
def vertex_form(a, b, c):
a, b, c = F(a), F(b), F(c)
h = -b / (2 * a)
k = c - b * b / (4 * a)
return a, h, k
for coeffs in [(2, -12, 7), (1, 6, -7), (-1, 3, 1)]:
a, h, k = vertex_form(*coeffs)
print(coeffs, "->", f"{a}(x - ({h}))² + ({k})")
x = sp.symbols("x")
print(sp.expand(2*(x - 3)**2 - 11))(2, -12, 7) -> 2(x - (3))² + (-11)
(1, 6, -7) -> 1(x - (-3))² + (-16)
(-1, 3, 1) -> -1(x - (3/2))² + (13/4)
2*x**2 - 12*x + 7
The second line, , is the form we used to solve . The third is a downward parabola with maximum at .
In software
- Peak of a jump. A jump with height reaches its peak at , the of the vertex form, with height , the . Platformer games often work backwards: pick the jump height and time, then solve for and .
- Least squares in one variable. Fitting a constant to data by minimizing is minimizing a quadratic in . Completing the square shows the minimum is at the mean (linear models). The same reasoning, with matrices, gives the normal equations of linear regression.
- Gaussians. In probability and machine learning, multiplying two normal distributions means adding quadratics in the exponent and completing the square to read off the new mean and variance. Kalman filters (sensor fusion in robots and phones) do this at every step.
from fractions import Fraction as F
data = [4, 7, 5, 10, 9]
n = len(data)
# Σ(xᵢ − m)² = n·m² − 2(Σxᵢ)·m + Σxᵢ² → a = n, b = −2Σx, c = Σx²
a, b, c = n, -2 * sum(data), sum(v * v for v in data)
h = F(-b, 2 * a)
k = F(c) - F(b * b, 4 * a)
print("best constant m =", h, " minimum sum of squares =", k)
print("mean of data =", F(sum(data), n))best constant m = 7 minimum sum of squares = 26
mean of data = 7
Exercises
Exercise 1: Complete the square
Write in the form .
Answer
Solution
Exercise 2: Solve
Solve by completing the square.
Answer
Solution
Exercise 3: Odd
Solve by completing the square.
Answer
Solution
Exercise 4: Vertex with
Find the vertex of . Is it a maximum or minimum?
Answer
, a maximum.
Solution
import sympy as sp
x = sp.symbols("x")
print(sp.expand(-3*(x - 2)**2 + 7))-3*x**2 + 12*x - 5
Exercise 5: Jump design
A game character jumps with (metres, seconds). Use vertex form to find the time and height of the peak, and check by sampling in code.
Answer
Peak at , height .
Solution
best = max((6 * t - 4.5 * t * t, t) for t in [i / 1000 for i in range(1500)])
print(round(best[0], 4), "m at t =", best[1], "s")2.0 m at t = 0.667 s
Test yourself
What number completes the square for ?
- 14
- −7
- 196
- 49
What is the vertex of ?
Completing the square on with letters gives…
- the quadratic formula
- the factor theorem
- the distributive law
- the zero product property