Linear algebra part 7 - Angle between vectors
In the last post we proved that the dot product of perpendicular vectors is zero. In this post we will show how you can calculate the angle between any two vectors using the dot product. First, I need to introduce you to the concept of unit vectors.
Unit vectors
A unit vector is any vector with a length of one. If you need a refresher on how to calculate the length of a vector look at this previous post.
The most obvious examples of unit vectors are probably , , and so on. But there are an infinite number of unit vectors. For example, and are also unit vectors.
For the purpose of calculating the angle between vectors, an important unit vector is . Let us take a moment to prove that definitely is a unit vector. What we are saying is that the lenght of is one:
Since one squared equals one, this simplifies to:
Some of you might already recognise that proof from triganometry, but let us step through it as refresher. Consider a circle of radius one. The length of any line between the origin and the circle will be one, this is just the radius. Let us draw this line in the top right (positive x and y) quadrant of the graph and define the angle between the line and the positive x axis as .
Consider the right angle triangle created by that radius and the positive x axis. The hypotenuse of that triangle is the radius of the circle. The side adjacent to the angle is the x value. The side opposite to the angle is the y value.
Remember the definitions of cos and sine from triganometry:
In this case we know the hypotenuse has length one, can simplify these equations to:
Or in other terms:
It should the be a small leap to say that if hypotenuse in this example is a vector, , then:
Since we know that the length of is one, we know that is a unit vector.
Angle between two unit vectors
Let us take two vectors we have already shown to be unit vectors:
My assertion is that the dot product of these two vectors is the cosine of the angle between them. Let us start by plotting the two vectors. We have already shown how to plot and should be straight forward. Have a look at this post on plotting vectors if you need a refresher.
The angle, , between and is the same as the angle between and the positive x axis. Now let us calculate the dot product of and :
This shows that the dot product of two vectors is the angle between them, as long as one of them sits along the positive x axis. Now let us prove that this is also true if neither of the vectors sit on the axis of the graph. Let us define two new unit vectors:
We will define the angle between and , the angle we are trying to calculate, as . Notice that is the difference between and . Now let us calculate the dot product of and :
Hopefully you will recall from triganometry that can be rewritten as . We have defined the angle between and as , meaning that . We have therefore shown that:
Angle between non-unit vectors
So, what happens if our vectors happen to have any length other than one? We can just devide the vector by its length to make it equal to one. We can adjust our formula for the angle between vectors like this: