Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Multivariable calculus

Lagrange Multipliers for Constrained Optimization

To optimize under a constraint, find where the objective's contours just touch the constraint, where ∇f = λ∇g. The multiplier λ measures how much the optimum would improve if the constraint were relaxed. In mechanics it is the constraint force. Lagrange multipliers give max-entropy distributions, SVMs and safe reinforcement learning.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

The highest point on a trail

You must stay on a winding hiking trail. Where on the trail are you highest? Not at the mountain’s peak, which may be off the trail. At the best spot, the trail just touches a contour line without crossing it. If the trail crossed the contour, you could keep walking uphill along the trail.

That touching condition is the idea behind Lagrange multipliers.

Lagrange multipliers: slide along the constraint
·
f along the constraint
f = 0.7174 · ∇f = (0.55, 1.30) · ∇g = (2.61, 1.10) · not parallel: moving along the curve still changes f
Orange arrow = ∇f, black = ∇g (normal to the constraint). Yellow rings = where f peaks or bottoms out along the curve: exactly where the arrows line up.
Why engineers care

Engineers rarely optimize freely. A rocket must reach orbit using a fixed amount of fuel, a robot arm must keep its hand on a door handle, and a battery can only supply so much power. Lagrange multipliers solve these “best under a rule” problems, and they also say how much better things would be if the rule were loosened.

📝 Quick check (Level 0)
0/3 answered
  1. 1. At the trail's highest point, the trail…
  2. 2. Two numbers add to 10. To make their product as big as possible, choose…
  3. 3. With 20 m of fence, the biggest rectangle has side…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The method

To optimize f(x,y)f(x,y) subject to g(x,y)=cg(x,y) = c, solve

∇f=λ∇g,g(x,y)=c\nabla f = \lambda\nabla g, \qquad g(x,y) = c

That’s three equations in x,y,λx, y, \lambda. Equivalently, find the critical points of the Lagrangian L=f−λ(g−c)\mathcal L = f - \lambda(g - c).

Example. Maximize xyxy subject to x+y=10x + y = 10. Then (y,x)=λ(1,1)(y, x) = \lambda(1,1), so x=y=λx = y = \lambda, and the constraint gives x=y=5x = y = 5. The maximum is 2525, with λ=5\lambda = 5.

Fence. Maximize xyxy subject to 2x+2y=P2x + 2y = P. This gives a square of side P/4P/4.

📝 Quick check (Level 1)
0/3 answered
  1. 1. max xy subject to x + y = 10?
  2. 2. The Lagrange condition is…
  3. 3. The best rectangle with perimeter 40 has area…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Why the gradients must be parallel

∇g\nabla g is normal to the constraint curve (A3.2-3). If ∇f\nabla f had a component along the curve, moving along the curve would change ff, so the point couldn’t be optimal. So ∇f\nabla f must point along ∇g\nabla g. Equivalently, the level curve of ff is tangent to the constraint.

What λ means: the shadow price

If the constraint level changes from cc to c+δc + \delta, the best value changes by about λδ\lambda\delta:

df∗dc=λ\frac{df^*}{dc} = \lambda

For max⁡xy\max xy with x+y=cx + y = c: f∗=c2/4f^* = c^2/4, so df∗/dc=c/2=5=λdf^*/dc = c/2 = 5 = \lambda. In economics, λ\lambda is the price you would pay for one more unit of the resource.

More examples

Problem Lagrange solution
closest point to the origin on the plane n⋅x=d\mathbf n\cdot\mathbf x = d x=d n/∥n∥2\mathbf x = d\,\mathbf n/\lVert\mathbf n\rVert^2, distance ∣d∣/∥n∥\lvert d\rvert/\lVert\mathbf n\rVert
min-norm solution of Ax=bA\mathbf x = \mathbf b x=A⊤(AA⊤)−1b\mathbf x = A^\top(AA^\top)^{-1}\mathbf b, the pseudo-inverse (A2.3-7)
extremize a quadratic form on the sphere ∥x∥=1\lVert\mathbf x\rVert = 1 Ax=λxA\mathbf x = \lambda\mathbf x: eigenvectors (A2.4-2)
max entropy with known mean pi∝eμip_i\propto e^{\mu i}, an exponential family

Several constraints: ∇f=∑kλk∇gk\nabla f = \sum_k\lambda_k\nabla g_k, with one multiplier per constraint.

🐍 Exercise 1: solve Lagrange systems with Newton's method
Apply Newton to the joint system ∇f − λ∇g = 0, g = 0. Solve max xy on x + y = 10 (λ = 5) and min x + 2y on x² + y² = 5 (λ = −½), and check λ = df*/dc.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Distance from the origin to x + 2y + 2z = 6?
  2. 2. Extremizing xᵀAx on the unit sphere gives…
  3. 3. λ equals…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

The theorem and its fine print

Theorem (Lagrange)

If x∗\mathbf x^* is a local extremum of ff on {g=c}\{g = c\}, ff and gg are C1C^1, and ∇g(x∗)≠0\nabla g(\mathbf x^*)\ne\mathbf 0, then ∇f(x∗)=λ∇g(x∗)\nabla f(\mathbf x^*) = \lambda\nabla g(\mathbf x^*) for some λ\lambda.

Proof. Since ∇g≠0\nabla g\ne0, the implicit function theorem (A3.2-4) makes {g=c}\{g = c\} a smooth curve (or surface) near x∗\mathbf x^*. For any curve r(t)\mathbf r(t) in it through x∗\mathbf x^*, f(r(t))f(\mathbf r(t)) has an extremum at t=0t = 0, so ∇f⋅r′(0)=0\nabla f\cdot\mathbf r'(0) = 0. So ∇f\nabla f is orthogonal to every tangent direction, as is ∇g\nabla g. In codimension one, this forces them to be parallel. ∎

The condition ∇g≠0\nabla g\ne0 (a constraint qualification) matters. For min⁡x\min x on y2=x3y^2 = x^3, the minimum is at the cusp (0,0)(0,0), where ∇g=0\nabla g = 0 and no λ\lambda exists.

Second-order check and inequality constraints

The Lagrange condition finds candidates. To tell a max from a min, look at the Hessian of L\mathcal L restricted to the tangent space of the constraint (the “bordered Hessian”). For inequality constraints g(x)≤cg(\mathbf x)\le c, the KKT conditions add λ≥0\lambda\ge0 and complementary slackness λ(g(x)−c)=0\lambda(g(\mathbf x) - c) = 0. An inactive constraint gets λ=0\lambda = 0. This is the foundation of convex optimization and duality (A6).

Exercise 3.1: Show that λ = df*/dc (the envelope theorem)

f∗(c)=f(x∗(c))f^*(c) = f(\mathbf x^*(c)), so df∗dc=∇f⋅dx∗dc=λ∇g⋅dx∗dc\frac{df^*}{dc} = \nabla f\cdot\frac{d\mathbf x^*}{dc} = \lambda\nabla g\cdot\frac{d\mathbf x^*}{dc}. Differentiating g(x∗(c))=cg(\mathbf x^*(c)) = c gives ∇g⋅dx∗dc=1\nabla g\cdot\frac{d\mathbf x^*}{dc} = 1. Hence df∗/dc=λdf^*/dc = \lambda.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The Lagrange theorem needs…
  2. 2. An inactive inequality constraint has multiplier…
  3. 3. With 3 equality constraints, how many multipliers?

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

In mechanics, the multiplier is the constraint force

A pendulum bob is a particle constrained to ∥x∥=L\lVert\mathbf x\rVert = L. Lagrange’s mechanics (1788) writes mx¨=F+λ∇gm\ddot{\mathbf x} = \mathbf F + \lambda\nabla g. Differentiating the constraint twice gives λ\lambda at every instant, and λ∇g\lambda\nabla g is the rod’s tension. Physics engines simulate joints, contacts and closed kinematic chains exactly this way, solving for multipliers (constraint impulses) every timestep (Featherstone, 2008; Todorov, Erez and Tassa, 2012).

🐍 Exercise 2 (research): the pendulum's tension as a Lagrange multiplier
Simulate a particle constrained to a circle under gravity. Solve for the multiplier at each step from the twice-differentiated constraint, and match the textbook tension m(g cos θ + v²/L) along the swing.

Multipliers in ML and robotics

  • Support vector machines. The dual of the max-margin problem has one multiplier per training point. Points with λi>0\lambda_i>0 are the support vectors (Cortes and Vapnik, 1995; D5).
  • Maximum entropy. The least-committal distribution matching given moments is an exponential family whose parameters are the Lagrange multipliers (Jaynes, 1957; Practice Code 5). This underlies logistic regression, softmax, and maximum-entropy inverse RL (Ziebart et al., 2008).
  • Safe reinforcement learning. Constrained MDPs maximize reward subject to expected cost ≤d\le d. Lagrangian methods learn λ\lambda alongside the policy, raising the “price” of unsafe behaviour until the constraint holds (Altman, 1999; Tessler, Mankowitz and Mannor, 2019; Stooke, Achiam and Abbeel, 2020).
  • Trajectory optimization. Dynamics and contact constraints are enforced with multipliers (direct collocation, augmented Lagrangian solvers like ALTRO). The multipliers at the solution are the contact forces and their sensitivities.

References

  1. J.-L. Lagrange (1788). Mécanique analytique.
  2. E. T. Jaynes (1957). “Information theory and statistical mechanics.” Physical Review, 106(4).
  3. H. Kuhn, A. Tucker (1951). “Nonlinear programming.” Proc. 2nd Berkeley Symposium. (KKT conditions; also W. Karush, 1939.)
  4. C. Cortes, V. Vapnik (1995). “Support-vector networks.” Machine Learning, 20.
  5. E. Altman (1999). Constrained Markov Decision Processes. Chapman & Hall.
  6. B. Ziebart, A. Maas, J. A. Bagnell, A. Dey (2008). “Maximum entropy inverse reinforcement learning.” AAAI.
  7. R. Featherstone (2008). Rigid Body Dynamics Algorithms. Springer.
  8. C. Tessler, D. Mankowitz, S. Mannor (2019). “Reward constrained policy optimization.” ICLR.
  9. S. Boyd, L. Vandenberghe (2004). Convex Optimization. Cambridge University Press. Chapter 5.