Level Curves, Tangents, and Gradients

Code

graphicsInitial:=(
levelP=Join[P,{0}];
graphP=Join[P,{f@@P}];
gradient={Derivative[1,0][f]@@P,Derivative[0,1][f]@@P};
gradientUnit=N[Normalize[gradient]];
tangent = {gradientUnit[[2]],-gradientUnit[[1]]};
shadowGrad = If[showGradient,{Gray,Thickness[0.008],Arrow[{levelP,levelP+Join[gradientUnit,{0}]}]},{}];shadowNegGrad = If[showNegativeGradient,{Gray,Thickness[0.008],Arrow[{levelP,levelP+Join[-gradientUnit,{0}]}]},{}];shadowTan = If[showTangentToLevelCurve,{Gray,Thickness[0.008],Arrow[{levelP,levelP+Join[tangent,{0}]}]},{}];
graphPoint={PointSize[0.02],Point[graphP]};
verticalLine=If[showVerticalLine,{Dashed,Yellow,Line[{levelP,graphP}]},{}];
contours=Graphics3D[{Texture[ContourPlot[f[x,y],{x,-1,3},{y,-1,3},Frame->False,Axes->False,PlotRangePadding->0,
ContourShading->Automatic,ContourStyle->{{Yellow,Thickness[0.01]}},Contours->Table[i,{i,-6,6,1}]]],EdgeForm[],Polygon[{{-1,-1,0},{-1,3,0},{3,3,0},{3,-1,0}},VertexTextureCoordinates->{{0,0},{1,0},{1,1},{0,1}}]}];
surface=Plot3D[f[x,y],{x,-1,3},{y,-1,3},Mesh->20];
)

graphicsFinal[t_]:=(

animationP=(1-t)*levelP+t*graphP;
gradientVector=Join[gradientUnit,{t*gradientUnit.gradient}];
negGradientVector=Join[-gradientUnit,{-t*gradientUnit.gradient}];
tangentVector=Join[tangent,{t*gradientUnit.tangent}];

point = {PointSize[0.02],Point[animationP]};

gradientArrow=If[showGradient,{Cyan,Thickness[0.008],Arrow[{animationP,animationP+gradientVector}]},{}];
negGradientArrow=If[showNegativeGradient,{Red,Thickness[0.008],Arrow[{animationP,animationP+negGradientVector}]},{}];tangentArrow=If[showTangentToLevelCurve,{Green,Thickness[0.008],Arrow[{animationP,animationP+tangentVector}]},{}];

Show[Graphics3D[{shadowGrad,shadowNegGrad,shadowTan,verticalLine,point,graphPoint,gradientArrow,negGradientArrow,tangentArrow}],contours,surface,Axes->Automatic,AxesLabel->{x,y,z},BoxRatios->{1,1,1},PlotRange->{{-1,3} ,{-1,3},{-1,6}},Ticks->ticks,ViewPoint->{20,4,8}])

animation:=(
graphicsInitial;
Table[Image[graphicsFinal[t]],{t,0,1,1/12}]
)

Graphics

f[x_,y_]:=(x^2+y^2+2x y)/4.5;
P={1,2};
showTangentToLevelCurve=True;
showGradient=True;
showNegativeGradient=True;
showVerticalLine=True;

(*Display parameters*)
ticks=Automatic;

frames = animation;
ListAnimate[frames,DefaultDuration->1]

Gradient Vector Field

Code

graphics:=(
grad=Grad[f[x,y],{x,y}];
vectorField=If[showGradientVectorField,VectorPlot[grad,{x,-5,5},{y,-5,5}],{}];
levelCurves=If[showLevelCurves,ContourPlot[f[x,y],{x,-5,5},{y,-5,5},Contours->10,ContourShading->False],{}];
Show[vectorField,levelCurves,Axes->Automatic,AxesLabel->{x,y},PlotRange->{{-5,5} ,{-5,5}}])

Graphics

f[x_,y_]=x^2+y^2-x y/2;
showLevelCurves=True;
showGradientVectorField=True;

graphics

Level Surfaces

Code

graphics:=(
grad=Grad[f[x,y,z],{x,y,z}];
point = Graphics3D[{Red,PointSize[0.02],Point[showPoint]}];
pointGradient = Graphics3D[{RGBColor[0,0.75,0],Arrowheads[.02],Thickness[0.005],Arrow[{showPoint,showPoint+grad/.{x->#1,y->#2,z->#3}&@@showPoint}]}];
vectorField=If[showGradientVectorField,VectorPlot3D[grad,{x,xLeft,xRight},{y,yLeft,yRight},{z,zLeft,zRight},VectorPoints->{4,5,4},VectorStyle->{RGBColor[0,0.75,0],Arrowheads[.02],Thickness[0.005]}],{}];
levelSurfaces=If[Length[showLevelSurfacesAt]>0,ContourPlot3D[f[x,y,z],{x,xLeft,xRight},{y,yLeft,yRight},{z,zLeft,zRight},Contours->showLevelSurfacesAt,ContourStyle->Opacity[0.8],Mesh->None],{}];
Show[vectorField,levelSurfaces,point,pointGradient,Axes->Automatic,AxesLabel->{x,y,z},PlotRange->All]
)

Graphics

Use showLevelSurfacesAt to specify a list of level surfaces to display.
To display no level surfaces, use the empty list { }.
To display a single surface, say, at f(x,y,z)=4, use {4}.
To display multiple, separate the values with commas, like {4,6}.

f[x_,y_,z_]:=x^2-x y^2/3+z^4;
showLevelSurfacesAt={1};
showGradientVectorField=False;
showPoint={3,3,1};

(*Display Parameters*)
xLeft=-3;
xRight=6;
yLeft=-6;
yRight=6;
zLeft=-3;
zRight=3;
mesh=None;

graphics

(*Key concepts plot is obtained from:
f[x_,y_,z_]:=x^2-y^2+z^2;
showLevelSurfacesAt={16};*)