Skip to content

Constrained Optimization and Lagrange Multipliers

Everything so far assumed the answer could be anywhere. It usually cannot. A probability must stay in [0,1][0,1], a portfolio’s weights must sum to one, an SVM’s margin variables must stay non-negative (Chapter 12). So:

minxf(x)subject togi(x)0for all i=1,,m\min_{\mathbf{x}} f(\mathbf{x}) \qquad\text{subject to}\qquad g_i(\mathbf{x}) \leq 0 \quad\text{for all } i = 1, \dots, m

The trouble is that gradient descent has no idea what a constraint is. This page is the standard way to teach it one, and the move is smaller than it looks: replace an infinite wall with a slope.

  • Equations 7.18 and 7.19: the indicator-function reformulation, which is exactly correct and completely unoptimisable — measured, its gradient is undefined everywhere outside the feasible set.
  • Equation 7.20, the Lagrangian, and why a linear penalty is the right relaxation.
  • Definition 7.1: the primal problem, the dual problem, and D(λ)=minxL(x,λ)D(\boldsymbol\lambda) = \min_{\mathbf{x}} L(\mathbf{x}, \boldsymbol\lambda).
  • The minimax inequality (Equation 7.23) with a worked case where the gap is exactly 1/41/4, so you can see it is an inequality rather than an identity.
  • Weak duality (Equation 7.27), and why the dual is always concave even when the primal is a mess.
  • Measured on the book’s Example 7.6: λ=2.5\lambda^\star = 2.5, duality gap exactly zero, and complementary slackness holding to machine precision.
  • Measured on a nonconvex problem: a duality gap of 1.984123, with the dual’s optimum attained at x=2x = -2, a point the primal forbids.
  • Equation 7.28: how equality constraints fold in, and why their multipliers come out unconstrained.

Intuition: a wall you can feel, or a wall you cannot

Section titled “Intuition: a wall you can feel, or a wall you cannot”

You are minimising altitude, and there is a fence.

The naive fix is to declare the far side of the fence infinitely high. That is honest — you will never end up there — but it is useless to a hiker in fog. Step over the fence and everything reads “infinitely high”, in every direction. Nothing tells you which way to go back.

The Lagrangian fix is to charge rent instead. Crossing the fence by one metre costs λ\lambda altitude-units. Now the far side is still discouraging, but it has a slope, and a slope is the one thing a gradient method can use. Better still, you get to pick λ\lambda: too small and the optimum sits outside the fence, too large and it hugs the fence unnecessarily. Somewhere in between is a price at which the unconstrained optimum of the rented landscape lands exactly on the fence — and that price is λ\lambda^\star.

diagram From an unoptimisable reformulation to a solvable one mermaid

Turn the constraint into a penalty that is free when satisfied and unbearable when not:

J(x)=f(x)+i=1m1(gi(x)),1(z)={0if z0otherwiseJ(\mathbf{x}) = f(\mathbf{x}) + \sum_{i=1}^{m}\mathbf{1}\big(g_i(\mathbf{x})\big), \qquad \mathbf{1}(z) = \begin{cases} 0 & \text{if } z \leq 0\\ \infty & \text{otherwise}\end{cases}

Minimising JJ over all of RD\mathbb{R}^D gives exactly the constrained answer, because any infeasible point scores ++\infty. So this reformulation is correct. It is also, in the book’s words, “equally difficult to optimize”, and it is worth being precise about why: the penalty is not merely steep, it is flat at infinity. Every infeasible point looks identical to every other one, so a gradient computed out there is undefined and carries no information about the direction of the feasible set. There is nothing to descend.

Introduce Lagrange multipliers λi0\lambda_i \geq 0, one per inequality, and replace the step function with a linear function:

L(x,λ)=f(x)+i=1mλigi(x)=f(x)+λg(x)L(\mathbf{x}, \boldsymbol\lambda) = f(\mathbf{x}) + \sum_{i=1}^{m}\lambda_i g_i(\mathbf{x}) = f(\mathbf{x}) + \boldsymbol\lambda^\top\mathbf{g}(\mathbf{x})

stacking the constraints into g(x)\mathbf{g}(\mathbf{x}) and the multipliers into λRm\boldsymbol\lambda \in \mathbb{R}^m.

Two properties, and everything else on this page follows from them:

  • For λ0\boldsymbol\lambda \geq 0 and x\mathbf{x} feasible, L(x,λ)f(x)L(\mathbf{x}, \boldsymbol\lambda) \leq f(\mathbf{x}). Each gi(x)0g_i(\mathbf{x}) \leq 0 and each λi0\lambda_i \geq 0, so every term in the sum is non-positive. The Lagrangian under-estimates the objective on the feasible set.
  • Taking the maximum over λ\boldsymbol\lambda recovers the indicator exactly. J(x)=maxλ0L(x,λ)J(\mathbf{x}) = \max_{\boldsymbol\lambda \geq 0} L(\mathbf{x}, \boldsymbol\lambda) If x\mathbf{x} is feasible, the best you can do is set λ=0\boldsymbol\lambda = \mathbf{0} and get f(x)f(\mathbf{x}). If some gi(x)>0g_i(\mathbf{x}) > 0, push that λi\lambda_i \to \infty and the supremum is ++\infty. The infinite step function has been reconstructed as a maximum of linear functions, which is the actual trick.

So the original problem is

minxRdmaxλ0L(x,λ)\min_{\mathbf{x}\in\mathbb{R}^d} \max_{\boldsymbol\lambda \geq 0} L(\mathbf{x}, \boldsymbol\lambda)

The primal problem is the one we started with, in the variables x\mathbf{x}. Its Lagrangian dual problem is

maxλRmD(λ)subject toλ0,where D(λ)=minxRdL(x,λ)\max_{\boldsymbol\lambda\in\mathbb{R}^m} D(\boldsymbol\lambda) \qquad\text{subject to}\qquad \boldsymbol\lambda \geq 0, \qquad\text{where } D(\boldsymbol\lambda) = \min_{\mathbf{x}\in\mathbb{R}^d} L(\mathbf{x}, \boldsymbol\lambda)

Duality in general means converting a problem in one set of variables into a problem in another. Note what has been traded: the primal has dd variables and mm constraints; the dual has mm variables and the single requirement λ0\boldsymbol\lambda \geq 0. If mdm \ll d that is a bargain, and §7.3.1 makes the choice explicit for linear programs.

For any function of two arguments,

maxyminxϕ(x,y)    minxmaxyϕ(x,y)\max_{\mathbf{y}} \min_{\mathbf{x}} \phi(\mathbf{x}, \mathbf{y}) \;\leq\; \min_{\mathbf{x}} \max_{\mathbf{y}} \phi(\mathbf{x}, \mathbf{y})

Maximin is at most minimax. The proof is two lines. Start from the observation that for all x,y\mathbf{x}, \mathbf{y},

minxϕ(x,y)    maxyϕ(x,y)\min_{\mathbf{x}} \phi(\mathbf{x}, \mathbf{y}) \;\leq\; \max_{\mathbf{y}} \phi(\mathbf{x}, \mathbf{y})

The left side does not depend on x\mathbf{x} and the right side does not depend on y\mathbf{y}, so take the maximum over y\mathbf{y} on the left — legitimate, since the inequality holds for every y\mathbf{y} — and the minimum over x\mathbf{x} on the right. The inequality survives both, giving the result.

The intuition worth keeping: the player who moves second has the advantage. On the minimax side y\mathbf{y} sees x\mathbf{x} before choosing; on the maximin side x\mathbf{x} sees y\mathbf{y}. Whoever reacts does at least as well.

Apply that to the Lagrangian:

minxRdmaxλ0L(x,λ)    maxλ0minxRdL(x,λ)\min_{\mathbf{x}\in\mathbb{R}^d} \max_{\boldsymbol\lambda \geq 0} L(\mathbf{x}, \boldsymbol\lambda) \;\geq\; \max_{\boldsymbol\lambda \geq 0} \min_{\mathbf{x}\in\mathbb{R}^d} L(\mathbf{x}, \boldsymbol\lambda)

The left side is the primal optimum pp^\star; the right side is the dual optimum dd^\star. So dpd^\star \leq p^\star, always — that is weak duality, and it means any dual feasible λ\boldsymbol\lambda hands you a certified lower bound on the primal answer, for free.

minxf(x)s.t.gi(x)0    i,hj(x)=0    j\min_{\mathbf{x}} f(\mathbf{x}) \quad\text{s.t.}\quad g_i(\mathbf{x}) \leq 0 \;\; \forall i, \qquad h_j(\mathbf{x}) = 0 \;\; \forall j

Each equality hj(x)=0h_j(\mathbf{x}) = 0 splits into the pair hj(x)0h_j(\mathbf{x}) \leq 0 and hj(x)0-h_j(\mathbf{x}) \leq 0. Both get non-negative multipliers, say λj+\lambda_j^{+} and λj\lambda_j^{-}, and their contribution is λj+hjλjhj=(λj+λj)hj\lambda_j^{+}h_j - \lambda_j^{-}h_j = (\lambda_j^{+} - \lambda_j^{-})h_j. The difference of two non-negative numbers is an arbitrary real number, so:

Inequality multipliers are constrained to be non-negative; equality multipliers are unconstrained. That asymmetry is not a convention, it is a consequence — and it is a fast way to check that you have written a Lagrangian correctly.

The book’s Example 7.6, which is also its Figure 7.4:

minxR212[x1x2][2114][x1x2]+[53][x1x2]\min_{\mathbf{x}\in\mathbb{R}^2} \frac{1}{2}\begin{bmatrix}x_1\\x_2\end{bmatrix}^\top\begin{bmatrix}2&1\\1&4\end{bmatrix}\begin{bmatrix}x_1\\x_2\end{bmatrix} + \begin{bmatrix}5\\3\end{bmatrix}^\top\begin{bmatrix}x_1\\x_2\end{bmatrix}

subject to 1x11-1 \leq x_1 \leq 1 and 1x21-1 \leq x_2 \leq 1, written in the standard form Axb\mathbf{A}\mathbf{x} \leq \mathbf{b} with

A=[10100101],b=[1111]\mathbf{A} = \begin{bmatrix}1&0\\-1&0\\0&1\\0&-1\end{bmatrix}, \qquad \mathbf{b} = \begin{bmatrix}1\\1\\1\\1\end{bmatrix}

Step 1: ignore the constraints. With f=12xQx+cxf = \tfrac12\mathbf{x}^\top\mathbf{Q}\mathbf{x} + \mathbf{c}^\top\mathbf{x} the gradient is Qx+c\mathbf{Q}\mathbf{x} + \mathbf{c}, zero at x=Q1c\mathbf{x} = -\mathbf{Q}^{-1}\mathbf{c}. Since detQ=81=7\det\mathbf{Q} = 8 - 1 = 7,

xunc=17[4112][53]=17[171]=[2.4285710.142857]\mathbf{x}_{\text{unc}} = -\frac{1}{7}\begin{bmatrix}4&-1\\-1&2\end{bmatrix}\begin{bmatrix}5\\3\end{bmatrix} = -\frac{1}{7}\begin{bmatrix}17\\1\end{bmatrix} = \begin{bmatrix}-2.428571\\-0.142857\end{bmatrix}

with f=6.285714f = -6.285714. This point is infeasible: x1=2.43x_1 = -2.43 violates x11x_1 \geq -1 by 1.4285711.428571.

Step 2: the constraint must bind. Since the objective is a strictly convex bowl and its centre lies outside the box, the constrained optimum sits on the boundary — on the face nearest the centre, which is x1=1x_1 = -1. Substituting:

f(1,x2)=12(22x2+4x22)5+3x2=2x22+2x24f(-1, x_2) = \tfrac12\big(2 - 2x_2 + 4x_2^2\big) - 5 + 3x_2 = 2x_2^2 + 2x_2 - 4

A one-variable quadratic. Its derivative 4x2+24x_2 + 2 vanishes at x2=12x_2 = -\tfrac12, comfortably inside [1,1][-1,1], giving

x=[11/2],f(x)=21414=4.5\mathbf{x}^\star = \begin{bmatrix}-1\\-1/2\end{bmatrix}, \qquad f(\mathbf{x}^\star) = 2\cdot\tfrac14 - 1 - 4 = -4.5

So the constraint costs 4.5(6.285714)=1.785714-4.5 - (-6.285714) = 1.785714 in objective value.

Step 3: read off the multipliers. At a constrained optimum the stationarity condition is Qx+c+Aλ=0\mathbf{Q}\mathbf{x}^\star + \mathbf{c} + \mathbf{A}^\top\boldsymbol\lambda^\star = \mathbf{0}.

Qx+c=[2(1)+1(12)1(1)+4(12)]+[53]=[2.53]+[53]=[2.50]\mathbf{Q}\mathbf{x}^\star + \mathbf{c} = \begin{bmatrix}2(-1) + 1(-\tfrac12)\\1(-1) + 4(-\tfrac12)\end{bmatrix} + \begin{bmatrix}5\\3\end{bmatrix} = \begin{bmatrix}-2.5\\-3\end{bmatrix} + \begin{bmatrix}5\\3\end{bmatrix} = \begin{bmatrix}2.5\\0\end{bmatrix}

Only the second row of A\mathbf{A} (the constraint x11-x_1 \leq 1) is active, and Aλ=λ2[10]\mathbf{A}^\top\boldsymbol\lambda = \lambda_2\begin{bmatrix}-1\\0\end{bmatrix}. So we need 2.5λ2=02.5 - \lambda_2 = 0:

λ=[02.500]\boldsymbol\lambda^\star = \begin{bmatrix}0\\2.5\\0\\0\end{bmatrix}

Notice that the second component of Qx+c\mathbf{Q}\mathbf{x}^\star + \mathbf{c} came out exactly zero. That is not luck: x2x_2 is interior, so no constraint pushes on it, so ff must already be flat in that direction. Constrained optimality is “flat where you are free, pushed where you are pinned”.

Step 4: complementary slackness. For each ii, either gi(x)=0g_i(\mathbf{x}^\star) = 0 or λi=0\lambda_i^\star = 0:

iiconstraintgi(x)g_i(\mathbf{x}^\star)λi\lambda_i^\starproductstatus
1x11x_1 \leq 12-20000slack
2x11-x_1 \leq 10\mathbf{0}2.5\mathbf{2.5}00active
3x21x_2 \leq 11.5-1.50000slack
4x21-x_2 \leq 10.5-0.50000slack

A multiplier is the price of a constraint. A constraint that is not binding is free.

The single most useful thing to feel here is λ\lambda as a price. Drag it and watch the unconstrained minimiser of the Lagrangian walk toward the boundary; the price at which it arrives is λ\lambda^\star.

sketch A Lagrange multiplier is a price, and lambda-star is the market rate p5.js
The objective is Example 7.6 sliced at x2 = -0.5, so the picture is one-dimensional and honest. Drag lambda and watch two things: the Lagrangian tilting, and its minimiser sliding toward the constraint boundary. Below, D(lambda) is traced out, and its peak is the primal optimum.

A second sketch, for the fact that surprises people: the dual of a nonconvex problem is still concave and still gives a bound, but the bound need not be tight.

sketch Where the duality gap comes from p5.js
A nonconvex objective with one constraint. Drag lambda and watch the Lagrangian's minimiser jump between two wells rather than sliding. The dual can only ever see the lower envelope, so it never reports the primal answer — the shortfall is the duality gap.
lagrange.py
import numpy as np
 
# The book's Example 7.6, which is also its Figure 7.4.
Q = np.array([[2.0, 1.0], [1.0, 4.0]])
c = np.array([5.0, 3.0])
A = np.array([[1.0, 0.0], [-1.0, 0.0], [0.0, 1.0], [0.0, -1.0]])
b = np.ones(4)                      # the box -1 <= x_i <= 1
 
def f(x):
    return 0.5 * x @ Q @ x + c @ x
 
def g(x):
    return A @ x - b                # feasible means every entry <= 0
 
# --- Equation 7.18: correct, and impossible to descend ---------------------
def J(x):
    return f(x) if np.all(g(x) <= 0) else np.inf
 
x_unc = np.linalg.solve(Q, -c)
print(f"unconstrained minimiser (-17/7, -1/7) = "
      f"[{x_unc[0]:.6f}, {x_unc[1]:.6f}]   f = {f(x_unc):.6f}")
print(f"feasible? {np.all(g(x_unc) <= 0)}   worst violation "
      f"{g(x_unc).max():+.6f}")
print(f"J at that point = {J(x_unc)}")
 
h = 1e-6
for probe in (np.array([-1.5, -0.5]), np.array([-3.0, -0.5])):
    fd = []
    for d in range(2):
        e = np.zeros(2); e[d] = h
        num = J(probe + e) - J(probe - e)
        fd.append("undefined" if not np.isfinite(num) else f"{num / (2*h):+.4f}")
    print(f"  finite-difference grad of J at {probe.tolist()}: ({fd[0]}, {fd[1]})")
print("  -> outside the box every direction looks identical. No slope to follow.")
 
# --- Equation 7.20: the Lagrangian ---------------------------------------
def L(x, lam):
    return f(x) + lam @ g(x)
 
def D(lam):
    """min over x of L. For a positive definite Q this is closed form."""
    v = c + A.T @ lam
    xs = np.linalg.solve(Q, -v)
    return 0.5 * xs @ Q @ xs + v @ xs - lam @ b, xs
 
x_con = np.array([-1.0, -0.5])      # solved by hand on the x1 = -1 edge
print(f"\nconstrained optimum [{x_con[0]}, {x_con[1]}]   f = {f(x_con):.6f}")
print(f"the constraint costs {f(x_con) - f(x_unc):.6f}")
 
print("\nfor lambda >= 0 the Lagrangian is a LOWER BOUND at any feasible x:")
for lam in (np.zeros(4), np.array([0.0, 1.0, 0.0, 0.0]), np.full(4, 0.5)):
    print(f"  lambda = {lam.tolist()}   L = {L(x_con, lam):+.6f}   "
          f"<= f = {f(x_con):+.6f}: {L(x_con, lam) <= f(x_con) + 1e-12}")
 
# --- the dual, maximised over the one coordinate that matters ------------
ts = np.linspace(0.0, 6.0, 60001)
dv = np.array([D(np.array([0.0, t, 0.0, 0.0]))[0] for t in ts])
k = int(np.argmax(dv))
lam_star = np.array([0.0, ts[k], 0.0, 0.0])
dstar, x_from_dual = D(lam_star)
print(f"\nlambda* = {np.round(lam_star, 6).tolist()}")
print(f"d* = {dstar:.9f}    p* = {f(x_con):.9f}    gap = {f(x_con) - dstar:.3e}")
print(f"x recovered from the dual = "
      f"[{x_from_dual[0]:.6f}, {x_from_dual[1]:.6f}]")
 
print("\ncomplementary slackness:")
for i, (l, gi) in enumerate(zip(lam_star, g(x_con))):
    print(f"  i={i}  lambda={l:8.5f}  g={gi:+8.5f}  product={l * gi:+.1e}   "
          f"{'ACTIVE' if abs(gi) < 1e-9 else 'slack'}")
print("stationarity ||Qx + c + A^T lambda|| =",
      f"{np.linalg.norm(Q @ x_con + c + A.T @ lam_star):.2e}")
 
# --- weak duality only, on a nonconvex primal ---------------------------
print("\n--- a nonconvex primal keeps a real gap -------------------------")
xs = np.linspace(-4.0, 4.0, 40001)
fn = xs ** 4 - 8.0 * xs ** 2 + xs
gn = -xs                             # the constraint x >= 0
p_star = fn[gn <= 0].min()
x_p = xs[gn <= 0][int(np.argmin(fn[gn <= 0]))]
lams = np.linspace(0.0, 6.0, 1201)
dn = np.array([(fn + t * gn).min() for t in lams])
kb = int(np.argmax(dn))
x_d = xs[int(np.argmin(fn + lams[kb] * gn))]
print(f"min x^4 - 8x^2 + x  subject to  x >= 0")
print(f"  p* = {p_star:.6f} at x = {x_p:.6f}")
print(f"  d* = {dn[kb]:.6f} at lambda = {lams[kb]:.4f}, attained at x = {x_d:.4f}")
print(f"  that x is feasible: {x_d >= 0}")
print(f"  duality GAP = {p_star - dn[kb]:.6f}")
text
unconstrained minimiser (-17/7, -1/7) = [-2.428571, -0.142857]   f = -6.285714
feasible? False   worst violation +1.428571
J at that point = inf
  finite-difference grad of J at [-1.5, -0.5]: (undefined, undefined)
  finite-difference grad of J at [-3.0, -0.5]: (undefined, undefined)
  -> outside the box every direction looks identical. No slope to follow.
 
constrained optimum [-1.0, -0.5]   f = -4.500000
the constraint costs 1.785714
 
for lambda >= 0 the Lagrangian is a LOWER BOUND at any feasible x:
  lambda = [0.0, 0.0, 0.0, 0.0]   L = -4.500000   <= f = -4.500000: True
  lambda = [0.0, 1.0, 0.0, 0.0]   L = -4.500000   <= f = -4.500000: True
  lambda = [0.5, 0.5, 0.5, 0.5]   L = -6.500000   <= f = -4.500000: True
 
lambda* = [0.0, 2.5, 0.0, 0.0]
d* = -4.500000000    p* = -4.500000000    gap = 0.000e+00
x recovered from the dual = [-1.000000, -0.500000]
 
complementary slackness:
  i=0  lambda= 0.00000  g=-2.00000  product=-0.0e+00   slack
  i=1  lambda= 2.50000  g=+0.00000  product=+0.0e+00   ACTIVE
  i=2  lambda= 0.00000  g=-1.50000  product=-0.0e+00   slack
  i=3  lambda= 0.00000  g=-0.50000  product=-0.0e+00   slack
stationarity ||Qx + c + A^T lambda|| = 0.00e+00
 
--- a nonconvex primal keeps a real gap -------------------------
min x^4 - 8x^2 + x  subject to  x >= 0
  p* = -14.015877 at x = 1.968000
  d* = -16.000000 at lambda = 1.0000, attained at x = -2.0000
  that x is feasible: False
  duality GAP = 1.984123
figure Replace an infinite step with a linear penalty matplotlib
Three panels. Left, a one-dimensional slice showing f as a dashed curve and J as a solid curve that jumps vertically to infinity at the constraint boundary, with the infeasible region shaded and labelled as having no usable gradient. Middle, the Lagrangian for four values of lambda, each a smooth curve whose minimiser sits further right as lambda grows. Right, the dual function D of lambda, concave, peaking exactly on the dashed line marking the primal optimum. Three panels. Left, a one-dimensional slice showing f as a dashed curve and J as a solid curve that jumps vertically to infinity at the constraint boundary, with the infeasible region shaded and labelled as having no usable gradient. Middle, the Lagrangian for four values of lambda, each a smooth curve whose minimiser sits further right as lambda grows. Right, the dual function D of lambda, concave, peaking exactly on the dashed line marking the primal optimum.
The indicator reformulation is exactly correct and has no gradient to follow outside the feasible set. The Lagrangian is smooth for every lambda, and its minimiser reaches the boundary at exactly lambda star = 2.5, where D of lambda touches the primal optimum.
figure Example 7.6, computed rather than sketched matplotlib
Two panels. Left, elliptical contours of the quadratic with a shaded box from minus one to one, a red circle at the infeasible unconstrained minimiser at minus 2.43 comma minus 0.14, and a green star at the constrained optimum on the left face of the box. Right, a table of the four constraints showing their values, multipliers and products, with only the second constraint active and carrying a multiplier of 2.5. Two panels. Left, elliptical contours of the quadratic with a shaded box from minus one to one, a red circle at the infeasible unconstrained minimiser at minus 2.43 comma minus 0.14, and a green star at the constrained optimum on the left face of the box. Right, a table of the four constraints showing their values, multipliers and products, with only the second constraint active and carrying a multiplier of 2.5.
The unconstrained minimiser violates the box by 1.428571, so the optimum is pinned on the x1 = minus 1 face at minus 1 comma minus one half. Only that constraint gets a nonzero multiplier, and the stationarity residual is exactly zero.
figure Weak duality always; strong duality only for convex problems matplotlib
Three panels. Left, the inner maximum and inner minimum of the function x minus y squared on the unit square, separated by a shaded band labelled gap equals 0.25. Middle, the dual of the convex quadratic program rising to touch the dashed primal optimum line. Right, a nonconvex quartic with its Lagrangian at the optimal lambda, a blue star at the feasible primal optimum and an amber dot at the dual's minimiser in the forbidden region. Three panels. Left, the inner maximum and inner minimum of the function x minus y squared on the unit square, separated by a shaded band labelled gap equals 0.25. Middle, the dual of the convex quadratic program rising to touch the dashed primal optimum line. Right, a nonconvex quartic with its Lagrangian at the optimal lambda, a blue star at the feasible primal optimum and an amber dot at the dual's minimiser in the forbidden region.
Swapping a min and a max costs exactly one quarter in the left example. For the convex quadratic program the dual closes the gap to zero. For the nonconvex quartic it leaves 1.984123 on the table, and its best guess sits at x = minus 2, which the primal forbids.

The first figure is the argument of the whole page in three steps. The left panel is Equation 7.18 drawn honestly. Inside the feasible region, JJ is ff — same curve, same gradient — and note which way that gradient points: out through the wall, toward the unconstrained minimum at 2.43-2.43. So a gradient method started anywhere feasible walks straight at the boundary and then leaves. One step past it, the value is ++\infty and the measured finite-difference gradient is undefined in both coordinates. Not large. Not steep. Undefined, and identically so at x1=1.5x_1 = -1.5 and at x1=3.0x_1 = -3.0. The penalty knows you have broken the rule and cannot tell you how badly.

The middle panel is the repair. Each curve is L(x,λ)L(x,\lambda) for a fixed price, and every one of them is a smooth quadratic — differentiable everywhere, no wall anywhere. Watch the marked minimisers move right as λ\lambda grows: at λ=0\lambda = 0 the minimiser is the unconstrained one, and by λ=2.5\lambda = 2.5 it has arrived exactly at the boundary. The constraint has been converted from a prohibition into an incentive.

The right panel closes it. D(λ)D(\lambda) rises, peaks, and falls, and its peak touches the dashed line p=4.5p^\star = -4.5 — the measured gap at the peak is 9×1089\times10^{-8}, which is my λ\lambda grid resolution rather than a real shortfall. This is strong duality, and §7.3 explains why we were entitled to it: the problem is convex.

The second figure is where constrained optimality stops resembling unconstrained optimality. The red circle is where f=0\nabla f = \mathbf{0}, and it is not the answer — it lies outside the box by 1.4285711.428571. The green star is the answer, and at the green star the gradient is emphatically not zero: Qx+c=[2.5,0]\mathbf{Q}\mathbf{x}^\star + \mathbf{c} = [2.5, 0]^\top. It points straight out through the left wall with magnitude 2.52.5, and the wall pushes back with force λ2=2.5\lambda_2^\star = 2.5. The condition is a balance of forces, not a vanishing derivative.

The table earns its place through the second component of that gradient: exactly 00. The variable x2x_2 is interior — nothing is pinning it — so ff must already be flat along it. That is the general shape of a constrained optimum: flat in the directions you are free to move, pushed in the directions you are pinned. And complementary slackness is the bookkeeping for that: three of four constraints are slack and carry multiplier zero, one is active and carries 2.52.5. A multiplier is a price, and unbinding constraints are free.

The third figure is the one to be careful with, because two of its panels look similar and mean opposite things. The left panel makes the minimax inequality concrete. With ϕ(x,y)=(xy)2\phi(x,y) = (x-y)^2 on the unit square: for a fixed xx, the adversary picks the far corner, so maxyϕ=max(x2,(1x)2)\max_y \phi = \max(x^2, (1-x)^2), minimised at x=1/2x = 1/2 giving 1/41/4. Going the other way, for a fixed yy the minimiser just picks x=yx = y and scores 00, so maxyminx=0\max_y \min_x = 0. Gap exactly 1/41/4. The order of the min and the max is not a formality.

The middle and right panels are the same machinery on two problems, and the difference is convexity. The convex quadratic program’s dual rises to touch pp^\star — gap 0.000e+000.000\text{e}{+}00, and the x\mathbf{x} recovered from λ\boldsymbol\lambda^\star is [1,0.5][-1, -0.5], the primal answer exactly. Solve either problem and you have solved the other.

The right panel is the same procedure on f(x)=x48x2+xf(x) = x^4 - 8x^2 + x subject to x0x \geq 0, and it fails instructively. The primal answer is p=14.015877p^\star = -14.015877 at x=1.968x = 1.968, in the right-hand well. The dual’s best is d=16d^\star = -16 at λ=1\lambda^\star = 1 — and look where it is attained: x=2x = -2, in the forbidden left well. At λ=1\lambda = 1 the penalty term λ(x)\lambda \cdot (-x) cancels the objective’s +x+x term exactly, the two wells become symmetric at depth 16-16, and the minimisation picks the infeasible one. No price fixes this. Raising λ\lambda tilts the landscape but cannot delete a well; the minimiser jumps between wells rather than sliding, and it is precisely that discontinuity that the concave DD cannot represent. The gap is 1.9841231.984123, and it is structural: the dual only ever sees the convex envelope of the problem.

indicator, Eq 7.18Lagrangian, Eq 7.20
equivalent to the primalexactlyonly after maxλ0\max_{\boldsymbol\lambda\geq 0}
value on an infeasible point++\inftyfinite
differentiable in x\mathbf{x}noyes, wherever ff and gg are
gradient outside the feasible setundefined, and uninformativepoints back toward feasibility
extra variablesnonemm multipliers
usable by a gradient methodnoyes
primaldual
variablesxRd\mathbf{x} \in \mathbb{R}^dλRm\boldsymbol\lambda \in \mathbb{R}^m
directionminimisemaximise
constraintsmm of them, gi0g_i \leq 0just λ0\boldsymbol\lambda \geq 0
shapewhatever ff and gig_i arealways concave
optimumpp^\stardpd^\star \leq p^\star
cheaper whendmd \ll mmdm \ll d
gap on Example 7.60.000e+000.000\text{e}{+}00
gap on the nonconvex quartic1.9841231.984123
pch.quizTag Do you know what a multiplier is doing?
  1. Equation 7.18 replaces the constraints with an infinite step function. Why is that not a solution?

    pch.quizShowAnswer

    B — Because the penalty is +infinity on the whole infeasible region, so a gradient there is undefined and identical everywhere — there is nothing to descend — The reformulation is exactly correct — that is what makes it tempting. The problem is that it is flat at infinity: measured finite-difference gradients of J came back undefined in both coordinates at x1 = -1.5 and, identically, at x1 = -3.0. The penalty knows you broke the rule and cannot say how badly.

  2. At the constrained optimum of Example 7.6, what is the gradient of f?

    pch.quizShowAnswer

    B — [2.5, 0], pointing out through the active wall and balanced by lambda-2 = 2.5 — Constrained optimality is a balance of forces, not a vanishing derivative: grad f + A-transpose lambda = 0. The second component being exactly zero is the tell that x2 is interior — free directions must be flat, pinned directions are pushed.

  3. Why is the dual function D always concave, even for a horrible nonconvex primal?

    pch.quizShowAnswer

    B — Because for each fixed x, L is affine in lambda, and a pointwise minimum of affine functions is concave — This is the structural payoff of duality. D is a minimum over x of functions that are each affine in lambda, so concavity is automatic and maximising D is always tractable. Whether the resulting bound is tight is the separate question that convexity answers.

  4. For the nonconvex problem min x^4 - 8x^2 + x subject to x >= 0, the dual optimum is -16 at lambda = 1. Where is it attained?

    pch.quizShowAnswer

    B — At x = -2, which violates the constraint — the dual picks the infeasible left well — At lambda = 1 the penalty exactly cancels the objective's +x term, the two wells tie at -16, and the minimisation takes the forbidden one. No price deletes a well, so the minimiser jumps between wells rather than sliding — and that discontinuity is what the concave dual cannot represent. Gap 1.984123.

  5. How do equality constraints h(x) = 0 fit into this framework?

    pch.quizShowAnswer

    B — Each splits into h <= 0 and -h <= 0, and the difference of their two non-negative multipliers makes the equality multiplier unconstrained — The two non-negative multipliers appear only through their difference, and a difference of non-negative numbers is an arbitrary real. So the sign constraint on inequality multipliers is a consequence rather than a convention — and clamping an equality multiplier to be non-negative silently converts the equality into a one-sided inequality.

Exercise 1 – Check feasibility before you trust an optimum

Section titled “Exercise 1 – Check feasibility before you trust an optimum”

Exercise 2 – Equation 7.19 has nothing to descend

Section titled “Exercise 2 – Equation 7.19 has nothing to descend”

Exercise 3 – The Lagrangian bounds the primal from below

Section titled “Exercise 3 – The Lagrangian bounds the primal from below”

Exercise 4 – Solve the dual and read off the multiplier

Section titled “Exercise 4 – Solve the dual and read off the multiplier”

Exercise 5 – Weak duality always, strong duality only sometimes

Section titled “Exercise 5 – Weak duality always, strong duality only sometimes”
  • Equation 7.18 is right and useless. The indicator reformulation gives the correct answer and is +infinity on the whole infeasible region, so its measured gradient is undefined — identically at x1 = -1.5 and at x1 = -3.0. Flat at infinity means nothing to descend.
  • Equation 7.20 is the whole idea: replace the infinite step with lambda-transpose g, a linear penalty. Smooth in x for every fixed lambda.
  • Two facts do all the work. For non-negative lambda and feasible x, L is at most f, because every term is non-positive. And J(x) is the maximum over non-negative lambda of L, which rebuilds the infinite step as a maximum of linear functions.
  • Definition 7.1: the dual is max over lambda >= 0 of D(lambda), where D(lambda) = min over x of L. The primal has d variables and m constraints; the dual has m variables and one sign condition.
  • The minimax inequality is strict in general. For phi = (x-y)^2 on the unit square, maximin = 0 and minimax = 1/4. Whoever moves second has the advantage.
  • Weak duality, always: the dual optimum never exceeds the primal optimum. Any dual-feasible lambda is a certified lower bound on the primal answer, for free.
  • D is always concave, because it is a pointwise minimum of functions affine in lambda — however nonconvex f and the g_i are. That is duality’s structural payoff; tightness is a separate question.
  • Example 7.6 measured: unconstrained minimiser (-17/7, -1/7) with f = -6.285714, infeasible by 1.428571. Constrained optimum (-1, -1/2) with f = -4.5, so the constraint costs 1.785714. lambda-star = 2.5 on the single active face, gap exactly zero.
  • The gradient does NOT vanish at a constrained optimum. There it is [2.5, 0]: pushed in the pinned direction, flat in the free one. The condition is grad f + A-transpose lambda = 0.
  • Complementary slackness: for each i, either g_i = 0 or lambda_i = 0. A multiplier is the PRICE of a constraint, so a slack constraint is free and a big multiplier means expensive, not violated.
  • A nonconvex primal keeps a real gap. On min x^4 - 8x^2 + x subject to x >= 0: p* = -14.015877, d* = -16 at lambda = 1, gap 1.984123 — and the dual’s minimiser is x = -2, which the primal forbids. The minimiser JUMPS between wells rather than sliding, and the concave dual cannot represent that.
  • Equality multipliers are unconstrained, because h = 0 splits into two inequalities whose two non-negative multipliers enter only as a difference. The sign condition on inequality multipliers is a consequence, not a convention.

Next: the class of problems where the gap is guaranteed to be zero. Convex Sets and Convex Functions

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading