TExcelTAD
Home › Algebra › Completing the Square

Completing the Square

Completing the square rewrites x2+bxx^2 + bx as a perfect square minus a correction: x2+6x=(x+3)2−9x^2 + 6x = (x + 3)^2 - 9. 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 (x+h)2=x2+2hx+h2(x + h)^2 = x^2 + 2hx + h^2: if the x-coefficient is bb, then h=b/2h = b/2, and

x2+bx=(x+b2)2−(b2)2x^2 + bx = \left(x + \frac{b}{2}\right)^2 - \left(\frac{b}{2}\right)^2

In words: take half the x-coefficient, square it, add it and subtract it.

x2+6x=(x+3)2−9x2−10x=(x−5)2−25x2+5x=(x+52)2−254\begin{aligned} x^2 + 6x &= (x + 3)^2 - 9 \\ x^2 - 10x &= (x - 5)^2 - 25 \\ x^2 + 5x &= \left(x + \tfrac{5}{2}\right)^2 - \tfrac{25}{4} \end{aligned}

The added term makes a perfect square trinomial (Special Products); subtracting it again keeps the value unchanged.

Solving an equation

x2+6x−7=0x2+6x=7move the constantx2+6x+9=7+9add (6/2)2=9 to BOTH sides(x+3)2=16x+3=±4square root methodx=1 or x=−7\begin{aligned} &x^2 + 6x - 7 = 0 \\ &x^2 + 6x = 7 && \text{move the constant} \\ &x^2 + 6x + 9 = 7 + 9 && \text{add } (6/2)^2 = 9 \text{ to BOTH sides} \\ &(x + 3)^2 = 16 \\ &x + 3 = \pm 4 && \text{square root method} \\ &x = 1 \text{ or } x = -7 \end{aligned}

It works just as well when the roots are irrational:

x2−4x−3=0x2−4x+4=3+4(x−2)2=7x=2±7\begin{aligned} &x^2 - 4x - 3 = 0 \\ &x^2 - 4x + 4 = 3 + 4 \\ &(x - 2)^2 = 7 \\ &x = 2 \pm \sqrt{7} \end{aligned}

When a≠1a \ne 1

Divide by aa first (or factor it out of the x terms):

2x2+8x−5=0x2+4x−52=0divide by 2x2+4x+4=52+4add (4/2)2=4(x+2)2=132x=−2±132≈0.550 or −4.550\begin{aligned} &2x^2 + 8x - 5 = 0 \\ &x^2 + 4x - \tfrac{5}{2} = 0 && \text{divide by 2} \\ &x^2 + 4x + 4 = \tfrac{5}{2} + 4 && \text{add } (4/2)^2 = 4 \\ &(x + 2)^2 = \tfrac{13}{2} \\ &x = -2 \pm \sqrt{\tfrac{13}{2}} \approx 0.550 \text{ or } -4.550 \end{aligned}

Doing these steps with general aa, bb, cc 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:

y=ax2+bx+c=a(x−h)2+k,h=−b2a,k=c−b24ay = ax^2 + bx + c = a(x - h)^2 + k, \qquad h = -\frac{b}{2a}, \quad k = c - \frac{b^2}{4a}

Since (x−h)2≥0(x - h)^2 \ge 0, the value of yy is kk at x=hx = h and moves away from kk everywhere else. So (h,k)(h, k) is the vertex of the parabola: the minimum if a>0a > 0, the maximum if a<0a < 0 (more in Equations of the Parabola).

y=2x2−12x+7=2(x2−6x)+7=2[(x−3)2−9]+7=2(x−3)2−11vertex (3,−11), a minimum\begin{aligned} y &= 2x^2 - 12x + 7 \\ &= 2(x^2 - 6x) + 7 \\ &= 2[(x - 3)^2 - 9] + 7 \\ &= 2(x - 3)^2 - 11 && \text{vertex } (3, -11), \text{ a minimum} \end{aligned}

Tip: When you factor aa out of the x terms, remember that the subtracted square is multiplied by aa too: 2⋅(−9)=−182 \cdot (-9) = -18, and −18+7=−11-18 + 7 = -11.

In Python

A function that returns aa, hh, kk with exact fractions, checked against SymPy:

python · try it yourself
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, (x+3)2−16(x + 3)^2 - 16, is the form we used to solve x2+6x−7=0x^2 + 6x - 7 = 0. The third is a downward parabola with maximum 134\frac{13}{4} at x=32x = \frac{3}{2}.

In software

  • Peak of a jump. A jump with height h(t)=v0t−12gt2h(t) = v_0 t - \frac{1}{2}gt^2 reaches its peak at t=v0/gt = v_0/g, the hh of the vertex form, with height v022g\frac{v_0^2}{2g}, the kk. Platformer games often work backwards: pick the jump height and time, then solve for v0v_0 and gg.
  • Least squares in one variable. Fitting a constant mm to data x1…xnx_1 \ldots x_n by minimizing ∑(xi−m)2\sum (x_i - m)^2 is minimizing a quadratic in mm. Completing the square shows the minimum is at the mean m=xˉm = \bar{x} (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.
python · try it yourself
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 x2−8x+3x^2 - 8x + 3 in the form (x−h)2+k(x - h)^2 + k.

Answer

(x−4)2−13(x - 4)^2 - 13

Solution
x2−8x=(x−4)2−16x2−8x+3=(x−4)2−16+3=(x−4)2−13\begin{aligned} x^2 - 8x &= (x - 4)^2 - 16 \\ x^2 - 8x + 3 &= (x - 4)^2 - 16 + 3 = (x - 4)^2 - 13 \end{aligned}

Exercise 2: Solve

Solve x2+10x+18=0x^2 + 10x + 18 = 0 by completing the square.

Answer

x=−5±7x = -5 \pm \sqrt{7}

Solution
x2+10x=−18x2+10x+25=7(x+5)2=7x=−5±7\begin{aligned} &x^2 + 10x = -18 \\ &x^2 + 10x + 25 = 7 \\ &(x + 5)^2 = 7 \\ &x = -5 \pm \sqrt{7} \end{aligned}

Exercise 3: Odd bb

Solve x2+3x−1=0x^2 + 3x - 1 = 0 by completing the square.

Answer

x=−3±132x = \frac{-3 \pm \sqrt{13}}{2}

Solution
x2+3x+94=1+94(x+32)2=134x+32=±132x=−3±132\begin{aligned} &x^2 + 3x + \tfrac{9}{4} = 1 + \tfrac{9}{4} \\ &\left(x + \tfrac{3}{2}\right)^2 = \tfrac{13}{4} \\ &x + \tfrac{3}{2} = \pm\frac{\sqrt{13}}{2} \\ &x = \frac{-3 \pm \sqrt{13}}{2} \end{aligned}

Exercise 4: Vertex with a≠1a \ne 1

Find the vertex of y=−3x2+12x−5y = -3x^2 + 12x - 5. Is it a maximum or minimum?

Answer

(2,7)(2, 7), a maximum.

Solution
y=−3(x2−4x)−5=−3[(x−2)2−4]−5=−3(x−2)2+12−5=−3(x−2)2+7a=−3<0, so the parabola opens down and (2,7) is the highest point.\begin{aligned} y &= -3(x^2 - 4x) - 5 \\ &= -3[(x - 2)^2 - 4] - 5 \\ &= -3(x - 2)^2 + 12 - 5 \\ &= -3(x - 2)^2 + 7 \\[1ex] &a = -3 < 0, \text{ so the parabola opens down and } (2, 7) \text{ is the highest point.} \end{aligned}
python · try it yourself
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 h(t)=6t−4.5t2h(t) = 6t - 4.5t^2 (metres, seconds). Use vertex form to find the time and height of the peak, and check by sampling hh in code.

Answer

Peak at t=23 st = \frac{2}{3}\ \text{s}, height 2 m2\ \text{m}.

Solution
h=−4.5(t2−43t)=−4.5[(t−23)2−49]=−4.5(t−23)2+2h = -4.5\left(t^2 - \tfrac{4}{3}t\right) = -4.5\left[\left(t - \tfrac{2}{3}\right)^2 - \tfrac{4}{9}\right] = -4.5\left(t - \tfrac{2}{3}\right)^2 + 2
python · try it yourself
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 x2−14xx^2 - 14x?

  1. 14
  2. −7
  3. 196
  4. 49

What is the vertex of y=(x+1)2−6y = (x + 1)^2 - 6?

  1. (1,−6)(1, -6)
  2. (−1,−6)(-1, -6)
  3. (−1,6)(-1, 6)
  4. (6,−1)(6, -1)

Completing the square on ax2+bx+c=0ax^2 + bx + c = 0 with letters gives…

  1. the quadratic formula
  2. the factor theorem
  3. the distributive law
  4. the zero product property