An equation says two things are equal, and solving it means finding the number that makes that true. The picture to keep is a balance: whatever you do to one pan, you must do to the other, or it tips. With that one rule, and undoing steps in reverse order, every equation in this lesson falls. In machine learning it answers backward questions, like which temperature makes a model predict 40 rentals. It also covers the two odd cases, an equation with no answer and one with every answer, and how to check any answer by putting it back, the habit that catches nearly every slip.
What you'll be able to do
Test whether a number solves an equation by putting it in
Solve a linear equation by doing the same to both sides, undoing the steps in reverse order
Solve and check equations in Python, with a function and by guess, check and adjust
Run a model's formula backward: which input gives a target prediction
Quick check
Have a guess before you start. Which value of x makes 3x + 5 = 20 true?
15
25/3
8.3
5
Show the answer
5
Try it: 3 × 5 + 5 = 15 + 5 = 20. Putting a number in is the quickest way to test a solution.
The math: an equation is a balance
Purpose. Find the number that makes a statement true.
An equation says two expressions are equal: 3x + 5 = 20. A solution is a value of x that makes it true. Before solving anything, you can always test a proposed answer by putting it in. If both sides come out equal, it's a solution.
To find the answer, picture a balance with 3x + 5 on the left pan and 20 on the right. They balance. You may do anything you like to the pans, as long as you do the same to both: then they still balance.
Quick check
From an earlier lesson: work out 4x − 3 when x = 2.
5
−4
39
11
Show the answer
5
Substitute: 4 × 2 − 3 = 8 − 3 = 5. That's exactly how a solution is tested.
Undo in reverse order. Think of how the left side was built. Start with x, times by 3, then add 5. To get x back, undo those steps in the reverse order, on both pans: first take away 5, then divide by 3.
Watch a harder one, 4x + 7 = 2x + 15, with an x on both sides. The first move gathers the x's on one pan. The picture draws the two pans on a level beam. Each step writes its move above the beam and does it to both pans, and on each pan the part that move changed lights up. The last step puts the answer back in, and both pans come to the same number.
Try it now
Each step does one thing to both pans. Change the numbers and press Use these numbers to solve another.
Symbols and formula. For any equation of the form ax + b = c:
ax+b=c⇒ax=c−b⇒x=ac−b
In plain words: take b from both sides, then divide both sides by a.
Always finish by checking: put the answer back into the original equation and see that both sides match. A solution you haven't checked is only a guess.
Quick check
Solve 7x − 4 = 31.
x = 3.9
x = 35
x = 5
x = 27/7
Show the answer
x = 5
Add 4 to both sides: 7x = 35. Divide both by 7: x = 5. Check: 7 × 5 − 4 = 31.
Quick check
To solve x/3 + 2 = 6, what's the first move?
Times both sides by 3
Divide both sides by 3
Take 2 from the left side
Take 2 from both sides
Show the answer
Take 2 from both sides
The left was built as x, divide by 3, add 2. Undo in reverse: first take 2 from both sides (x/3 = 4), then times both by 3 (x = 12).
In your own words
In your own words: why do you undo the steps in reverse order?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: solve it, then check it
Purpose. Python can check a solution in one line, and solve a whole family of equations with one function.
It runs right here in your browser, with nothing to install.
The formula x = (c − b)/a, written once, solves every equation of the shape ax + b = c:
defsolve_linear(a, b, c):
return (c - b) / a
print(solve_linear(3, 5, 20), solve_linear(2, -5, 11), solve_linear(7, -4, 31))
Solve, then check, one line at a time. Here is the flow's equation, 4x + 7 = 2x + 15, in code. The x's are gathered by hand first, 4x − 2x on the left, so solve_linear gets a = 4 − 2, b = 7 and c = 15. Then the last three lines put the answer back into both sides of the original equation. Press Next to run one line at a time, and watch a, b and c fill when solve_linear is called.
A named mistake: c − b / a. The brackets in (c − b) / a matter. Without them, Python divides only b, because dividing comes before subtracting:
c = 20
b = 5
a = 3print((c - b) / a, c - b / a)
Guess, check and adjust. There's a second way that needs no rearranging at all. Guess, see how far off you are, and adjust. For 4x + 7 = 31:
print(4 * 5 + 7) # guess 5: 27, too low by 4print(4 * 6 + 7) # guess 6: 31, just right
It's slower by hand, but it works on equations no formula covers. Computers solve many hard problems this way.
Worked example
Worked example: solve, then check, three equations
Solve 4x + 7 = 31, x/2 + 3 = 8 and −2x + 10 = 4 with one function, and check each answer by putting it back.
Each equation is written as a, b and c for ax + b = c. Dividing by 2 is the same as multiplying by 0.5, so x/2 + 3 = 8 has a = 0.5. The answers are 6.0, 10.0 and 3.0, and the second line confirms each one balances its equation. The checks use == safely here because every number is exact; with messier decimals you'd use math.isclose, as in lesson 7.
Quick check
What does this print?
defsolve_linear(a, b, c):
return (c - b) / a
print(solve_linear(5, 10, 0))
Which line checks whether x = 4 solves 2x + 7 = 15?
print(2x + 7 == 15)
print((15 - 7) * 2)
print(2 * 4 + 7 == 15)
print(2 * 4 + 7 = 15)
Show the answer
print(2 * 4 + 7 == 15)
Substitute 4 for x and compare with ==, which gives True or False.
No answer, or every answer. Most linear equations have exactly one solution, but two other cases turn up. In 2x + 3 = 2x + 5, taking 2x from both sides leaves 3 = 5, which is never true: there's no solution. In 2x + 3 = 2x + 3, everything cancels to 3 = 3, which is always true: every x is a solution. In code, both cases make a zero once the x's are gathered, and solve_linear would stop with a division-by-zero error. A careful solver checks a first and says which case it's in. You'll meet the same three outcomes again in lesson 22, as lines that cross, coincide or run side by side.
Drill
Solve and check
Fix solve_linear(a, b, c) so it solves ax + b = c, then set x to the solution of 6x − 9 = 27 and ok to whether it balances.
defsolve_linear(a, b, c):
return c - b / a
x = solve_linear(6, -9, 27)
ok = 6 * x - 9 == 20print(x, ok)
In the lesson, your code is checked as you write it.
In your own words
Why is it worth checking an answer even when a formula produced it?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: running a formula backward
Purpose. A model's formula answers "what will happen?". Solving it answers the backward question: "what input gives the result I want?"
The tent shop's model is rentals = 3 × temperature − 20. At what temperature does it predict 40 rentals?
y^=wx+b⇒x=wy^−b
In plain words: take the bias away from the target, then divide by the weight.
definput_for(target, w, b):
return (target - b) / w
print(input_for(40, 3, -20))
At 20 degrees, the model expects 40 rentals.
Quick check
A model predicts score = 2.5 × hours + 25. How many hours does it say a score of 100 needs?
40
50
250
30
Show the answer
30
(100 − 25) ÷ 2.5 = 75 ÷ 2.5 = 30 hours.
Training arithmetic, solved for any letter. In one pass over n examples (an epoch) in batches of size B, a model takes n ÷ B steps. So steps = epochs × n ÷ B. Solving it lets you plan: which batch size gives exactly 500 steps per epoch on 16,000 examples?
print(16000 / 500)
print(16000 / 32)
A batch of 32 gives 500 steps, and the second line checks it. The same equation answers "how many epochs fit in a budget of steps", by solving for epochs instead.
In the real tools. In PyTorch, the library most models are built with, a DataLoader cuts the examples into batches, and len(loader) counts the batches in one epoch. So steps = epochs × len(loader). Here it is on the 16,000 examples in batches of 32, solved for the epochs that fit a budget of 1,500 steps:
Backward answers need a sense check. Solving gives a number, and the formula decides its unit. Asking the rental model for 100 rentals gives (100 + 20) ÷ 3 = 40 degrees: a real number, but far outside the days the model was built from. A formula will answer for temperatures it has never seen, so a backward answer should be checked against the data too. If the shop never opened above 30 degrees, a temperature of 40 is a guess dressed as a fact. Predict both answers below before you run them.
Quick check
What does this print?
definput_for(target, w, b):
return (target - b) / w
print(input_for(100, 3, -20), input_for(-20, 3, -20))
26.7 -13.3
26.7 0.0
40.0 0.0
40.0 -13.3
Show the answer
40.0 0.0
Taking away b = −20 adds 20: (100 + 20) ÷ 3 = 40.0. A target of −20 gives (−20 + 20) ÷ 3 = 0.0: the bias alone, at 0 degrees.
Drill
Two backward questions
Write input_for(target, w, b), then set temp to the temperature where rentals = 3 × temperature − 20 predicts 61, and batch to the batch size that gives 250 steps per epoch on 8,000 examples.
definput_for(target, w, b):
return target / w - b
temp = input_for(61, 3, -20)
batch = 8000 * 250print(temp, batch)
In the lesson, your code is checked as you write it.
In your own words
What kind of question does solving a model's formula answer, that running it forward can't?
Write a sentence or two, then compare it with a good answer in the lesson.
Goes deeper in
Formulas: rearranging and using formulas in more Python.
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