Lagrange Multipliers

blank template for two independent variables and one constraint, ready to fill in

Clear[f,g,fx,fy,gx,gy,x,y,L,eqns,pts,nearPts]
f[x_,y_]:=
fx[x_,y_]=(∂_x) f[x,y]
fy[x_,y_]=(∂_y) f[x,y]
g[x_,y_]:=
gx[x_,y_]=(∂_x) g[x,y]
gy[x_,y_]=(∂_y)g[x,y]
eqns={fx[x,y] == L* gx[x,y],fy[x,y] == L* gy[x,y],g[x,y]==0}
pts=Solve[eqns,{x,y,L}]
f[x,y]/.pts
N[f[x,y]/.pts]
Print["Beware if the gradient is zero!"]
{gx[x,y], gy[x,y]}/.pts
Print["Some points near the solutions"]
nearPts=N[Table[{x+a*.1,y+b*.1,f[x+a*.1,y+b*.1]},{a,-1,1},{b,-1,1}]/.pts]

blank template for two independent variables and two constraints, ready to fill in

Clear[f,g1, g2,fx,fy,g1x,g1y,g2x,g2y, x,y,L1, L2,eqns,pts,nearPts]
f[x_,y_]:=
fx[x_,y_]=(∂_x) f[x,y]
fy[x_,y_]=(∂_y) f[x,y]
g1[x_,y_]:=
g1x[x_,y_]=(∂_x) g1[x,y]
g1y[x_,y_]=(∂_y)g1[x,y]
g2[x_,y_]:=
g2x[x_,y_]=(∂_x) g2[x,y]
g2y[x_,y_]=(∂_y)g2[x,y]
eqns={fx[x,y] == L1* g1x[x,y] +  L2* g2x[x,y] ,fy[x,y] == L1* g1y[x,y]+L2* g2y[x,y],g1[x,y]==0, g2[x,y]==0}
pts=Solve[eqns,{x,y,L1, L2}]
f[x,y]/.pts
N[f[x,y]/.pts]
Print["Beware if a gradient of a constraint function is zero at a constrained critical point!"]
{{x,y},{g1x[x,y], g1y[x,y]}{g2x[x,y], g2y[x,y]}}/.pts
Print["Some points near the solutions"]
nearPts=N[Table[{x+a*.1,y+b*.1,f[x+a*.1,y+b*.1]},{a,-1,1},{b,-1,1}]/.pts]

blank template for three independent variables and one constraint, ready to fill in

Clear[f,g,fx,fy,fz,gx,gy,gz,x,y,L,eqns,pts,nearPts]
f[x_,y_,z_]:=
fx[x_,y_,z_]=(∂_x) f[x,y,z]
fy[x_,y_,z_]=(∂_y) f[x,y,z]
fz[x_,y_,z_]=(∂_z) f[x,y,z]
g[x_,y_,z_]:= 
gx[x_,y_,z_]=(∂_x) g[x,y,z]
gy[x_,y_,z_]=(∂_y)g[x,y,z]
gz[x_,y_,z_]=(∂_z)g[x,y,z]
eqns={fx[x,y,z] == L* gx[x,y,z],fy[x,y,z] == L* gy[x,y,z],fz[x,y,z] == L* gz[x,y,z],g[x,y,z]==0}
pts=Solve[eqns,{x,y,z,L}]
f[x,y,z]/.pts
N[f[x,y,z]/.pts]
Print["Beware if a gradient of a constraint function is zero at a constrained critical point!"]
{gx[x,y,z], gy[x,y,z], gz[x,y,z]}/.pts
Print["Some points near the solutions"]
nearPts=N[Table[{x+a*.1,y+b*.1,z+ c*.1,f[x+a*.1,y+b*.1,z+c*.1]},{a,-1,1},{b,-1,1},{c,-1,1}]/.pts]

blank template for three independent variables and two constraints, ready to fill in

Clear[f,g,fx,fy,fz,g1x,g1y,g1z, g2x,g2y, g2z,x,y,z,L1, L2,eqns]
f[x_,y_,z_]:= 
fx[x_,y_,z_]=(∂_x) f[x,y,z]
fy[x_,y_,z_]=(∂_y) f[x,y,z]
fz[x_,y_,z_]=(∂_z) f[x,y,z]

g1[x_,y_,z_]:= 
g1x[x_,y_,z_]=(∂_x) g1[x,y,z]
g1y[x_,y_,z_]=(∂_y)g1[x,y,z]
g1z[x_,y_,z_]=(∂_z)g1[x,y,z]

g2[x_,y_,z_]:=
g2x[x_,y_,z_]=(∂_x) g2[x,y,z]
g2y[x_,y_,z_]=(∂_y)g2[x,y,z]
g2z[x_,y_,z_]=(∂_z)g2[x,y,z]

Print["The gradient and constraint equations"]
eqns={
fx[x,y,z] == L1* g1x[x,y,z]+L2* g2x[x,y,z],fy[x,y,z] == L1* g1y[x,y,z]+L2* g2y[x,y,z],fz[x,y,z] == L1* g1z[x,y,z]+L2* g2z[x,y,z],g1[x,y,z]==0,g2[x,y,z]==0}

Print["The constrained critical values for the independent variables and the values of the Lagrange multipliers."]
pts=Solve[eqns,{x,y,z,L1, L2}]

Print["The exact and decimal values of the function at the constrained critical values."]
f[x,y,z]/.pts
N[f[x,y,z]/.pts]

Print["Beware if a gradient of a constraint function is zero at a constrained critical point!"]
{g1x[x,y,z], g1y[x,y,z], g1z[x,y,z]}/.pts
{g2x[x,y,z], g2y[x,y,z], g2z[x,y,z]}/.pts

Print["Some points near the solutions"]
nearPts=N[Table[{x+a*.1,y+b*.1,z+ c*.1,f[x+a*.1,y+b*.1,z+c*.1]},{a,-1,1},{b,-1,1},{c,-1,1}]/.pts]