Visual walkthrough — Normal equation closed-form solution
This page rebuilds the normal equation as a sequence of pictures. We will not write a single symbol you have not seen drawn first. By the end you will see why the answer is — not just believe it.
Step 1 — The dots and the miss
WHAT. We start with the rawest thing possible: data points scattered on a plane, and one candidate straight line drawn through them.
WHY. Before any matrices, you must feel what we are trying to do. Every dot has a true height . The line has a predicted height at the same . The gap between them — the little vertical stick — is the error (also called the residual). Our entire goal is to make all these sticks small.
PICTURE. Look at the red vertical sticks. Each one is . The line in the figure is a guess; it is not yet the best.

Step 2 — Why "square" the misses?
WHAT. We turn the whole bundle of red sticks into one single number that measures how bad the line is. We square each stick and add them up.
WHY squaring and not just adding? If we simply added the sticks, a stick pointing up () and one pointing down () would cancel to — a terrible line would look perfect. We need every miss to count as positive badness. Squaring does exactly that: , . Squaring also punishes big misses much harder than small ones, which is what we want — a wildly wrong point should scream louder.
Why this tool and not absolute value? We could use , which also kills signs. But has a sharp corner at zero — it is not smooth, so calculus (finding where the slope is zero) gets messy. The square is a smooth bowl, and its derivative is the clean . Smoothness is what buys us a closed-form answer later.
PICTURE. Each red stick becomes a red square with that stick as its side. Total badness = total red area.

Step 3 — The cost is a bowl, so the bottom is where slope = 0
WHAT. Forget the data plane for a moment. Now we plot the badness number against the parameters of the line. For a line there are two knobs, (height) and (tilt). Turning the knobs changes .
WHY. Because is built from squares of things that are linear in , is a quadratic in — and a quadratic in two variables is a smooth bowl (a paraboloid) with exactly one lowest point. There is no second valley, no plateau. This is the deep reason a one-shot answer exists: we are not searching a messy landscape, we are dropping to the bottom of a single bowl.
Why "set the derivative to zero"? At the very bottom of a bowl the surface is momentarily flat in every direction — walk any way and you go up. "Flat in every direction" is exactly "all partial derivatives (the gradient) equal zero." So finding the best line = solving . (Compare with Gradient Descent, which instead rolls down this same bowl step by step.)
PICTURE. The bowl over the floor; the red dot marks the unique flat bottom.

Step 4 — Stack everything into matrices
WHAT. Writing over every point is tiring. We pack all points into a grid (the design matrix), all targets into a column , all knobs into a column .
WHY. One matrix multiply produces all predictions at once. The whole bundle of red sticks becomes a single error vector . Then "total squared miss" is just that vector dotted with itself — one clean expression instead of a sum.
Why the column of 1's? The intercept must multiply something in every row. We give it a fake feature that is always , so survives in every prediction.
PICTURE. The grid (leftmost column all 's, in red), times the knob column, lands on the prediction column.

Step 5 — Differentiate the bowl, land on the perpendicular fact
WHAT. Expand the squared length and take its gradient with respect to , then set it to .
WHY expand? Because turns the cost into three friendly pieces we know how to differentiate.
Differentiate term by term (these are the matrix versions of and ):
Set to zero and the dies:
WHAT this says geometrically. Rearrange to , i.e. . In words: the error vector is perpendicular to every column of . That is why it is called the normal equation — "normal" means perpendicular.
PICTURE. The flat sheet is the column space of (all lines we can possibly build). The truth floats off the sheet. The best prediction is the shadow of on the sheet, and the red error joins them at a right angle.

Step 6 — Solve for the knobs
WHAT. Peel out of by multiplying both sides by the inverse of .
WHY an inverse? is a small square matrix sitting in front of . Undoing a matrix multiply is exactly what the inverse does, provided the matrix is not degenerate.
PICTURE. The two sides of the equation as a scale; multiplying both pans by leaves alone on the left.

Step 7 — The degenerate case: when the bowl becomes a trough
WHAT. Sometimes has no inverse. This happens when two feature columns are secretly the same information (linearly dependent), so is rank-deficient.
WHY it breaks. If a feature is redundant, many different knob settings give the identical line. The bowl of Step 3 flattens along one direction into a trough — a whole valley floor of equally-best answers, not a single point. A trough has no unique bottom, so no unique , so cannot exist (determinant ).
Concrete trap (Example 2 from the parent). With always, column column column of 's. Then and the formula fails. Infinitely many exact fits exist, e.g. gives , , — perfect, zero error.
The fix. Use the Moore-Penrose Pseudoinverse (computed via SVD, or stabilize with QR Decomposition). Among all trough-floor solutions it returns the minimum-norm one. Or add a tiny penalty — that is Ridge Regression, which tilts the trough back into a bowl.
PICTURE. Left: healthy bowl, one red bottom. Right: trough, a red line of equal minima.

The one-picture summary
Everything above is one geometric fact: drop the truth perpendicularly onto the flat sheet of achievable predictions; the landing point is , and the perpendicular error gives , which unfolds into the normal equation.

Recall Feynman retelling — say it like a story
We threw dots on a page and drew a line. For each dot we measured the vertical miss — the red stick. We squared the sticks so ups and downs both count as bad and big misses shout louder, then averaged them into one badness number . Plotting against the line's two knobs gave a smooth bowl, and the best line lives at the bowl's flat bottom where the slope vanishes. We packed all points into a grid so one multiply predicts everything and the misses become one arrow . Setting the slope to zero said : the leftover error points straight out from the sheet of all possible lines — that's the perpendicular (normal) rule, and it's just the fact that the shortest path to a surface is straight down. Solving that gives . And if two features secretly repeat, the bowl flattens into a trough with infinitely many equal bottoms — then we call the pseudoinverse to pick the smallest one.
Recall Quick self-test
Why square the residuals instead of adding them? ::: Plain sums let and cancel, hiding a bad line; squaring makes every miss positive and punishes big misses more, while staying smooth for calculus. What does "normal" in normal equation mean? ::: Perpendicular — the error vector is perpendicular (normal) to the column space of , i.e. . When can't you invert ? ::: When is rank-deficient (linearly dependent feature columns); the cost bowl becomes a trough with infinitely many minima. What replaces the inverse then? ::: The Moore-Penrose pseudoinverse (via SVD), returning the minimum-norm least-squares solution.