Complete the square to write any quadratic in vertex form and read its lowest or highest point straight off, derive the quadratic formula, count the real zeros from the discriminant, and find the slope with the smallest squared error for the course’s data, all without calculus.
Complete the square to rewrite a quadratic in vertex form, showing every step.
Read the vertex form as the graph of x squared slid sideways and up or down, and say whether the vertex is the lowest or the highest point.
Derive the quadratic formula from the completed square, and count the real zeros from the sign of the discriminant.
Find the slope with the smallest squared error for the course’s data by locating the vertex of a parabola.
In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.
The question
Training a model means finding the settings with the smallest loss. Hold the course’s intercept at 0.5, and the mean squared error of the line \({\hat{y} = wx + 0.5}\) on the four data points is
Which slope \({w}\) makes it smallest? Calculus can answer that, but for this curve plain algebra finds the exact answer.
From the last lesson
1. What is the slope of the line through \({(1, 2)}\) and \({(4, 6)}\)?
Show answer
Rise over run: \({\frac{6 - 2}{4 - 1} = \frac{4}{3}}\).
2. Where do \({y = 2x + 1}\) and \({y = -x + 7}\) cross?
Show answer
Set the outputs equal: \({2x + 1 = -x + 7}\), so \({3x = 6}\) and \({x = 2}\). Then \({y = 5}\): the point \({(2, 5)}\).
Intuition
The graph of \({x^2}\) is the U of lesson M01.3.1, with its lowest point at the origin. Every quadratic, a function \({ax^2 + bx + c}\), has a graph that is the same U, slid sideways, slid up or down, and perhaps stretched or turned upside down. That graph is called a parabola.
M01.3.3-F1
The parabola \({x^2 - 4x + 3}\) is the U of \({x^2}\) slid 2 to the right and 1 down. Written as \({(x - 2)^2 - 1}\), the slide can be read straight off the formula.
Take \({x^2 - 4x + 3}\). Written another way it is \({(x - 2)^2 - 1}\), and that form tells the whole story. The square \({(x - 2)^2}\) is never negative, and it is 0 only when \({x = 2}\). So the output is never below \({-1}\), and it equals \({-1}\) only at \({x = 2}\). The lowest point, the vertex, is \({(2, -1)}\). The outputs that equal 0, at \({x = 1}\) and \({x = 3}\), are the zeros: where the graph meets the \({x}\)-axis. They sit the same distance either side of the vertex.
This is how a loss is minimised without calculus: rewrite it so that a square shows, and read off where the square is 0. The rewriting has a name, completing the square.
Build it up
Step 1. Slide right. The graph of \({(x - h)^2}\) is the U moved \({h}\) to the right. Whatever \({x^2}\) gave at \({0}\), \({(x - h)^2}\) gives at \({h}\), so the vertex moves to \({(h, 0)}\).
Step 2. Slide up. Adding \({k}\) lifts every output by \({k}\): \({(x - h)^2 + k}\) has its vertex at \({(h, k)}\).
Step 3. Stretch or flip. Multiplying by \({a}\) scales every height. If \({a > 1}\) the U is narrower, if \({a}\) is between 0 and 1 it is wider, and if \({a < 0}\) it turns upside down, so that the vertex is the highest point. Together:
\[
y = a(x - h)^2 + k .
\]
Step 4. Make a square. To reach that form from \({ax^2 + bx + c}\) we need one trick. The expression \({x^2 + 6x}\) is a square of side \({x}\) and two strips of \({3x}\), one on each side. Add the missing corner, \({3 \times 3 = 9}\), and it becomes a whole square of side \({x + 3}\).
M01.3.3-F2
Completing the square, as a picture. Split \({6x}\) into two strips of \({3x}\), and the square of half of 6 fills the gap.
In general, half of \({p}\), squared, completes \({x^2 + px}\). Expanding with the rule \({(u + v)^2} = {u^2 + 2uv + v^2}\) of lesson M01.1.2 checks it:
A quadratic function is \({f(x) = ax^2 + bx + c}\) with \({a \neq 0}\). Its graph is a parabola. A zero of \({f}\) is an input \({x}\) with \({f(x) = 0}\). The vertex is the lowest point of the graph if \({a > 0}\), and the highest point if \({a < 0}\).
Result 1: completing the square. Every step, for any \({a \neq 0}\). First take \({a}\) out of the two terms that contain \({x}\):
\[
f(x) = a\Big(x^2 + \tfrac{b}{a}x\Big) + c .
\]
Half of \({\frac{b}{a}}\) is \({\frac{b}{2a}}\), and its square is \({\frac{b^2}{4a^2}}\). Add it and take it away inside the bracket, which changes nothing:
\[
h = -\frac{b}{2a}, \qquad k = c - \frac{b^2}{4a} .
\]
Result 2: the vertex. The square \({(x - h)^2}\) is never negative and is 0 only at \({x = h}\). If \({a > 0}\), then \({a(x - h)^2 \geq 0}\), so \({f(x) \geq k}\), with equality only at \({x = h}\): the lowest point is \({(h, k)}\). If \({a < 0}\), then \({a(x - h)^2 \leq 0}\) and \({(h, k)}\) is the highest point. Putting \({x = h}\) into \({f}\) gives \({f(h) = k}\), which is often the quickest way to find \({k}\).
Result 3: the quadratic formula. A zero solves \({a(x - h)^2 + k = 0}\). Take \({k}\) from both sides and divide by \({a}\):
The second line puts \({k = c - \frac{b^2}{4a}}\) in; the third writes both terms over \({4a^2}\). Call the top \({D = b^2 - 4ac}\), the discriminant. The bottom, \({4a^2}\), is positive.
If \({D > 0}\), \({x - h}\) is a square root of \({\frac{D}{4a^2}}\), so \({x - h = \pm\frac{\sqrt{D}}{2a}}\). (The square root of \({4a^2}\) is \({2a}\) or \({-2a}\), whichever is positive; with \({\pm}\) in front, both give the same pair of numbers.) Since \({h = -\frac{b}{2a}}\), the two zeros are
\[
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} .
\]
If \({D = 0}\), then \({(x - h)^2 = 0}\), so \({x = h}\): one zero, where the vertex touches the axis.
If \({D < 0}\), the right-hand side is negative, and no square is negative: there is no real zero. Lesson M01.9.4 returns to this case with new numbers.
M01.3.3-F3
The discriminant counts the real zeros. Raising \({c}\) lifts the U: it crosses the axis twice, then touches it, then misses it.
Go deeper: the sum and the product of the zeros
When \({D \geq 0}\) the zeros are \({h + \frac{\sqrt{D}}{2a}}\) and \({h - \frac{\sqrt{D}}{2a}}\). Their average is \({h}\): the zeros sit symmetrically about the vertex. Adding them gives \({2h = -\frac{b}{a}}\). Multiplying them, with \({(u + v)(u - v)} = {u^2 - v^2}\) and \({h^2 = \frac{b^2}{4a^2}}\), gives
The vertex is \({(2, -1)}\), the lowest point because \({a = 1 > 0}\). The formulas agree: \({h = -\frac{-4}{2} = 2}\) and \({k = 3 - \frac{16}{4} = -1}\). The zeros solve \({(x - 2)^2 = 1}\), so \({x - 2 = 1}\) or \({x - 2 = -1}\): \({x = 3}\) or \({x = 1}\). The quadratic formula agrees too: \({D = 16 - 12 = 4}\), and \({x = \frac{4 \pm 2}{2}}\), which is 3 or 1.
Part 2: the course’s loss. With the intercept held at 0.5, the line predicts \({wx_i + 0.5}\), and the loss is the mean of the four squared misses. Four times the loss is their sum:
Each miss is \({wx_i + 0.5 - y_i}\); for the first point, \({w + 0.5 - 2 = w - 1.5}\). Expand each square with \({(u - v)^2} = {u^2 - 2uv + v^2}\) and add the columns:
square
\({w^2}\)
\({w}\)
number
\({(w - 1.5)^2}\)
1
\({-3}\)
2.25
\({(2w - 2.5)^2}\)
4
\({-10}\)
6.25
\({(3w - 4.5)^2}\)
9
\({-27}\)
20.25
\({(4w - 5.5)^2}\)
16
\({-44}\)
30.25
sum
30
−84
59
So \({4L(w)} = {30w^2 - 84w + 59}\), and a quarter of that is \({L(w)} = {7.5w^2 - 21w + 14.75}\). Now the vertex:
So \({L(w)} = {7.5(w - 1.4)^2 + 0.05}\). The best slope is \({w = 1.4}\), with loss \({0.05}\): the course’s line \({\hat{y} = 1.4x + 0.5}\), found by algebra alone.
M01.3.3-F4
Each slope is a line on the left and a point on the right. The loss is a parabola in \({w}\), and its vertex, at \({w = 1.4}\), is the best slope.
The parabola has no zero, because its discriminant is negative:
That is no mistake. A loss of 0 would mean a line through all four points, and none exists.
Code
The vertex and the zeros, straight from Results 1 and 3. math.sqrt takes the square root; the if lines handle the three cases of the discriminant, and [] is an empty list.
import mathdef vertex(a, b, c): h =-b / (2* a) k = c - b * b / (4* a)return h, kdef zeros(a, b, c):# the discriminant d = b * b -4* a * cif d <0:# no real zeroreturn []if d ==0:# one zero: the vertexreturn [-b / (2* a)] root = math.sqrt(d) x1 = (-b - root) / (2* a) x2 = (-b + root) / (2* a)return [x1, x2]print(vertex(1, -4, 3))print(zeros(1, -4, 3))h, k = vertex(7.5, -21, 14.75)print(round(h, 4), round(k, 4))print(zeros(7.5, -21, 14.75))
(2.0, -1.0)
[1.0, 3.0]
1.4 0.05
[]
The rounding only tidies the display: without it, k shows a tiny error in its last digits, because 14.7 has no exact binary form. Next, the loss computed straight from the data, beside the formula from the worked example. zip pairs each input with its target.
x = [1, 2, 3, 4]y = [2, 3, 5, 6]def loss(w, b=0.5): total =0for xi, yi inzip(x, y): miss = w * xi + b - yi total += miss **2# the mean squared missreturn total /len(x)def formula(w):return7.5* w * w -21* w +14.75for w in [1.0, 1.4, 1.8]: direct =round(loss(w), 4) by_formula =round(formula(w), 4)print(w, direct, by_formula)
1.0 1.25 1.25
1.4 0.05 0.05
1.8 1.25 1.25
NumPy finds the zeros of a polynomial from its coefficients, highest power first.
import numpy as npprint(np.roots([1, -4, 3]))print(np.roots([7.5, -21, 14.75]))
[3. 1.]
[1.4+0.08164966j 1.4-0.08164966j]
The first answer is the pair of zeros, 3 and 1. The second is printed with the letter j: those are not real numbers but the complex numbers of module M01.9, NumPy’s way of reporting that the loss has no real zero.
Explore
The function machine of this module is in lesson M01.3.1. Choose the rule \({x^2}\) and press Play: the U builds up point by point, and the outputs for \({-2}\) and \({2}\) land at the same height, as they must for a curve that is symmetric about its vertex.
Pitfalls
M01.3.3-F5
Wrong beside right. The minus sign in \({(x - 2)^2}\) slides the U to the right.
“\({(x - 2)^2 - 1}\) has its vertex at \({x = -2}\).”
The vertex is where the bracket is 0, and \({x - 2 = 0}\) at \({x = 2}\). A minus inside the bracket slides the graph right; a plus slides it left. Test the claim by putting \({x = -2}\) in: the output is \({(-4)^2 - 1 = 15}\), nowhere near the lowest value \({-1}\).
“Complete the square on \({2x^2 - 8x + 6}\) by halving \({-8}\).”
The trick works on \({x^2 + px}\), so take the 2 out first: \({2(x^2 - 4x) + 6}\). Now halve \({-4}\):
The vertex is \({(2, -2)}\), as \({h = \frac{8}{4} = 2}\) and \({k = 6 - \frac{64}{8} = -2}\) confirm.
“Only \({\sqrt{b^2 - 4ac}}\) is divided by \({2a}\).”
The whole top, \({-b \pm \sqrt{b^2 - 4ac}}\), is divided by \({2a}\). For \({x^2 - 4x + 3}\) the right reading gives \({\frac{4 \pm 2}{2}}\), that is 3 and 1. Dividing only the root gives \({4 \pm 1}\), that is 5 and 3, and 5 is not a zero: \({25 - 20 + 3 = 8}\).
Check yourself
Recall
1. Give the vertex of \({y = -2(x - 1)^2 + 5}\). Is it the lowest or the highest point?
Show answer
\({(1, 5)}\). It is the highest point, because \({a = -2}\) is negative and the U is upside down.
2. What number completes the square for \({x^2 + 10x}\)?
Show answer
Half of 10, squared: \({25}\). Then \({x^2 + 10x + 25} = {(x + 5)^2}\).
3. State the quadratic formula for the zeros of \({ax^2 + bx + c}\).
The vertex is \({(-3, -4)}\). The zeros solve \({(x + 3)^2 = 4}\), so \({x + 3 = 2}\) or \({x + 3 = -2}\): \({x = -1}\) or \({x = -5}\). Check: \({1 - 6 + 5 = 0}\) and \({25 - 30 + 5 = 0}\).
2.★★ For \({f(x) = -x^2 + 4x + 5}\), find the vertex and say what kind of point it is, then find the zeros with the quadratic formula.
Show solution
Here \({a = -1}\), \({b = 4}\), \({c = 5}\). \({h = -\frac{4}{2 \times (-1)} = 2}\) and \({k = f(2) = -4 + 8 + 5} = {9}\). Since \({a < 0}\), \({(2, 9)}\) is the highest point. The discriminant is \({16 - 4 \times (-1) \times 5} = {36}\), so
\[
x = \frac{-4 \pm 6}{-2} ,
\]
which gives \({\frac{2}{-2} = -1}\) and \({\frac{-10}{-2} = 5}\). Check: \({-1 - 4 + 5 = 0}\) and \({-25 + 20 + 5 = 0}\). The zeros sit 3 either side of \({h = 2}\).
3.★★★ Now hold the slope at \({w = 1.4}\) and let the intercept \({b}\) vary. Show that the course’s loss is \({L(b)} = {(b - 0.5)^2 + 0.05}\), and give the best intercept.
Show solution
The misses \({1.4x_i + b - y_i}\) are \({b - 0.6}\), \({b - 0.2}\), \({b - 0.8}\) and \({b - 0.4}\). Each square is \({b^2}\), minus twice the number times \({b}\), plus the number squared. The four numbers and their squares add up to
So the four squares add up to \({4b^2 - 4b + 1.2}\), and a quarter of that is \({b^2 - b + 0.3}\). Half of \({-1}\) is \({-0.5}\), and \({0.5^2 = 0.25}\), so
The best intercept is \({b = 0.5}\), with loss 0.05. Both knobs of the course’s line are at the bottom of their parabolas.
Summary card
A quadratic is the U of \({x^2}\), slid and stretched; completing the square shows where.
M01.3.3-F1
Slide the U to its vertex.
Vertex form.\({ax^2 + bx + c} = {a(x - h)^2 + k}\), with \({h = -\frac{b}{2a}}\) and \({k = f(h)}\).
Vertex. The lowest point if \({a > 0}\), the highest if \({a < 0}\).
Zeros.\({x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}}\): two, one or none as \({b^2 - 4ac}\) is positive, zero or negative.
The course’s loss.\({L(w)} = {7.5(w - 1.4)^2 + 0.05}\): best slope 1.4, loss 0.05.
Sources and next steps
Sources. Each was opened and checked against this lesson on 12 November 2026.
Jay Abramson and others, Algebra and Trigonometry 2e, OpenStax. Section 2.5: completing the square by adding the square of half the \({x}\) coefficient once the leading coefficient is 1, the quadratic formula derived by completing the square, and the discriminant table (zero gives one double solution, negative gives two complex solutions, so no real one). Section 5.1: the vertex form \({a(x - h)^2 + k}\) with \({h = -\frac{b}{2a}}\) and \({k = f(h)}\), a minimum when \({a > 0}\) and a maximum when \({a < 0}\), the graph as a shift of \({y = x^2}\), and zeros as \({x}\)-intercepts.