Source: algebrica.org — CC BY-NC 4.0 https://algebrica.org/law-of-cosines/
Definition
The law of cosines relates the sides of any triangle through the angle opposite to one of them. It can be viewed as a generalisation of the Pythagorean theorem, valid not only for right triangles but for every triangle: the square of a side equals the sum of the squares of the other two sides, minus a corrective term that accounts for how open the angle between them is. For a triangle with sides $a, b, c$ and angle $\theta$ opposite to side $c$, the law states:
When $\theta = 90^\circ$ the cosine term vanishes and the formula reduces exactly to the Pythagorean theorem, which confirms that the law of cosines is a strict generalisation of that result. For any other angle, the corrective term either subtracts from or adds to the sum $a^2 + b^2$, depending on whether $\theta$ is acute or obtuse.
To derive the formula, drop the altitude $h$ from the vertex opposite to $c$ to the side $b$. This divides $b$ into two segments: $m = a\cos(\theta)$ and $n = b - a\cos(\theta)$, while the altitude itself satisfies $h = a\sin(\theta)$. Applying the Pythagorean theorem to the right triangle formed by $n$, $h$ and $c$ gives:
Since the Pythagorean identity gives $\sin^2(\theta) + \cos^2(\theta) = 1$, the expression simplifies to:
The law of cosines is often used in conjunction with the law of sines, which provides a complementary approach to solving triangles when different combinations of sides and angles are known.
Example 1
Consider a triangle with sides $a = 8$, $b = 6$ and included angle $\theta = 60^\circ$. The goal is to determine the length of the third side $c$. Substituting the known values into the law of cosines gives:
Taking the positive square root, one obtains $c = \sqrt{52} = 2\sqrt{13} \approx 7.21$.
The length of the third side is approximately $7.21$ units.
Example 2
Consider a triangle with sides $a = 5$, $b = 7$ and $c = 9$. The goal is to determine the angle $\theta$ opposite to side $c$. Solving the law of cosines for $\cos(\theta)$ gives:
Substituting the known values:
Since $\cos(\theta) < 0$, the angle $\theta$ is obtuse. Taking the inverse cosine yields:
The angle opposite to the longest side is approximately $95.7^\circ$.
Vector interpretation
The law of cosines admits a reading in terms of vectors that exposes its deeper structure and connects it to the inner product. Consider a triangle with vertex $O$, and let $\vec{u}$ and $\vec{v}$ denote the two sides of length $a$ and $b$ issuing from $O$, so that $a = \|\vec{u}\|$ and $b = \|\vec{v}\|$. The third side of the triangle, of length $c$, is then represented by the vector $\vec{v} - \vec{u}$, which joins the endpoints of $\vec{u}$ and $\vec{v}$. Expanding the squared norm of this vector through the bilinearity of the inner product gives:
The geometric definition of the inner product states that:
$\theta$ is the angle between the two vectors at $O$, which coincides with the angle between the sides $a$ and $b$ of the triangle. Substituting this identity into the expansion above gives:
From this point of view the law of cosines is a reformulation of the identity that defines the inner product in terms of lengths and angles. The corrective term $-2ab\cos\theta$ that distinguishes a generic triangle from a right one is nothing other than $-2\,\vec{u} \cdot \vec{v}$, and the Pythagorean case corresponds to the situation in which the two vectors are orthogonal, so that $\vec{u} \cdot \vec{v} = 0$.