Tutorials › Linear Algebra › Finding The Closest Vector In A Subspace

Orthogonal Complements and Projections · Tutorial 677 of 1000

Finding The Closest Vector In A Subspace

Use projection formulas to find a subspace’s closest vector and confirm the result with an orthogonal-residual check.

Intermediate 10 min read

What You'll Learn

  • Identify the closest vector using projection onto a subspace.
  • Check a candidate projection by testing whether its residual is orthogonal to the subspace.
  • Project onto a line using a scalar coefficient.
  • Project onto a plane using a normal vector.
  • Apply the projection formula to a full-column-rank matrix.
  • Handle redundant column generators by selecting an independent basis.

Closest Points and Orthogonal Residuals

Suppose a vector \(\mathbf{x}\in\mathbb{R}^n\) does not lie in a subspace \(S\). A natural approximation is a vector in \(S\) that is as close as possible to \(\mathbf{x}\). The key is not to compare \(\mathbf{x}\) with every vector in \(S\). Instead, find a candidate whose error points perpendicular to the whole subspace. That perpendicular error is called the residual.

Throughout, \(\mathbb{R}^n\) has the standard dot product \(\mathbf{u}\cdot\mathbf{v}=\mathbf{u}^{\top}\mathbf{v}\) and norm \(\|\mathbf{u}\|=\sqrt{\mathbf{u}\cdot\mathbf{u}}\). A vector \(\mathbf{r}\) is orthogonal to a subspace \(S\) if \(\mathbf{r}\cdot\mathbf{s}=0\) for every \(\mathbf{s}\in S\). For a candidate \(\mathbf{p}\in S\), the residual for approximating \(\mathbf{x}\) by \(\mathbf{p}\) is \(\mathbf{x}-\mathbf{p}\).

Definition: Let \(S\) be a subspace of \(\mathbb{R}^n\). A vector \(\mathbf{p}\in S\) is a closest vector in \(S\) to \(\mathbf{x}\) if \(\|\mathbf{x}-\mathbf{p}\|\leq\|\mathbf{x}-\mathbf{y}\|\) for every \(\mathbf{y}\in S\). The orthogonal projection of \(\mathbf{x}\) onto \(S\), written \(\operatorname{proj}_S\mathbf{x}\), is this closest vector.

The characterization below is the practical test for a proposed answer: check that it belongs to the target subspace, then check that the residual is perpendicular to that subspace. It also proves that a vector passing both checks really is the closest one.

Theorem (Orthogonal residual characterization): Let \(S\) be a subspace of \(\mathbb{R}^n\), and let \(\mathbf{x}\in\mathbb{R}^n\). A vector \(\mathbf{p}\) is the closest vector in \(S\) to \(\mathbf{x}\) if and only if \(\mathbf{p}\in S\) and \(\mathbf{x}-\mathbf{p}\) is orthogonal to \(S\). If such a vector exists, it is unique.

Proof. First suppose \(\mathbf{p}\in S\) and \(\mathbf{r}=\mathbf{x}-\mathbf{p}\) is orthogonal to \(S\). For any \(\mathbf{y}\in S\), the difference \(\mathbf{p}-\mathbf{y}\) belongs to \(S\), so \(\mathbf{r}\cdot(\mathbf{p}-\mathbf{y})=0\). Since \(\mathbf{x}-\mathbf{y}=\mathbf{r}+(\mathbf{p}-\mathbf{y})\), expansion of the dot product gives \(\|\mathbf{x}-\mathbf{y}\|^2=\|\mathbf{r}\|^2+\|\mathbf{p}-\mathbf{y}\|^2\). The second term is nonnegative, and it is zero exactly when \(\mathbf{y}=\mathbf{p}\). Thus \(\mathbf{p}\) is the unique closest vector.

Conversely, suppose \(\mathbf{p}\in S\) is closest, and set \(\mathbf{r}=\mathbf{x}-\mathbf{p}\). Fix any \(\mathbf{w}\in S\). For every real \(t\), \(\mathbf{p}+t\mathbf{w}\in S\), so minimality at \(\mathbf{p}\) implies \(\|\mathbf{r}\|^2\leq\|\mathbf{r}-t\mathbf{w}\|^2 =\|\mathbf{r}\|^2-2t(\mathbf{r}\cdot\mathbf{w})+t^2\|\mathbf{w}\|^2\). If \(\mathbf{w}=\mathbf{0}\), then \(\mathbf{r}\cdot\mathbf{w}=0\). If \(\mathbf{w}\ne\mathbf{0}\) and \(\mathbf{r}\cdot\mathbf{w}\ne0\), choose \(t=(\mathbf{r}\cdot\mathbf{w})/\|\mathbf{w}\|^2\). Substitution makes the right side \(\|\mathbf{r}\|^2-(\mathbf{r}\cdot\mathbf{w})^2/\|\mathbf{w}\|^2\), which is strictly less than \(\|\mathbf{r}\|^2\), a contradiction. Therefore \(\mathbf{r}\cdot\mathbf{w}=0\) for every \(\mathbf{w}\in S\). This proves both directions and uniqueness. \(\square\)

A Reliable Method for Finding the Projection

The theorem turns a nearest-vector problem into a construction problem. Choose a convenient way to describe the subspace, find a vector in it, and impose orthogonality on the residual. For a line, this leads to a single scalar coefficient. For a plane in \(\mathbb{R}^3\), a normal vector often gives the shortest route. For a column space, an orthonormal basis or the full-column-rank projection formula is useful.

1
Describe the subspace.
Write it as a line, a span of vectors, or \(\operatorname{Col}\mathbf{A}\), as appropriate.
2
Construct a candidate in the subspace.
Use a scalar multiple for a line, a span representation for a plane, or a projection formula for a column space.
3
Check the residual.
Compute \(\mathbf{x}-\mathbf{p}\) and verify that it is orthogonal to the subspace’s spanning vectors. The theorem then guarantees that the candidate is the unique closest vector.

If \(\mathbf{q}_1,\dots,\mathbf{q}_r\) form an orthonormal basis of \(S\), meaning that each has norm \(1\) and distinct vectors have dot product \(0\), the projection has a particularly direct form.

Projection formula for an orthonormal basis: If \(S=\operatorname{span}\{\mathbf{q}_1,\dots,\mathbf{q}_r\}\) and the listed vectors are orthonormal, then \(\operatorname{proj}_S\mathbf{x}=\sum_{j=1}^{r}(\mathbf{x}\cdot\mathbf{q}_j)\mathbf{q}_j\).

Indeed, the formula produces a vector in \(S\). Its residual has dot product zero with each basis vector: the dot product with \(\mathbf{q}_k\) is \(\mathbf{x}\cdot\mathbf{q}_k-\sum_j(\mathbf{x}\cdot\mathbf{q}_j)(\mathbf{q}_j\cdot\mathbf{q}_k)=0\), since \(\mathbf{q}_j\cdot\mathbf{q}_k\) equals \(1\) when \(j=k\) and \(0\) otherwise. By the theorem, the formula gives the closest vector.

Projecting onto a Line

Worked Example: A Line in \(\mathbb{R}^3\)

Find the closest vector in \(L=\operatorname{span}\{(1,2,2)^{\top}\}\) to \(\mathbf{x}=(2,0,1)^{\top}\), and check the residual.

Write \(\mathbf{u}=(1,2,2)^{\top}\). Every vector in \(L\) has the form \(c\mathbf{u}\). To make the residual \(\mathbf{x}-c\mathbf{u}\) perpendicular to \(L\), require its dot product with \(\mathbf{u}\) to be zero:

$$ \mathbf{u}\cdot(\mathbf{x}-c\mathbf{u})=0,\qquad 4-9c=0,\qquad c=\frac{4}{9}. $$

Here \(\mathbf{u}\cdot\mathbf{x}=1(2)+2(0)+2(1)=4\), and \(\mathbf{u}\cdot\mathbf{u}=1^2+2^2+2^2=9\). Thus the candidate and residual are

$$ \mathbf{p}=\frac{4}{9}\mathbf{u} =\begin{bmatrix}4/9\\8/9\\8/9\end{bmatrix}, \qquad \mathbf{x}-\mathbf{p} =\begin{bmatrix}14/9\\-8/9\\1/9\end{bmatrix}. $$

Check: \(\mathbf{p}\in L\), and the residual is orthogonal to the line’s generator: \(\mathbf{u}\cdot(\mathbf{x}-\mathbf{p})=14/9-16/9+2/9=0\). Every vector in \(L\) is a multiple of \(\mathbf{u}\), so the residual is orthogonal to all of \(L\). The characterization proves that \(\mathbf{p}\) is the closest vector.

This calculation also explains the line projection formula. For a nonzero generator \(\mathbf{u}\), the candidate is \(c\mathbf{u}\), and the residual condition gives \(c=(\mathbf{x}\cdot\mathbf{u})/(\mathbf{u}\cdot\mathbf{u})\). The denominator is nonzero because \(\mathbf{u}\ne\mathbf{0}\). If the line is instead described by an orthonormal vector \(\mathbf{q}\), then \(\mathbf{q}\cdot\mathbf{q}=1\), so the coefficient reduces to \(\mathbf{x}\cdot\mathbf{q}\).

Projecting onto a Plane

Worked Example: A Plane Given by Two Spanning Vectors

Find the closest vector in \(P=\operatorname{span}\{(1,1,0)^{\top},(1,0,1)^{\top}\}\) to \(\mathbf{x}=(2,1,3)^{\top}\), and verify the residual.

A normal vector to the plane is \(\mathbf{n}=(-1,1,1)^{\top}\): its dot products with the two spanning vectors are \(-1+1=0\) and \(-1+1=0\). The plane is the set of vectors perpendicular to \(\mathbf{n}\). The residual must be parallel to \(\mathbf{n}\), so subtract the component of \(\mathbf{x}\) in the normal direction.

$$ \mathbf{x}\cdot\mathbf{n}=-2+1+3=2,\qquad \mathbf{n}\cdot\mathbf{n}=3,\qquad \mathbf{r}=\frac{2}{3}\mathbf{n} =\begin{bmatrix}-2/3\\2/3\\2/3\end{bmatrix}. $$

Subtracting this residual gives

$$ \mathbf{p}=\mathbf{x}-\mathbf{r} =\begin{bmatrix}8/3\\1/3\\7/3\end{bmatrix} =\frac{1}{3}\begin{bmatrix}1\\1\\0\end{bmatrix} +\frac{7}{3}\begin{bmatrix}1\\0\\1\end{bmatrix}. $$

Check: The displayed combination shows \(\mathbf{p}\in P\). The residual is orthogonal to both generators: \((-2/3)+(2/3)=0\) and \((-2/3)+(2/3)=0\). Hence it is orthogonal to every linear combination of those generators, and the theorem confirms that \(\mathbf{p}\) is the closest vector in \(P\).

A normal vector is especially convenient when a plane is described by a single equation. For example, if \(P=\{\mathbf{y}:\mathbf{n}\cdot\mathbf{y}=0\}\) with \(\mathbf{n}\ne\mathbf{0}\), then \(\mathbf{x}-\operatorname{proj}_P\mathbf{x}\) lies in the normal direction. In the example, this let us find the residual first, then obtain the projected vector by subtraction.

Projecting onto a Column Space

The column space \(\operatorname{Col}\mathbf{A}\) is the set of all linear combinations of the columns of \(\mathbf{A}\). If \(\mathbf{A}\in\mathbb{R}^{m\times n}\) has linearly independent columns, the projection matrix for this space is \(\mathbf{A}(\mathbf{A}^{\top}\mathbf{A})^{-1}\mathbf{A}^{\top}\). The independence condition matters: it ensures \(\mathbf{A}^{\top}\mathbf{A}\) is invertible. Equivalently, if \(\mathbf{A}\) has orthonormal columns, this expression simplifies to \(\mathbf{A}\mathbf{A}^{\top}\). If the given columns are dependent, choose an independent set of columns that spans the same column space, or use an orthonormal basis for that space; do not apply the inverse formula to a singular Gram matrix.

Worked Example: A Vector Projected onto a Column Space

Let \(\mathbf{A}=\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}\) and \(\mathbf{x}=(1,2,0)^{\top}\). Find the closest vector to \(\mathbf{x}\) in \(\operatorname{Col}\mathbf{A}\).

The two columns of \(\mathbf{A}\) are independent: if \(c_1(1,0,1)^{\top}+c_2(0,1,1)^{\top}=\mathbf{0}\), the first coordinate gives \(c_1=0\) and the second gives \(c_2=0\). Thus the full-column-rank projection formula applies. First compute

$$ \mathbf{A}^{\top}\mathbf{A} =\begin{bmatrix}2&1\\1&2\end{bmatrix}, \qquad (\mathbf{A}^{\top}\mathbf{A})^{-1} =\frac{1}{3}\begin{bmatrix}2&-1\\-1&2\end{bmatrix}, \qquad \mathbf{A}^{\top}\mathbf{x} =\begin{bmatrix}1\\2\end{bmatrix}. $$

The coefficients for the projected vector are therefore

$$ (\mathbf{A}^{\top}\mathbf{A})^{-1}\mathbf{A}^{\top}\mathbf{x} =\frac{1}{3} \begin{bmatrix}2&-1\\-1&2\end{bmatrix} \begin{bmatrix}1\\2\end{bmatrix} =\begin{bmatrix}0\\1\end{bmatrix}. $$

Multiplying by \(\mathbf{A}\) gives the candidate:

$$ \mathbf{p} =\mathbf{A}\begin{bmatrix}0\\1\end{bmatrix} =\begin{bmatrix}0\\1\\1\end{bmatrix}, \qquad \mathbf{x}-\mathbf{p} =\begin{bmatrix}1\\1\\-1\end{bmatrix}. $$

Check: The vector \(\mathbf{p}\) is the second column of \(\mathbf{A}\), so it lies in \(\operatorname{Col}\mathbf{A}\). The residual is orthogonal to both columns: \((1,0,1)\cdot(1,1,-1)=1-1=0\) and \((0,1,1)\cdot(1,1,-1)=1-1=0\). Thus it is orthogonal to their entire column space, and the theorem proves that \(\mathbf{p}\) is the unique closest vector.

The projection formula can be checked through this same residual test. For a full-column-rank matrix, let \(\mathbf{c}=(\mathbf{A}^{\top}\mathbf{A})^{-1}\mathbf{A}^{\top}\mathbf{x}\) and \(\mathbf{p}=\mathbf{A}\mathbf{c}\). Then \(\mathbf{A}^{\top}(\mathbf{x}-\mathbf{p})=\mathbf{A}^{\top}\mathbf{x}-\mathbf{A}^{\top}\mathbf{A}\mathbf{c}=\mathbf{0}\). The entries of this product are the dot products of the residual with the columns of \(\mathbf{A}\). Consequently the residual is orthogonal to every column and every vector in their span. The orthogonal residual characterization then verifies the formula’s result.

A Common Mistake: Adding Projections onto Nonorthogonal Generators

In the plane example, the two spanning vectors are not orthogonal: their dot product is \(1\). A tempting calculation is to add the individual line projections of \(\mathbf{x}\) onto each generator. That method is valid for an orthogonal list, but not for this pair.

For these generators, the separate line projections are \(\frac{3}{2}(1,1,0)^{\top}=(3/2,3/2,0)^{\top}\) and \(\frac{5}{2}(1,0,1)^{\top}=(5/2,0,5/2)^{\top}\). Adding them gives the incorrect candidate \(\widetilde{\mathbf{p}}=(4,3/2,5/2)^{\top}\). Although this vector lies in the plane, its residual is \(\mathbf{x}-\widetilde{\mathbf{p}}=(-2,-1/2,1/2)^{\top}\), and \((1,1,0)\cdot(\mathbf{x}-\widetilde{\mathbf{p}})=-5/2\ne0\). So the residual is not perpendicular to the plane, and the candidate cannot be the closest vector. The correction is to use a normal vector, an orthonormal basis, or the appropriate column-space projection formula, then verify the residual.

Check Your Understanding

For each problem, identify the subspace and use the residual test to check your result.

  1. Find the closest vector in \(\operatorname{span}\{(2,1)^{\top}\}\) to \((1,3)^{\top}\). What is the residual?
  2. Find the closest vector in the plane \(\{\mathbf{y}\in\mathbb{R}^3:y_1+y_2+y_3=0\}\) to \((1,0,2)^{\top}\).
  3. For \(\mathbf{A}=\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}\), find the closest vector in \(\operatorname{Col}\mathbf{A}\) to \((2,0,1)^{\top}\).
  4. Why does the formula \(\mathbf{A}(\mathbf{A}^{\top}\mathbf{A})^{-1}\mathbf{A}^{\top}\) require linearly independent columns?
  5. Explain why a residual orthogonal to every vector in a subspace certifies that a proposed vector in that subspace is the unique closest vector.

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon