Lagrange Multipliers in 3D

Example 1

Clear[f,g,fx,fy,fz,gx,gy,gz,x,y,L,eqn,pts,nearPts]
f[x_,y_,z_]:= (x^2)+(y^2)+(z^2)
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_]:= x + y + z - 25
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}]
N[pts]
f[x,y,z]/.pts
N[f[x,y,z]/.pts]
Print["Beware if the gradient is zero!"]
{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]

Clear[gradf, gradg];
gradf[x_,y_,z_]:= {fx[x,y,z],fy[x,y,z],fz[x,y,z]}
gradg[x_,y_,z_]:= {gx[x,y,z],gy[x,y,z],gz[x,y,z]}
VectorPlot3D[gradf[x,y,z],{x,7,9},{y,7,9},{z,7,9}]
VectorPlot3D[gradg[x,y,z],{x,5,10},{y,5,10},{z,5,10}]

Example 2

Clear[f,g,fx,fy,fz,gx,gy,gz,x,y,L,eqn,pts,nearPts]
f[x_,y_,z_]:= z - (x^2)+(y^2)
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_]:= x + y + z -1
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 the gradient is zero!"]
{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]