Introduction
The setup is a function and a curve . The problem is to find the extremal values of on the curve. If the curve is given with a parametrization , then we could just extremize using the 1D theory. If the curve is instead given by a constraint such as , then there is another technique that can be much easier to implement than finding a parametrization for . It is called Lagrange multipliers. Since the constraint is written as , you can think of this as a relative of implicit differentiation.
The method of Lagrange multipliers makes use of the fact that when is extremized at on , then the gradients must align: for some scalar .

Gradients align at extremal values
The constraint curve is given as the level curve with . To show the gradients align, we show that they are both perpendicular to this level curve.
Let be a parametrization of . We have already seen that:for any , so is always perpendicular to the level curves. In addition, at the specific time where is extremized, we also have from the 1D optimization theory and the chain rule:
and this shows that is also perpendicular to the level curve at . Since both are perpendicular to the level curve, they must point along the same line.
Lagrange multipliers: parallel level curves
It is convenient to visualize the Lagrange condition on a contour map. The curve will be parallel to the level curves of at the points where the value of is extremized on .
If the level curves of are not parallel to at a point , then it is possible to increase or decrease by moving along because this motion will cross over level curves of .Because is a level curve of , it is perpendicular to . So the level curves of and must be parallel at an extremal point. This happens if and only if the gradients are parallel.
