Linear AlgebraOrthogonality and Least Squares

Orthogonal Projections

Find the closest point in a subspace.

Not started Jump to practice

What is the closest point of a subspace to a given vector? In the plane, drop a perpendicular. In any dimension, the answer is the orthogonal projection, and it rests on the splitting $\mathbb{R}^n = W \oplus W^{\perp}$ from orthogonal complements and on a basis of $W$.

Onto a line. The projection of $\mathbf{y}$ onto the line through $\mathbf{u}$ is $$\hat{\mathbf{y}} = \frac{\mathbf{y}\cdot\mathbf{u}}{\mathbf{u}\cdot\mathbf{u}}\mathbf{u},$$ and $\mathbf{z} = \mathbf{y} - \hat{\mathbf{y}}$ is orthogonal to $\mathbf{u}$ (check: $\mathbf{z}\cdot\mathbf{u}$ $= \mathbf{y}\cdot\mathbf{u} - \frac{\mathbf{y}\cdot\mathbf{u}}{\mathbf{u}\cdot\mathbf{u}}\mathbf{u}\cdot\mathbf{u}$ $= 0$).

Key idea

If $\{\mathbf{u}_1, \dots, \mathbf{u}_p\}$ is an orthogonal basis of $W$, the projection of $\mathbf{y}$ onto $W$ is $$\hat{\mathbf{y}} = \frac{\mathbf{y}\cdot\mathbf{u}_1}{\mathbf{u}_1\cdot\mathbf{u}_1}\mathbf{u}_1 + \cdots + \frac{\mathbf{y}\cdot\mathbf{u}_p}{\mathbf{u}_p\cdot\mathbf{u}_p}\mathbf{u}_p.$$ Then $\mathbf{y} - \hat{\mathbf{y}}$ is in $W^{\perp}$, and $\hat{\mathbf{y}}$ is the point of $W$ closest to $\mathbf{y}$.

Why it works

Why closest? Take any $\mathbf{v}$ in $W$. Then $\mathbf{y} - \mathbf{v}$ $= (\mathbf{y} - \hat{\mathbf{y}}) + (\hat{\mathbf{y}} - \mathbf{v})$. The first piece is in $W^{\perp}$ and the second is in $W$, so they are orthogonal and the Pythagorean theorem gives $\lVert\mathbf{y} - \mathbf{v}\rVert^{2}$ $= \lVert\mathbf{y} - \hat{\mathbf{y}}\rVert^{2} + \lVert\hat{\mathbf{y}} - \mathbf{v}\rVert^{2}$. That is smallest exactly when $\mathbf{v} = \hat{\mathbf{y}}$. And why does the formula give a vector with $\mathbf{y} - \hat{\mathbf{y}}$ in $W^{\perp}$? Dot $\mathbf{y} - \hat{\mathbf{y}}$ with $\mathbf{u}_1$: because $\mathbf{u}_1\cdot\mathbf{u}_j = 0$ for $j \neq 1$, only the first term of $\hat{\mathbf{y}}$ survives, and it cancels $\mathbf{y}\cdot\mathbf{u}_1$ exactly.

Worked example: Projection onto a plane

Project $\mathbf{y} = \begin{bmatrix} 1 \\ 3 \\ 5 \end{bmatrix}$ onto $W = \operatorname{Span}\{\mathbf{u}_1, \mathbf{u}_2\}$ with $\mathbf{u}_1 = \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix}$, $\mathbf{u}_2 = \begin{bmatrix} -1 \\ 1 \\ 2 \end{bmatrix}$, and find the distance from $\mathbf{y}$ to $W$.
  1. First check the basis is orthogonal: $\mathbf{u}_1\cdot\mathbf{u}_2 = -1 + 1 + 0 = 0$. ✓
  2. $\dfrac{\mathbf{y}\cdot\mathbf{u}_1}{\mathbf{u}_1\cdot\mathbf{u}_1} = \dfrac{4}{2} = 2$ and $\dfrac{\mathbf{y}\cdot\mathbf{u}_2}{\mathbf{u}_2\cdot\mathbf{u}_2} = \dfrac{12}{6} = 2$.
  3. $\hat{\mathbf{y}}$ $= 2\begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix} + 2\begin{bmatrix} -1 \\ 1 \\ 2 \end{bmatrix}$ $= \begin{bmatrix} 0 \\ 4 \\ 4 \end{bmatrix}$.
  4. $\mathbf{z} = \mathbf{y} - \hat{\mathbf{y}} = \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}$. Check: $\mathbf{z}\cdot\mathbf{u}_1 = 0$ and $\mathbf{z}\cdot\mathbf{u}_2 = -1 - 1 + 2 = 0$. ✓
  5. Distance $= \lVert\mathbf{z}\rVert = \sqrt{3}$.

Answer: $\hat{\mathbf{y}} = (0, 4, 4)$; distance $\sqrt{3}$

The projection $\hat{\mathbf{y}}$ lies in the plane $W$; the leftover $\mathbf{z}$ meets $W$ at a right angle. Turn the picture and edit $\mathbf{y}$.

Worked example: Projection onto a line

Project $\mathbf{y} = (7, 6)$ onto the line through $\mathbf{u} = (4, 2)$.
  1. $\mathbf{y}\cdot\mathbf{u} = 28 + 12 = 40$ and $\mathbf{u}\cdot\mathbf{u} = 20$.
  2. $\hat{\mathbf{y}} = 2(4, 2) = (8, 4)$, and $\mathbf{y} - \hat{\mathbf{y}} = (-1, 2)$, which is orthogonal to $(4, 2)$. ✓

Answer: $(8, 4)$

Common mistake

Projecting onto each vector of a non-orthogonal basis and adding. The formula above needs an orthogonal basis; with a slanted basis the pieces overlap and the sum overshoots. Example: $W$ = the $xy$-plane with basis $(1, 0, 0)$, $(1, 1, 0)$ and $\mathbf{y} = (0, 2, 1)$. The true projection is $(0, 2, 0)$, but the separate projections are $(0, 0, 0)$ and $(1, 1, 0)$, which add to $(1, 1, 0)$: wrong. Fix it by making the basis orthogonal first (Gram–Schmidt) or by solving the normal equations of least squares. Check: $\mathbf{y} - \hat{\mathbf{y}}$ must be orthogonal to every basis vector.
Press "Add the separate projections" to see the wrong answer; then "Make the basis orthogonal" and press it again: now the two agree.
With an orthonormal basis (orthogonal unit vectors) as the columns of $U$, the formula becomes $\hat{\mathbf{y}} = UU^{T}\mathbf{y}$: the matrix $UU^{T}$ is the projection matrix onto $W$.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • Margalit and Rabinoff, Interactive Linear Algebra (Georgia Tech), §6.3 Orthogonal Projection
  • MIT OpenCourseWare 18.06 Linear Algebra (Gilbert Strang, Spring 2010), Lecture 15: Projections onto Subspaces
@teddyrookbook GitHub