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 subject to " into a system of equations, based on one geometric fact: at the optimum, the level curve of 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 and have continuous first partials. If , restricted to the curve (or surface) , has a local extremum at , and , then there is a number (the Lagrange multiplier) with
Proof. Let be any differentiable curve on the constraint set with . Then has a local extremum at , so by the chain rule
So is perpendicular to every tangent vector of the constraint set at . So is , because the gradient is normal to the level set (directional derivatives). In two dimensions the tangent directions form a line, its normals a line, and two vectors on that line are parallel: . 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 subject to :
- Solve the system , (four equations, four unknowns ).
- Evaluate 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 on the constraint set; the theorem says nothing there.
Example: closest point on a plane
Find the point of the plane closest to the origin. Minimise the squared distance (same minimiser, easier derivatives):
This matches the point-to-plane formula from Equations of Lines and Planes.
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 is 2 (at ) and the minimum is (at ).
The meaning of
Let be the optimal value of subject to . Under mild conditions
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 on gives , so at , and indeed .
Two constraints
To optimise subject to and , the tangent directions of the constraint curve are perpendicular to both and , so must lie in their span:
(five equations in ). Example: the highest point (largest ) on the curve where the plane meets the cylinder :
Inequality constraints: KKT in one paragraph
For "minimise subject to ", either the optimum is inside (, and then as usual) or on the boundary (, and then with : the objective must want to push outward). Both cases are summarised by the Karush–Kuhn–Tucker conditions:
These are the conditions numerical solvers such as SciPy's SLSQP try to satisfy; see Constrained Optimization.
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, , and the KKT residual is zero to the printed precision.
In software
- Maximum entropy gives softmax. Maximise the entropy subject to and a fixed expected score . The Lagrange conditions give : the softmax (Boltzmann) distribution, with 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 is a Lagrange problem; at the optimum every channel has the same marginal return per unit, .
- 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 square, area 100.
Solution
Exercise 2: Extremes on a circle
Find the maximum and minimum of on the circle .
Answer
Maximum 5 at ; minimum at .
Solution
This is Cauchy–Schwarz: for unit .
Exercise 3: The shadow price
Maximise subject to for , express the maximum , and verify that equals .
Answer
, .
Solution
Exercise 4: Budget allocation (code)
A service splits a budget of 100 between two caches; the hit-rate gain is . Solve the Lagrange conditions by hand, then check with SciPy.
Answer
, , , .
Solution
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 of on (with ), what holds?
- is parallel to
- is perpendicular to
The multiplier at an optimum equals 3. If the constraint level increases by 0.1, the optimal value changes by about…
- 0.03
- 3
- 0.3
- 30
In the KKT conditions, what does complementary slackness say?
- Either the constraint is active () or its multiplier is zero
- The multiplier is always zero
- The constraint is always active
- The gradient of f is zero