Tutorials › Calculus III › Deriving the Angle Formula From the Law of Cosines

Dot Products and Projections · Tutorial 54 of 999

Deriving the Angle Formula From the Law of Cosines

See why the law of cosines leads to a formula connecting the dot product of two nonzero vectors with the cosine of their angle.

Beginner 9 min read

What You'll Learn

  • Set up the triangle whose sides are determined by two vectors and their difference
  • Derive the dot-product angle formula from the law of cosines
  • Explain why the formula also holds for parallel and opposite vectors
  • Use the formula to find an angle from vector components
  • Check calculations by comparing squared side lengths
  • Recognize and correct a common error when expanding a vector difference

From Triangle Geometry to a Vector Formula

Two vectors based at the same point determine two sides of a triangle. The third side is the difference of the vectors. This simple geometric setup connects the law of cosines to the dot product: both describe the squared length of that third side, but in different ways.

The result is useful because it lets us find the angle between vectors from their components. The derivation also explains why the dot product contains information about angle, not only about coordinates.

Vectors, Lengths, and the Angle Between Them

For vectors \(\mathbf{a}\) and \(\mathbf{b}\) in three-dimensional space, their dot product is computed from components by multiplying corresponding components and adding the products. Their lengths, also called norms, are denoted by \(\|\mathbf{a}\|\) and \(\|\mathbf{b}\|\). For example, if \(\mathbf{a}=(a_1,a_2,a_3)\), then \(\|\mathbf{a}\|=\sqrt{a_1^2+a_2^2+a_3^2}\).

The angle between two nonzero vectors is the smaller angle formed when they are placed with their tails at the same point. When measured in radians, it satisfies \(0\leq\theta\leq\pi\). When measured in degrees, it satisfies \(0^\circ\leq\theta\leq180^\circ\). The vectors must be nonzero because a zero vector has no direction and therefore no angle with another vector.

Definition: For two nonzero vectors \(\mathbf{a}\) and \(\mathbf{b}\), the angle \(\theta\) between them is the angle between their directions when their tails coincide. Its range is \(0\leq\theta\leq\pi\) in radians, or \(0^\circ\leq\theta\leq180^\circ\) in degrees.

Imagine placing the tails of \(\mathbf{a}\) and \(\mathbf{b}\) at a point \(O\). Let their endpoints be \(A\) and \(B\). Then \(OA=\|\mathbf{a}\|\), \(OB=\|\mathbf{b}\|\), and the vector from \(A\) to \(B\) is \(\mathbf{b}-\mathbf{a}\), so \(AB=\|\mathbf{b}-\mathbf{a}\|\). The angle at \(O\) is \(\theta\).

The Angle Formula

The law of cosines applied to triangle \(OAB\) says that the square of the side opposite \(\theta\) is the sum of the squares of the other two sides, minus twice their product times \(\cos\theta\). Thus, when the triangle is nondegenerate, it gives an expression for \(\|\mathbf{b}-\mathbf{a}\|^2\).

There is another way to calculate the same quantity. Using the dot product and its distributive properties, expand the squared norm of \(\mathbf{b}-\mathbf{a}\). Comparing these two expressions yields the central relationship.

Theorem: If \(\mathbf{a}\) and \(\mathbf{b}\) are nonzero vectors and \(\theta\) is the angle between them, then
$$ \mathbf{a}\cdot\mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta. $$
Equivalently,
$$ \cos\theta = \frac{\mathbf{a}\cdot\mathbf{b}} {\|\mathbf{a}\|\,\|\mathbf{b}\|}, \qquad \theta = \arccos\left( \frac{\mathbf{a}\cdot\mathbf{b}} {\|\mathbf{a}\|\,\|\mathbf{b}\|} \right). $$
The inverse cosine here returns the angle in the range \(0\leq\theta\leq\pi\).

Deriving the Relationship

We now justify the formula by calculating the same squared distance in two ways. First suppose \(\mathbf{a}\) and \(\mathbf{b}\) are not parallel. Then the points \(O,A,B\) form a nondegenerate triangle, so the ordinary law of cosines applies.

1
Apply the law of cosines.
The side opposite the angle \(\theta\) has length \(\|\mathbf{b}-\mathbf{a}\|\). Therefore
$$ \|\mathbf{b}-\mathbf{a}\|^2 = \|\mathbf{a}\|^2+\|\mathbf{b}\|^2 -2\|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta. $$
2
Expand the squared norm.
A vector's squared norm is its dot product with itself. Using distributivity and symmetry of the dot product gives
$$ \begin{aligned} \|\mathbf{b}-\mathbf{a}\|^2 &=(\mathbf{b}-\mathbf{a})\cdot(\mathbf{b}-\mathbf{a})\\ &=\mathbf{b}\cdot\mathbf{b} -\mathbf{b}\cdot\mathbf{a} -\mathbf{a}\cdot\mathbf{b} +\mathbf{a}\cdot\mathbf{a}\\ &=\|\mathbf{b}\|^2-2\mathbf{a}\cdot\mathbf{b} +\|\mathbf{a}\|^2. \end{aligned} $$
3
Equate the expressions.
Both expressions equal \(\|\mathbf{b}-\mathbf{a}\|^2\), so
$$ \|\mathbf{a}\|^2+\|\mathbf{b}\|^2-2\mathbf{a}\cdot\mathbf{b} = \|\mathbf{a}\|^2+\|\mathbf{b}\|^2 -2\|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta. $$
Cancel the two squared-norm terms on both sides. Then divide by \(-2\):
$$ \mathbf{a}\cdot\mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta. $$

This proves the formula when the vectors are not parallel. The parallel cases can be checked directly, so no degenerate triangle is needed in the argument. If \(\mathbf{b}=\lambda\mathbf{a}\) for a nonzero real number \(\lambda\), then \(\mathbf{a}\cdot\mathbf{b}=\lambda\|\mathbf{a}\|^2\), and \(\|\mathbf{b}\|=|\lambda|\|\mathbf{a}\|\). If \(\lambda>0\), the angle is \(0\), and the formula's right side is \(|\lambda|\|\mathbf{a}\|^2=\lambda\|\mathbf{a}\|^2\). If \(\lambda<0\), the angle is \(\pi\), and the right side is \(-|\lambda|\|\mathbf{a}\|^2=\lambda\|\mathbf{a}\|^2\). Thus the formula holds for all nonzero vectors.

Worked Examples

Worked Example: Find the Angle Between Two Vectors in the Plane

Let \(\mathbf{a}=(1,2)\) and \(\mathbf{b}=(3,1)\). First calculate their dot product and lengths:

$$ \mathbf{a}\cdot\mathbf{b} =(1)(3)+(2)(1)=3+2=5, $$
and
$$ \|\mathbf{a}\|=\sqrt{1^2+2^2}=\sqrt{5}, \qquad \|\mathbf{b}\|=\sqrt{3^2+1^2}=\sqrt{10}. $$
The angle formula gives
$$ \cos\theta = \frac{5}{\sqrt{5}\sqrt{10}} = \frac{5}{\sqrt{50}} = \frac{1}{\sqrt{2}}. $$
Since the angle is in the interval \([0,\pi]\), this gives \(\theta=\frac{\pi}{4}\), or \(45^\circ\).

Check the geometry directly. The difference is \(\mathbf{b}-\mathbf{a}=(2,-1)\), so \(\|\mathbf{b}-\mathbf{a}\|^2=2^2+(-1)^2=5\). The law of cosines gives

$$ \|\mathbf{a}\|^2+\|\mathbf{b}\|^2 -2\|\mathbf{a}\|\|\mathbf{b}\|\cos\theta = 5+10-2(\sqrt{5})(\sqrt{10})\left(\frac{1}{\sqrt{2}}\right) =15-10=5. $$
Both calculations give the same squared side length.

Worked Example: Obtain an Obtuse Angle in Three Dimensions

Let \(\mathbf{a}=(1,0,0)\) and \(\mathbf{b}=(-1,\sqrt{3},0)\). The dot product is

$$ \mathbf{a}\cdot\mathbf{b} =(1)(-1)+(0)(\sqrt{3})+(0)(0)=-1. $$
Their lengths are
$$ \|\mathbf{a}\|=\sqrt{1^2+0^2+0^2}=1, \qquad \|\mathbf{b}\|=\sqrt{(-1)^2+(\sqrt{3})^2+0^2} =\sqrt{1+3}=2. $$
Therefore
$$ \cos\theta=\frac{-1}{(1)(2)}=-\frac12, \qquad \theta=\frac{2\pi}{3}=120^\circ. $$
To check, \(\mathbf{b}-\mathbf{a}=(-2,\sqrt{3},0)\), so its squared length is \(4+3=7\). The law of cosines gives
$$ 1^2+2^2-2(1)(2)\left(-\frac12\right) =1+4+2=7. $$
This agrees with the squared length of the difference vector.

Worked Example: Use the Formula Without Simplifying the Angle

Let \(\mathbf{a}=(2,-1,2)\) and \(\mathbf{b}=(1,2,2)\). Compute the dot product:

$$ \mathbf{a}\cdot\mathbf{b} =(2)(1)+(-1)(2)+(2)(2)=2-2+4=4. $$
The norms are
$$ \|\mathbf{a}\|=\sqrt{2^2+(-1)^2+2^2}=\sqrt{9}=3, \qquad \|\mathbf{b}\|=\sqrt{1^2+2^2+2^2}=\sqrt{9}=3. $$
Thus
$$ \cos\theta=\frac{4}{(3)(3)}=\frac49, \qquad \theta=\arccos\left(\frac49\right). $$
There is no need to force this angle into a familiar special-angle value. For a check, \(\mathbf{b}-\mathbf{a}=(-1,3,0)\), and its squared length is \(1+9=10\). The law-of-cosines expression is
$$ 3^2+3^2-2(3)(3)\left(\frac49\right) =9+9-8=10. $$
The result matches the squared length computed from components.

A Common Expansion Mistake

A frequent error is to treat the squared length of a difference as the sum of the squared lengths, leaving out the cross terms. In general, \(\|\mathbf{b}-\mathbf{a}\|^2\) is not \(\|\mathbf{a}\|^2+\|\mathbf{b}\|^2\). The dot-product expansion shows exactly what is missing.

For a concrete example, take \(\mathbf{a}=(1,0)\) and \(\mathbf{b}=(1,1)\). The incorrect calculation would say \(\|\mathbf{b}-\mathbf{a}\|^2=1+2=3\). But \(\mathbf{b}-\mathbf{a}=(0,1)\), so the actual squared length is \(0^2+1^2=1\). The mistaken value would also give an incorrect angle: substituting \(3\) into the law of cosines would yield \(\cos\theta=(1+2-3)/(2\sqrt{2})=0\), despite the dot product being \(1\).

The correction is to retain the cross terms:

$$ \|\mathbf{b}-\mathbf{a}\|^2 = \|\mathbf{a}\|^2+\|\mathbf{b}\|^2-2\mathbf{a}\cdot\mathbf{b} = 1+2-2(1)=1. $$
Consequently, \(\cos\theta=1/(\sqrt{1}\sqrt{2})=1/\sqrt{2}\), consistent with the actual angle of \(45^\circ\).

Why the Formula Matters

The formula converts geometric information into a component calculation. Once the dot product and lengths are known, the cosine of the angle follows by division. Conversely, if the angle and lengths are known, the formula gives the dot product. This is why the dot product is central to both coordinate calculations and geometric reasoning.

The nonzero condition matters in the angle formula: if either vector is zero, the denominator \(\|\mathbf{a}\|\|\mathbf{b}\|\) is zero, and the zero vector has no direction whose angle could be measured. For nonzero vectors, the earlier Cauchy-Schwarz inequality ensures that the ratio \((\mathbf{a}\cdot\mathbf{b})/(\|\mathbf{a}\|\|\mathbf{b}\|)\) lies between \(-1\) and \(1\), as required for the inverse cosine. The formula therefore produces an angle in the stated range.

Keep the geometric setup in mind: the two vectors are sides meeting at the angle, while their difference gives the opposite side. The law of cosines and the dot-product expansion are two descriptions of that same triangle, and equating them is the entire reason the angle formula works.

Check Your Understanding

Use the relationship between the law of cosines and the dot product to answer these questions.

  1. Why is \(\|\mathbf{b}-\mathbf{a}\|\) the length of the side opposite the angle between \(\mathbf{a}\) and \(\mathbf{b}\)?
  2. Expand \(\|\mathbf{b}-\mathbf{a}\|^2\) using dot products and identify the cross term.
  3. What is the angle range in radians, and what is the corresponding range in degrees?
  4. Why must both vectors be nonzero to use the angle formula?
  5. For \(\mathbf{a}=(1,1,0)\) and \(\mathbf{b}=(1,0,1)\), calculate the dot product, both norms, and \(\cos\theta\).