Login
📚 Linear Algebra, Interactive Edition
Chapters ▾
⇩ Download ▾

6.3 Least Squares: Beat the Normal Equations

Seven measurements: (2,2,2.4), (2,1,0.5), (1,1,0.9), (0,0,0.6), (1,1,0.3), (1,2,2.7), (2,1,2.2). One model: a plane z=ax+by+c. Each data point would like the coefficients to satisfy axi+byi+c=zi — and read that way, each measurement is a linear equation in the unknowns (a,b,c): seven planes in coefficient space. Chapter 1 says three independent planes already pin down a point, so seven generically share nothing. The system is overdetermined and inconsistent; no plane passes through all seven points, and the interesting question is which plane misses them least.

"Least" needs a score. The residual of point i is the vertical gap ri=zi(axi+byi+c), and the score is the sum of squared residuals over all seven points — squared so that misses above and below both count against you, and big misses count hardest. Your job in the first figure: tune a, b, c by hand and drive the score as low as you can. Then find out whether you beat the machine. (You will not. The point is understanding why.)

Seven small dark spheres scattered in 3D space around a tilted, semi-transparent plane through the origin. Two vertical dashed red segments connect two of the spheres to the plane, showing how far the plane misses them vertically. A second, better-fitting plane is included but hidden until the student reveals it.Explore in 3D (opens in a new tab)
Seven data points and the deliberately imperfect starting plane z=0.5x+0.5y. The dashed red segments at (1,2) and (2,2) are residuals — vertical gaps between point and plane. A least-squares plane is hidden in the scene; leave it hidden until you have a fit you are proud of.

Explore the figure

  1. Orbit the data and read the two drawn residuals. Above (1,2) the plane sits at z=1.5 while the point is at 2.7 — a gap of 1.2 above. At (2,2) the plane is at 2 and the point at 2.4 — a gap of 0.4 below.
  2. Judge all seven points: five sit above the starting plane, only two below. A plane that splits its misses is doing better than one that is systematically low — this one is too low, and too flat in the y direction. Its score is 3.55.
  3. Hide the two objects labeled residual at (1,2) and residual at (2,2) — they are drawn for the starting plane only and will not follow your edits. Then edit the plane's expression: try 0.5x+0.9y+0.3 first, and watch the plane move as you retype. Better or worse? How can you tell from the picture alone?
  4. Keep tuning — treat the three coefficients like three sliders and iterate until you cannot visibly improve. Hand fits usually land near a0.40.5, b0.91.0, c0.30.5, scoring somewhere between 0.1 and 0.3. Write your three numbers down.
  5. Now reveal the hidden object labeled least-squares plane and orbit. How close did you come — and can you find any single coefficient change that would visibly improve on it?

The normal equations

Your tuning loop was a search of a subspace. Collect the seven heights a candidate plane produces into a vector in 7: it equals a times the column of xi's, plus b times the column of yi's, plus c times a column of ones. As (a,b,c) range over all values, these vectors sweep out the span of three columns — a three-dimensional subspace of 7 — and you were hunting the point of it closest to the data vector 𝐳=(z1,,z7). Section 6.1 answered that hunt in general: closest means the residual vector (r1,,r7) is perpendicular to the subspace, i.e. to each of the three spanning columns. Writing out those three dot products — xiri=0, yiri=0, ri=0 — and substituting the definition of ri gives the normal equations:

15 a + 6 b c = 11.5 , 6 a + 12 b = 14.1 , a + 7 c = 2.4

Three equations, three unknowns, one solution — the whole search compressed onto an index card.

The winner scores 0.0723 — nearly fifty times better than the starting plane's 3.55, and better than any hand fit. Its seven residuals are 0.046, 0.065, +0.077, +0.199, 0.132, 0.051, +0.018: small everywhere, mixed in sign, and summing to zero — the visible face of the perpendicularity conditions, since ri=0 is literally one of them.

Feel the trade-off

The second figure replays the contest with the coefficients on actual sliders and the two residual segments now live — they stretch and shrink as you drag.

Seven small dark spheres scattered in 3D space around a tilted, semi-transparent plane. Three sliders named a, b, and c control the plane z = ax + by + c. Two vertical dashed red segments connect two of the spheres to the plane and stretch or shrink live as the sliders move. A second, better-fitting plane is included but hidden.Explore in 3D (opens in a new tab)
The same seven data points, with the plane z=ax+by+c driven by sliders a, b, c and the dashed residual segments at (1,2) and (2,2) updating live as you drag. The least-squares plane is hidden in this scene too.
  1. From the starting setting (0.5,0.5,0), drag b upward and watch the residual at (1,2) shrink — then watch what your improvement costs at (2,2). Every coefficient helps some points at other points' expense; that tension is what the score arbitrates.
  2. Try to make both drawn residuals small at once, then check the rest of the data by orbiting. The best reachable setting on these sliders is about (0.40,0.95,0.40), scoring 0.0800 — the true optimum lies between slider clicks, and no setting the sliders can reach attains 0.0723.
  3. Reveal the hidden least-squares plane and set the sliders to hug it as closely as you can. At the optimum, no single-slider move improves the score: each slider direction has been made exactly perpendicular to the remaining error. That is the normal equations, felt by hand.

An original work of XYZ Homework, built around interactive XYZ 3D figures. Its chapter sequence is aligned to Interactive Linear Algebra (Margalit & Rabinoff, Georgia Tech, GNU FDL); this work is original, copies nothing from it, and is not affiliated with or endorsed by its authors. License: CC-BY-NC-SA-4.0.