Part I — Calculus, Optimization & Gradients · Chapter 9

Constrained optimization & Lagrange multipliers

Hook

Every optimum so far has been free to sit anywhere. What happens when the answer is required to lie exactly on some boundary — a fixed budget, a fixed total — and "downhill" would otherwise walk straight off of it?

Intuition
x + y = 4

The dashed line is a hard constraint: every point on it satisfies x+y=4x+y=4. The circles are level sets of f(x,y)=x2+y2f(x,y)=x^2+y^2 — the smaller the circle, the smaller ff is there. Slide along the line and watch the circle passing through the point grow and shrink.

Formalize

At the constrained optimum, the objective's level curve is exactly tangent to the constraint — their gradients point the same direction:

f(x,y)=λg(x,y)\nabla f(x,y) = \lambda\, \nabla g(x,y)
  • f(x,y)f(x,y) — the objective being minimized (or maximized).
  • g(x,y)=0g(x,y)=0 — the equality constraint the solution must satisfy.
  • λ\lambda — the Lagrange multiplier: the scalar that makes the two gradients equal.
  1. Why tangency is the condition

    If the level curve crossed the constraint instead of touching it, you could slide a little further along the constraint and keep decreasing ff — so a crossing point can never be optimal. Only where the curve is tangent to the line is there nowhere better left to slide.

  2. Tangent curves share a normal direction

    Two curves are tangent exactly when their gradients (normal directions) are parallel — which is what f=λg\nabla f = \lambda\nabla g says algebraically.

  3. λ measures sensitivity to the constraint

    λ\lambda isn't just bookkeeping: it says how much the optimal ff would change per unit of slack if the constraint (here, the "4") were relaxed.

Play
x + y = 4

f(x,y) = 16.00 — alignment error (∂f/∂x − ∂f/∂y) = -8.00

λ implied by x: 0.00, λ implied by y: 8.00

The accent arrow is f\nabla f at the current point; the dashed-line-colored arrow is the fixed direction of g=(1,1)\nabla g=(1,1). Slide until they point the same way — that's the only spot on the whole line where they agree, and it's exactly where the level circle stops crossing the line and just touches it.

Worked example

Minimize f(x,y)=x2+y2f(x,y)=x^2+y^2 subject to g(x,y)=x+y4=0g(x,y)=x+y-4=0:

  1. Write the Lagrange condition componentwise

    f=(2x,2y)\nabla f = (2x, 2y) and g=(1,1)\nabla g=(1,1), so f=λg\nabla f=\lambda\nabla g means 2x=λ2x=\lambda and 2y=λ2y=\lambda — which forces x=yx=y.

  2. Combine with the constraint

    Substituting x=yx=y into x+y=4x+y=4 gives 2x=42x=4, so x=y=2x=y=2 and λ=2x=4\lambda=2x=4.

  3. Check a non-optimal point for contrast

    At (0,4)(0,4): f=(0,8)\nabla f=(0,8). Matching components gives λ=0\lambda=0 from xx but λ=8\lambda=8 from yy — they disagree, so (0,4)(0,4) is not a critical point of the constrained problem, and indeed f(0,4)=16>f(2,2)=8f(0,4)=16 > f(2,2)=8.

Checkpoint

Slide the point along the constraint line until ∇f is parallel to ∇g — the accent arrow lines up with the dashed constraint arrow.

x + y = 4
alignment error = -8.00
Move the slider to try it
Summary
f(x,y)=λg(x,y)\nabla f(x,y) = \lambda\, \nabla g(x,y)

Constrained optimization doesn't abandon "follow the gradient" — it just adds one requirement: stay on the constraint, and stop only where the objective's gradient has nothing left to offer that the constraint would still allow. This exact condition, generalized to inequality constraints via the KKT conditions, is what SVMs solve to find their maximum-margin boundary.