Lagrange Multipliers

The idea

Many engineering questions ask for the best value of something while something else is held fixed: the least material for a set volume, the most power for a set budget, the closest point on a surface. Lagrange multipliers turn that constrained problem into a set of ordinary equations you can solve by hand.

Picture the level curves of the function f(x,y)f(x, y) you want to optimize and the curve g(x,y)=cg(x, y) = c you must stay on. Walk along the constraint curve and watch the value of ff. Where ff reaches a maximum or minimum, the constraint curve can't cross a level curve of ff (crossing would mean ff is still rising on one side), so the two curves just touch. Touching curves share a tangent line, so their gradients point along the same line:

∇f=λ ∇g\nabla f = \lambda \, \nabla g

The number λ\lambda (lambda) is the Lagrange multiplier. It says how many times longer one gradient is than the other, and, as shown below, it also measures how fast the best value changes when the constraint is loosened.

Ellipse x² + 4y² = 8 touching the level curve xy = 2 at (2, 1) and (-2, -1)
Level curves of f = xy (for f = 1, 2 and 3) and the constraint ellipse x² + 4y² = 8. The largest level curve the ellipse reaches is f = 2, and it touches the ellipse at (2, 1), where both gradients point the same way.

The method

  1. Write the constraint as g(x,y)=cg(x, y) = c and the function to optimize as f(x,y)f(x, y).
  2. Set up ∇f=λ∇g\nabla f = \lambda \nabla g as one equation per variable: fx=λgxf_x = \lambda g_x and fy=λgyf_y = \lambda g_y (add fz=λgzf_z = \lambda g_z in three variables).
  3. Solve these together with the constraint g=cg = c. That is n+1n + 1 equations for the nn variables and λ\lambda.
  4. Evaluate ff at every point you found. On a closed, bounded constraint (an ellipse, a sphere) the largest value is the maximum and the smallest is the minimum.

Some books write the same thing as one function, L(x,y,λ)=f(x,y)−λ (g(x,y)−c)L(x, y, \lambda) = f(x, y) - \lambda\,(g(x, y) - c), and set all its partial derivatives to zero. The equations are identical; use whichever you find easier to remember.

Worked example: the largest product on an ellipse

Find the maximum and minimum of f(x,y)=xyf(x, y) = xy on the ellipse x2+4y2=8x^2 + 4y^2 = 8.

Gradients. ∇f=(y, x)\nabla f = (y,\ x) and with g=x2+4y2g = x^2 + 4y^2, ∇g=(2x, 8y)\nabla g = (2x,\ 8y). So

y=2λx,x=8λyy = 2\lambda x, \qquad x = 8\lambda y

Substitute the first into the second: x=8λ(2λx)=16λ2xx = 8\lambda(2\lambda x) = 16\lambda^2 x. Either x=0x = 0, which forces y=0y = 0 and misses the ellipse, or λ2=1/16\lambda^2 = 1/16, so λ=±1/4\lambda = \pm 1/4.

With λ=1/4\lambda = 1/4, y=x/2y = x/2. The constraint gives x2+x2=8x^2 + x^2 = 8, so x=±2x = \pm 2 and the points are (2,1)(2, 1) and (−2,−1)(-2, -1), where f=2f = 2. With λ=−1/4\lambda = -1/4, y=−x/2y = -x/2, giving (2,−1)(2, -1) and (−2,1)(-2, 1), where f=−2f = -2.

Answer: the maximum is 22 and the minimum is −2-2. A quick check: parametrize the ellipse as x=22cos⁡tx = 2\sqrt{2}\cos t, y=2sin⁡ty = \sqrt{2}\sin t, so xy=4sin⁡tcos⁡t=2sin⁡2txy = 4\sin t\cos t = 2\sin 2t, which runs from −2-2 to 22.

Worked example: the nearest point on a plane

Find the point on the plane x+2y+2z=9x + 2y + 2z = 9 closest to the origin.

Minimizing the distance is the same as minimizing its square, which avoids a square root: f=x2+y2+z2f = x^2 + y^2 + z^2, with g=x+2y+2z=9g = x + 2y + 2z = 9.

(2x, 2y, 2z)=λ (1, 2, 2)  ⇒  x=λ2,y=λ,z=λ(2x,\ 2y,\ 2z) = \lambda\,(1,\ 2,\ 2) \;\Rightarrow\; x = \tfrac{\lambda}{2},\quad y = \lambda,\quad z = \lambda

Put these into the plane: λ2+2λ+2λ=4.5λ=9\tfrac{\lambda}{2} + 2\lambda + 2\lambda = 4.5\lambda = 9, so λ=2\lambda = 2 and the point is (1,2,2)(1, 2, 2). Its distance from the origin is 1+4+4=3\sqrt{1 + 4 + 4} = 3.

This agrees with the point-to-plane distance formula, ∣9∣/12+22+22=9/3=3|9| / \sqrt{1^2 + 2^2 + 2^2} = 9/3 = 3. Notice that (1,2,2)(1, 2, 2) is a multiple of the plane's normal vector, exactly as ∇f=λ∇g\nabla f = \lambda \nabla g says it must be.

Worked example: the cheapest open-top box

An open-top box (an enclosure tray, say) must hold 500 cm3500\ \text{cm}^3. Find the base xx by yy and height zz that use the least sheet material.

The area is S=xy+2xz+2yzS = xy + 2xz + 2yz (base plus four sides) and the constraint is V=xyz=500V = xyz = 500. Setting ∇S=λ∇V\nabla S = \lambda \nabla V:

y+2z=λyz,x+2z=λxz,2x+2y=λxyy + 2z = \lambda yz, \qquad x + 2z = \lambda xz, \qquad 2x + 2y = \lambda xy

Multiply the first by xx and the second by yy: both right sides become λxyz\lambda xyz, so xy+2xz=xy+2yzxy + 2xz = xy + 2yz, which gives x=yx = y. Doing the same with the second and third equations gives x=2zx = 2z. So the base is square and the height is half the side.

Then xyz=(2z)(2z)z=4z3=500xyz = (2z)(2z)z = 4z^3 = 500, so z3=125z^3 = 125, z=5z = 5 cm and x=y=10x = y = 10 cm. The material used is S=100+100+100=300 cm2S = 100 + 100 + 100 = 300\ \text{cm}^2. A brute-force search over base sizes from 5 to 15 cm (with the height set by the volume) finds the same minimum of 300 cm2300\ \text{cm}^2 at 10×1010 \times 10.

Material for a few 500 cm³ open boxes (height chosen to fix the volume)
Base (cm)Height (cm)Area (cm²)
8 × 87.81314.0
10 × 105.00300.0
12 × 123.47310.7
10 × 12.54.00305.0

What λ tells you

The multiplier is not just a helper variable. At the optimum, λ\lambda equals the rate at which the best value of ff changes as the constraint level cc changes:

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

In the ellipse example the constraint was x2+4y2=cx^2 + 4y^2 = c with c=8c = 8. Repeating the working for any cc gives x2=c/2x^2 = c/2 and y2=c/8y^2 = c/8, so the best product is f∗=c/4f^{*} = c/4. Its derivative is 1/41/4, the value of λ\lambda found at the maximum. In economics this is the shadow price; in engineering it tells you how much a design improves per unit of extra budget, area or power.

Checking that it is a maximum or minimum

Lagrange's equations find every point where the two curves touch, but touching is not the same as being the best. On a closed, bounded constraint, comparing ff at all the candidate points is enough, as in the first example. On an open constraint, like the box (whose sides can grow without limit), argue from the shape of the problem: the area grows without bound as any side shrinks to zero or grows very large, so the single candidate must be the minimum. For a formal test, use the bordered Hessian, covered in most multivariable calculus texts.

Common mistakes

  • Dropping the constraint equation. ∇f=λ∇g\nabla f = \lambda \nabla g alone has too few equations; you always need g=cg = c as well.
  • Dividing by a variable that could be zero. In the first example, dividing x=16λ2xx = 16\lambda^2 x by xx assumes x≠0x \neq 0. Check the x=0x = 0 case separately.
  • Forgetting the negative value of λ. Square roots give ±\pm; each sign can be a different extremum.
  • Minimizing distance instead of distance squared. Both have the same minimizer, but the square has far easier derivatives.
  • Assuming every solution is a maximum. Evaluate ff at each point, or check the problem's shape, before naming it.

Sources

Continue learning

Lagrange multipliers build on partial derivatives and the gradient; the second-derivative test handles the unconstrained case.