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.
The projection of bv onto bu is a scalar multiple xbu that lies on the line through bu, with the scalar x given by the definition below. The dashed segment from the tip of xbu to the tip of bv meets that line at a right angle; this segment is the residual vector br, 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 and a nonzero vector bu with the same number of components, the projection of bv onto bu is the vector xbu, where
x=bu⋅bubv⋅bu.
The vector xbu is the projection vector, and the number x is the projection scalar.
The vector bv=(34) has no projection onto the zero vector b0=(00), because b0⋅b0=0 and the quotient (bv⋅b0)/(b0⋅b0)=0/0 is undefined. The definition requires bu to be nonzero.
Why it works: the formula gives the closest linear combination
Take bu other than the zero vector. Then bu⋅bu>0 and x is defined. The residual br=bv−xbu is orthogonal to bu, by the formula for x:
br⋅bu=bv⋅bu−x(bu⋅bu)=0.
For every scalar t, the difference bv−tbu equals br+(x−t)bu. 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.
The middle term is 0, because br⋅bu=0, which leaves
∥bv−tbu∥2=∥br∥2+(x−t)2∥bu∥2≥∥br∥2
with equality only at t=x. Every other linear combination of bu is farther from bv.
Order of the vectors. The projection of bu onto bv is a multiple of bv, and in general it differs from the projection of bv onto bu.
Sign of the scalar. The denominator bu⋅bu is positive for every bu other than the zero vector. The scalar x therefore takes the sign of bv⋅bu. In each graph below, bu=(21) points along a road through the origin, and the projection of bv marks the point of the road closest to bv.
Positive scalar
For bv=(23), the dot product bv⋅bu is 7, and the scalar x=1.4 is positive. The projection 1.4bu, in orange, points the same way as bu: the closest point lies ahead of the origin on the road.
Negative scalar
For bv=(−21), the dot product bv⋅bu is −3, and the scalar x=−0.6 is negative. The projection −0.6bu points opposite bu: the closest point lies behind the origin on the road.
Zero scalar
For bv=(−12), the dot product bv⋅bu is 0, and so is the scalar x. 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 onto bu by hand, for
bv=(34),bu=(21).
ShowHide solution
By the definition of projection, the projection of bv onto bu is
xbu=(bu⋅bubv⋅bu)bu.
Substitute the components of bv and bu into the formula for the projection scalar:
x=2(2)+1(1)3(2)+4(1)=4+16+4=510=2.
Scale bu by x to get the projection vector:
xbu=2(21)=(42).
Example 2
Compute the projection of bu onto bv by hand, for
bv=(34),bu=(21).
ShowHide solution
The projection formula, with the roles of bu and bv exchanged, gives the projection ybv. Its scalar is named y to keep it apart from the scalar x of the projection onto bu:
ybv=(bv⋅bvbu⋅bv)bv,
y=3(3)+4(4)2(3)+1(4)=2510=0.4,
ybv=0.4(34)=(1.21.6).
This projection is a multiple of bv, unlike the projection (42) of bv onto bu in Example 1.
Example 3
Compute the projection scalar and the projection vector of bv onto bu by hand, for
bv=138,bu=30−1.
ShowHide solution
Substitute the components into the formula for x, then scale bu by x:
x=3(3)+0(0)+(−1)(−1)1(3)+3(0)+8(−1)=10−5=−0.5,
xbu=−0.530−1=−1.500.5.
The projection scalar is −0.5, and the projection vector points opposite bu, because the scalar is negative.
In R, project(v ~ u) returns the projection scalar x, and x * u scales bu 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 component by component. For the vectors of Example 1, the result is (82) in place of the projection vector (42):
> 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 onto bu in R, for
bv=3−126,bu=11−11.
ShowHide solution
Store the two vectors, compute the projection scalar with project(v ~ u), and scale bu by it:
The projection vector is 1.5bu=(1.51.5−1.51.5), and the scalar stored in x is
x=1+1+1+13−1−2+6=46=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 x stays unrounded, as a fraction by hand or as the full value R stores in x. Rounding x first can change the third decimal of a component.
Example 5
Compute the projection of bv onto bu from MMAC 3.4 Example 7(b):
bv=15−2,bu=231.
ShowHide solution
Substitute the components into the formula, keep the projection scalar as an exact fraction, then scale bu by it:
Rounding x to 1.071 first gives 1.071(231)=(2.1423.2131.071). Its first two components are off in the third decimal. R stores the full value of 1415 in x:
Compute the projection of bv onto bu, or describe why it is not possible, for
bv=412,bu=(13).
ShowHide solution
The projection is not possible, because the dot product bv⋅bu 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')
Objective. Describe a projection as the closest possible linear combination of one vector to the other.
A linear combination of the single vector bu is a multiple tbu, for a scalar t (Lesson 17 section 1), and every linear combination of bu lies on the line through bu. When bv lies off that line, no linear combination of bu equals bv, and the closest linear combination of bu to bv is the one at the smallest distance from bv.
The distance from tbu to bv is the length ∥br∥ of the residual vector br=bv−tbu, defined in section 3.
Theorem 2 (Closest linear combination).
For a vector bv and a nonzero vector bu with the same number of components, the projection of bv onto bu is the closest linear combination of bu to bv. Every other linear combination of bu is farther from bv.
Example 7
An artillery gun is restricted to a straight road through the origin. Km marker t on the road sits at tbu, and the target sits at bv, in km:
bv=(24),bu=(0.80.6).
Compare the distance from the gun to the target at km markers t=−1, 3, 4, and 5, and identify the closest km marker.
ShowHide solution
At each km marker, find the distance to the target in three steps:
Compute tbu.
Compute the residual vector br=bv−tbu.
Compute its length ∥br∥=br⋅br, the distance to the target.
At t=−1, for example, the distance is
∥br∥=(2.8)2+(4.6)2=7.84+21.16=29≈5.385.
t
tbu
br=bv−tbu
∥br∥
−1
(−0.8−0.6)
(2.84.6)
≈5.385
3
(2.41.8)
(−0.42.2)
≈2.236
4
(3.22.4)
(−1.21.6)
2
5
(43)
(−21)
≈2.236
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:
By the closest linear combination theorem, the projection 4bu=(3.22.4) is closer to the target than every other point tbu on the road. At t=4 the residual vector br=(−1.21.6) is orthogonal to bu, because (−1.2)(0.8)+1.6(0.6)=0.
Example 8
Find the linear combination of bu closest to bv and its distance from bv, and compare it with 1bu and 3bu, for
bv=213,bu=111.
ShowHide solution
By the closest linear combination theorem, the closest linear combination is the projection of bv onto bu. Compute its scalar and the projection vector:
Objective. Calculate the residual vector between a vector and its projection.
Definition 3 (Residual vector).
For vectors bv and bu and a scalar t, the residual vector for the linear combination tbu and the target vector bv is br=bv−tbu.
For bv=(34) and bu=(21), the difference bv−(10)=(24) is not a residual vector for bu, because (10) is not a multiple tbu of bu. The definition subtracts a linear combination tbu.
Proposition 4 (Projection and residual).
For the projection xbu of bv onto a nonzero vector bu, the residual vector br=bv−xbu is orthogonal to bu and to the projection xbu, and the projection and the residual vector add to the target vector:
xbu+br=bv.
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−15.
Example 9
Compute the residual vector for the projection of bv onto bu by hand, and check that the residual is orthogonal to bu, for
bv=(34),bu=(21).
ShowHide solution
By the definition of the residual vector, with t=x the projection scalar, the residual vector is br=bv−xbu. Compute the projection first:
x=2(2)+1(1)3(2)+4(1)=510=2,xbu=2(21)=(42),
br=(34)−(42)=(−12).
The residual vector is orthogonal to bu, because
br⋅bu=(−1)(2)+(2)(1)=0.
Example 10
Compute the residual vector for the projection of bv onto bu by hand, and check that the residual is orthogonal to bu, for
bv=138,bu=30−1.
ShowHide solution
Compute the projection as in Example 3, then subtract it from bv: