Graphical Illustration of Bivariate Constrained Optimization

Manipulate[
With[{f=Switch[i,1,x+y,2,x y,3,x^2-y^2]},Show[If[cylinder,ContourPlot3D[{x^2+y^2==1,z==0},{x,-1.5,1.5},{y,-1.5,1.5},{z,-1.5,1.5},Mesh->None,BoxRatios->Automatic,BoundaryStyle->None,ContourStyle->{Directive[{RGBColor[1, 0, 0],Opacity[0.2]}],Directive[{RGBColor[0, 1, 0.25098],Opacity[0.2]}]}],{}],ContourPlot3D[{z==f},{x,-1.5,1.5},{y,-1.5,1.5},{z,-2,2},Mesh->None,BoxRatios->Automatic,ContourStyle->{Directive[{RGBColor[0, 0, 1],Opacity[op]}]}],Graphics3D[{Opacity[1],PointSize[0.02],RGBColor[0.501961, 0.25098, 0],Point[{Cos[t],Sin[t],0}],RGBColor[0, 1, 1],Point[{Cos[t],Sin[t],f/.{x->Cos[t],y->Sin[t]}}],RGBColor[0.25098, 0, 0.501961],Line[{{Cos[t],Sin[t],0},{Cos[t],Sin[t],f/.{x->Cos[t],y->Sin[t]}}}]}],ParametricPlot3D[{{Cos[t],Sin[t],0},{Cos[t],Sin[t],f/.{x->Cos[t],y->Sin[t]}}},{t,0,2Pi}],Graphics3D[{Text[Style[Row[{Style["z",Italic]," = ",ToString[N[f/.{x->Cos[t],y->Sin[t]},3]]}],14],{-1.2,-1.2,2.5}],Text[Style[Row[{Style["x",Italic]," = ",ToString[N[Cos[t],3]]}],14],{-1.2,-1.2,2.3}],Text[Style[Row[{Style["y",Italic]," = ",ToString[N[Sin[t],3]]}],14],{-1.2,-1.2,2.1}]}],Axes->axes,Boxed->boxed,ImageSize->{400,400}]],
{{i,1,"choose function"},{1->TraditionalForm[x+y],2->TraditionalForm[x y],3->TraditionalForm[x^2-y^2]}},
{{t,0,"move point"},0,2 3.1415,ImageSize->Tiny},
{{cylinder,False},{False,True}},
{{axes,False},{False,True}},
{{boxed,False},{False,True}},
{{op,.4,"surface opacity"},0,1,ImageSize->Tiny},
ControlPlacement->Left]

This Demonstration illustrates graphically finding a constrained extrema of a function of two variables z=f(x,y) with a constraint g(x,y)=0. In this Demonstration g(x,y)=(x^2)+(y^2)-1. While the brown point moves on the unit circle, the cyan point moves on the curve, which is the intersection of the surface that is the graph of the function f and the cylinder given by z=g(x,y).

To find the extrema of a function z=f(x,y), subject to the constraint that g(x,y)=0, form an auxiliary function w=f(x,y)+λg(x,y). The coordinates (x,y) of the extrema satisfy three equations: g(x,y)=0, (Subscript[w, x])=0, and (Subscript[w, y])=0.  The function w is referred to as the Lagrangian, and λ as the Lagrange multiplier. One observes that the first of the three equations is simply (Subscript[w, λ])=0, and that we really have three variables (x,y,λ), so the system we solve is not overdetermined. This method assumes that the constraint equation defines a smooth (differentiable) closed curve, so that no points require special testing.

Contributed by: Izidor Hafner