A quadratic, ax² + bx + c, is the simplest curve with a lowest (or highest) point. That makes it the shape of learning itself: the squared-error loss of a one-weight model is a quadratic bowl, and training means finding its bottom. This lesson finds where a quadratic crosses zero, two ways, and then the exact bottom of the bowl, which for a model is the best weight. You'll also write one Python function that handles every quadratic, including the ones with no roots.
What you'll be able to do
Recognize ax² + bx + c as a bowl or a hill, and find where it crosses zero
Find roots by factoring and by the formula, and tell 2, 1 or 0 roots from the discriminant
Find the lowest point of a bowl at x = −b/(2a), and compute roots and vertices in Python
Find the best weight for a one-weight model as the bottom of its loss bowl
Quick check
Have a guess before you start. Where does y = x² − 4 cross zero?
At x = 2 and x = −2
At x = 2 only
It never crosses zero
At x = 4
Show the answer
At x = 2 and x = −2
x² − 4 = 0 when x² = 4, and both 2 and −2 square to 4. A bowl crosses its zero line on both sides.
The math: the bowl and its bottom
Purpose. The simplest curve with a lowest point.
A quadratic is ax² + bx + c, with a not 0. Its graph is a U-shaped parabola: a bowl when a is positive, a hill when a is negative. Two places on it matter:
the roots, where it crosses zero (y = 0), and
the vertex, its lowest point (or highest, for a hill).
Roots by factoring. A product is zero only when one of its factors is zero. So once a quadratic is factored, its roots appear at sight: x² − 2x − 3 = (x − 3)(x + 1), which is zero when x = 3 or x = −1.
Quick check
From an earlier lesson: factor x² − 2x − 3.
(x − 2)(x − 3)
(x − 3)(x − 1)
(x − 3)(x + 1)
(x + 3)(x − 1)
Show the answer
(x − 3)(x + 1)
Two numbers that multiply to −3 and add to −2: −3 and +1. So (x − 3)(x + 1).
Roots by formula. Not every quadratic factors neatly, but the quadratic formula always works. Its first step, b² − 4ac, is the discriminant: above 0 there are two roots, exactly 0 means one (the bowl just touches zero), and below 0 means none, because the curve never reaches zero. Watch it for x² − 2x − 3.
The picture draws the curve y = x² − 2x − 3 beside the numbers. At each step, look at where it meets the line y = 0: how many times, how far each root sits from the middle, the roots themselves, and last the bottom of the bowl.
Try it now
Then set c to 5 and press Use these numbers: the bowl lifts clear of y = 0, so the discriminant goes negative and there are no roots, but the bowl still has a bottom.
Symbols and formula. The roots and the vertex:
x=2a−b±b2−4acxvertex=−2ab
In plain words: the ± makes two answers, one with + and one with −. The vertex is where the ± part is zero, halfway between the roots, so it's at −b/(2a) even when there are no roots.
To get the lowest value, put the vertex's x back into the quadratic. For x² − 2x − 3, the vertex is at x = 1, and 1 − 2 − 3 = −4.
Quick check
How many roots does x² + 4x + 5 have?
None
Two
One
It depends on x
Show the answer
None
b² − 4ac = 16 − 20 = −4, below 0. There's no real square root of a negative, so the bowl never reaches zero.
Quick check
Where is the vertex of 2x² − 8x + 1?
x = 4
x = 8
x = 2
x = −2
Show the answer
x = 2
x = −b/(2a) = 8/4 = 2. The lowest value there is 2 × 4 − 16 + 1 = −7.
In your own words
In your own words: what does the discriminant tell you, and why?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: roots, and none
Purpose. One function for every quadratic, including the ones with no roots.
The function returns a list of roots, which is empty when there are none:
import math
defroots(a, b, c):
d = b * b - 4 * a * c
if d < 0:
return []
r = math.sqrt(d)
return [(-b - r) / (2 * a), (-b + r) / (2 * a)]
print(roots(1, -2, -3), roots(1, 2, 5), roots(1, -4, 4))
It runs right here in your browser, with nothing to install.
The second has no roots, so the list is empty. The third has a discriminant of 0, so both roots are the same.
Step through the function for two quadratics in a loop. Each time round, q holds one quadratic's three numbers as boxes, and you can watch which return it reaches.
A named mistake: the square root of a negative.math.sqrt of a negative number is an error: Python stops with "math domain error", because no real number squared is negative. That's why the function tests d < 0 first and returns an empty list instead.
Another named mistake: brackets around 2a. The formula divides by the whole of 2a. Written as (-b + r) / 2 * a, Python divides by 2 and then multiplies by a, which is only right when a is 1. That slip hides on easy examples, so test with an a other than 1.
Worked example
Worked example: five quadratics in a loop
Find the roots and the vertex of five quadratics, each stored as a list [a, b, c].
import math
defroots(a, b, c):
d = b * b - 4 * a * c
if d < 0:
return []
r = math.sqrt(d)
return [(-b - r) / (2 * a), (-b + r) / (2 * a)]
defvertex_x(a, b):
return -b / (2 * a)
for q in [[1, -2, -3], [1, -6, 9], [2, 1, -3], [1, 0, 4], [-1, 4, 5]]:
print(q, roots(q[0], q[1], q[2]), vertex_x(q[0], q[1]))
In the lesson you can change this and run it.
Each row is one quadratic. The first has two roots with the vertex halfway. The second has a repeated root at its vertex, x = 3: the bowl just touches zero. x² + 4 has none, so its list is empty, but its vertex is still at 0. The last has a = −1, a hill, so its vertex at x = 2 is the highest point, and the roots come out in the other order.
The brackets make Python work out 2 × a first, then divide by it.
The bowl, drawn. Matplotlib draws the curve from a table of points, with the same lines as in lesson 19. Thirteen inputs from −2 to 4, half a step apart, are enough to see the shape:
import matplotlib.pyplot as plt
xs = [-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
ys = []
for x in xs:
ys.append(x ** 2 - 2 * x - 3)
plt.plot(xs, ys)
plt.xlabel("x")
plt.ylabel("x² − 2x − 3")
plt.show()
The curve comes down, dips below 0, turns at its bottom and climbs back up.
Quick check
Read the graph. Where does the curve cross y = 0?
Only at x = 1, its lowest point
At x = −3 and x = 1
At x = −1 and x = 3
At y = −3, on the y-axis
Show the answer
At x = −1 and x = 3
It crosses 0 at −1 and at 3, the two roots that roots(1, -2, -3) returned. Between them the bowl dips below 0, to −4 at x = 1.
Checking roots by putting them back. A root makes the quadratic zero, so the surest check is to put each root back in. For 2x² + x − 3, the roots −1.5 and 1 give 2 × 2.25 − 1.5 − 3 = 0 and 2 + 1 − 3 = 0. In code, a loop over the list of roots does it for any quadratic, and a value within a hair of zero, abs(value) < 1e-9, counts as zero.
Drill
Fix the roots function
roots(a, b, c) has two slips. Fix them so it returns both roots, or an empty list when there are none.
import math
defroots(a, b, c):
d = b * b - 4 * a * c
r = math.sqrt(abs(d))
return [(-b - r) / 2 * a, (-b + r) / 2 * a]
print(roots(2, 1, -3), roots(1, 2, 5))
In the lesson, your code is checked as you write it.
In your own words
Why test a roots function with an a other than 1?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: the best weight is the vertex
Purpose. For a one-weight model ŷ = w·x, the loss is a quadratic in w, so its bottom, the best weight, comes straight from the vertex formula.
Lesson 15 expanded the squared error for x = 2 and y = 6: L(w) = 4w² − 24w + 36. Its vertex is at w = 24/8 = 3, where the loss is 0. With many data points, the same expansion gives a = Σx² and b = −2Σxy, so:
wbest=−2ab=∑x2∑xy
In plain words: add up x times y over the data, add up x squared, and divide. Σ means "add them all up"; lesson 26 covers it fully.
So the best weight is 2,810 ÷ 1,400 = 2.007…. The picture draws the loss from the table of ten weights in lesson 19, the average squared miss over the three days, for every w, with its exact bottom marked. Press Next to zoom in on the bottom.
Quick check
Lesson 19's table found w = 2.0 the best of ten weights. The vertex gives 2.007…. Why the difference?
The data changed
The table's loss was rounded wrong
The table only tried steps of 0.1
The vertex formula is wrong
Show the answer
The table only tried steps of 0.1
The table tried 1.5, 1.6, …, 2.4. The true bottom, 2.007…, lies between rows, and 2.0 was the closest row. The vertex finds it exactly.
Exact here, searched in general. The vertex formula works because a one-weight squared-error loss is exactly a quadratic. Real models have millions of weights and losses that aren't simple bowls, so there's no formula to jump to the bottom. They walk downhill instead, step by step, which is the gradient descent of lesson 79. Knowing the exact answer for the simple case lets you check that walking downhill lands in the right place.
More data, the same bowl. With three days instead of one, the summed squared error is still a quadratic in w, just with bigger numbers: a = Σx² = 1,400 and b = −2Σxy = −5,620. The picture drew the average instead, the sum divided by 3: every height a third as tall, the bottom in the same place. A steeper bowl than one day's, but still one bottom, where the vertex formula says. That's why the one-weight answer is so tidy: however much data there is, the best weight is one division. Predict it from a and b.
Quick check
What does this print?
a = 1400
b = -5620print(-b / (2 * a))
-2.007142857142857
1400
4.014285714285714
2.007142857142857
Show the answer
2.007142857142857
−b/(2a) = 5,620 ÷ 2,800 = 2.007…, the same best weight Σxy ÷ Σx² gave.
In the real tools. In PyTorch, the library most models are built with, the three days sit in two tensors, and the best weight is the same division, Σxy over Σx², written with * and .sum():
Drill
The best weight from the vertex
For the data xs = [1, 2, 3] and ys = [3, 5, 8], fix the loop so w_best is Σxy ÷ Σx².
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write