Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 · Vectors and Matrices

The Dot Product and Angles

Use the dot product to find angles between vectors, test for perpendicular vectors, compute projections and work, with runnable examples.

You can add vectors and scale them, but how do you measure the angle between two of them? The dot product answers that. It multiplies two vectors and returns a single number that records how much they point in the same direction.

Definition

For u = ⟨a, b⟩ and v = ⟨c, d⟩:

Text
u · v = ac + bd

There is a second, geometric form. If θ is the angle between the two vectors when drawn tail to tail (0° ≤ θ ≤ 180°), then

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

Both forms are always equal. The geometric form follows from the law of cosines applied to the triangle formed by u, v and u − v.

Putting them together gives the angle formula:

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

Example

Find the angle between u = ⟨3, 1⟩ and v = ⟨1, 2⟩.

Text
u · v = 3·1 + 1·2 = 5
|u| = √10,  |v| = √5
cos θ = 5 / (√10 · √5) = 5 / √50 = 1/√2
θ = 45°

What the sign tells you

  • u · v > 0: the angle is acute (the vectors lean the same way).
  • u · v = 0: the vectors are perpendicular (orthogonal), because cos 90° = 0.
  • u · v < 0: the angle is obtuse.

Properties worth knowing: u · v = v · u, u · u = |u|², and u · (v + w) = u · v + u · w.

Python Angle between two vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Projection

The scalar projection of u onto v is the signed length of the shadow of u along v:

Text
comp_v(u) = (u · v) / |v|

The vector projection is that length times the unit vector along v:

Text
proj_v(u) = ((u · v) / (v · v)) v

Example: project u = ⟨4, 3⟩ onto v = ⟨2, 0⟩. u · v = 8, v · v = 4, so proj = 2⟨2, 0⟩ = ⟨4, 0⟩. The shadow of ⟨4, 3⟩ on the x-axis is ⟨4, 0⟩, as you would expect.

Subtracting the projection leaves the part of u perpendicular to v: u − proj_v(u) = ⟨0, 3⟩.

Work

In physics, the work done by a constant force F moving an object through displacement d is

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

Only the part of the force along the motion counts. A 20 N force at 60° to the ground pulling a cart 5 m does 20 × 5 × cos60° = 50 J of work.

Python Projection, perpendicular part and work

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Finding a perpendicular vector

In 2D, a vector perpendicular to ⟨a, b⟩ is ⟨−b, a⟩ (a quarter turn counterclockwise): ⟨a, b⟩ · ⟨−b, a⟩ = −ab + ab = 0. This is how you build a direction for a line perpendicular to another.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is ⟨2, -3⟩ · ⟨4, 5⟩?

    2·4 + (-3)·5 = 8 - 15 = -7.
  2. 2. For which value of k are ⟨k, 3⟩ and ⟨2, 4⟩ perpendicular?

    2k + 12 = 0, so k = -6.
  3. 3. If u · v is negative, the angle between u and v is

    cos θ is negative exactly when θ is between 90° and 180°.
  4. 4. What is the work done by a force of 10 N along the direction of motion over 3 m?

    cos 0° = 1, so W = 10 × 3 = 30 J.

Summary

  • u · v = ac + bd = |u||v| cos θ.
  • cos θ = (u · v)/(|u||v|) gives the angle between vectors.
  • A zero dot product means perpendicular; the sign shows acute or obtuse.
  • Projection proj_v(u) = ((u · v)/(v · v)) v splits a vector into parallel and perpendicular parts.
  • Work is the dot product of force and displacement.

Next: Matrix Operations