The Directional Derivative

This Demonstration visually explains the theorem stating that the directional derivative (Subscript[D, u])f((Subscript[x, 0]),(Subscript[y, 0])) of the function f at the point ((Subscript[x, 0]), (Subscript[y, 0])) in the direction of the unit vector u is equal to the dot product ∇f((Subscript[x, 0]),(Subscript[y, 0]))·u of the gradient of f with u. If we denote the partial derivatives of f at this point by (Subscript[f, x]) and (Subscript[f, y]) and the components of the unit vector u by (Subscript[u, 1]) and (Subscript[u, 2]), we can state the theorem as follows:

(Subscript[D, u])f((Subscript[x, 0]),(Subscript[y, 0]))=(Subscript[f, x])(Subscript[u, 1])+(Subscript[f, y])(Subscript[u, 2]).

In this Demonstration there are controls for θ, the angle that determines the direction vector u, and for the values of the partial derivatives (Subscript[f, x]) and (Subscript[f, y]). The partial derivative values determine the tilt of the tangent plane to f at the point ((Subscript[x, 0]), (Subscript[y, 0])); this is the plane shown in the graphic. When you view the "directional derivative triangle", observe that its horizontal leg has length 1 (since u is a unit vector), and so the signed length of its vertical leg repesents the value of the directional derivative (Subscript[D, u])f((Subscript[x, 0]),(Subscript[y, 0])). When you view the "partial derivative triangles", this signed vertical distance is decomposed as the sum (Subscript[f, x])(Subscript[u, 1])+(Subscript[f, y])(Subscript[u, 2]). The first summand is represented by the vertical leg of the blue triangle; the second is represented by the vertical leg of the green triangle. The visual representation is most clear when the two summands have the same sign.

Soiurce: http : // demonstrations.wolfram.com/DirectionalDerivativesAndTheGradient/

Manipulate[
Module[{c=Cos[θ],s=Sin[θ]},
Show[
Plot3D[fx*x+fy*y,{x,-1,1},{y,-1,1},PlotRange->1,BoxRatios->1,Mesh->None,ClippingStyle->None,PlotStyle->Directive[Opacity[.5],Gray],Ticks->None,AxesLabel->{Style["x",Italic],Style["y",Italic],Style["z",Italic]}],
ParametricPlot3D[{Cos[t],Sin[t],fx*Cos[t]+fy*Sin[t]},{t,0,2π}],
Graphics3D[{
{Red,PointSize[.01],Point[{{0,0,0},{c,s,fx*c+fy*s}}]},
If[xyPolys,{Blue,Opacity[.5],Polygon[{{0,0,0},{c,0,fx*c},{c,0,0}}],
Polygon[Plus[{0,s,0},#]&/@{{0,0,0},{c,0,fx*c},{c,0,0}}],Polygon[{{c,0,0},{c,s,0},{c,s,fx*c},{c,0,fx*c}}]},{}],
If[xyPolys,{{Green,Opacity[.5],Polygon[{{0,0,0},{0,s,fy*s},{0,s,0}}],
Polygon[Plus[{c,0,fx*c},#]&/@{{0,0,0},{0,s,fy*s},{0,s,0}}]},Line[{{0,s,fy*s},{c,s,fx*c+fy*s}}]},{}],
If[xyPolys,{Thick,{Blue,Line[{{c,s,0},{c,s,fx*c}}]},{Green,Line[{{c,s,fx*c},{c,s,fx*c+fy*s}}]}},{}],
If[slopeTriangle,{Line[{{0,0,0},{Cos[θ],0,0},{Cos[θ],Sin[θ],0},{0,Sin[θ],0},{0,0,0}}],
{Red,Line[{{0,0,0},{Cos[θ],Sin[θ],0}}]},{Opacity[.5],Polygon[{{0,0,0},{Cos[θ],Sin[θ],0},{Cos[θ],Sin[θ],fx*Cos[θ]+fy*Sin[θ]}}]}},{}]
}],ImageSize->{400,400}
]
]
,
Dynamic[Graphics[{Circle[{0,0},1],{Red,Arrow[{{0,0},{Cos[θ],Sin[θ]}}]},{Gray,Line[{{0,0},{Cos[θ],0},{Cos[θ],Sin[θ]},{0,Sin[θ]},{0,0}}]},Text["(Subscript[u, 1])",{Cos[θ]/2.,0}],Text["(Subscript[u, 2])",{0,Sin[θ]/2.}]},ImageSize->100]],
{{θ,1.},0,2π,ImageSize->Tiny},
{{fx,.4,"(Subscript[f, x])"},-1,1,ImageSize->Tiny},
{{fy,.4,"(Subscript[f, y])"},-1,1,ImageSize->Tiny},
{{slopeTriangle,True,"show directional derivative triangle"},{True,False},ControlPlacement->Top},
{{xyPolys,False,"show partial derivative triangles"},{True,False},ControlPlacement->Top},
ControlPlacement->Left,SaveDefinitions->True]

Contour Plots and the Gradient

Manipulate[
DynamicModule[{grad,unitgrad,function,buttonText,functionButtons},
function[x_,y_]:={5-(x^2)/2-(y^2),(x^2)/2+(y^2),1-2x+y,(x^2)/2-(y^2),-(x^2)/2+(y^2),Sin[Sin[y]+Sin[x]],x y (E^(-0.4((x^2)+(y^2))))};
buttonText={" maximum"," minimum"," linear"," laddle 1"," saddle 2"," example 1"," example 2"};
functionButtons =Map[#[[1]]-> #[[2]]&,Transpose[{Range[Length[buttonText]],buttonText}]];
grad=D[function[x,y][[fff]],{{x,y}}];
unitgrad=grad/(Sqrt[grad.grad]); 
Plot3D[function[x,y][[fff]],{x,-3,3},{y,-3,3}]
Deploy[
Show[{
ContourPlot[function[x,y][[fff]],{x,-3,3},{y,-3,3},Contours->10],
Graphics[{
Red,
If[MemberQ[options,gradient],
If[MemberQ[options,normalize]&&Dynamic[grad/.{x->pt[[1]],y->pt[[2]]}]=!={0,0},
Dynamic[Arrow[{pt,pt+(unitgrad/.{x->pt[[1]],y->pt[[2]]})}]],
Dynamic[Tooltip[
Arrow[{pt,pt+(grad/.{x->pt[[1]],y->pt[[2]]})}],Style[grad/.{x->pt[[1]],y->pt[[2]]},Red]]],
],
Red],
If[MemberQ[options,normalize],{Black,Dynamic[Circle[pt,1]]},Blue],
Blue,
If[MemberQ[options,neggradient],
If[MemberQ[options,normalize]&&Dynamic[grad/.{x->pt[[1]],y->pt[[2]]}]=!={0,0},Dynamic[Arrow[{pt,pt-(unitgrad/.{x->pt[[1]],y->pt[[2]]})}]],
Dynamic[Tooltip[Arrow[{pt,pt-(grad/.{x->pt[[1]],y->pt[[2]]})}],Style[-grad/.{x->pt[[1]],y->pt[[2]]},Blue]]]
],
Blue],
Tooltip[Locator[Dynamic[pt]],Dynamic[pt]]}]},
ImageSize->If[MemberQ[options,format],500,380],
PlotLabel->If[MemberQ[options,label],ToString[f[x,y],TraditionalForm]<>" = "<>ToString[function[x,y][[fff]],TraditionalForm],Column[{" "},ItemSize->{Automatic,2.75}]]]]
],
{{options,{gradient},""},
{gradient->Style[" ∇f",Red],neggradient->Style[" -∇f",Blue],normalize->" normalize          ",format->Style[" large format",10],
label->Style[" show function",10]}},
{{fff,1,"function"},functionButtons,ControlType->Setter},
{{pt,{1,0},"location"},{-3,-3},{3,3},ImageSize->If[MemberQ[options,format],Medium,Tiny]},
ControlType->{CheckboxBar,Setter,Slider2D},
ControlPlacement->{Top,Top,Left},
Initialization:>{Clear[x,y];
buttonText={" maximum"," minimum"," linear"," saddle 1"," saddle 2"," example 1"," example 2"};
functionButtons =Map[#[[1]]-> #[[2]]&,Transpose[{Range[Length[buttonText]],buttonText}]];},AutorunSequencing->{1,2,3}]

Using the Gradient on surfaces defined implicitly (ex 2)

Manipulate[
x[y_,z_,k_]=z+4ArcTan[y z]-k;
{x0,y0,z0}={1+π,1,1};

wx[x_,y_,z_]:=-1;
wy[x_,y_,z_]:=((4z)/(1+(((y z))^2)));
wz[x_,y_,z_]:=1+((4y)/(1+(((y z))^2)));
L[x_,y_,a_,b_,c_]:=c+((-1)/(wz[a,b,c]))(wx[a,b,c](x-a)+wy[a,b,c](y-b));
Show[
{Graphics3D[{
PointSize[Large],Point[{x0,y0,z0}]}],
ParametricPlot3D[{x[y,z,k],y,z},{y,-2,2},{z,-2,2}],
ParametricPlot3D[{u,v,L[u,v,x0,y0,z0]},{u,x0-.5,x0+.5},{v,y0-.5,y0+.5}],
ParametricPlot3D[{x0+t wx[x0,y0,z0],y0+t wy[x0,y0,z0],z0+t wz[x0,y0,z0]},{t,0,1}]
}
,AxesLabel->{x,y,z},Ticks->None,PlotRange->{{-2,5},{-2,2},{-2,3}}
]

,{{k,0},-5,5,1}, SaveDefinitions->True]