Orthogonality & Least Squares

Study Sheet

Orthogonality & Least Squares

Projections, distance, and best fit

Projection and Best Approximation

Tip
The projection formula
u = (1, 3)vproju − proj
u = (1, 3) splits into its shadow on the line through v = (3, 1) and a perpendicular leftover.

The projection of u\mathbf u onto v\mathbf v is

proj⁡vu=u⋅vv⋅v v,\operatorname{proj}_{\mathbf v}\mathbf u = \frac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v}\,\mathbf v,

the shadow u\mathbf u casts on the line through v\mathbf v. What is left over, u−proj⁡vu\mathbf u - \operatorname{proj}_{\mathbf v}\mathbf u, is perpendicular to v\mathbf v, and its length is the distance from u\mathbf u to the line.

The coefficient is chosen to make the leftover perpendicular: (u−cv)⋅v=0(\mathbf u - c\mathbf v)\cdot\mathbf v = 0 forces c=u⋅vv⋅vc = \tfrac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v}. For u=(1,3)\mathbf u = (1, 3) and v=(3,1)\mathbf v = (3, 1): c=3+39+1=35c = \tfrac{3 + 3}{9 + 1} = \tfrac35, the shadow is 35(3,1)=(95,35)\tfrac35(3, 1) = (\tfrac95, \tfrac35), and the leftover (−45,125)(-\tfrac45, \tfrac{12}{5}) has dot product −125+125=0-\tfrac{12}{5} + \tfrac{12}{5} = 0 with v\mathbf v. Replacing v\mathbf v by any nonzero multiple gives the same shadow: v\mathbf v appears twice on top and twice below, so the scale cancels.

Concept
Least squares: closest, not exact

When Ax=bA\mathbf x = \mathbf b has no solution, solve ATA x^=ATbA^{\mathsf T}A\,\hat{\mathbf x} = A^{\mathsf T}\mathbf b instead: Ax^A\hat{\mathbf x} is the projection of b\mathbf b onto the column space — the best possible fit. Every regression line in statistics is this computation.

Why the normal equations: the vectors AxA\mathbf x fill the column space of AA, and the closest of them to b\mathbf b is the one whose error b−Ax^\mathbf b - A\hat{\mathbf x} is perpendicular to that whole space — the projection idea again, one dimension up. Perpendicular to every column means AT(b−Ax^)=0A^{\mathsf T}(\mathbf b - A\hat{\mathbf x}) = \mathbf 0, which rearranges to ATAx^=ATbA^{\mathsf T}A\hat{\mathbf x} = A^{\mathsf T}\mathbf b. If AA has independent columns, ATAA^{\mathsf T}A is invertible and the best fit is unique.

Example
The simplest regression
xy12312345c = 3
The data 1, 3, 5 and the best constant c = 3; the dashed errors −2, 0, 2 sum to zero.

Fitting a constant cc to data 1,3,51, 3, 5 minimizes (c−1)2+(c−3)2+(c−5)2(c-1)^2 + (c-3)^2 + (c-5)^2; the derivative vanishes at the mean, c=3c = 3. Least squares generalizes the average — and orthogonality is why it works.

In matrix form this is A=(1,1,1)TA = (1, 1, 1)^{\mathsf T} and b=(1,3,5)\mathbf b = (1, 3, 5): one column, all ones. The normal equation reads 3c=1+3+5=93c = 1 + 3 + 5 = 9, so c=3c = 3 again. The errors 1−3,3−3,5−31 - 3, 3 - 3, 5 - 3, that is −2,0,2-2, 0, 2, add to zero — the error vector (−2,0,2)(-2, 0, 2) is perpendicular to the column (1,1,1)(1, 1, 1). The total squared error is 4+0+4=84 + 0 + 4 = 8, and no other constant does better.

Side note
Significance: least squares built the modern world

Gauss invented least squares to find a lost asteroid (Ceres, 1801) — and today every regression, every GPS position fix (overdetermined satellite equations), and every curve fit in every lab runs the normal equations from this topic. Orthogonality is also why JPEG and MP3 work: signals are projected onto orthogonal cosine bases and the small components discarded.

Try it
Try it: a distance via projection

Find the distance from the point (5,1)(5, 1) to the line through the origin along (2,1)(2, 1).

Show the answer

Project first. The coefficient is c=(5,1)⋅(2,1)(2,1)⋅(2,1)=10+14+1=115c = \dfrac{(5, 1)\cdot(2, 1)}{(2, 1)\cdot(2, 1)} = \dfrac{10 + 1}{4 + 1} = \dfrac{11}{5}, so the shadow is 115(2,1)=(225,115)\tfrac{11}{5}(2, 1) = \left(\tfrac{22}{5}, \tfrac{11}{5}\right). The leftover is (5,1)−(225,115)=(35,−65)(5, 1) - \left(\tfrac{22}{5}, \tfrac{11}{5}\right) = \left(\tfrac35, -\tfrac65\right); check it is perpendicular to the line: 35⋅2−65⋅1=0\tfrac35\cdot2 - \tfrac65\cdot1 = 0 ✓. The distance is its length: 925+3625=4525=355≈1.34\sqrt{\tfrac{9}{25} + \tfrac{36}{25}} = \sqrt{\tfrac{45}{25}} = \tfrac{3\sqrt5}{5} \approx 1.34. Cross-check with the point-to-line formula for x−2y=0x - 2y = 0: ∣5−2∣1+4=35\tfrac{|5 - 2|}{\sqrt{1 + 4}} = \tfrac{3}{\sqrt5} ✓.

Orthogonal Bases and Gram–Schmidt

Tip
Coordinates without solving

If u1,…,uk\mathbf u_1, \ldots, \mathbf u_k are mutually orthogonal and nonzero, the projection of b\mathbf b onto their span is the sum of the one-dimensional projections:

p=b⋅u1u1⋅u1u1+⋯+b⋅ukuk⋅ukuk.\mathbf p = \frac{\mathbf b\cdot\mathbf u_1}{\mathbf u_1\cdot\mathbf u_1}\mathbf u_1 + \cdots + \frac{\mathbf b\cdot\mathbf u_k}{\mathbf u_k\cdot\mathbf u_k}\mathbf u_k.

Gram–Schmidt manufactures such a basis: keep u1=a1\mathbf u_1 = \mathbf a_1, then from each next vector subtract its projections onto the u\mathbf u's already built.

Orthogonality makes the pieces independent. To check that b−p\mathbf b - \mathbf p is perpendicular to u1\mathbf u_1, dot it with u1\mathbf u_1: every term with uj\mathbf u_j, j≠1j \ne 1, vanishes, and the u1\mathbf u_1 term was built to cancel b⋅u1\mathbf b\cdot\mathbf u_1. So each coefficient can be found alone, with one dot product, instead of solving a system. Gram–Schmidt works for the same reason: subtracting the projections leaves exactly the part of the new vector perpendicular to everything before it, and the span never changes along the way.

Example
Worked: Gram–Schmidt on two vectors in R³

Make a1=(1,1,0)\mathbf a_1 = (1, 1, 0), a2=(1,0,1)\mathbf a_2 = (1, 0, 1) orthogonal. Keep u1=a1\mathbf u_1 = \mathbf a_1. The projection of a2\mathbf a_2 onto u1\mathbf u_1 has coefficient a2⋅u1u1⋅u1=12\tfrac{\mathbf a_2\cdot\mathbf u_1}{\mathbf u_1\cdot\mathbf u_1} = \tfrac12.

Subtract it: u2=(1,0,1)−12(1,1,0)=(12,−12,1)\mathbf u_2 = (1, 0, 1) - \tfrac12(1, 1, 0) = \left(\tfrac12, -\tfrac12, 1\right). Check: u1⋅u2=12−12+0=0\mathbf u_1\cdot\mathbf u_2 = \tfrac12 - \tfrac12 + 0 = 0 ✓. Clearing the fraction, (1,−1,2)(1, -1, 2) points the same way and is easier to use. To make the basis orthonormal, divide by the lengths 2\sqrt2 and 6\sqrt6: 12(1,1,0)\tfrac{1}{\sqrt2}(1, 1, 0) and 16(1,−1,2)\tfrac{1}{\sqrt6}(1, -1, 2). Both still span the same plane as a1,a2\mathbf a_1, \mathbf a_2, because u2\mathbf u_2 is a combination of a2\mathbf a_2 and a1\mathbf a_1.

Example
Worked: coordinates in an orthogonal basis of R³

Write w=(3,1,2)\mathbf w = (3, 1, 2) in the orthogonal basis (1,1,0),(1,−1,0),(0,0,1)(1, 1, 0), (1, -1, 0), (0, 0, 1). The three pairwise dot products are 00, so each coefficient is one dot product divided by one squared length.

Coefficients: 3+12=2\tfrac{3 + 1}{2} = 2, then 3−12=1\tfrac{3 - 1}{2} = 1, then 21=2\tfrac{2}{1} = 2. So w=2(1,1,0)+1(1,−1,0)+2(0,0,1)\mathbf w = 2(1, 1, 0) + 1(1, -1, 0) + 2(0, 0, 1). Check: (2+1, 2−1, 2)=(3,1,2)(2 + 1,\ 2 - 1,\ 2) = (3, 1, 2) ✓. For a basis that is not orthogonal this would be a 3×33\times3 system; here it is three independent one-line computations.

Example
Worked: projecting onto a plane
u₁u₂bp
b drops perpendicularly (dashed) to its projection p in the plane of u₁ and u₂.

Project b=(1,2,3)\mathbf b = (1, 2, 3) onto the plane spanned by u1=(1,1,0)\mathbf u_1 = (1, 1, 0) and u2=(1,−1,1)\mathbf u_2 = (1, -1, 1), which are orthogonal (1−1+0=01 - 1 + 0 = 0). The coefficients are b⋅u1u1⋅u1=32\tfrac{\mathbf b\cdot\mathbf u_1}{\mathbf u_1\cdot\mathbf u_1} = \tfrac32 and b⋅u2u2⋅u2=1−2+33=23\tfrac{\mathbf b\cdot\mathbf u_2}{\mathbf u_2\cdot\mathbf u_2} = \tfrac{1 - 2 + 3}{3} = \tfrac23.

So p=32(1,1,0)+23(1,−1,1)=(136,56,23)\mathbf p = \tfrac32(1, 1, 0) + \tfrac23(1, -1, 1) = \left(\tfrac{13}{6}, \tfrac56, \tfrac23\right). The error is b−p=(−76,76,73)=76(−1,1,2)\mathbf b - \mathbf p = \left(-\tfrac76, \tfrac76, \tfrac73\right) = \tfrac76(-1, 1, 2). Check it is perpendicular to the plane: (−1,1,2)⋅(1,1,0)=0(-1, 1, 2)\cdot(1, 1, 0) = 0 ✓ and (−1,1,2)⋅(1,−1,1)=−1−1+2=0(-1, 1, 2)\cdot(1, -1, 1) = -1 - 1 + 2 = 0 ✓. The distance from b\mathbf b to the plane is ∣b−p∣=766|\mathbf b - \mathbf p| = \tfrac76\sqrt6.

Side note
Where it goes wrong: adding projections onto a non-orthogonal basis

The sum-of-projections formula needs orthogonality. The plane spanned by (1,0,0)(1, 0, 0) and (1,1,0)(1, 1, 0) is the xyxy-plane, so the projection of b=(1,2,3)\mathbf b = (1, 2, 3) is (1,2,0)(1, 2, 0). Adding the two separate projections instead gives 1⋅(1,0,0)+32(1,1,0)=(52,32,0)1\cdot(1, 0, 0) + \tfrac32(1, 1, 0) = \left(\tfrac52, \tfrac32, 0\right) — wrong, because the two directions overlap and the shared part is counted twice. Run Gram–Schmidt first (it turns (1,1,0)(1, 1, 0) into (0,1,0)(0, 1, 0)), or solve the normal equations.

Try it
Try it: Gram–Schmidt

Apply Gram–Schmidt to a1=(1,1,1)\mathbf a_1 = (1, 1, 1), a2=(1,2,3)\mathbf a_2 = (1, 2, 3), and give an orthonormal basis for their span.

Show the answer

Keep u1=(1,1,1)\mathbf u_1 = (1, 1, 1). The projection coefficient of a2\mathbf a_2 is 1+2+33=2\tfrac{1 + 2 + 3}{3} = 2, so u2=(1,2,3)−2(1,1,1)=(−1,0,1)\mathbf u_2 = (1, 2, 3) - 2(1, 1, 1) = (-1, 0, 1). Check: u1⋅u2=−1+0+1=0\mathbf u_1\cdot\mathbf u_2 = -1 + 0 + 1 = 0 ✓. Normalize: ∣u1∣=3|\mathbf u_1| = \sqrt3 and ∣u2∣=2|\mathbf u_2| = \sqrt2, so the orthonormal basis is 13(1,1,1)\tfrac{1}{\sqrt3}(1, 1, 1), 12(−1,0,1)\tfrac{1}{\sqrt2}(-1, 0, 1). The second vector is the "spread" of (1,2,3)(1, 2, 3) around its average 22 — the same subtraction statistics calls centering.

Least Squares in Practice

Tip
The recipe for a best-fit line

To fit y=mx+cy = mx + c to points (xi,yi)(x_i, y_i), i=1,…,ni = 1, \ldots, n, the normal equations ATAx^=ATbA^{\mathsf T}A\hat{\mathbf x} = A^{\mathsf T}\mathbf b with A=(x1)A = \begin{pmatrix}\mathbf x & \mathbf 1\end{pmatrix} become

(∑xi2)m+(∑xi)c=∑xiyi,(∑xi)m+n c=∑yi.\Big(\sum x_i^2\Big)m + \Big(\sum x_i\Big)c = \sum x_iy_i, \qquad \Big(\sum x_i\Big)m + n\,c = \sum y_i.

The residuals ri=yi−(mxi+c)r_i = y_i - (mx_i + c) then satisfy ∑ri=0\sum r_i = 0 and ∑xiri=0\sum x_ir_i = 0.

The two equations are the two orthogonality conditions: the residual vector is perpendicular to the column of xx-values and to the column of ones. The second one, divided by nn, says the line passes through the centroid (xˉ,yˉ)(\bar x, \bar y) of the data. Those two residual sums are also the fastest check on the arithmetic — if either is not zero, something went wrong.

Example
Worked: a line through four points
xy123123y = 0.9x − 0.1
The best-fit line y = 0.9x − 0.1; the dashed residuals 0.1, 0.2, −0.7, 0.4 sum to zero.

Fit y=mx+cy = mx + c to (0,0),(1,1),(2,1),(3,3)(0, 0), (1, 1), (2, 1), (3, 3). The sums: ∑xi=6\sum x_i = 6, ∑xi2=14\sum x_i^2 = 14, ∑yi=5\sum y_i = 5, ∑xiyi=0+1+2+9=12\sum x_iy_i = 0 + 1 + 2 + 9 = 12, n=4n = 4. So the normal equations are 14m+6c=1214m + 6c = 12 and 6m+4c=56m + 4c = 5.

Solve by Cramer's rule: the determinant is 14⋅4−6⋅6=2014\cdot4 - 6\cdot6 = 20, so m=12⋅4−6⋅520=1820=910m = \tfrac{12\cdot4 - 6\cdot5}{20} = \tfrac{18}{20} = \tfrac{9}{10} and c=14⋅5−6⋅1220=−220=−110c = \tfrac{14\cdot5 - 6\cdot12}{20} = -\tfrac{2}{20} = -\tfrac{1}{10}. The line is y=0.9x−0.1y = 0.9x - 0.1. Residuals yi−(0.9xi−0.1)y_i - (0.9x_i - 0.1): 0.1, 0.2, −0.7, 0.40.1,\ 0.2,\ -0.7,\ 0.4. Checks: they sum to 00 ✓, and ∑xiri=0+0.2−1.4+1.2=0\sum x_ir_i = 0 + 0.2 - 1.4 + 1.2 = 0 ✓. The centroid (1.5,1.25)(1.5, 1.25) is on the line: 0.9⋅1.5−0.1=1.250.9\cdot1.5 - 0.1 = 1.25 ✓.

Example
Worked: an overdetermined system

The system x+y=2x + y = 2, x−y=0x - y = 0, x+2y=5x + 2y = 5 has no solution (the first two force x=y=1x = y = 1, and then 1+2≠51 + 2 \ne 5). Find the least-squares solution. A=(111−112)A = \begin{pmatrix}1&1\\1&-1\\1&2\end{pmatrix}, b=(2,0,5)\mathbf b = (2, 0, 5), so ATA=(3226)A^{\mathsf T}A = \begin{pmatrix}3&2\\2&6\end{pmatrix} and ATb=(7,12)A^{\mathsf T}\mathbf b = (7, 12).

Solve 3x+2y=73x + 2y = 7, 2x+6y=122x + 6y = 12: the determinant is 18−4=1418 - 4 = 14, so x=7⋅6−2⋅1214=1814=97x = \tfrac{7\cdot6 - 2\cdot12}{14} = \tfrac{18}{14} = \tfrac97 and y=3⋅12−2⋅714=2214=117y = \tfrac{3\cdot12 - 2\cdot7}{14} = \tfrac{22}{14} = \tfrac{11}{7}. The residual is b−Ax^=(2−207, 0+27, 5−317)=(−67,27,47)\mathbf b - A\hat{\mathbf x} = \left(2 - \tfrac{20}{7},\ 0 + \tfrac27,\ 5 - \tfrac{31}{7}\right) = \left(-\tfrac67, \tfrac27, \tfrac47\right). Check against both columns: −6+2+47=0\tfrac{-6 + 2 + 4}{7} = 0 ✓ and −6−2+87=0\tfrac{-6 - 2 + 8}{7} = 0 ✓. The smallest possible squared error is 36+4+1649=87\tfrac{36 + 4 + 16}{49} = \tfrac87.

Side note
Where it goes wrong: dropping an inconvenient equation

Faced with three equations in two unknowns, it is tempting to solve the first two exactly and ignore the third: x=y=1x = y = 1. That fits two equations perfectly and misses the third by 5−3=25 - 3 = 2, a squared error of 44. The least-squares answer (97,117)(\tfrac97, \tfrac{11}{7}) misses all three a little, with total squared error 87≈1.14\tfrac87 \approx 1.14. Least squares spreads the error; dropping data concentrates it.

Try it
Try it: fit a line

Find the least-squares line y=mx+cy = mx + c through (0,2),(1,1),(2,3)(0, 2), (1, 1), (2, 3), and check the residuals.

Show the answer

Sums: ∑xi=3\sum x_i = 3, ∑xi2=5\sum x_i^2 = 5, ∑yi=6\sum y_i = 6, ∑xiyi=0+1+6=7\sum x_iy_i = 0 + 1 + 6 = 7, n=3n = 3. Normal equations: 5m+3c=75m + 3c = 7 and 3m+3c=63m + 3c = 6. Subtracting, 2m=12m = 1, so m=12m = \tfrac12; then 3c=6−323c = 6 - \tfrac32, so c=32c = \tfrac32. The line is y=12x+32y = \tfrac12x + \tfrac32. Residuals: 2−32=122 - \tfrac32 = \tfrac12, 1−2=−11 - 2 = -1, 3−52=123 - \tfrac52 = \tfrac12. They sum to 00 ✓, and ∑xiri=0−1+1=0\sum x_ir_i = 0 - 1 + 1 = 0 ✓.

Proofs & Why It Matters

Tip
Proof: the projection formula

Seek the multiple cvc\mathbf v of v\mathbf v making the remainder u−cv\mathbf u - c\mathbf v orthogonal to v\mathbf v: (u−cv)⋅v=0(\mathbf u - c\mathbf v)\cdot\mathbf v = 0 gives c=u⋅vv⋅vc = \dfrac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v} in one step.

This cvc\mathbf v is also the closest point on the line: for any other multiple tvt\mathbf v, Pythagoras gives ∣u−tv∣2=∣u−cv∣2+∣cv−tv∣2≥∣u−cv∣2|\mathbf u - t\mathbf v|^2 = |\mathbf u - c\mathbf v|^2 + |c\mathbf v - t\mathbf v|^2 \ge |\mathbf u - c\mathbf v|^2. ■\blacksquare

Tip
Proof: the normal equations of least squares

The best x^\hat{\mathbf x} minimizes ∣Ax−b∣|A\mathbf x - \mathbf b|, i.e. makes Ax^A\hat{\mathbf x} the closest point to b\mathbf b in the column space. Closest means the error b−Ax^\mathbf b - A\hat{\mathbf x} is orthogonal to every column of AA (else sliding along that column would shrink it — the projection argument again).

Orthogonality to all columns at once is AT(b−Ax^)=0A^{\mathsf T}(\mathbf b - A\hat{\mathbf x}) = \mathbf 0, i.e. ATAx^=ATbA^{\mathsf T}A\hat{\mathbf x} = A^{\mathsf T}\mathbf b. Fitting a constant to data makes this one equation whose solution is the mean. ■\blacksquare

Going Deeper: Explanations & Worked Problems

Concept
Decomposing a vector: the along-and-across split

The projection formula does more than find shadows — it splits any u\mathbf u into two orthogonal pieces relative to a direction v\mathbf v: the part along, projvu=u⋅vv⋅vv\text{proj}_{\mathbf v}\mathbf u = \tfrac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v}\mathbf v, and the part across, u−projvu\mathbf u - \text{proj}_{\mathbf v}\mathbf u, which is orthogonal to v\mathbf v by construction. Pythagoras then bookkeeps the lengths: ∣u∣2=∣along∣2+∣across∣2|\mathbf u|^2 = |\text{along}|^2 + |\text{across}|^2. The "across" length is the distance from the point to the line — that is where the point-to-line distance formula comes from. Iterating the split against several directions in turn is Gram–Schmidt: peel off the component along each previously built direction, keep what remains, normalize — manufacturing an orthogonal basis from any basis, one subtraction at a time.

Reminder — Distance and midpoint:  d=(x2−x1)2+(y2−y1)2,M=(x1+x22, y1+y22)d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}, \qquad M=\left(\frac{x_1+x_2}{2},\ \frac{y_1+y_2}{2}\right)
Reminder — The Pythagorean theorem:  a2+b2=c2a^2+b^2=c^2

Why that particular multiple: write the along part as cvc\mathbf v and demand that the remainder be perpendicular to v\mathbf v. Then (u−cv)⋅v=u⋅v−c v⋅v=0(\mathbf u - c\mathbf v)\cdot\mathbf v = \mathbf u\cdot\mathbf v - c\,\mathbf v\cdot\mathbf v = 0, which has exactly one solution, c=u⋅vv⋅vc = \tfrac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v} — the formula is forced, not chosen. For u=(3,4)\mathbf u = (3, 4) and v=(1,1)\mathbf v = (1, 1): c=72c = \tfrac72, the along part is (72,72)(\tfrac72, \tfrac72), the across part is (−12,12)(-\tfrac12, \tfrac12), and the Pythagorean bookkeeping checks: 492+12=25=∣u∣2\tfrac{49}{2} + \tfrac12 = 25 = |\mathbf u|^2.

Example
Worked: project, split, and measure — one example, all the way

Take u=(3,4)\mathbf u = (3, 4) and direction v=(1,1)\mathbf v = (1, 1).

Step 1 — the coefficient: c=u⋅vv⋅v=3+41+1=72c = \dfrac{\mathbf u\cdot\mathbf v}{\mathbf v\cdot\mathbf v} = \dfrac{3 + 4}{1 + 1} = \dfrac72.

Step 2 — the along part: 72(1,1)=(72,72)\tfrac72(1,1) = \left(\tfrac72, \tfrac72\right).

Step 3 — the across part: (3,4)−(72,72)=(−12,12)(3,4) - \left(\tfrac72,\tfrac72\right) = \left(-\tfrac12, \tfrac12\right); check orthogonality: (−12)(1)+12(1)=0\left(-\tfrac12\right)(1) + \tfrac12(1) = 0 ✓.

Step 4 — distance from (3,4)(3,4) to the line y=xy = x: the across length, 14+14=22\sqrt{\tfrac14 + \tfrac14} = \tfrac{\sqrt2}{2} — matching the point-to-line formula ∣3−4∣2\tfrac{|3-4|}{\sqrt2} ✓.

Step 5 — Pythagoras audit: (722)2+(22)2=492+12=25=∣u∣2\left(\tfrac{7\sqrt2}{2}\right)^2 + \left(\tfrac{\sqrt2}{2}\right)^2 = \tfrac{49}{2} + \tfrac12 = 25 = |\mathbf u|^2 ✓. Five steps, three self-checks — the decomposition verifies itself.

Example
Worked: an actual least-squares line, normal equations and all
xy121234y = 3x/2 + 5/6
The least-squares line y = 3x/2 + 5/6 with its residuals 1/6, −1/3, 1/6 (dashed).

Fit a line y=mx+cy = mx + c to the points (0,1),(1,2),(2,4)(0, 1), (1, 2), (2, 4), which do not lie on one line.

Step 1 — the (unsolvable) system: c=1c = 1, m+c=2m + c = 2, 2m+c=42m + c = 4, i.e. A=(011121)A = \begin{pmatrix}0&1\\1&1\\2&1\end{pmatrix}, b=(1,2,4)\mathbf b = (1,2,4).

Step 2 — normal equations ATAx^=ATbA^{\mathsf T}A\hat{\mathbf x} = A^{\mathsf T}\mathbf b: compute ATA=(5333)A^{\mathsf T}A = \begin{pmatrix}5&3\\3&3\end{pmatrix} and ATb=(107)A^{\mathsf T}\mathbf b = \begin{pmatrix}10\\7\end{pmatrix}.

Step 3 — solve: 5m+3c=105m + 3c = 10 and 3m+3c=73m + 3c = 7; subtracting, 2m=32m = 3, m=32m = \tfrac32, then c=7−4.53=56c = \tfrac{7 - 4.5}{3} = \tfrac56.

Step 4 — the fitted line y=32x+56y = \tfrac32x + \tfrac56 predicts 56,73,236\tfrac56, \tfrac{7}{3}, \tfrac{23}{6}; residuals 16,−13,16\tfrac16, -\tfrac13, \tfrac16 sum to 00 — as they must, since orthogonality of the error to the all-ones column is literally one of the normal equations.