Lagrange Multipliers: ∇f=λ∇g

Example: Locate the min/max of f(x,y)=2(x^2)+4(y^2)+3 under the constraint g(x,y)=(x^2)+(y^2)=1.

Execute the command below and drag the time slider. (You may wish to enlarge the picture by clicking on it with the mouse and dragging the corners.) As you walk along the (red) constraint curve g(x,y)=1, both gradients are plotted, that for f (in black) and that for g (in red), each being perpendicular to its function’s level curve. The values of f are at a maximum/minimum when the direction of the (red) path along g(x,y)=1 momentarily agrees with the direction of the (black) level curve for f: ∇f=λ∇g.

f[x_,y_]=2x^2+4y^2+3;
Df[x_,y_]={4x,8y};
g[x_,y_]=x^2+y^2;
Dg[x_,y_]={2x,2y};
F=ContourPlot[f[x,y],{x,-4,4},{y,-4,4},Contours->{3,4,5,6,7,8,9,10,20,30,40,50},ContourLabels->True];
G=ContourPlot[g[x,y]==1,{x,-4,4},{y,-4,4},ContourStyle->Red];

Manipulate[
a=Cos[t];
b=Sin[t];
P=Graphics[{PointSize[Large],Red,Point[{a,b}]}];
A=Graphics[{Red,Arrow[{{a,b},Dg[a,b]}]}];
B=Graphics[{Black,Arrow[{{a,b},Df[a,b]}]}];
F2=ContourPlot[f[x,y]==f[a,b],{x,-4,4},{y,-4,4},PlotPoints->100,ContourStyle->Black];
Show[F,F2,G,B,A,P],{t,0,2Pi}]