TExcelTAD
Home › Calculus in Depth › Lagrange Multipliers

Lagrange Multipliers

Many optimisation problems come with a constraint: maximise volume with a fixed amount of material, minimise cost at a required output, find the point of a surface closest to the origin. The method of Lagrange multipliers turns "optimise ff subject to g=kg = k" into a system of equations, based on one geometric fact: at the optimum, the level curve of ff touches the constraint curve, so their gradients are parallel. This lesson proves that fact, handles one and two constraints, sketches inequality constraints (KKT), and shows where multipliers appear in machine learning.

The survey version is Lagrange Multipliers; boundaries in Maximum and Minimum Values are a typical use.

The method

Theorem. Let ff and gg have continuous first partials. If ff, restricted to the curve (or surface) g=kg = k, has a local extremum at PP, and ∇g(P)≠0\nabla g(P) \ne \mathbf{0}, then there is a number λ\lambda (the Lagrange multiplier) with

∇f(P)=λ∇g(P)\nabla f(P) = \lambda \nabla g(P)

Proof. Let r(t)\mathbf{r}(t) be any differentiable curve on the constraint set with r(t0)=P\mathbf{r}(t_0) = P. Then h(t)=f(r(t))h(t) = f(\mathbf{r}(t)) has a local extremum at t0t_0, so by the chain rule

0=h′(t0)=∇f(P)⋅r′(t0)0 = h'(t_0) = \nabla f(P) \cdot \mathbf{r}'(t_0)

So ∇f(P)\nabla f(P) is perpendicular to every tangent vector of the constraint set at PP. So is ∇g(P)\nabla g(P), because the gradient is normal to the level set g=kg = k (directional derivatives). In two dimensions the tangent directions form a line, its normals a line, and two vectors on that line are parallel: ∇f=λ∇g\nabla f = \lambda \nabla g. In three dimensions with one constraint the tangent plane has a one-dimensional normal space, and the same argument works.

Recipe. To find the extreme values of f(x,y,z)f(x, y, z) subject to g(x,y,z)=kg(x, y, z) = k:

  1. Solve the system ∇f=λ∇g\nabla f = \lambda \nabla g, g=kg = k (four equations, four unknowns x,y,z,λx, y, z, \lambda).
  2. Evaluate ff at every solution. If the constraint set is closed and bounded, the largest value is the maximum and the smallest the minimum.

Also check points where ∇g=0\nabla g = \mathbf{0} on the constraint set; the theorem says nothing there.

Example: closest point on a plane

Find the point of the plane x+2y+2z=9x + 2y + 2z = 9 closest to the origin. Minimise the squared distance f=x2+y2+z2f = x^2 + y^2 + z^2 (same minimiser, easier derivatives):

2x=λ,2y=2λ,2z=2λ⟹y=z=2xx+4x+4x=9⟹x=1,(x,y,z)=(1,2,2)distance=1+4+4=3\begin{aligned} &2x = \lambda, \quad 2y = 2\lambda, \quad 2z = 2\lambda && \Longrightarrow \quad y = z = 2x \\ &x + 4x + 4x = 9 && \Longrightarrow \quad x = 1, \quad (x, y, z) = (1, 2, 2) \\ &\text{distance} = \sqrt{1 + 4 + 4} = 3 \end{aligned}

This matches the point-to-plane formula ∣0−9∣/∥⟨1,2,2⟩∥=9/3=3\lvert 0 - 9 \rvert/\lVert \langle 1, 2, 2 \rangle \rVert = 9/3 = 3 from Equations of Lines and Planes.

python · try it yourself
import sympy as sp

x, y, lam = sp.symbols("x y lam", real=True)
f = x * y                     # maximise and minimise xy ...
g = x**2 / 4 + y**2 - 2       # ... on the ellipse x²/4 + y² = 2

eqs = [sp.diff(f, x) - lam * sp.diff(g, x),
       sp.diff(f, y) - lam * sp.diff(g, y),
       g]
sols = sp.solve(eqs, [x, y, lam], dict=True)
for s in sorted(sols, key=lambda d: (d[x], d[y])):
    print(f"x = {s[x]}, y = {s[y]}, lambda = {s[lam]}, f = {f.subs(s)}")
x = -2, y = -1, lambda = 1, f = 2
x = -2, y = 1, lambda = -1, f = -2
x = 2, y = -1, lambda = -1, f = -2
x = 2, y = 1, lambda = 1, f = 2

The ellipse is closed and bounded, so the maximum of xyxy is 2 (at ±(2,1)\pm(2, 1)) and the minimum is −2-2 (at ±(2,−1)\pm(2, -1)).

The meaning of λ\lambda

Let M(k)M(k) be the optimal value of ff subject to g=kg = k. Under mild conditions

dMdk=λ\frac{dM}{dk} = \lambda

The multiplier is the sensitivity of the optimum to the constraint level: in economics, the "shadow price" of one more unit of budget. In the closest-point example, minimising x2+y2+z2x^2 + y^2 + z^2 on x+2y+2z=kx + 2y + 2z = k gives M(k)=k2/9M(k) = k^2/9, so dM/dk=2k/9=2dM/dk = 2k/9 = 2 at k=9k = 9, and indeed λ=2x=2\lambda = 2x = 2.

Two constraints

To optimise ff subject to g=kg = k and h=ch = c, the tangent directions of the constraint curve are perpendicular to both ∇g\nabla g and ∇h\nabla h, so ∇f\nabla f must lie in their span:

∇f=λ∇g+μ∇h,g=k,h=c\nabla f = \lambda \nabla g + \mu \nabla h, \qquad g = k, \qquad h = c

(five equations in x,y,z,λ,μx, y, z, \lambda, \mu). Example: the highest point (largest zz) on the curve where the plane x+y+z=1x + y + z = 1 meets the cylinder x2+y2=2x^2 + y^2 = 2:

⟨0,0,1⟩=λ⟨1,1,1⟩+μ⟨2x,2y,0⟩⟹λ=1,1+2μx=0,1+2μy=0⟹x=yx2+y2=2⟹x=y=±1;z=1−2x(1,1,−1): z=−1 (lowest)(−1,−1,3): z=3 (highest)\begin{aligned} &\langle 0, 0, 1 \rangle = \lambda\langle 1, 1, 1 \rangle + \mu\langle 2x, 2y, 0 \rangle \\ &\Longrightarrow \lambda = 1, \quad 1 + 2\mu x = 0, \quad 1 + 2\mu y = 0 \quad \Longrightarrow \quad x = y \\ &x^2 + y^2 = 2 \Longrightarrow x = y = \pm 1; \qquad z = 1 - 2x \\ &(1, 1, -1)\text{: } z = -1 \text{ (lowest)} \qquad (-1, -1, 3)\text{: } z = 3 \text{ (highest)} \end{aligned}

Inequality constraints: KKT in one paragraph

For "minimise ff subject to g(x)≤0g(x) \le 0", either the optimum is inside (g<0g < 0, and then ∇f=0\nabla f = \mathbf{0} as usual) or on the boundary (g=0g = 0, and then ∇f=−λ∇g\nabla f = -\lambda \nabla g with λ≥0\lambda \ge 0: the objective must want to push outward). Both cases are summarised by the Karush–Kuhn–Tucker conditions:

∇f+λ∇g=0,g≤0,λ≥0,λg=0(complementary slackness)\nabla f + \lambda \nabla g = \mathbf{0}, \qquad g \le 0, \qquad \lambda \ge 0, \qquad \lambda g = 0 \quad \text{(complementary slackness)}

These are the conditions numerical solvers such as SciPy's SLSQP try to satisfy; see Constrained Optimization.

python · try it yourself
import numpy as np
from scipy.optimize import minimize

# minimise (x-2)^2 + (y-1)^2 subject to x^2 + y^2 <= 1  (the target lies outside the disk)
f = lambda p: (p[0] - 2)**2 + (p[1] - 1)**2
cons = [{"type": "ineq", "fun": lambda p: 1 - p[0]**2 - p[1]**2}]   # SciPy wants fun >= 0
res = minimize(f, x0=[0.0, 0.0], constraints=cons, method="SLSQP")

p = res.x
print("optimum:", np.round(p, 5), " f =", round(float(res.fun), 5))
print("exact  :", np.round(np.array([2, 1]) / np.sqrt(5), 5))
# KKT: grad f = -lambda * grad g with g = x^2 + y^2 - 1
gf, gg = 2 * (p - np.array([2, 1])), 2 * p
lam = -gf @ gg / (gg @ gg)
print("lambda =", round(float(lam), 4), " residual", round(float(np.linalg.norm(gf + lam * gg)), 6))
optimum: [0.89443 0.44721]  f = 1.52786
exact  : [0.89443 0.44721]
lambda = 1.2361  residual 0.0

The constraint is active, λ=5−1≈1.2361>0\lambda = \sqrt{5} - 1 \approx 1.2361 > 0, and the KKT residual is zero to the printed precision.

In software

  • Maximum entropy gives softmax. Maximise the entropy −∑piln⁡pi-\sum p_i \ln p_i subject to ∑pi=1\sum p_i = 1 and a fixed expected score ∑pisi=c\sum p_i s_i = c. The Lagrange conditions −ln⁡pi−1=λ+μsi-\ln p_i - 1 = \lambda + \mu s_i give pi∝e−μsip_i \propto e^{-\mu s_i}: the softmax (Boltzmann) distribution, with −μ-\mu acting as an inverse temperature. This is the calculus behind softmax layers and temperature sampling in LLMs.
  • Support vector machines. Training an SVM maximises the margin subject to one inequality per training point; the KKT multipliers are non-zero only for the support vectors, and the dual problem is written entirely in these multipliers. See SVM.
  • Constrained layout and resource allocation. Splitting a budget across ad channels, servers or portfolio assets to maximise return (or minimise risk) under total=B\text{total} = B is a Lagrange problem; at the optimum every channel has the same marginal return per unit, λ\lambda.
  • Physics engines. Joint and contact constraints in rigid-body solvers are enforced with Lagrange multipliers, which turn out to be the constraint forces (impulses); see Distance Constraints.

Exercises

Exercise 1: Fixed perimeter

Use Lagrange multipliers to find the rectangle of perimeter 40 with the largest area.

Answer

The 10×1010 \times 10 square, area 100.

Solution
maximise A=xy subject to 2x+2y=40⟨y,x⟩=λ⟨2,2⟩⟹x=y=2λ4x=40⟹x=y=10,A=100,λ=5\begin{aligned} &\text{maximise } A = xy \text{ subject to } 2x + 2y = 40 \\ &\langle y, x \rangle = \lambda\langle 2, 2 \rangle \Longrightarrow x = y = 2\lambda \\ &4x = 40 \Longrightarrow x = y = 10, \quad A = 100, \quad \lambda = 5 \end{aligned}

Exercise 2: Extremes on a circle

Find the maximum and minimum of f(x,y)=3x+4yf(x, y) = 3x + 4y on the circle x2+y2=1x^2 + y^2 = 1.

Answer

Maximum 5 at (3/5,4/5)(3/5, 4/5); minimum −5-5 at (−3/5,−4/5)(-3/5, -4/5).

Solution
⟨3,4⟩=λ⟨2x,2y⟩⟹x=3/(2λ),y=4/(2λ)=2/λx2+y2=25/(4λ2)=1⟹λ=±5/2λ=5/2:(3/5,4/5),f=9/5+16/5=5λ=−5/2:(−3/5,−4/5),f=−5\begin{aligned} &\langle 3, 4 \rangle = \lambda\langle 2x, 2y \rangle \Longrightarrow x = 3/(2\lambda), \quad y = 4/(2\lambda) = 2/\lambda \\ &x^2 + y^2 = 25/(4\lambda^2) = 1 \Longrightarrow \lambda = \pm 5/2 \\ &\lambda = 5/2\text{:} \quad (3/5, 4/5), \quad f = 9/5 + 16/5 = 5 \\ &\lambda = -5/2\text{:} \quad (-3/5, -4/5), \quad f = -5 \end{aligned}

This is Cauchy–Schwarz: ∣⟨3,4⟩⋅u∣≤5\lvert \langle 3, 4 \rangle \cdot \mathbf{u} \rvert \le 5 for unit u\mathbf{u}.

Exercise 3: The shadow price

Maximise f=x+yf = x + y subject to x2+4y2=kx^2 + 4y^2 = k for k>0k > 0, express the maximum M(k)M(k), and verify that dM/dkdM/dk equals λ\lambda.

Answer

M(k)=5k/2M(k) = \sqrt{5k}/2, λ=5/(4k)=dM/dk\lambda = \sqrt{5}/(4\sqrt{k}) = dM/dk.

Solution
⟨1,1⟩=λ⟨2x,8y⟩⟹x=1/(2λ),y=1/(8λ)x2+4y2=14λ2+116λ2=516λ2=k⟹λ=54kM=x+y=58λ=5⋅4k85=5k2=5k2dMdk=54k=λ ✓\begin{aligned} &\langle 1, 1 \rangle = \lambda\langle 2x, 8y \rangle \Longrightarrow x = 1/(2\lambda), \quad y = 1/(8\lambda) \\ &x^2 + 4y^2 = \frac{1}{4\lambda^2} + \frac{1}{16\lambda^2} = \frac{5}{16\lambda^2} = k \Longrightarrow \lambda = \frac{\sqrt{5}}{4\sqrt{k}} \\ &M = x + y = \frac{5}{8\lambda} = \frac{5 \cdot 4\sqrt{k}}{8\sqrt{5}} = \frac{\sqrt{5}\sqrt{k}}{2} = \frac{\sqrt{5k}}{2} \\ &\frac{dM}{dk} = \frac{\sqrt{5}}{4\sqrt{k}} = \lambda \ \checkmark \end{aligned}

Exercise 4: Budget allocation (code)

A service splits a budget of 100 between two caches; the hit-rate gain is G(x,y)=3ln⁡(1+x)+2ln⁡(1+y)G(x, y) = 3\ln(1 + x) + 2\ln(1 + y). Solve the Lagrange conditions by hand, then check with SciPy.

Answer

x=60.2x = 60.2, y=39.8y = 39.8, G≈19.76G \approx 19.76, λ=3/61.2≈0.049\lambda = 3/61.2 \approx 0.049.

Solution
3/(1+x)=λ,2/(1+y)=λ⟹1+x=1.5(1+y)x+y=100⟹1.5(1+y)−1+y=100⟹2.5y=99.5⟹y=39.8, x=60.2\begin{aligned} &3/(1 + x) = \lambda, \quad 2/(1 + y) = \lambda \Longrightarrow 1 + x = 1.5(1 + y) \\ &x + y = 100 \Longrightarrow 1.5(1 + y) - 1 + y = 100 \Longrightarrow 2.5y = 99.5 \Longrightarrow y = 39.8,\ x = 60.2 \end{aligned}
python · try it yourself
import numpy as np
from scipy.optimize import minimize
G = lambda p: 3 * np.log1p(p[0]) + 2 * np.log1p(p[1])
res = minimize(lambda p: -G(p), x0=[50, 50], method="SLSQP",
               constraints=[{"type": "eq", "fun": lambda p: p[0] + p[1] - 100}],
               bounds=[(0, 100), (0, 100)])
print(np.round(res.x, 1), round(float(G(res.x)), 3))  # SLSQP stops within ~1e-3
[60.2 39.8] 19.76

Test yourself

At a constrained extremum PP of ff on g=kg = k (with ∇g(P)≠0\nabla g(P) \ne \mathbf{0}), what holds?

  1. ∇f(P)=0\nabla f(P) = \mathbf{0}
  2. ∇f(P)\nabla f(P) is parallel to ∇g(P)\nabla g(P)
  3. ∇f(P)\nabla f(P) is perpendicular to ∇g(P)\nabla g(P)
  4. f(P)=g(P)f(P) = g(P)

The multiplier λ\lambda at an optimum equals 3. If the constraint level kk increases by 0.1, the optimal value changes by about…

  1. 0.03
  2. 3
  3. 0.3
  4. 30

In the KKT conditions, what does complementary slackness λg=0\lambda g = 0 say?

  1. Either the constraint is active (g=0g = 0) or its multiplier is zero
  2. The multiplier is always zero
  3. The constraint is always active
  4. The gradient of f is zero