Lagrange Multipliers

Example

Find the minimum and maximum values of f(x,y)=(x^3)+(y^3)+3x y subject to the constraint (((x-3))^2)+(((y-3))^2)=9.

Solution

Let's define the function and the contraint.

f[x_,y_]=(x^3)+(y^3)+3 x y
g[x_,y_] = (x-3)^2+(y-3)^2-9

We can get a plot of the surface:

Plot3D[f[x,y],{x,-6,9},{y,-6,9},AxesLabel->{x,y,z}]

The above plot doesn't contain information about the constraint, so it isn't as useful to us as we might hope. It's better to explore this using contour plots:

plot1 = ContourPlot[f[x,y],{x,-6,9},{y,-6,9},Contours->12,PlotPoints->40,FrameLabel->{x,y},ContourShading->False,ContourStyle->{{Red,Thick}}]

plot2 =ContourPlot[g[x,y]==0,{x,-1,9},{y,-1,9},ContourStyle->{{Blue,Thick}}]

Show[plot2,plot1,PlotRange->All]

The red lines are contours of the surface z=f(x,y), and each contour represents a specific value of the surface. If you hover your mouse over the contour, Mathematica will tell you the value. So, for example, the line with the loop represents the points that satisfy f(x,y)=0. 
The blue circle is the contour line for g(x,y)=0.

To find the maximum and minimum of the function z=f(x,y) subject to the constraint g(x,y)=0, we need to construct some equations to solve. The equations we want to solve are where the contour lines for z=f(x,y) touch the contour g(x,y)=0 at one point, which means where the tangent lines are the same. If the tangent lines are the sames, the normal vectors must point in the same direction, so we arrive at ∇f=λ ∇g. Combined with the contraint equation, we must solve the system of equations:

∇f=λ ∇g
g=0

for the unknowns x,y, λ. The quantity λ is the Lagrange multiplier, and it just represents the fact that although the gradients of f and gmay point in the same direction, they might not have the same length.

Let's solve the equations (notice they are nonlinear and can be quite nasty):

eq1=D[f[x,y],x]==λ D[g[x,y],x]
eq2=D[f[x,y],y]==λ D[g[x,y],y]
eq3=g[x,y]==0
Solve[{eq1,eq2,eq3},{x,y,λ}]

N[%]

The solutions we seek should be real valued. There are two real valued solutions. Evaluate the function at the points you find, and the maximum from the list is the maximum, the minimum is the minimum.

f[0.87868,0.87868]
f[5.12132,5.12132]

So the max is 347.327 which occurs at (5.12132,5.12132) and the min is 3.67306 which occurs at (0.87868,0.87868).

Here's a plot with the two contours that produce the max and min:

f[x_,y_]=(x^3)+(y^3)+3 x y;
g[x_,y_] = (x-3)^2+(y-3)^2-9;

plot1 = ContourPlot[f[x,y],{x,-6,9},{y,-6,9},Contours->{3.67306,347.327},PlotPoints->40,FrameLabel->{x,y},ContourShading->False,ContourStyle->{{Red,Thick}}];

plot2 =ContourPlot[g[x,y]==0,{x,-1,9},{y,-1,9},ContourStyle->{{Blue,Thick}}];

Show[plot2,plot1,PlotRange->All]

Example

Find the minimum and maximum values of f(x,y)=(x^3)+(y^3)+(z^4)+3x y+2z subject to the constraint (((x-3))^2)+(((y-3))^2)+(((x-2))^2)=9.

Solution

Let's define the function and the contraint.

Clear[f,g]
f[x_,y_,z_]=(x^3)+(y^3)+(z^4)+3 x y+2z
g[x_,y_,z_] = (((x-3))^2)+(((y-3))^2)+(((x-2))^2)-9

To find the maximum and minimum of the function w=f(x,y,z) subject to the constraint g(x,y,z)=0, we must solve the system of equations:

∇f=λ ∇g
g=0

for the unknowns x,y,z,  λ.

Let's solve the equations (notice they are nonlinear and can be quite nasty):

eq1=D[f[x,y,z],x]==λ D[g[x,y,z],x]
eq2=D[f[x,y,z],y]==λ D[g[x,y,z],y]
eq3=D[f[x,y,z],z]==λ D[g[x,y,z],z]
eq4=g[x,y,z]==0
Solve[{eq1,eq2,eq3,eq4},{x,y,z,λ}]

Nasty!

sol=N[%]

The solutions we seek should be real valued. Evaluate the function at the points you find, and the maximum from the list is the maximum, the minimum is the minimum. Here's how we can create the table:

{x,y,z,f[x,y,z]}/.sol//TableForm

So the max is 276.48 which occurs at x=3.08331,y=5.79634,z=-0.793701 and the min is 3.58269 which occurs at x=1.16352,y=0.780173,z=-0.793701.