Tutorials › Linear Algebra › Projection Onto A Line

Orthogonal Complements and Projections · Tutorial 667 of 1000

Projection Onto A Line

Learn how to calculate the projection onto a line and verify that the remainder is perpendicular to that line.

Intermediate 9 min read

What You'll Learn

  • Express a line as the span of a nonzero vector.
  • Derive the scalar that makes the projection error perpendicular to the line.
  • Apply the projection formula using exact arithmetic.
  • Handle the case when the vector being projected is zero.
  • Check a projection by verifying line membership and perpendicularity.
  • Recognize and correct a common denominator error.

The Projection Problem for a Line

A line through the origin in \(\mathbb{R}^n\) can be described by all scalar multiples of one nonzero vector. Given another vector \(\mathbf{b}\), we want to identify the vector on that line that fits \(\mathbf{b}\)'s component along the line. The defining geometric condition is that the difference between \(\mathbf{b}\) and its projection is perpendicular to the line. This condition leads directly to a formula, and also gives a simple way to check a calculation.

Throughout this tutorial, vectors are in \(\mathbb{R}^n\) with the standard dot product \(\mathbf{x}\cdot\mathbf{y}=\mathbf{x}^{\top}\mathbf{y}\). Two vectors are perpendicular, or orthogonal, when their dot product is zero. For a nonzero vector \(\mathbf{u}\), the line it spans is \(L=\operatorname{span}\{\mathbf{u}\}=\{t\mathbf{u}:t\in\mathbb{R}\}\). The requirement \(\mathbf{u}\ne\mathbf{0}\) matters: if \(\mathbf{u}=\mathbf{0}\), its span is just \(\{\mathbf{0}\}\), not a line.

Definition: The projection of \(\mathbf{b}\) onto the line \(L=\operatorname{span}\{\mathbf{u}\}\), where \(\mathbf{u}\ne\mathbf{0}\), is the vector \(\mathbf{p}\in L\) such that \(\mathbf{b}-\mathbf{p}\) is perpendicular to every vector in \(L\). The vector \(\mathbf{b}-\mathbf{p}\) is the residual, or projection error.

To find \(\mathbf{p}\), write it as \(t\mathbf{u}\), since it must lie in \(L\). Every vector in the line is a multiple \(s\mathbf{u}\). The residual \( \mathbf{b}-t\mathbf{u}\) is perpendicular to every such multiple exactly when it is perpendicular to \(\mathbf{u}\): taking \(s=1\) gives the necessary condition, and multiplying a zero dot product by any \(s\) gives the sufficient condition. Thus the defining condition reduces to one scalar equation.

$$ (\mathbf{b}-t\mathbf{u})\cdot\mathbf{u}=0 \quad\Longleftrightarrow\quad \mathbf{b}\cdot\mathbf{u}-t(\mathbf{u}\cdot\mathbf{u})=0. $$

Because \(\mathbf{u}\ne\mathbf{0}\), at least one of its coordinates is nonzero, so the sum of the squares of its coordinates, \(\mathbf{u}\cdot\mathbf{u}\), is positive. We may divide by it to find \(t\).

Theorem (Projection formula onto a line): If \(\mathbf{u}\ne\mathbf{0}\) in \(\mathbb{R}^n\) and \(L=\operatorname{span}\{\mathbf{u}\}\), then for every \(\mathbf{b}\in\mathbb{R}^n\), the projection onto \(L\) exists and is unique, and is given by $$\operatorname{proj}_{L}\mathbf{b}=\frac{\mathbf{b}\cdot\mathbf{u}}{\mathbf{u}\cdot\mathbf{u}}\mathbf{u}.$$ This formula also applies when \(\mathbf{b}=\mathbf{0}\).

Proof. Any candidate in \(L\) has the form \(t\mathbf{u}\). By the definition of projection, its residual must be perpendicular to every vector in \(L\). This is equivalent to \((\mathbf{b}-t\mathbf{u})\cdot\mathbf{u}=0\), since \(\mathbf{u}\in L\) and each vector in \(L\) is a multiple of \(\mathbf{u}\). Expanding gives \(\mathbf{b}\cdot\mathbf{u}-t(\mathbf{u}\cdot\mathbf{u})=0\). Since \(\mathbf{u}\ne\mathbf{0}\), we have \(\mathbf{u}\cdot\mathbf{u}>0\), so the equation has exactly one solution, \(t=(\mathbf{b}\cdot\mathbf{u})/(\mathbf{u}\cdot\mathbf{u})\). The resulting \(t\mathbf{u}\) lies in \(L\) and its residual has zero dot product with \(\mathbf{u}\), hence with every multiple of \(\mathbf{u}\). It therefore satisfies the definition and is the unique projection. If \(\mathbf{b}=\mathbf{0}\), the same equation gives \(t=0\), so the formula returns \(\mathbf{0}\). \(\square\)

Calculating and Checking a Projection

The numerator measures how strongly \(\mathbf{b}\) points along the chosen direction \(\mathbf{u}\); the denominator adjusts for the length of that direction. The denominator is the squared length, not the length itself. A reliable calculation has three parts: compute both dot products, multiply \(\mathbf{u}\) by their ratio, and check that the residual is perpendicular to \(\mathbf{u}\). Membership in the line is checked by confirming that the proposed projection is a scalar multiple of \(\mathbf{u}\).

Worked Example: Projecting in \(\mathbb{R}^2\)

Find the projection of \(\mathbf{b}=\begin{bmatrix}3\\4\end{bmatrix}\) onto the line spanned by \(\mathbf{u}=\begin{bmatrix}1\\2\end{bmatrix}\), and verify the result.

First calculate the dot products:

$$ \mathbf{b}\cdot\mathbf{u}=3(1)+4(2)=3+8=11, \qquad \mathbf{u}\cdot\mathbf{u}=1(1)+2(2)=1+4=5. $$

The formula gives

$$ \operatorname{proj}_{L}\mathbf{b} =\frac{11}{5}\begin{bmatrix}1\\2\end{bmatrix} =\begin{bmatrix}\frac{11}{5}\\[2pt]\frac{22}{5}\end{bmatrix}. $$

This is in \(L\), since it is \(11/5\) times \(\mathbf{u}\). The residual is

$$ \mathbf{b}-\operatorname{proj}_{L}\mathbf{b} =\begin{bmatrix}3\\4\end{bmatrix} -\begin{bmatrix}\frac{11}{5}\\[2pt]\frac{22}{5}\end{bmatrix} =\begin{bmatrix}\frac{4}{5}\\[2pt]-\frac{2}{5}\end{bmatrix}. $$

Check: its dot product with the direction vector is \(\frac45(1)+(-\frac25)(2)=\frac45-\frac45=0\). Thus the projection lies on the line and the residual is perpendicular to it, as required.

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

Project \(\mathbf{b}=\begin{bmatrix}2\\-1\\3\end{bmatrix}\) onto the line spanned by \(\mathbf{u}=\begin{bmatrix}2\\1\\0\end{bmatrix}\).

Compute the numerator and denominator separately, retaining the negative sign in the second coordinate of \(\mathbf{b}\):

$$ \mathbf{b}\cdot\mathbf{u}=2(2)+(-1)(1)+3(0)=4-1+0=3, \qquad \mathbf{u}\cdot\mathbf{u}=2^2+1^2+0^2=5. $$

Therefore,

$$ \operatorname{proj}_{L}\mathbf{b} =\frac35\begin{bmatrix}2\\1\\0\end{bmatrix} =\begin{bmatrix}\frac65\\[2pt]\frac35\\[2pt]0\end{bmatrix}. $$

Subtract coordinate by coordinate to obtain the residual:

$$ \mathbf{b}-\operatorname{proj}_{L}\mathbf{b} =\begin{bmatrix}2\\-1\\3\end{bmatrix} -\begin{bmatrix}\frac65\\[2pt]\frac35\\[2pt]0\end{bmatrix} =\begin{bmatrix}\frac45\\[2pt]-\frac85\\[2pt]3\end{bmatrix}. $$

Check: the proposed projection is \(3/5\) times \(\mathbf{u}\), so it lies on \(L\). The residual's dot product with \(\mathbf{u}\) is \(\frac45(2)+(-\frac85)(1)+3(0)=\frac85-\frac85+0=0\). Both defining conditions hold.

Zero and Orthogonal Inputs

The formula covers two useful special cases without needing a separate method. If the vector being projected is zero, its dot product with any direction is zero, so the projection is zero. If \(\mathbf{b}\) is already perpendicular to the line, then \(\mathbf{b}\cdot\mathbf{u}=0\) and again the projection is zero. In the second case, \(\mathbf{b}\) need not itself be zero; it has no component along the line.

Worked Example: Projecting the Zero Vector

Let \(\mathbf{b}=\begin{bmatrix}0\\0\\0\end{bmatrix}\) and \(\mathbf{u}=\begin{bmatrix}3\\-2\\1\end{bmatrix}\). Calculate the projection onto \(L=\operatorname{span}\{\mathbf{u}\}\).

The direction is nonzero, and its squared length is \(3^2+(-2)^2+1^2=9+4+1=14\). The numerator is

$$ \mathbf{b}\cdot\mathbf{u}=0(3)+0(-2)+0(1)=0. $$

Hence

$$ \operatorname{proj}_{L}\mathbf{b} =\frac{0}{14}\begin{bmatrix}3\\-2\\1\end{bmatrix} =\begin{bmatrix}0\\0\\0\end{bmatrix}. $$

Check: the result is \(0\mathbf{u}\), so it lies on \(L\). The residual is \(\mathbf{b}-\operatorname{proj}_{L}\mathbf{b}=\mathbf{0}\), and \(\mathbf{0}\cdot\mathbf{u}=0\). The formula is defined because the denominator is \(14\), not zero.

Worked Example: A Nonzero Vector Perpendicular to the Line

Project \(\mathbf{b}=\begin{bmatrix}2\\-1\end{bmatrix}\) onto the line generated by \(\mathbf{u}=\begin{bmatrix}1\\2\end{bmatrix}\).

The vector being projected is nonzero, but its dot product with the direction is \(2(1)+(-1)(2)=2-2=0\). Also, \(\mathbf{u}\cdot\mathbf{u}=1+4=5\). Thus

$$ \operatorname{proj}_{L}\mathbf{b} =\frac{0}{5}\begin{bmatrix}1\\2\end{bmatrix} =\begin{bmatrix}0\\0\end{bmatrix}. $$

Check: the zero vector belongs to every subspace, including \(L\). The residual is \(\begin{bmatrix}2\\-1\end{bmatrix}\), and its dot product with \(\mathbf{u}\) is \(2(1)+(-1)(2)=0\). Therefore the residual is perpendicular to the entire line.

The Direction Vector Is Not Unique

A line has many possible direction vectors. If \(c\in\mathbb{R}\) and \(c\ne0\), then \(c\mathbf{u}\) generates the same line as \(\mathbf{u}\): every multiple of \(c\mathbf{u}\) is a multiple of \(\mathbf{u}\), and every multiple of \(\mathbf{u}\) is a multiple of \(c\mathbf{u}\). The projection formula must therefore give the same point when the chosen generator is rescaled.

Proposition: For every nonzero scalar \(c\), replacing \(\mathbf{u}\) by \(c\mathbf{u}\) leaves the projection onto its line unchanged.

Proof. Since \(c\ne0\), the vectors \(\mathbf{u}\) and \(c\mathbf{u}\) span the same line. Applying the formula with \(c\mathbf{u}\) gives \(\frac{\mathbf{b}\cdot(c\mathbf{u})}{(c\mathbf{u})\cdot(c\mathbf{u})}(c\mathbf{u})\). By scalar multiplication in the dot product, the numerator is \(c(\mathbf{b}\cdot\mathbf{u})\), and the denominator is \(c^2(\mathbf{u}\cdot\mathbf{u})\). Since \(c\ne0\), the expression simplifies to \(\frac{c(\mathbf{b}\cdot\mathbf{u})}{c^2(\mathbf{u}\cdot\mathbf{u})}(c\mathbf{u})=\frac{\mathbf{b}\cdot\mathbf{u}}{\mathbf{u}\cdot\mathbf{u}}\mathbf{u}\). This is the original projection, proving the claim. \(\square\)

Rescaling can make the arithmetic simpler, but the denominator must be rescaled too. For instance, using \(-2\mathbf{u}\) reverses the direction and doubles its length; the coefficient changes accordingly, while the final projected vector stays fixed. The zero scalar is excluded because \(0\mathbf{u}=\mathbf{0}\) no longer generates the same line.

Common Mistake: Using the Wrong Denominator

A frequent error is to divide by \(\mathbf{b}\cdot\mathbf{b}\) instead of \(\mathbf{u}\cdot\mathbf{u}\). The scalar being found is the coefficient \(t\) in the candidate \(t\mathbf{u}\). The perpendicularity equation contains \(t(\mathbf{u}\cdot\mathbf{u})\), so it is the direction vector's squared length that must appear in the denominator.

For the first worked example, the incorrect calculation would be \(t=(\mathbf{b}\cdot\mathbf{u})/(\mathbf{b}\cdot\mathbf{b})=11/(3^2+4^2)=11/25\), producing the wrong candidate \(\frac{11}{25}\begin{bmatrix}1\\2\end{bmatrix}=\begin{bmatrix}11/25\\22/25\end{bmatrix}\). Its residual is \(\begin{bmatrix}64/25\\78/25\end{bmatrix}\), whose dot product with \(\mathbf{u}\) is \(64/25+2(78/25)=220/25=44/5\ne0\). The candidate lies on the line, but its residual is not perpendicular to it, so it cannot be the projection. The corrected denominator is \(\mathbf{u}\cdot\mathbf{u}=5\), giving the previously checked projection \(\begin{bmatrix}11/5\\22/5\end{bmatrix}\).

Another error is to omit the squared length altogether. Unless \(\mathbf{u}\) is a unit vector, the ratio \(\mathbf{b}\cdot\mathbf{u}\) alone is not generally the coefficient of \(\mathbf{u}\). The displayed formula works for any nonzero direction vector, whether or not it has length one.

Using the Formula Reliably

For a vector \(\mathbf{b}\) and a nonzero line generator \(\mathbf{u}\), calculate \(\mathbf{b}\cdot\mathbf{u}\) and \(\mathbf{u}\cdot\mathbf{u}\), divide, and multiply the result by \(\mathbf{u}\). Then verify two facts: the answer is a multiple of \(\mathbf{u}\), and the residual has dot product zero with \(\mathbf{u}\). The second check confirms perpendicularity to every vector on the line because every vector on it is a multiple of \(\mathbf{u}\). In particular, if \(\mathbf{b}=\mathbf{0}\), the numerator is zero and the projection is \(\mathbf{0}\); no special exception to the formula is needed.

Check Your Understanding

Use the line-projection formula and check each result by testing line membership and residual orthogonality.

  1. Project \(\begin{bmatrix}1\\3\end{bmatrix}\) onto the line spanned by \(\begin{bmatrix}2\\1\end{bmatrix}\).
  2. Project \(\begin{bmatrix}0\\0\\0\end{bmatrix}\) onto the line spanned by \(\begin{bmatrix}1\\-1\\2\end{bmatrix}\). Which dot product in the formula makes the result zero?
  3. Find the projection of \(\begin{bmatrix}4\\2\end{bmatrix}\) onto the line spanned by \(\begin{bmatrix}1\\-2\end{bmatrix}\). Explain why the answer is zero even though the vector being projected is nonzero.
  4. For a nonzero scalar \(c\), explain why \(\operatorname{span}\{\mathbf{u}\}=\operatorname{span}\{c\mathbf{u}\}\).
  5. A proposed projection is a multiple of \(\mathbf{u}\), but its residual has nonzero dot product with \(\mathbf{u}\). Which defining condition does it fail?

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