Linear Algebra, Interactive EditionXYZ Homework Edition

⇩ Download ▾

6.3 Least Squares: Beat the Normal Equations

Seven measurements: (2,2,2.4)(-2, -2, -2.4), (2,1,0.5)(-2, 1, 0.5), (1,1,0.9)(-1, -1, -0.9), (0,0,0.6)(0, 0, 0.6), (1,1,0.3)(1, -1, -0.3), (1,2,2.7)(1, 2, 2.7), (2,1,2.2)(2, 1, 2.2). One model: a plane z=ax+by+cz = ax + by + c. Each data point would like the coefficients to satisfy axi+byi+c=zia x_i + b y_i + c = z_i — and read that way, each measurement is a linear equation in the unknowns (a,b,c)(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 ii is the vertical gap ri=zi(axi+byi+c)r_i = z_i - (a x_i + b y_i + 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 aa, bb, cc 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.5yz = 0.5x + 0.5y. The dashed red segments at (1,2)(1, 2) and (2,2)(-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)(1, 2) the plane sits at z=1.5z = 1.5 while the point is at 2.72.7 — a gap of 1.21.2 above. At (2,2)(-2, -2) the plane is at 2-2 and the point at 2.4-2.4 — a gap of 0.40.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 yy direction. Its score is 3.553.55.
  3. Hide the two objects labeled residual at (1,2)(1, 2) and residual at (2,2)(-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.30.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.4a \approx 0.40.50.5, b0.9b \approx 0.91.01.0, c0.3c \approx 0.30.50.5, scoring somewhere between 0.10.1 and 0.30.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\mathbb{R}^7: it equals aa times the column of xix_i's, plus bb times the column of yiy_i's, plus cc times a column of ones. As (a,b,c)(a, b, c) range over all values, these vectors sweep out the span of three columns — a three-dimensional subspace of 7\mathbb{R}^7 — and you were hunting the point of it closest to the data vector 𝐳=(z1,,z7)\mathbf{z} = (z_1, \dots, z_7). Section 6.1 answered that hunt in general: closest means the residual vector (r1,,r7)(r_1, \dots, r_7) is perpendicular to the subspace, i.e. to each of the three spanning columns. Writing out those three dot products — xiri=0\sum x_i r_i = 0, yiri=0\sum y_i r_i = 0, ri=0\sum r_i = 0 — and substituting the definition of rir_i gives the normal equations:

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

[15616120107]\begin{bmatrix}15 & 6 & -1 \\ 6 & 12 & 0 \\ -1 & 0 & 7\end{bmatrix}
The normal equations' left side, from the data ✓ Computed · mojocas 0.1.0 ✓ Agrees with the text The normal equations' left side, from the data, computed exactly by mojocas 0.1.0, and confirmed to agree with the result stated in the text.

Where those nine coefficients come from. Stack the seven measurements into a 7×37 \times 3 matrix AA — a column of xix_i, a column of yiy_i, a column of ones — and the three dot-product conditions are exactly A𝖳AA^{\mathsf{T}}A, computed here from the measurements themselves rather than summed by hand.

[23214110125]\begin{bmatrix}\frac{23}{2} \\ \frac{141}{10} \\ \frac{12}{5}\end{bmatrix}
The normal equations' right side, from the data ✓ Computed · mojocas 0.1.0 ✓ Agrees with the text The normal equations' right side, from the data, computed exactly by mojocas 0.1.0, and confirmed to agree with the result stated in the text.

And the three numbers on the right: A𝖳𝐳A^{\mathsf{T}}\mathbf{z}, the same seven measurements dotted against the same three columns. Between this and the matrix above, every number in the system is now derived from the data — so a slip in one of the twelve dot-product sums would stop the build rather than quietly solve the wrong system correctly.

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

[1006711660010323332001133332]\begin{bmatrix}1 & 0 & 0 & \frac{671}{1660} \\ 0 & 1 & 0 & \frac{323}{332} \\ 0 & 0 & 1 & \frac{133}{332}\end{bmatrix}
The normal equations, reduced ✓ Computed · mojocas 0.1.0 ✓ Agrees with the text The normal equations, reduced, computed exactly by mojocas 0.1.0, and confirmed to agree with the result stated in the text.

The three normal equations as an augmented matrix, reduced to solved form by a computer algebra system. The final column is the winning plane in exact arithmetic: a=6711660a = \tfrac{671}{1660}, b=323332b = \tfrac{323}{332}, c=133332c = \tfrac{133}{332} — the fractions that round to the 0.40420.4042, 0.97290.9729, 0.40060.4006 worked out above.

The winner scores 0.07230.0723 — nearly fifty times better than the starting plane's 3.553.55, and better than any hand fit. Its seven residuals are 0.046-0.046, 0.065-0.065, +0.077+0.077, +0.199+0.199, 0.132-0.132, 0.051-0.051, +0.018+0.018: small everywhere, mixed in sign, and summing to zero — the visible face of the perpendicularity conditions, since ri=0\sum r_i = 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+cz = ax + by + c driven by sliders aa, bb, cc and the dashed residual segments at (1,2)(1, 2) and (2,2)(-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)(0.5, 0.5, 0), drag bb upward and watch the residual at (1,2)(1, 2) shrink — then watch what your improvement costs at (2,2)(-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)(0.40, 0.95, 0.40), scoring 0.08000.0800 — the true optimum lies between slider clicks, and no setting the sliders can reach attains 0.07230.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.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.