Often you cannot go anywhere you like: you must stay on a curve or a surface. Maximize the area of a fence with a fixed length, or find the hottest point on a circular wire. That is constrained optimization: extremes of $f$ subject to $g(x, y) = k$. The key picture uses two facts you know: the gradient is perpendicular to level curves, and a level curve of $f$ that crosses the constraint cannot mark an extreme.
🔑 Key idea
At a maximum or minimum of $f$ on the curve $g(x, y) = k$ (with $\nabla g \ne \mathbf{0}$), the gradients are parallel:
$$\nabla f = \lambda\nabla g, \qquad g(x, y) = k.$$
Solve this system for $x$, $y$ and $\lambda$ (the Lagrange multiplier), then evaluate $f$ at every solution and compare.
💡 Why it works
Move along the constraint curve $\mathbf{r}(t)$. If $f$ has its largest value on the curve at $\mathbf{r}(t_0)$, then $\frac{d}{dt}f(\mathbf{r}(t)) = \nabla f \cdot \mathbf{r}'(t)$ is ${}0$ at $t_0$: $\nabla f$ is perpendicular to the curve. But $\nabla g$ is also perpendicular to the curve, because the curve is a level curve of $g$. In the plane, two vectors perpendicular to the same direction are parallel, so $\nabla f = \lambda \nabla g$. In pictures: at the extreme, the level curve of $f$ just touches the constraint curve without crossing it.
Level curves of $f = xy$ (hyperbolas) and the constraint $x^2 + y^2 = 4$ (blue). Where the bold level curve crosses the circle, you can still climb along the circle; where it only touches, you cannot.
Worked examples
✏️ Worked example: A linear function on a circle
Find the maximum and minimum of $f(x, y) = 3x + 4y$ on $x^2 + y^2 = 25$.
$\nabla f = \lambda\nabla g$: ${}3 = 2\lambda x$ and ${}4 = 2\lambda y$. So $\lambda \ne 0$ and $(x, y) = \frac{1}{2\lambda}(3, 4)$: parallel to $\langle 3, 4 \rangle$.
On the circle of radius 5 that gives $(3, 4)$ and $(-3, -4)$.
$f(3, 4) = 25$ and $f(-3, -4) = -25$.
Answer: maximum ${}25$, minimum $-25$
✏️ Worked example: Check every solution
Find the extremes of $f(x, y) = xy$ on $x^2 + y^2 = 8$.
$y = 2\lambda x$ and $x = 2\lambda y$. Then $y = 4\lambda^2 y$, so $y = 0$ or $\lambda = \pm\frac{1}{2}$.
$y = 0$ forces $x = 0$, which is not on the circle. $\lambda = \frac{1}{2}$ gives $y = x$; $\lambda = -\frac{1}{2}$ gives $y = -x$.
Points: $(2, 2)$, $(-2, -2)$ with $f = 4$; $(2, -2)$, $(-2, 2)$ with $f = -4$.
Answer: maximum ${}4$, minimum $-4$
✏️ Worked example: Closest point on a plane
Find the point of the plane $x + 2y + 2z = 9$ closest to the origin.
Minimize the squared distance $f = x^2 + y^2 + z^2$ with $g = x + 2y + 2z = 9$.
$\langle 2x, 2y, 2z \rangle = \lambda\langle 1, 2, 2 \rangle$, so $(x, y, z) = \frac{\lambda}{2}(1, 2, 2)$.
The constraint gives $\frac{\lambda}{2}(1 + 4 + 4) = 9$, so $\frac{\lambda}{2} = 1$: the point $(1, 2, 2)$, at distance ${}3$.
Answer: $(1, 2, 2)$
⚠️ Common mistake
Solving for $\lambda$ and stopping. The multiplier is a helper, not the answer. The system usually has several solutions; each is a candidate, and you must evaluate $f$ at all of them (as in the second example, where two give the maximum and two the minimum). Also watch for lost solutions: dividing an equation by $x$ or $y$ can throw away the case where that variable is ${}0$, so factor instead.
Practice
Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.