Euler Hall — free maths guides

Project a vector onto a line

Compute the projection p = (aT b / aT a) a and verify the residual is orthogonal. The idea in plain words, a worked example, and a check with the answers shown — part of the free Linear Algebra guides.

Slide along the line to the closest point

Projectingbb onto the line throughaa means: stay on the line, get as close tobb as possible.

The closest point is where the leftover is perpendicular to the line. Write the projection as a multiple of the direction,p=x^ap=\hat{x}a. Requiringa⋅(b−x^a)=0a\cdot(b-\hat{x}a)=0 gives

x^=aTbaTa,p=x^a.\hat{x}=\frac{a^{\mathrm{T}}b}{a^{\mathrm{T}}a},\qquad p=\hat{x}a.

Takea=[12]a=\begin{bmatrix}1\\2\end{bmatrix} andb=[43]b=\begin{bmatrix}4\\3\end{bmatrix}. ThenaTb=4+6=10a^{\mathrm{T}}b=4+6=10 andaTa=1+4=5a^{\mathrm{T}}a=1+4=5, sox^=2\hat{x}=2 and

p=2[12]=[24].p=2\begin{bmatrix}1\\2\end{bmatrix}=\begin{bmatrix}2\\4\end{bmatrix}.

Check:b−p=[2−1]b-p=\begin{bmatrix}2\\-1\end{bmatrix} anda⋅(b−p)=2−2=0a\cdot(b-p)=2-2=0 ✓

The perpendicularity condition is the whole idea

Why mustb−pb-p be perpendicular toaa? Because otherwise you could walk a little further along the line and reduce the distance.

Supposeb−pb-p had a component alongaa. Movingpp slightly in that direction would shortenb−pb-p — sopp was not the closest point. The closest point is exactly the one where no direction along the line improves things, and the only way that can happen is for the error to have zero component alongaa.

Formally, the distance squared frombb to a pointx^a\hat{x}a on the line is

∥b−x^a∥2=∥b∥2−2x^(a⋅b)+x^2∥a∥2,\|b-\hat{x}a\|^2=\|b\|^2-2\hat{x}(a\cdot b)+\hat{x}^2\|a\|^2,

a quadratic inx^\hat{x} opening upward. Its minimum is atx^=a⋅ba⋅a\hat{x}=\frac{a\cdot b}{a\cdot a} — the same formula, derived by calculus instead of by orthogonality. Two routes, one answer, which is a good sign that the answer is right.

Note what the formula refuses to do. It never divides bybb, only byaTaa^{\mathrm{T}}a. The direction matters and the target does not.

One projection with a negative coefficient

Problem. Projectb=[4−1]b=\begin{bmatrix}4\\-1\end{bmatrix} onto the line througha=[−12]a=\begin{bmatrix}-1\\2\end{bmatrix}.

Solution.aTb=−4−2=−6a^{\mathrm{T}}b=-4-2=-6 andaTa=1+4=5a^{\mathrm{T}}a=1+4=5, so

x^=−65,p=−65[−12]=[6/5−12/5].\def\arraystretch{1.6}\hat{x}=\frac{-6}{5},\qquad p=-\frac65\begin{bmatrix}-1\\2\end{bmatrix}=\begin{bmatrix}6/5\\-12/5\end{bmatrix}.

The check.b−p=[4−6/5−1+12/5]=[14/57/5]b-p=\begin{bmatrix}4-6/5\\-1+12/5\end{bmatrix}=\begin{bmatrix}14/5\\7/5\end{bmatrix}, anda⋅(b−p)=−145+145=0a\cdot(b-p)=-\frac{14}{5}+\frac{14}{5}=0 ✓

On the negative coefficient. A negativex^\hat{x} means the closest point lies on the oppposite side of the origin fromaa — the line extends both ways, so this is perfectly normal. It is not a sign error. Ifx^\hat{x} comes out zero you would be in the perpendicular case; any other value is legitimate.

By the end you can
  • Compute the projection p = (a^T b / a^T a) a and verify the residual is orthogonal
  • Explain why the closest point is the one with a perpendicular residual
  • Use the special cases b = a and b perpendicular to a as checks
Two questions

(a) Projectb=[50]b=\begin{bmatrix}5\\0\end{bmatrix} onto the line througha=[11]a=\begin{bmatrix}1\\1\end{bmatrix}.

(b) A projectionpp has∥p∥=3\|p\|=3 and∥b∥=6\|b\|=6. What is the angle betweenbb and the line?

Answers

(a)aTb=5a^{\mathrm{T}}b=5,aTa=2a^{\mathrm{T}}a=2, sox^=52\hat{x}=\frac52 andp=[5/25/2]p=\begin{bmatrix}5/2\\5/2\end{bmatrix}. Check:b−p=[5/2−5/2]b-p=\begin{bmatrix}5/2\\-5/2\end{bmatrix} anda⋅(b−p)=52−52=0a\cdot(b-p)=\frac52-\frac52=0 ✓

(b)∥p∥=∥b∥cos⁡θ\|p\|=\|b\|\cos\theta gives3=6cos⁡θ3=6\cos\theta, socos⁡θ=12\cos\theta=\frac12 andθ=60∘\theta=60^\circ.

Work out where to start — free, no accountAnswer six real questions — free, no account