Calculus

Lagrange Multipliers

Stand the objective up as a hill and the constraint becomes a fence winding across it. Walk the fence to its highest point. There the ground below is tangent to a contour line, which is exactly the statement that the two gradients line up.

Height f here
Slope along the fence
∠(∇f, ∇g)
Multiplier λ
Verdict

Drag to orbit, scroll to zoom. The hill is f, the amber loop is f along the constraint circle, and the ball rolls to wherever the slope along the fence is zero.

What to observe

  1. The fence is the constraint circle lifted onto the hill, so its height is the objective. Optimizing under a constraint is just finding the highest point of that fence, and the flat spots are where walking along it stops changing your height.
  2. Watch the slope-along-the-fence readout as you drag. Where the contour on the ground crosses the circle the slope is nonzero, so you can still climb. At the top it passes through zero: the contour there is tangent to the circle.
  3. Tangent contours mean the two ground gradients point the same way, so∇f = λ∇g. The angle between them hits 0° exactly when the slope along the fence hits 0. That is the whole method in one picture.
  4. The multiplier λ is how steeply the peak height would rise if you let the circle grow. Switch objectives: x · y gives four flat spots, two peaks and two dips, and the fence has a top and a bottom to match.

Shortcuts: space run/pause · s step · r reset · f fullscreen