Scientia sine finibus

Linear Algebra / Unit 1, Vectors / Topic 1.2

Units and Topics

Unit 1. Vectors

1.2 Length and the Dot Product

This topic shows how to calculate the length of a vector and introduces the dot product, which takes two vectors and gives one number. The dot product is used to find the angle between two vectors and to test whether two vectors are perpendicular.

You need Topic 1.1 for this topic. You also need the Pythagorean theorem and the cosine function.

1. The Length of a Vector

The vector v = (3, 4) goes 3 units to the right and 4 units up. These two moves are the legs of a right triangle, and the arrow is the hypotenuse. The Pythagorean theorem gives the length of the arrow:

|v| = √3² + 4²the Pythagorean theorem|v| = √25add the squares|v| = 5take the square root

The length of a vector is written with bars, |v|. It is also called the magnitude of the vector. For any vector with components v₁ and v₂,

|v| = √v₁² + v₂²

The length is a scalar, and it is never negative. Only the zero vector has a length of 0.

Scaling changes the length

Multiplying a vector by a scalar c multiplies its length by the size of c. The vector 2v is twice as long as v. The vector −2v is also twice as long as v, because the negative sign changes only the direction.

Unit vectors

A vector with a length of 1 is called a unit vector. To make a unit vector that points in the direction of v, divide v by its length. For v = (3, 4), the unit vector is (3/5, 4/5), which is (0.6, 0.8). A unit vector gives a direction without a size.

The distance between two points

The distance between two points is the length of the vector from one point to the other. From (1, 2) to (4, 6), the vector is (3, 4), so the distance is 5.

2. The Dot Product

The dot product of two vectors is found by multiplying the matching components and adding the results.

u · v = u₁v₁ + u₂v₂

For example,

41 · 23 = (4)(2) + (1)(3)multiply the matching components41 · 23 = 11add the products

The dot product of two vectors is a number. It is not a vector. This is the most common point of confusion with the dot product.

The dot product of a vector with itself is the square of its length:

v · v = v₁² + v₂² = |v|²

3. What the Dot Product Measures

The dot product measures how much two vectors point in the same direction. If θ is the angle between u and v, then

u · v = |u| |v| cos θ

The two formulas for the dot product always give the same number. The first formula uses the components. The second formula uses the lengths and the angle.

The sign of the dot product gives information about the angle, because the lengths are positive and only cos θ can be negative.

Dot productAngle between the vectors
PositiveLess than 90°
ZeroExactly 90°. The vectors are perpendicular.
NegativeGreater than 90°

Try it: the dot product

Drag the tip of the blue arrow or the tip of the red arrow. The green arrow is the shadow of u on the line of v. Press Play to turn u through a full circle and observe the sign of the dot product.

Finding the angle

To find the angle between two vectors, calculate the dot product from the components, divide by the two lengths, and take the inverse cosine.

cos θ = (u · v)/(|u| |v|)

For u = (4, 1) and v = (2, 3):

u · v = (4)(2) + (1)(3) = 11the dot product|u| = √4² + 1² = √17the length of u|v| = √2² + 3² = √13the length of vcos θ = 11/√221 = 0.740divide the dot product by the two lengths, with √17 √13 = √221θ = 42.3°take the inverse cosine

The test for perpendicular vectors

Two vectors that are not zero are perpendicular exactly when their dot product is 0. For example:

(2, 3) · (−3, 2) = (2)(−3) + (3)(2)multiply the matching components(2, 3) · (−3, 2) = −6 + 6 = 0add the products

The dot product is 0, so these two vectors are perpendicular. No angle has to be calculated.

4. The Component of One Vector Along Another

In the drawing, the green arrow is the shadow of u on the line of v. Its signed length is called the component of u along v. From the right triangle in the drawing, this component is |u| cos θ. Using the dot product,

component of u along v = (u · v) / |v|

The green arrow is called the projection of u onto v. It is the component multiplied by the unit vector in the direction of v:

projection of u onto v = (u · v)/(v · v) v

The dot product in physics

Work is the best known example. When a constant force F acts on an object that moves through a displacement d, the work done by the force is

W = F · d = |F| |d| cos θ

Only the component of the force along the displacement does work. A force that is perpendicular to the displacement does no work, because the dot product is 0. This is the reason the normal force does no work on a block that slides along a level floor.

Common Mistakes

  • Writing the dot product as a vector. The dot product of (4, 1) and (2, 3) is the number 11. It is not the vector (8, 3).
  • Adding the components before squaring. The length of (3, 4) is √9 + 16 = 5. It is not 3 + 4 = 7.
  • Forgetting to divide by both lengths when finding the angle. The dot product equals cos θ only when both vectors are unit vectors.
  • Losing a negative sign. In (2, −1) · (3, 4), the second product is (−1)(4) = −4, so the dot product is 6 − 4 = 2.
  • Using the calculator in radians when the answer is expected in degrees.

Summary

TermMeaning
Length|v| = √v₁² + v₂²
Unit vectorA vector with a length of 1
Dot productu · v = u₁v₁ + u₂v₂ = |u| |v| cos θ
Perpendicularu · v = 0
ProjectionThe shadow of one vector on the line of another vector

Practice

Work each problem on paper first. Then open the answer to check your work.

1. Calculate the length of (−5, 12).

length = √(−5)² + 12²the Pythagorean theoremlength = √25 + 144 = √169add the squareslength = 13take the square root

2. Find the unit vector in the direction of (6, 8).

length = √6² + 8² = √100 = 10the length of the vectorunit vector = (6/10, 8/10) = (0.6, 0.8)divide each component by the length

3. Calculate (3, −2) · (1, 5).

(3, −2) · (1, 5) = (3)(1) + (−2)(5)multiply the matching components(3, −2) · (1, 5) = 3 − 10 = −7add the products

The dot product is negative, so the angle between the vectors is greater than 90°.

4. Find the angle between (1, 0) and (1, 1).

dot product = (1)(1) + (0)(1) = 1multiply the matching components and addlength of (1, 0) = 1length of (1, 1) = √1² + 1² = √2cos θ = 1/√2divide the dot product by the two lengthsθ = 45°take the inverse cosine

5. Find the value of k that makes (2, k) perpendicular to (3, −6).

(2)(3) + (k)(−6) = 0the dot product must be 06 − 6k = 0multiplyk = 1add 6k to both sides, then divide by 6

6. A force of (10, 5) N acts on a cart that moves through a displacement of (4, 0) m. Calculate the work done by the force.

W = F · dthe work done by a constant forceW = (10)(4) + (5)(0)multiply the matching componentsW = 40 Jadd the products

The vertical part of the force does no work, because the cart does not move vertically.

Python Practice

Step 1. Calculate the length of a vector

Python reads the code one line at a time, from the top to the bottom. The equals sign stores the value on its right under the name on its left, and print shows a result. A vector is written as np.array([3, 4]), where np stands for NumPy, the part of Python that works with vectors and matrices.

The instruction np.linalg.norm calculates the length of a vector. The word norm is another name for length. The name of the vector goes inside the parentheses.

Python

The result is 5.0, because the square root of 3² + 4² is 5. Python writes 5.0 and not 5, because it stores the result of a square root as a decimal number.

Now change the vector so that the code prints the length of (5, 12).

Python

Step 2. Calculate a dot product

The instruction np.dot calculates the dot product of two vectors. The names of the two vectors go inside the parentheses, with a comma between them.

Python

The result is 10, because (3)(2) + (4)(1) = 10.

From Part 3 of this topic, two vectors are perpendicular when their dot product is 0. In the next box, change v so that it is perpendicular to u. Do not change u, and do not use the zero vector.

Python

Step 3. Find the angle between two vectors

The angle comes from the equation cos θ = (u · v) / (|u| |v|) in Part 3 of this topic. The code below does the calculation in three steps. The third line calculates cos θ and stores it under the name c. The fourth line finds the angle with np.arccos, which is the inverse cosine. Python gives that angle in radians, so np.degrees changes it to degrees.

Python

The angle between (3, 4) and (2, 1) is about 26.6 degrees.

Now change u and v so that the code finds the angle between (1, 0) and (1, 1). Before you press Run, decide what the angle should be from a sketch.

Python

Python may print 45.00000000000001 and not 45. A computer stores most decimal numbers with a very small rounding error, so the last digits of a result can be slightly off. This is normal.

Step 4. Make a unit vector

From Part 1 of this topic, dividing a vector by its length gives a unit vector in the same direction. The / sign divides each component by the number on its right.

Python

The first result is the unit vector [0.6 0.8]. The second result is its length, 1.0.

In the next box, change the code so that e is the unit vector in the direction of (6, −8).

Python

Quiz

This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.

Choose how your answers are checked

1. Find the length of the vector (6, −8).

2. Calculate the dot product of (2, −3) and (4, 1).

3. Which of these vectors is perpendicular to (3, 2)?

4. Find the angle between the vectors (0, 2) and (3, 3), in degrees.

5. Which vector is the unit vector in the direction of (3, −4)?

Next: Topic 1.3, Lines and Planes