14.8 Lagrange Multipliers

Introduction

Consider the following problem: you are walking along a trail that is on a mountain.  If the mountain can be described by the equation

f[x_,y_]=4-(x^2)-x y-2 (y^2);

and the trail is the part of the mountain where (x^2)+(y^2)=1, then at what (x,y) values on the trail are you at the highest (or lowest) altitude?

First let’s get a picture of this situation.  Note that the trail is the zero-level curve of g(x,y)=(x^2)+(y^2)-1.

g[x_,y_]=(x^2)+(y^2)-1;
mountain=Plot3D[f[x,y],{x,-2,2},{y,-2,2},PlotRange->{0,4},PlotPoints->20,AxesLabel->{x,y,z}];
trail=ParametricPlot3D[{Cos[t],Sin[t],f[Cos[t],Sin[t]]},{t,0,2 Pi},PlotStyle->Directive[Thick,Blue]];
Show[mountain,trail]

Now look at the level curves for the mountain and a birds-eye view of the trail.

levelmount=ContourPlot[f[x,y],{x,-2,2},{y,-2,2}, Contours->{.25,.75,1,1.25,1.79,2.25,2.75,3.2,3.75},PlotPoints->40, ContourShading->False];
leveltrail=ParametricPlot[{Cos[t],Sin[t]},{t,0,2 Pi},PlotStyle->Directive[Thick,Blue]];
Show[levelmount,leveltrail]

Now, it we start at (0,1) and walk counter-clockwise, then we cross level curves of f which indicates that we are going uphill until we get to about (-.9,.4) where we are at a height of 3.2 (a high point on the trail).  As we continue, we cross level curves of f until we reach a height of 1.79 at about (-.4,-.9).  This is a low point on the trail.  We then go up hill until we reach a high point at about (.9,-.4) and then downhill until we reach the other low point.

Notice that the trail (constraint) is tangent to a level curve of the mountain (suffice) at points that are extreme on the trail.  We can find possible extreme of a function, subject to a constraint, by finding these points of tangency.  We will use the gradient to do this.  Since the gradient is orthogonal to the level curve, (a,b) is a critical point if

∇f(a,b)=λ ∇g(a,b),

where λ is a constant.  That is, the gradient of f is parallel to the gradient of g.  In the above problem, this gives us two equations in three variables.  The third equation is given by the constraint:

dfdx=D[f[x,y],x];
dfdy=D[f[x,y],y];
dgdx=D[g[x,y],x];
dgdy=D[g[x,y],y];
cp=NSolve[{dfdx==λ*dgdx,dfdy==λ*dgdy,g[x,y]==0},{x,y,λ}]

To find the heights, we plug the critical points into the function describing the surface to get:

f[x,y]/.cp

So the high point(s) have an altitude of about 3.2 at coordinates (-.92,.38) and (.92,-.38).  The low points on the trail have an altitude of about 1.79 at coordinates (.38,.92) and (-.38,-.92).

More Examples:

Example (i)

Find the max of z=4-(x^2)-(y^2), if x+2y=2.

Here is a graph.  See hand written notes for solution.

f[x_,y_]=4-(x^2)-(y^2);
p1=Plot3D[f[x,y],{x,-2,2},{y,-2,2},PlotRange->{0,4},PlotPoints->20,AxesLabel->{x,y,z}];
p2=ParametricPlot3D[{2-2t,t,f[2-2t,t]},{t,0,2},PlotStyle->Directive[Thick,Blue]];
Show[p1,p2]

f[2/5,4/5]

Example (ii)

cp=Solve[{16x==8x λ,4z==2y λ,4y-16==8z λ,4(x^2)+(y^2)+4(z^2)==16},{x,y,z,λ}]

h[x_,y_,z_]=8(x^2)+4y z-16z;

h[x,y,z]/.cp

N[%]

Example (iii)

Solve[{3==λ+2x μ,-1==λ,-3==-λ+4z μ,x+y-z==0,(x^2)+2(z^2)==1},{x,y,z,λ,μ}]