Skip to contentSkip to lesson map

Vector Projection

Reading
MMAC Ch 3.4, pp 320-325
Practice
Optional HW 21 Q2.2, Q3.11, Q6.1, Q6.2, and Q11.3.
Lesson map

Objectives

  1. Given two non-zero vectors, compute the projection of one onto the other, by hand and in R, or describe why it is not possible.
  2. Describe a projection as the closest possible linear combination of one vector to the other.
  3. Calculate the residual vector between a vector and its projection.
  4. Given two 2-D non-zero vectors, sketch the projection and residual by hand.

1. Projection of One Vector onto Another

Objective. Given two non-zero vectors, compute the projection of one onto the other, by hand and in R, or describe why it is not possible.

Figure

The projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} is a scalar multiple xbu‾x\overline{\vphantom{b}u} that lies on the line through bu‾\overline{\vphantom{b}u}, with the scalar xx given by the definition below. The dashed segment from the tip of xbu‾x\overline{\vphantom{b}u} to the tip of bv‾\overline{\vphantom{b}v} meets that line at a right angle; this segment is the residual vector br‾\overline{\vphantom{b}r}, defined in section 3. The dot product, length, and orthogonality used below are on the Lesson 20 sheet.

Definition 1 (Projection).

For a vector bv‾\overline{\vphantom{b}v} and a nonzero vector bu‾\overline{\vphantom{b}u} with the same number of components, the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} is the vector xbu‾x\overline{\vphantom{b}u}, where

x=bv‾⋅bu‾bu‾⋅bu‾. x = \frac{\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u}}{\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u}}.

The vector xbu‾x\overline{\vphantom{b}u} is the projection vector, and the number xx is the projection scalar.

The vector bv‾=(34)\overline{\vphantom{b}v} = \left(\begin{smallmatrix} 3\\4 \end{smallmatrix}\right) has no projection onto the zero vector b0‾=(00)\overline{\vphantom{b}0} = \left(\begin{smallmatrix} 0\\0 \end{smallmatrix}\right), because b0‾⋅b0‾=0\overline{\vphantom{b}0}\cdot\overline{\vphantom{b}0} = 0 and the quotient (bv‾⋅b0‾)/(b0‾⋅b0‾)=0/0(\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}0})/(\overline{\vphantom{b}0}\cdot\overline{\vphantom{b}0}) = 0/0 is undefined. The definition requires bu‾\overline{\vphantom{b}u} to be nonzero.

Why it works: the formula gives the closest linear combination

Take bu‾\overline{\vphantom{b}u} other than the zero vector. Then bu‾⋅bu‾>0\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u} > 0 and xx is defined. The residual br‾=bv‾−xbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - x\overline{\vphantom{b}u} is orthogonal to bu‾\overline{\vphantom{b}u}, by the formula for xx:

br‾⋅bu‾=bv‾⋅bu‾−x (bu‾⋅bu‾)=0. \overline{\vphantom{b}r}\cdot\overline{\vphantom{b}u} = \overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u} - x\,(\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u}) = 0.

For every scalar tt, the difference bv‾−tbu‾\overline{\vphantom{b}v} - t\overline{\vphantom{b}u} equals br‾+(x−t)bu‾\overline{\vphantom{b}r} + (x - t)\overline{\vphantom{b}u}. The dot product distributes over a sum the way multiplication does, and expanding gives

∥bv‾−tbu‾∥2=∥br‾∥2+2(x−t) br‾⋅bu‾+(x−t)2∥bu‾∥2. \lVert \overline{\vphantom{b}v} - t\overline{\vphantom{b}u} \rVert^2 = \lVert \overline{\vphantom{b}r} \rVert^2 + 2(x - t)\,\overline{\vphantom{b}r}\cdot\overline{\vphantom{b}u} + (x - t)^2\lVert \overline{\vphantom{b}u} \rVert^2.

The middle term is 0, because br‾⋅bu‾=0\overline{\vphantom{b}r}\cdot\overline{\vphantom{b}u} = 0, which leaves

∥bv‾−tbu‾∥2=∥br‾∥2+(x−t)2∥bu‾∥2≥∥br‾∥2 \lVert \overline{\vphantom{b}v} - t\overline{\vphantom{b}u} \rVert^2 = \lVert \overline{\vphantom{b}r} \rVert^2 + (x - t)^2\lVert \overline{\vphantom{b}u} \rVert^2 \ge \lVert \overline{\vphantom{b}r} \rVert^2

with equality only at t=xt = x. Every other linear combination of bu‾\overline{\vphantom{b}u} is farther from bv‾\overline{\vphantom{b}v}.

Order of the vectors. The projection of bu‾\overline{\vphantom{b}u} onto bv‾\overline{\vphantom{b}v} is a multiple of bv‾\overline{\vphantom{b}v}, and in general it differs from the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u}.

Sign of the scalar. The denominator bu‾⋅bu‾\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u} is positive for every bu‾\overline{\vphantom{b}u} other than the zero vector. The scalar xx therefore takes the sign of bv‾⋅bu‾\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u}. In each graph below, bu‾=(21)\overline{\vphantom{b}u} = \left(\begin{smallmatrix} 2\\1 \end{smallmatrix}\right) points along a road through the origin, and the projection of bv‾\overline{\vphantom{b}v} marks the point of the road closest to bv‾\overline{\vphantom{b}v}.

Positive scalar

Figure

For bv‾=(23)\overline{\vphantom{b}v} = \left(\begin{smallmatrix} 2\\3 \end{smallmatrix}\right), the dot product bv‾⋅bu‾\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u} is 7, and the scalar x=1.4x = 1.4 is positive. The projection 1.4bu‾1.4\overline{\vphantom{b}u}, in orange, points the same way as bu‾\overline{\vphantom{b}u}: the closest point lies ahead of the origin on the road.

Negative scalar

Figure

For bv‾=(−21)\overline{\vphantom{b}v} = \left(\begin{smallmatrix} -2\\1 \end{smallmatrix}\right), the dot product bv‾⋅bu‾\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u} is −3-3, and the scalar x=−0.6x = -0.6 is negative. The projection −0.6bu‾-0.6\overline{\vphantom{b}u} points opposite bu‾\overline{\vphantom{b}u}: the closest point lies behind the origin on the road.

Zero scalar

Figure

For bv‾=(−12)\overline{\vphantom{b}v} = \left(\begin{smallmatrix} -1\\2 \end{smallmatrix}\right), the dot product bv‾⋅bu‾\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u} is 0, and so is the scalar xx. The vectors are orthogonal, and the projection is the zero vector: the closest point is the origin itself.

Example 1

Compute the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} by hand, for

bv‾=(34),bu‾=(21). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 3 \\ 4 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 2 \\ 1 \end{pmatrix*}.
ShowHide solution

By the definition of projection, the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} is

xbu‾=(bv‾⋅bu‾bu‾⋅bu‾)bu‾. x\overline{\vphantom{b}u} = \left(\frac{\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u}}{\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u}}\right)\overline{\vphantom{b}u}.

Substitute the components of bv‾\overline{\vphantom{b}v} and bu‾\overline{\vphantom{b}u} into the formula for the projection scalar:

x=3(2)+4(1)2(2)+1(1)=6+44+1=105=2. x = \frac{3(2) + 4(1)}{2(2) + 1(1)} = \frac{6 + 4}{4 + 1} = \frac{10}{5} = 2.

Scale bu‾\overline{\vphantom{b}u} by xx to get the projection vector:

xbu‾=2(21)=(42). x\overline{\vphantom{b}u} = 2\begin{pmatrix*}[r] 2 \\ 1 \end{pmatrix*} = \begin{pmatrix*}[r] 4 \\ 2 \end{pmatrix*}.
Figure

Example 2

Compute the projection of bu‾\overline{\vphantom{b}u} onto bv‾\overline{\vphantom{b}v} by hand, for

bv‾=(34),bu‾=(21). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 3 \\ 4 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 2 \\ 1 \end{pmatrix*}.
ShowHide solution

The projection formula, with the roles of bu‾\overline{\vphantom{b}u} and bv‾\overline{\vphantom{b}v} exchanged, gives the projection ybv‾y\overline{\vphantom{b}v}. Its scalar is named yy to keep it apart from the scalar xx of the projection onto bu‾\overline{\vphantom{b}u}:

ybv‾=(bu‾⋅bv‾bv‾⋅bv‾)bv‾, y\overline{\vphantom{b}v} = \left(\frac{\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}v}}{\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}v}}\right)\overline{\vphantom{b}v},
y=2(3)+1(4)3(3)+4(4)=1025=0.4, y = \frac{2(3) + 1(4)}{3(3) + 4(4)} = \frac{10}{25} = 0.4,
ybv‾=0.4(34)=(1.21.6). y\overline{\vphantom{b}v} = 0.4\begin{pmatrix*}[r] 3 \\ 4 \end{pmatrix*} = \begin{pmatrix*}[r] 1.2 \\ 1.6 \end{pmatrix*}.

This projection is a multiple of bv‾\overline{\vphantom{b}v}, unlike the projection (42)\left(\begin{smallmatrix} 4\\2 \end{smallmatrix}\right) of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} in Example 1.

Figure

Example 3

Compute the projection scalar and the projection vector of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} by hand, for

bv‾=(138),bu‾=(30−1). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 1 \\ 3 \\ 8 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 3 \\ 0 \\ -1 \end{pmatrix*}.
ShowHide solution

Substitute the components into the formula for xx, then scale bu‾\overline{\vphantom{b}u} by xx:

x=1(3)+3(0)+8(−1)3(3)+0(0)+(−1)(−1)=−510=−0.5, x = \frac{1(3) + 3(0) + 8(-1)}{3(3) + 0(0) + (-1)(-1)} = \frac{-5}{10} = -0.5,
xbu‾=−0.5(30−1)=(−1.500.5). x\overline{\vphantom{b}u} = -0.5\begin{pmatrix*}[r] 3 \\ 0 \\ -1 \end{pmatrix*} = \begin{pmatrix*}[r] -1.5 \\ 0 \\ 0.5 \end{pmatrix*}.

The projection scalar is −0.5-0.5, and the projection vector points opposite bu‾\overline{\vphantom{b}u}, because the scalar is negative.

In R, project(v ~ u) returns the projection scalar xx, and x * u scales bu‾\overline{\vphantom{b}u} by it to give the projection vector. The tilde means “onto”.

Comma in place of the tilde. The command project(v, u) returns the projection vector itself and prints no error. When that output is stored as x, the line x * u multiplies the projection vector by bu‾\overline{\vphantom{b}u} component by component. For the vectors of Example 1, the result is (82)\left(\begin{smallmatrix} 8\\2 \end{smallmatrix}\right) in place of the projection vector (42)\left(\begin{smallmatrix} 4\\2 \end{smallmatrix}\right):

> v = c(3, 4)
> u = c(2, 1)
> project(v, u)
[1] 4 2
> x = project(v, u)
> x * u
[1] 8 2

Example 4

Compute the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} in R, for

bv‾=(3−126),bu‾=(11−11). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 3 \\ -1 \\ 2 \\ 6 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 1 \\ 1 \\ -1 \\ 1 \end{pmatrix*}.
ShowHide solution

Store the two vectors, compute the projection scalar with project(v ~ u), and scale bu‾\overline{\vphantom{b}u} by it:

v = c(3, -1, 2, 6)
u = c(1, 1, -1, 1)
x = project(v ~ u)
x * u
[1]  1.5  1.5 -1.5  1.5

The projection vector is 1.5bu‾=(1.51.5−1.51.5)1.5\overline{\vphantom{b}u} = \left(\begin{smallmatrix} 1.5\\1.5\\-1.5\\1.5 \end{smallmatrix}\right), and the scalar stored in x is

x=3−1−2+61+1+1+1=64=1.5. x = \frac{3 - 1 - 2 + 6}{1 + 1 + 1 + 1} = \frac{6}{4} = 1.5.

As a check, R prints [1] 1.5 1.5 -1.5 1.5 for the formula dot(v, u) / dot(u, u) * u as well.

Rounding. The scalar xx stays unrounded, as a fraction by hand or as the full value R stores in x. Rounding xx first can change the third decimal of a component.

Example 5

Compute the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} from MMAC 3.4 Example 7(b):

bv‾=(15−2),bu‾=(231). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 1 \\ 5 \\ -2 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 2 \\ 3 \\ 1 \end{pmatrix*}.
ShowHide solution

Substitute the components into the formula, keep the projection scalar as an exact fraction, then scale bu‾\overline{\vphantom{b}u} by it:

x=1(2)+5(3)+(−2)(1)2(2)+3(3)+1(1)=1514, x = \frac{1(2) + 5(3) + (-2)(1)}{2(2) + 3(3) + 1(1)} = \frac{15}{14},
xbu‾=1514(231)=(30/1445/1415/14)≈(2.1433.2141.071). x\overline{\vphantom{b}u} = \frac{15}{14}\begin{pmatrix*}[r] 2 \\ 3 \\ 1 \end{pmatrix*} = \begin{pmatrix*}[r] 30/14 \\ 45/14 \\ 15/14 \end{pmatrix*} \approx \begin{pmatrix*}[r] 2.143 \\ 3.214 \\ 1.071 \end{pmatrix*}.

Rounding xx to 1.071 first gives 1.071(231)=(2.1423.2131.071)1.071\left(\begin{smallmatrix} 2\\3\\1 \end{smallmatrix}\right) = \left(\begin{smallmatrix} 2.142\\3.213\\1.071 \end{smallmatrix}\right). Its first two components are off in the third decimal. R stores the full value of 1514\frac{15}{14} in x:

v = c(1, 5, -2)
u = c(2, 3, 1)
x = project(v ~ u)
x * u
[1] 2.142857 3.214286 1.071429

Example 6

Compute the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u}, or describe why it is not possible, for

bv‾=(412),bu‾=(13). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 4 \\ 1 \\ 2 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 1 \\ 3 \end{pmatrix*}.
ShowHide solution

The projection is not possible, because the dot product bv‾⋅bu‾\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u} of a 3-component vector and a 2-component vector is undefined. In R, project() stops with an error:

> v = c(4, 1, 2)
> u = c(1, 3)
> project(v ~ u)
Error in model.frame.default(x, data = data) :
  variable lengths differ (found for 'u')

Next: 2. Projection as the Closest Linear CombinationLesson map

2. Projection as the Closest Linear Combination

Objective. Describe a projection as the closest possible linear combination of one vector to the other.

A linear combination of the single vector bu‾\overline{\vphantom{b}u} is a multiple tbu‾t\overline{\vphantom{b}u}, for a scalar tt (Lesson 17 section 1), and every linear combination of bu‾\overline{\vphantom{b}u} lies on the line through bu‾\overline{\vphantom{b}u}. When bv‾\overline{\vphantom{b}v} lies off that line, no linear combination of bu‾\overline{\vphantom{b}u} equals bv‾\overline{\vphantom{b}v}, and the closest linear combination of bu‾\overline{\vphantom{b}u} to bv‾\overline{\vphantom{b}v} is the one at the smallest distance from bv‾\overline{\vphantom{b}v}.

The distance from tbu‾t\overline{\vphantom{b}u} to bv‾\overline{\vphantom{b}v} is the length ∥br‾∥\lVert \overline{\vphantom{b}r} \rVert of the residual vector br‾=bv‾−tbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - t\overline{\vphantom{b}u}, defined in section 3.

Theorem 2 (Closest linear combination).

For a vector bv‾\overline{\vphantom{b}v} and a nonzero vector bu‾\overline{\vphantom{b}u} with the same number of components, the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} is the closest linear combination of bu‾\overline{\vphantom{b}u} to bv‾\overline{\vphantom{b}v}. Every other linear combination of bu‾\overline{\vphantom{b}u} is farther from bv‾\overline{\vphantom{b}v}.

Example 7

An artillery gun is restricted to a straight road through the origin. Km marker tt on the road sits at tbu‾t\overline{\vphantom{b}u}, and the target sits at bv‾\overline{\vphantom{b}v}, in km:

bv‾=(24),bu‾=(0.80.6). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 2 \\ 4 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 0.8 \\ 0.6 \end{pmatrix*}.

Compare the distance from the gun to the target at km markers t=−1t = -1, 33, 44, and 55, and identify the closest km marker.

ShowHide solution

At each km marker, find the distance to the target in three steps:

  1. Compute tbu‾t\overline{\vphantom{b}u}.
  2. Compute the residual vector br‾=bv‾−tbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - t\overline{\vphantom{b}u}.
  3. Compute its length ∥br‾∥=br‾⋅br‾\lVert \overline{\vphantom{b}r} \rVert = \sqrt{\overline{\vphantom{b}r}\cdot\overline{\vphantom{b}r}}, the distance to the target.

At t=−1t = -1, for example, the distance is

∥br‾∥=(2.8)2+(4.6)2=7.84+21.16=29≈5.385. \lVert \overline{\vphantom{b}r} \rVert = \sqrt{(2.8)^2 + (4.6)^2} = \sqrt{7.84 + 21.16} = \sqrt{29} \approx 5.385.
tttbu‾t\overline{\vphantom{b}u}br‾=bv‾−tbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - t\overline{\vphantom{b}u}∥br‾∥\lVert \overline{\vphantom{b}r} \rVert
−1-1(−0.8−0.6)\left(\begin{smallmatrix} -0.8\\-0.6 \end{smallmatrix}\right)(2.84.6)\left(\begin{smallmatrix} 2.8\\4.6 \end{smallmatrix}\right)≈5.385\approx 5.385
33(2.41.8)\left(\begin{smallmatrix} 2.4\\1.8 \end{smallmatrix}\right)(−0.42.2)\left(\begin{smallmatrix} -0.4\\2.2 \end{smallmatrix}\right)≈2.236\approx 2.236
44(3.22.4)\left(\begin{smallmatrix} 3.2\\2.4 \end{smallmatrix}\right)(−1.21.6)\left(\begin{smallmatrix} -1.2\\1.6 \end{smallmatrix}\right)2
55(43)\left(\begin{smallmatrix} 4\\3 \end{smallmatrix}\right)(−21)\left(\begin{smallmatrix} -2\\1 \end{smallmatrix}\right)≈2.236\approx 2.236
Figure

Of the four markers, marker 4 is the closest, because its distance of 2 km is the smallest. The projection scalar shows that marker 4 is the closest point on the whole road:

x=bv‾⋅bu‾bu‾⋅bu‾=2(0.8)+4(0.6)(0.8)(0.8)+(0.6)(0.6)=1.6+2.40.64+0.36=41=4. x = \frac{\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u}}{\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u}} = \frac{2(0.8) + 4(0.6)}{(0.8)(0.8) + (0.6)(0.6)} = \frac{1.6 + 2.4}{0.64 + 0.36} = \frac{4}{1} = 4.

By the closest linear combination theorem, the projection 4bu‾=(3.22.4)4\overline{\vphantom{b}u} = \left(\begin{smallmatrix} 3.2\\2.4 \end{smallmatrix}\right) is closer to the target than every other point tbu‾t\overline{\vphantom{b}u} on the road. At t=4t = 4 the residual vector br‾=(−1.21.6)\overline{\vphantom{b}r} = \left(\begin{smallmatrix} -1.2\\1.6 \end{smallmatrix}\right) is orthogonal to bu‾\overline{\vphantom{b}u}, because (−1.2)(0.8)+1.6(0.6)=0(-1.2)(0.8) + 1.6(0.6) = 0.

Example 8

Find the linear combination of bu‾\overline{\vphantom{b}u} closest to bv‾\overline{\vphantom{b}v} and its distance from bv‾\overline{\vphantom{b}v}, and compare it with 1bu‾1\overline{\vphantom{b}u} and 3bu‾3\overline{\vphantom{b}u}, for

bv‾=(213),bu‾=(111). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 2 \\ 1 \\ 3 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 1 \\ 1 \\ 1 \end{pmatrix*}.
ShowHide solution

By the closest linear combination theorem, the closest linear combination is the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u}. Compute its scalar and the projection vector:

x=2(1)+1(1)+3(1)1(1)+1(1)+1(1)=63=2,xbu‾=2(111)=(222). x = \frac{2(1) + 1(1) + 3(1)}{1(1) + 1(1) + 1(1)} = \frac{6}{3} = 2, \qquad x\overline{\vphantom{b}u} = 2\begin{pmatrix*}[r] 1 \\ 1 \\ 1 \end{pmatrix*} = \begin{pmatrix*}[r] 2 \\ 2 \\ 2 \end{pmatrix*}.

The distance is the length of the residual vector br‾=bv‾−2bu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - 2\overline{\vphantom{b}u}:

br‾=(0−11),∥br‾∥=02+(−1)2+12=2≈1.414. \overline{\vphantom{b}r} = \begin{pmatrix*}[r] 0 \\ -1 \\ 1 \end{pmatrix*}, \qquad \lVert \overline{\vphantom{b}r} \rVert = \sqrt{0^2 + (-1)^2 + 1^2} = \sqrt{2} \approx 1.414.

The other two linear combinations are farther, as the theorem states:

∥bv‾−1bu‾∥=∥(102)∥=5≈2.236,∥bv‾−3bu‾∥=∥(−1−20)∥=5≈2.236. \lVert \overline{\vphantom{b}v} - 1\overline{\vphantom{b}u} \rVert = \lVert \begin{pmatrix*}[r] 1 \\ 0 \\ 2 \end{pmatrix*} \rVert = \sqrt{5} \approx 2.236, \qquad \lVert \overline{\vphantom{b}v} - 3\overline{\vphantom{b}u} \rVert = \lVert \begin{pmatrix*}[r] -1 \\ -2 \\ 0 \end{pmatrix*} \rVert = \sqrt{5} \approx 2.236.

R gives the same scalar and distance. The line for d computes ∥bv‾−xbu‾∥=(bv‾−xbu‾)⋅(bv‾−xbu‾)\lVert \overline{\vphantom{b}v} - x\overline{\vphantom{b}u} \rVert = \sqrt{(\overline{\vphantom{b}v} - x\overline{\vphantom{b}u})\cdot(\overline{\vphantom{b}v} - x\overline{\vphantom{b}u})}:

v = c(2, 1, 3)
u = c(1, 1, 1)
x = project(v ~ u)
x * u
[1] 2 2 2
d = sqrt(dot(v - x * u, v - x * u))
d
[1] 1.414214

The closest linear combination is 2bu‾=(222)2\overline{\vphantom{b}u} = \left(\begin{smallmatrix} 2\\2\\2 \end{smallmatrix}\right), at a distance of 2≈1.414\sqrt{2} \approx 1.414 from bv‾\overline{\vphantom{b}v}.

Next: 3. Residual VectorLesson map

3. Residual Vector

Objective. Calculate the residual vector between a vector and its projection.

Definition 3 (Residual vector).

For vectors bv‾\overline{\vphantom{b}v} and bu‾\overline{\vphantom{b}u} and a scalar tt, the residual vector for the linear combination tbu‾t\overline{\vphantom{b}u} and the target vector bv‾\overline{\vphantom{b}v} is br‾=bv‾−tbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - t\overline{\vphantom{b}u}.

For bv‾=(34)\overline{\vphantom{b}v} = \left(\begin{smallmatrix} 3\\4 \end{smallmatrix}\right) and bu‾=(21)\overline{\vphantom{b}u} = \left(\begin{smallmatrix} 2\\1 \end{smallmatrix}\right), the difference bv‾−(10)=(24)\overline{\vphantom{b}v} - \left(\begin{smallmatrix} 1\\0 \end{smallmatrix}\right) = \left(\begin{smallmatrix} 2\\4 \end{smallmatrix}\right) is not a residual vector for bu‾\overline{\vphantom{b}u}, because (10)\left(\begin{smallmatrix} 1\\0 \end{smallmatrix}\right) is not a multiple tbu‾t\overline{\vphantom{b}u} of bu‾\overline{\vphantom{b}u}. The definition subtracts a linear combination tbu‾t\overline{\vphantom{b}u}.

Proposition 4 (Projection and residual).

For the projection xbu‾x\overline{\vphantom{b}u} of bv‾\overline{\vphantom{b}v} onto a nonzero vector bu‾\overline{\vphantom{b}u}, the residual vector br‾=bv‾−xbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - x\overline{\vphantom{b}u} is orthogonal to bu‾\overline{\vphantom{b}u} and to the projection xbu‾x\overline{\vphantom{b}u}, and the projection and the residual vector add to the target vector:

xbu‾+br‾=bv‾. x\overline{\vphantom{b}u} + \overline{\vphantom{b}r} = \overline{\vphantom{b}v}.

In R, dot(r, u) prints 0 or a number within rounding of 0, such as 2.220446e-15, R’s notation for 2.220446×10−152.220446 \times 10^{-15}.

Example 9

Compute the residual vector for the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} by hand, and check that the residual is orthogonal to bu‾\overline{\vphantom{b}u}, for

bv‾=(34),bu‾=(21). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 3 \\ 4 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 2 \\ 1 \end{pmatrix*}.
ShowHide solution

By the definition of the residual vector, with t=xt = x the projection scalar, the residual vector is br‾=bv‾−xbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - x\overline{\vphantom{b}u}. Compute the projection first:

x=3(2)+4(1)2(2)+1(1)=105=2,xbu‾=2(21)=(42), x = \frac{3(2) + 4(1)}{2(2) + 1(1)} = \frac{10}{5} = 2, \qquad x\overline{\vphantom{b}u} = 2\begin{pmatrix*}[r] 2 \\ 1 \end{pmatrix*} = \begin{pmatrix*}[r] 4 \\ 2 \end{pmatrix*},
br‾=(34)−(42)=(−12). \overline{\vphantom{b}r} = \begin{pmatrix*}[r] 3 \\ 4 \end{pmatrix*} - \begin{pmatrix*}[r] 4 \\ 2 \end{pmatrix*} = \begin{pmatrix*}[r] -1 \\ 2 \end{pmatrix*}.

The residual vector is orthogonal to bu‾\overline{\vphantom{b}u}, because

br‾⋅bu‾=(−1)(2)+(2)(1)=0. \overline{\vphantom{b}r}\cdot\overline{\vphantom{b}u} = (-1)(2) + (2)(1) = 0.

Example 10

Compute the residual vector for the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} by hand, and check that the residual is orthogonal to bu‾\overline{\vphantom{b}u}, for

bv‾=(138),bu‾=(30−1). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 1 \\ 3 \\ 8 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 3 \\ 0 \\ -1 \end{pmatrix*}.
ShowHide solution

Compute the projection as in Example 3, then subtract it from bv‾\overline{\vphantom{b}v}:

x=1(3)+3(0)+8(−1)3(3)+0(0)+(−1)(−1)=−510=−0.5,xbu‾=−0.5(30−1)=(−1.500.5), x = \frac{1(3) + 3(0) + 8(-1)}{3(3) + 0(0) + (-1)(-1)} = \frac{-5}{10} = -0.5, \qquad x\overline{\vphantom{b}u} = -0.5\begin{pmatrix*}[r] 3 \\ 0 \\ -1 \end{pmatrix*} = \begin{pmatrix*}[r] -1.5 \\ 0 \\ 0.5 \end{pmatrix*},
br‾=(138)−(−1.500.5)=(2.537.5). \overline{\vphantom{b}r} = \begin{pmatrix*}[r] 1 \\ 3 \\ 8 \end{pmatrix*} - \begin{pmatrix*}[r] -1.5 \\ 0 \\ 0.5 \end{pmatrix*} = \begin{pmatrix*}[r] 2.5 \\ 3 \\ 7.5 \end{pmatrix*}.

The residual vector is orthogonal to bu‾\overline{\vphantom{b}u}, because

br‾⋅bu‾=(2.5)(3)+(3)(0)+(7.5)(−1)=0. \overline{\vphantom{b}r}\cdot\overline{\vphantom{b}u} = (2.5)(3) + (3)(0) + (7.5)(-1) = 0.

Example 11

Compute the residual vector for the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} in R, and check that the residual is orthogonal to bu‾\overline{\vphantom{b}u}, for

bv‾=(3−126),bu‾=(11−11). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 3 \\ -1 \\ 2 \\ 6 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 1 \\ 1 \\ -1 \\ 1 \end{pmatrix*}.
ShowHide solution

R computes the projection scalar, then the residual vector and its dot product with bu‾\overline{\vphantom{b}u}. The line r = v - x * u is the hand step br‾=bv‾−xbu‾\overline{\vphantom{b}r} = \overline{\vphantom{b}v} - x\overline{\vphantom{b}u}:

v = c(3, -1, 2, 6)
u = c(1, 1, -1, 1)
x = project(v ~ u)
r = v - x * u
r
[1]  1.5 -2.5  3.5  4.5
dot(r, u)
[1] 0

The residual vector is br‾=(1.5−2.53.54.5)\overline{\vphantom{b}r} = \left(\begin{smallmatrix} 1.5\\-2.5\\3.5\\4.5 \end{smallmatrix}\right), and it is orthogonal to bu‾\overline{\vphantom{b}u}, because dot(r, u) prints 0.

Next: 4. Sketching a Projection and Its ResidualLesson map

4. Sketching a Projection and Its Residual

Objective. Given two 2-D non-zero vectors, sketch the projection and residual by hand.

Sketching a projection and its residual.

  1. Compute x=bv‾⋅bu‾bu‾⋅bu‾x = \dfrac{\overline{\vphantom{b}v}\cdot\overline{\vphantom{b}u}}{\overline{\vphantom{b}u}\cdot\overline{\vphantom{b}u}} and xbu‾x\overline{\vphantom{b}u}.
  2. Draw bu‾\overline{\vphantom{b}u} from the origin, and draw the line through bu‾\overline{\vphantom{b}u}, extended both ways.
  3. Draw bv‾\overline{\vphantom{b}v} from the origin.
  4. Draw xbu‾x\overline{\vphantom{b}u} from the origin along the line. Its tip is the point where a segment from the tip of bv‾\overline{\vphantom{b}v} meets the line at a right angle.
  5. Draw br‾\overline{\vphantom{b}r} from the tip of xbu‾x\overline{\vphantom{b}u} to the tip of bv‾\overline{\vphantom{b}v}.

Example 12

Sketch the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} and its residual, for

bv‾=(43),bu‾=(12). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 4 \\ 3 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 1 \\ 2 \end{pmatrix*}.
ShowHide solution

The projection scalar and the two vectors to draw are

x=4(1)+3(2)1(1)+2(2)=105=2, x = \frac{4(1) + 3(2)}{1(1) + 2(2)} = \frac{10}{5} = 2,
xbu‾=2(12)=(24),br‾=(43)−(24)=(2−1). x\overline{\vphantom{b}u} = 2\begin{pmatrix*}[r] 1 \\ 2 \end{pmatrix*} = \begin{pmatrix*}[r] 2 \\ 4 \end{pmatrix*}, \qquad \overline{\vphantom{b}r} = \begin{pmatrix*}[r] 4 \\ 3 \end{pmatrix*} - \begin{pmatrix*}[r] 2 \\ 4 \end{pmatrix*} = \begin{pmatrix*}[r] 2 \\ -1 \end{pmatrix*}.

In the sketch, br‾\overline{\vphantom{b}r} meets the line through bu‾\overline{\vphantom{b}u} at a right angle.

Figure

Example 13

Sketch the projection of bv‾\overline{\vphantom{b}v} onto bu‾\overline{\vphantom{b}u} and its residual, for

bv‾=(0−4),bu‾=(22). \overline{\vphantom{b}v} = \begin{pmatrix*}[r] 0 \\ -4 \end{pmatrix*}, \qquad \overline{\vphantom{b}u} = \begin{pmatrix*}[r] 2 \\ 2 \end{pmatrix*}.
ShowHide solution

The projection scalar and the two vectors to draw are

x=0(2)+(−4)(2)2(2)+2(2)=−88=−1, x = \frac{0(2) + (-4)(2)}{2(2) + 2(2)} = \frac{-8}{8} = -1,
xbu‾=−1(22)=(−2−2),br‾=(0−4)−(−2−2)=(2−2). x\overline{\vphantom{b}u} = -1\begin{pmatrix*}[r] 2 \\ 2 \end{pmatrix*} = \begin{pmatrix*}[r] -2 \\ -2 \end{pmatrix*}, \qquad \overline{\vphantom{b}r} = \begin{pmatrix*}[r] 0 \\ -4 \end{pmatrix*} - \begin{pmatrix*}[r] -2 \\ -2 \end{pmatrix*} = \begin{pmatrix*}[r] 2 \\ -2 \end{pmatrix*}.

In the sketch, xbu‾x\overline{\vphantom{b}u} points opposite bu‾\overline{\vphantom{b}u}, because xx is negative.

Figure

Lesson map