One equation with two unknowns has endless answers; two equations usually pin both numbers down. The picture is two lines crossing, and the answer is where they meet. This lesson solves such pairs two ways, and shows the step that real models are built around: two data points fix a line exactly, but three usually can't all be hit, which is why models are fitted rather than solved. Along the way, you'll check every answer in both equations, by hand and in Python. It's the idea that real training rests on.
What you'll be able to do
Solve two equations in two unknowns by substitution and by elimination
See the solution as the point where two lines cross, and name the three possible outcomes
Check a pair of values in both equations, and eliminate with a few lines of Python
Find the line through two data points exactly, and see why three points need a fitted line
Quick check
Have a guess before you start. Two numbers add to 10 and differ by 2. What are they?
5 and 5
6 and 4
8 and 2
7 and 3
Show the answer
6 and 4
6 + 4 = 10 and 6 − 4 = 2. Both facts hold, and no other pair fits both.
The math: two facts, two numbers
Purpose. Pin down two unknown numbers with two facts about them.
The puzzle above is two equations: x + y = 10 and x − y = 2. Each on its own has endless answers: 9 and 1, 7 and 3, 6 and 4 all add to 10. Each equation is a line of all its answers. The pair that fits both is where the two lines cross.
There are two ways to find it.
Substitution: rearrange one equation for one letter, then put that into the other. Now there's one equation with one unknown, which you already know how to solve.
Elimination: add or take away whole equations so that one letter cancels out.
Quick check
From an earlier lesson: what do two lines with the same slope and different intercepts do?
They never meet: they're parallel
They cross once
They're the same line
They cross at right angles
Show the answer
They never meet: they're parallel
Same slope means they climb side by side, so they never cross. For two equations, that means no solution.
Elimination, step by step. Adding equal things to equal things keeps them equal. So two equations can be added, left side to left side and right side to right side, and the result is still true. Choose to add or take away so that one letter cancels. Watch x + y = 10 and x − y = 2: adding them cancels y.
The picture draws each equation as a line: every point on it is a pair (x, y) that fits that equation. Watch the new equation that adding makes. It has no y, so its line goes straight up, and it runs right through the crossing: the crossing fits both equations, so it fits their sum too.
Try it now
Then set the second equation to 1, 3, 22 and press Use these numbers: the y's don't cancel by adding, so the flow scales first, and the lines stay exactly where they were.
Symbols and formula. Two equations with letters for their numbers:
a1x+b1ya2x+b2y=c1=c2
In plain words: a pair of lines. Make the y-numbers match (multiplying a whole equation if needed), then add or take away the equations so y cancels. Solve for x, then put it back for y.
Three outcomes. Two lines can cross once (one solution), be the same line (endless solutions), or be parallel (no solution). In equations: if one is just the other multiplied, they're the same line. If the x and y parts match but the right sides don't, they're parallel. The picture keeps x + y = 10 and changes the second equation, one outcome at a time.
Quick check
Solve by substitution: y = 2x and x + y = 9.
x = 9, y = 18
x = 6, y = 3
x = 3, y = 6
x = 4.5, y = 4.5
Show the answer
x = 3, y = 6
Put y = 2x into the second: x + 2x = 9, so 3x = 9 and x = 3. Then y = 2 × 3 = 6.
Quick check
How many solutions do x + y = 3 and 2x + 2y = 6 have?
Endless: they're the same line
None: they're parallel
One
Two
Show the answer
Endless: they're the same line
The second is the first multiplied by 2, so every pair that fits one fits the other.
In your own words
In your own words: why is the solution of two equations where their lines cross?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: check both, then eliminate
Purpose. Checking a pair is the quickest test, and elimination is a few lines of arithmetic.
A solution must satisfy both equations, so the check uses and:
deffits(x, y):
return x + y == 10and x - y == 2print(fits(6, 4), fits(7, 3))
It runs right here in your browser, with nothing to install.
Now step through the same check. This version gives each equation's test a name of its own, first and second, before joining them with and, so you can watch which test a pair passes and which it fails.
Elimination in code. When the y-numbers already match, taking one equation from the other cancels y:
# 2x + 3y = 12 and x + 3y = 9: the 3y's match
x = (12 - 9) / (2 - 1)
y = (12 - 2 * x) / 3print(x, y)
A named mistake: checking one equation only. A pair that fits one equation can still miss the other, as 7 and 3 did. Always put the answer into both. If either fails, there's a slip, often a sign lost when taking one equation from the other: every term must be taken away, the right side included.
When the y's don't match. Multiply a whole equation first. For 5x + 2y = 20 and 3x + y = 7, double the second to get 6x + 2y = 14. Now the 2y's match, and taking one from the other leaves −x = 6, so x = −6. Multiply every term, or the equation stops being true.
Worked example
Worked example: tickets, solved and checked
Adult tickets are $8, child tickets $5. Ten tickets cost $65. Find both numbers, then check them.
# a + c = 10, so c = 10 - a; put it into 8a + 5c = 65
a = (65 - 5 * 10) / (8 - 5)
c = 10 - a
print(a, c)
print(a + c == 10and8 * a + 5 * c == 65)
In the lesson you can change this and run it.
Substitution turns the pair into one equation: 8a + 5(10 − a) = 65, which simplifies to 3a = 15. So 5 adult tickets and 5 child tickets. The last line checks both facts at once with and, and prints True. If someone had solved only 8a + 5c = 65, many wrong pairs would fit; the count of 10 tickets is what pins it down.
Quick check
What does this print?
deffits(x, y):
return3 * x + 2 * y == 16and x - 2 * y == 0print(fits(4, 2), fits(2, 5))
False False
True True
False True
True False
Show the answer
True False
4 and 2: 12 + 4 = 16 and 4 − 4 = 0, both true. 2 and 5: 6 + 10 = 16, but 2 − 10 isn't 0.
Quick check
For 5x + 2y = 20 and 3x + 2y = 14, which line finds x?
x = (20 - 14) / (5 - 3)
x = (20 - 14) / 2
x = 20 / 5 - 14 / 3
x = (20 + 14) / (5 + 3)
Show the answer
x = (20 - 14) / (5 - 3)
The 2y's match, so take the second equation from the first: 2x = 6, and x = 3.
Which method to choose. Substitution is quickest when one equation already says what a letter equals, like y = 2x. Elimination is quickest when a letter's numbers already match, or match after one multiplication. Both give the same answer, so pick whichever needs fewer steps, and check the result in both equations either way.
Drill
Solve a pair, then check it
Solve 4x + 3y = 26 and x + 3y = 17 by taking one from the other, then fix fits so it checks a pair in both equations.
x = (26 + 17) / (4 + 1)
y = (17 - x) / 3deffits(x, y):
return4 * x + 3 * y == 26print(x, y, fits(x, y))
In the lesson, your code is checked as you write it.
In your own words
Why does a check need both equations, joined with and?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: two points fix a line, three don't
Purpose. A line ŷ = w·x + b has two unknowns, w and b. Each data point gives one equation. So two points pin the line down exactly.
The tent days at 10 and 30 degrees, with 22 and 59 rentals, give 10w + b = 22 and 30w + b = 59. The b's match, so taking one from the other cancels b: 20w = 37.
w = (59 - 22) / (30 - 10)
b = 22 - 10 * w
print(w, b)
print(w * 20 + b, 41 - (w * 20 + b))
The line hits both days exactly, but the third day, 20 degrees with 41 rentals, misses by half a rental. The picture shows this two ways. First come the three days and the line through two of them. Press Next to see each day's equation drawn as a line of its own, with w across and b up, like the lines in the math part.
Quick check
A third data point doesn't land on the line through the first two. What does that mean for the model?
The model needs a bigger slope
Three points always fit a line
No line hits all three, so the best line must be fitted
The third point is wrong and should be deleted
Show the answer
No line hits all three, so the best line must be fitted
Three equations in two unknowns usually have no exact solution. So instead of solving, a model finds the line with the smallest total miss.
Solved versus fitted. With exactly two points, solving is enough. Real data has hundreds or millions of points, far more equations than unknowns, and no line hits them all. So instead of solving, training fits: it chooses w and b to make the total squared miss as small as possible, the bowl from lesson 15. The elimination step you did here comes back in lesson 60, when the same idea runs on whole matrices.
In the real tools. PyTorch, the library most models are built with, solves a pair like this in one call. The equations go in as a table: each row of A holds one day's numbers in front of w and b (the temperature, and 1 for the b), and y holds the rentals.
Any two points, one line. Two points with different x values always give exactly one line. In w·x₁ + b = y₁ and w·x₂ + b = y₂ the b's match, so taking one from the other leaves w·(x₂ − x₁) = y₂ − y₁. That's lesson 21's slope formula, found again from a pair of equations. Predict the line through (4, 11) and (10, 29).
Taking one equation from the other: 6w = 18, so w = 3.0. Then b = 11 − 3 × 4 = −1.0.
Drill
A line through two days, and its miss on a third
Days at 12 and 28 degrees had 30 and 62 rentals. Find w and b for the line through them, then set miss to how far it is from a third day: 20 degrees and 44 rentals (actual minus predicted).
w = (28 - 12) / (62 - 30)
b = 30
miss = 0print(w, b, miss)
In the lesson, your code is checked as you write it.
In your own words
Why do models fit a line instead of solving for one?
Write a sentence or two, then compare it with a good answer in the lesson.
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