← Linear Algebra
Contents · Linear Algebra

Part 1 · Vectors & Spaces

Part 4 · Orthogonality

  • Orthogonality and Least Squares

Part 5 · Eigenvalues

Part 6 · Singular Value Decomposition

Part 4 · Orthogonality

Orthogonality and Least Squares

6 min read

Most linear systems that come from real data have no exact solution: there are more equations than unknowns, and the measurements disagree. Orthogonality turns that failure into a well-posed question. Instead of solving Ax=bAx = b exactly, we project bb onto what AA can actually reach and accept the leftover as error.

Orthogonality

Two vectors are orthogonal when their dot product vanishes,

u⋅v=u⊤v=0.u \cdot v = u^{\top} v = 0.

Geometrically the angle between them is 90∘90^{\circ}, and the Pythagorean law holds exactly: ∥u+v∥2=∥u∥2+∥v∥2\|u + v\|^2 = \|u\|^2 + \|v\|^2 precisely when u⋅v=0u \cdot v = 0. A set of nonzero, mutually orthogonal vectors is automatically independent, which is what makes orthogonal directions such convenient coordinate axes.

Projecting onto a line

Let a line through the origin be spanned by a vector aa. Any bb splits uniquely as

b=p+r,p=x^ a∈line,r=b−p⊥a.b = p + r, \qquad p = \hat{x}\,a \in \text{line}, \qquad r = b - p \perp a.

The perpendicularity condition a⋅r=0a \cdot r = 0 becomes a⋅(b−x^a)=0a \cdot (b - \hat{x} a) = 0, which we solve for the one scalar x^\hat{x}:

x^=a⋅ba⋅a,p=a⋅ba⋅a a.\hat{x} = \frac{a \cdot b}{a \cdot a}, \qquad p = \frac{a \cdot b}{a \cdot a}\, a.

As a matrix, b↦pb \mapsto p is the projection P=aa⊤a⊤aP = \dfrac{a a^{\top}}{a^{\top} a}, which satisfies P⊤=PP^{\top} = P and P2=PP^2 = P (projecting a second time changes nothing).

Worked example. Project b=(3,4)b = (3, 4) onto a=(1,1)a = (1, 1). Then a⋅b=3+4=7a \cdot b = 3 + 4 = 7 and a⋅a=1+1=2a \cdot a = 1 + 1 = 2, so

x^=72=3.5,p=3.5 (1,1)=(3.5, 3.5),r=b−p=(−0.5, 0.5).\hat{x} = \frac{7}{2} = 3.5, \qquad p = 3.5\,(1, 1) = (3.5,\ 3.5), \qquad r = b - p = (-0.5,\ 0.5).

Check the right angle: r⋅a=−0.5+0.5=0r \cdot a = -0.5 + 0.5 = 0. The residual is perpendicular to the line, as it must be.

Projecting b onto a lineinteractive
b
(2.10, 0.50)
p
(1.30, 1.30)
r = b − p
(0.80, -0.80)
r · a
-0.000

Drag the point b. Its projection p is the closest point on the line, and the residual r meets the line at a right angle, so r · a stays ~0.

Aim the line with the angle slider (unit vector a) and drag the point b. Green p is its projection, the closest point on the line; the dashed red residual r meets the line at a right angle, so r · a stays ~0.

Projecting onto a subspace

Replace the single direction aa by the columns of a matrix AA, taken as a basis for a subspace SS. Now p=Ax^p = A\hat{x}, and the residual r=b−Ax^r = b - A\hat{x} must be orthogonal to every column at once, i.e. A⊤r=0A^{\top} r = 0:

A⊤(b−Ax^)=0  ⟹  A⊤A x^=A⊤b.A^{\top}(b - A\hat{x}) = 0 \;\Longrightarrow\; A^{\top} A\, \hat{x} = A^{\top} b.

When AA has independent columns, A⊤AA^{\top}A is invertible, so

x^=(A⊤A)−1A⊤b,p=A(A⊤A)−1A⊤b=Pb.\hat{x} = (A^{\top} A)^{-1} A^{\top} b, \qquad p = A(A^{\top} A)^{-1} A^{\top} b = P b.

The line formula is just the 11-column case: A=aA = a makes A⊤A=a⋅aA^{\top}A = a \cdot a a scalar.

Least squares and the normal equations

When Ax=bAx = b has no solution, bb lies outside the column space of AA. The least-squares choice is the x^\hat{x} that minimizes the squared error

∥Ax−b∥2.\|A x - b\|^2.

Setting its gradient to zero reproduces exactly the same normal equations,

A⊤A x^=A⊤b.A^{\top} A\, \hat{x} = A^{\top} b.

This is no coincidence: the minimizer makes the residual r=b−Ax^r = b - A\hat{x} orthogonal to the column space, so p=Ax^p = A\hat{x} is the closest point of SS to bb. Any other point in SS adds a component along the subspace, which by Pythagoras only lengthens rr.

Worked example. Take

A=[111−110],b=[240].A = \begin{bmatrix} 1 & 1 \\ 1 & -1 \\ 1 & 0 \end{bmatrix}, \qquad b = \begin{bmatrix} 2 \\ 4 \\ 0 \end{bmatrix}.

The columns are orthogonal, so A⊤AA^{\top}A is diagonal:

A⊤A=[3002],A⊤b=[6−2],x^=[2−1].A^{\top} A = \begin{bmatrix} 3 & 0 \\ 0 & 2 \end{bmatrix}, \qquad A^{\top} b = \begin{bmatrix} 6 \\ -2 \end{bmatrix}, \qquad \hat{x} = \begin{bmatrix} 2 \\ -1 \end{bmatrix}.

Then p=Ax^=(1,3,2)p = A\hat{x} = (1, 3, 2) and r=b−p=(1,1,−2)r = b - p = (1, 1, -2). Both columns are orthogonal to the residual: r⋅(1,1,1)=0r \cdot (1,1,1) = 0 and r⋅(1,−1,0)=0r \cdot (1,-1,0) = 0, which confirms pp is the projection.

Orthonormal bases: Gram-Schmidt and QR

The algebra collapses when the basis is orthonormal, since then A⊤A=IA^{\top}A = I. Gram-Schmidt builds such a basis from any independent set by subtracting off the projection onto the directions already chosen, then normalizing. Collecting the results as the columns of QQ (with Q⊤Q=IQ^{\top} Q = I) and the coefficients in an upper-triangular RR gives the factorization A=QRA = QR. The normal equations then reduce to Rx^=Q⊤bR\hat{x} = Q^{\top} b, solved by back-substitution with no matrix inverse and far better numerical behavior.1

Footnotes

  1. Strang, Introduction to Linear Algebra (5th ed., 2016). ↩