TExcelTAD
Home › Calculus Essentials › Optimization and Lagrange Multipliers

Optimization and Lagrange Multipliers

Finding the best value of a function of several variables works like the one-variable case: look where the derivative (now the gradient) is zero, then check what kind of point it is. When the variables must satisfy a constraint, such as a fixed budget, Lagrange multipliers turn the problem into a system of equations. The same idea sits underneath support vector machines and many other constrained optimisers.

Critical points

At a local maximum or minimum of a smooth f(x,y)f(x, y), the tangent plane is horizontal, so

∇f=0  ⟺  fx=0 and fy=0\nabla f = \mathbf{0} \quad\iff\quad f_x = 0 \text{ and } f_y = 0

Points where this holds are critical points. As in one variable, not every critical point is an extremum. In two variables there is a third kind, the saddle point: a minimum along one direction and a maximum along another, like the centre of z=x2−y2z = x^2 - y^2 in Surfaces in 3D.

The second-derivative test

At a critical point, compute the discriminant

D=fxx⋅fyy−(fxy)2D = f_{xx} \cdot f_{yy} - (f_{xy})^2
Condition Conclusion
D>0D > 0 and fxx>0f_{xx} > 0 local minimum (curves up in every direction)
D>0D > 0 and fxx<0f_{xx} < 0 local maximum
D<0D < 0 saddle point
D=0D = 0 the test says nothing

DD is the determinant of the 2×22 \times 2 Hessian matrix; Jacobian and Hessian explains the general version with eigenvalues.

Example. f(x,y)=x3−3x+y2−4yf(x, y) = x^3 - 3x + y^2 - 4y. Then fx=3x2−3f_x = 3x^2 - 3 and fy=2y−4f_y = 2y - 4, so the critical points are (1,2)(1, 2) and (−1,2)(-1, 2). With fxx=6xf_{xx} = 6x, fyy=2f_{yy} = 2, fxy=0f_{xy} = 0, D=12xD = 12x: at (1,2)(1, 2), D=12>0D = 12 > 0 and fxx=6>0f_{xx} = 6 > 0, a minimum; at (−1,2)(-1, 2), D=−12<0D = -12 < 0, a saddle.

python · try it yourself
import sympy as sp

x, y = sp.symbols('x y', real=True)
f = x**3 - 3*x + y**2 - 4*y

crit = sp.solve([sp.diff(f, x), sp.diff(f, y)], [x, y], dict=True)
fxx, fyy, fxy = sp.diff(f, x, 2), sp.diff(f, y, 2), sp.diff(f, x, y)
for p in crit:
    D = (fxx * fyy - fxy**2).subs(p)
    kind = "saddle" if D < 0 else ("minimum" if fxx.subs(p) > 0 else "maximum")
    print(p, " D =", D, " f =", f.subs(p), "->", kind)
{x: -1, y: 2}  D = -12  f = -2 -> saddle
{x: 1, y: 2}  D = 12  f = -6 -> minimum

Constraints and Lagrange multipliers

Often the variables are tied together: maximise f(x,y)f(x, y) subject to g(x,y)=cg(x, y) = c. Picture the level curves of ff and the constraint curve g=cg = c. Walking along the constraint, ff stops increasing exactly where the constraint curve touches a level curve of ff without crossing it. There the two curves share a tangent, so their normals, the gradients, are parallel:

∇f=λ∇gandg(x,y)=c\nabla f = \lambda \nabla g \qquad\text{and}\qquad g(x, y) = c

The number λ\lambda is the Lagrange multiplier. That gives three equations (two components plus the constraint) for three unknowns xx, yy, λ\lambda. Solve them, evaluate ff at each solution, and compare.

Example. A rectangular plot has width xx and length yy. Each metre of width costs €2, each metre of length €3, and the budget is €24. Maximise the area f=x⋅yf = x \cdot y subject to 2x+3y=242x + 3y = 24.

∇f=⟨y,x⟩,∇g=⟨2,3⟩y=2λ,x=3λ,2(3λ)+3(2λ)=24→λ=2x=6, y=4, area=24\begin{aligned} &\nabla f = \langle y, x \rangle, \qquad \nabla g = \langle 2, 3 \rangle \\ &y = 2\lambda, \qquad x = 3\lambda, \qquad 2(3\lambda) + 3(2\lambda) = 24 \quad\to\quad \lambda = 2 \\ &x = 6,\ y = 4,\ \text{area} = 24 \end{aligned}
python · try it yourself
import sympy as sp
from scipy.optimize import minimize

x, y, lam = sp.symbols('x y lambda', real=True)
f = x * y
g = 2*x + 3*y - 24

eqs = [sp.Eq(sp.diff(f, x), lam * sp.diff(g, x)),
       sp.Eq(sp.diff(f, y), lam * sp.diff(g, y)),
       sp.Eq(g, 0)]
print("Lagrange:", sp.solve(eqs, [x, y, lam], dict=True))

res = minimize(lambda p: -p[0] * p[1], x0=[1.0, 1.0],
               constraints=[{"type": "eq", "fun": lambda p: 2*p[0] + 3*p[1] - 24}])
print("SciPy   :", [round(float(v), 4) for v in res.x], " area", round(float(-res.fun), 4))
Lagrange: [{lambda: 2, x: 6, y: 4}]
SciPy   : [6.0, 4.0]  area 24.0

What λ\lambda means

The multiplier is a price: λ=df∗/dc\lambda = df^*/dc, the rate at which the optimal value improves per unit of extra budget. Here λ=2\lambda = 2, so one more euro of budget (c=25c = 25) should add about 2 m22\ \text{m}^2 of area. Check: with 2x+3y=252x + 3y = 25 the optimum is x=6.25x = 6.25, y=25/6y = 25/6, area≈26.04\text{area} \approx 26.04, two more than 24. Economists call λ\lambda the shadow price; in optimisation software it is the dual variable of the constraint.

Tip: Lagrange's condition finds candidates. If the constraint curve is closed and bounded (a circle, an ellipse), the largest and smallest of ff over the candidates are the global max and min. If it is unbounded, check that an optimum exists at all.

In software

  • Support vector machines. An SVM looks for the separating line (or hyperplane) with the widest margin, a minimisation of ∥w∥2\lVert \mathbf{w} \rVert^2 subject to one inequality constraint per training point. Writing it with Lagrange multipliers αi≥0\alpha_i \ge 0 gives the "dual" problem. At the solution only the points lying on the margin get αi>0\alpha_i > 0: those are the support vectors, and they alone determine the boundary. See SVM.
  • Softmax and maximum entropy. Maximising entropy −∑piln⁡pi-\sum p_i \ln p_i subject to ∑pi=1\sum p_i = 1 and a fixed average score gives pi∝eβ⋅scoreip_i \propto e^{\beta \cdot \text{score}_i}, the softmax distribution behind LLM sampling.
  • Solvers. SciPy's SLSQP and trust-constr methods (Constrained Optimization) and portfolio optimisers (maximise return subject to a risk budget) all work with Lagrange-style conditions (the KKT conditions for inequality constraints).

Exercises

Exercise 1: Classify critical points

Find and classify the critical points of f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x.

Answer

One critical point, (2,−1)(2, -1), a local (and global) minimum with f=−3f = -3.

Solution
fx=2x+y−3=0,fy=x+2y=0→x=−2y, −4y+y=3, y=−1, x=2fxx=2, fyy=2, fxy=1→D=4−1=3>0 and fxx>0: minimumf(2,−1)=4−2+1−6=−3\begin{aligned} &f_x = 2x + y - 3 = 0, \quad f_y = x + 2y = 0 \quad\to\quad x = -2y,\ -4y + y = 3,\ y = -1,\ x = 2 \\ &f_{xx} = 2,\ f_{yy} = 2,\ f_{xy} = 1 \quad\to\quad D = 4 - 1 = 3 > 0 \text{ and } f_{xx} > 0{:}\ \text{minimum} \\ &f(2, -1) = 4 - 2 + 1 - 6 = -3 \end{aligned}

Exercise 2: A saddle

Show that f(x,y)=xyf(x, y) = xy has a saddle point at the origin.

Answer

D=0⋅0−12=−1<0D = 0 \cdot 0 - 1^2 = -1 < 0, so (0,0)(0, 0) is a saddle.

Solution

fx=yf_x = y and fy=xf_y = x vanish only at (0,0)(0, 0). fxx=fyy=0f_{xx} = f_{yy} = 0 and fxy=1f_{xy} = 1, so D=−1<0D = -1 < 0. Directly: along y=xy = x, f=x2≥0f = x^2 \ge 0 (a minimum), along y=−xy = -x, f=−x2≤0f = -x^2 \le 0 (a maximum).

Exercise 3: Closest point on a line

Use a Lagrange multiplier to find the point of the line x+2y=10x + 2y = 10 closest to the origin (minimise x2+y2x^2 + y^2).

Answer

(2,4)(2, 4), at distance 20=25≈4.472\sqrt{20} = 2\sqrt{5} \approx 4.472.

Solution
∇(x2+y2)=λ∇(x+2y):2x=λ,2y=2λ→y=2xx+2(2x)=10→x=2, y=4distance=4+16=20≈4.472\begin{aligned} &\nabla(x^2 + y^2) = \lambda \nabla(x + 2y){:}\quad 2x = \lambda, \quad 2y = 2\lambda \quad\to\quad y = 2x \\ &x + 2(2x) = 10 \quad\to\quad x = 2,\ y = 4 \\ &\text{distance} = \sqrt{4 + 16} = \sqrt{20} \approx 4.472 \end{aligned}

The answer is a multiple of the line's normal ⟨1,2⟩\langle 1, 2 \rangle, as geometry predicts.

Exercise 4: Extremes on a circle (code)

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, first with Lagrange multipliers in SymPy, then check by sampling the circle.

Answer

Maximum 5 at (0.6, 0.8); minimum −5 at (−0.6, −0.8).

Solution

∇f=⟨3,4⟩=λ⟨2x,2y⟩\nabla f = \langle 3, 4 \rangle = \lambda \langle 2x, 2y \rangle gives x=3/(2λ)x = 3/(2\lambda), y=4/(2λ)y = 4/(2\lambda); the constraint gives 25/(4λ2)=125/(4\lambda^2) = 1, λ=±5/2\lambda = \pm 5/2.

python · try it yourself
import sympy as sp
import numpy as np

x, y, lam = sp.symbols('x y lambda', real=True)
sols = sp.solve([3 - 2*lam*x, 4 - 2*lam*y, x**2 + y**2 - 1], [x, y, lam], dict=True)
for s in sols:
    print(s, " f =", 3*s[x] + 4*s[y])

t = np.linspace(0, 2 * np.pi, 100001)
vals = 3 * np.cos(t) + 4 * np.sin(t)
print("sampled max/min:", round(float(vals.max()), 6), round(float(vals.min()), 6))
{lambda: -5/2, x: -3/5, y: -4/5}  f = -5
{lambda: 5/2, x: 3/5, y: 4/5}  f = 5
sampled max/min: 5.0 -5.0

Test yourself

At a critical point, D=fxxfyy−fxy2=−4D = f_{xx} f_{yy} - f_{xy}^2 = -4. What kind of point is it?

  1. Local minimum
  2. Local maximum
  3. Saddle point
  4. The test is inconclusive

What does the Lagrange condition ∇f=λ∇g\nabla f = \lambda \nabla g say geometrically?

  1. ff and gg are equal
  2. The level curve of ff is tangent to the constraint curve
  3. The gradient of ff is zero
  4. λ\lambda is the optimal value

In an SVM, which training points have non-zero Lagrange multipliers?

  1. All of them
  2. Only misclassified points
  3. Only the support vectors on the margin
  4. Only the points farthest from the boundary