DirectionalDerivatives::usage="Given a vector

u
= {​
u
1
​,​
u
2
​}​, the directional derivative of f at the point (x, y) in the direction of

u
is:
D
u
f(x, y) =
f
x
​(x, y)​
u
1
+
f
y
​(x, y)​
u
2
= {​
f
x
​(x, y)​,
f
y
​(x, y)​}.

u
or in three variables:
D
u
f(x, y, z) =
f
x
​(x, y, z)​
u
1
+
f
y
​(x, y, z)​
u
2
+
f
z
​(x, y, z)​
u
3
= {​
f
x
​(x, y, z)​,
f
y
​(x, y, z)​,
f
z
​(x, y, z) }.

u
. This is also how the directional derivative can be defined:
D
u
f = ∇f. u, where ∇f it is the gradient of f. ∇f(x, y) = {​
f
x
​(x,y)​,
f
y
​(x,y)​}​, in three variables: ∇f(x, y, z) = {​
f
x
​(x,y,z)​,
f
y
​(x,y,z)​,
f
z
​(x,y,z}​}​, it is valid for n variables. If ∇f ≠ 0, then ∇f give the direction of the largest increase in f, and - ∇f give the direction of the largest decrease in f. Find the directional derivative of the function f[x, y] = Cos[x y] at the point ​
1
4
​, π​ in the direction

u
= {Cos[θ]​, Sin[θ]​}. Graph the function in the interval of 0 ≤ θ ≤ 2π, and find the value where the extreme occurs. So for these values of θ, verify that

u
corresponds to ∇f or - ∇f. References: Hollis, Selwyn. CalcLabs with Mathematica. Multivariable Calculus. Cengage: p.98 4a. Ed, 2010;​ Personal. ";​​​​​​
In[]:=
f[x_,y_]:=Cos[xy];​​α=ResourceFunction["DirectionalD"][f[x,y],{Cos[θ],Sin[θ]},{x,y}]/.x
1
4
,yπ;​​Print[α];
-
πCos[θ]
2
-
Sin[θ]
4
2
In[]:=
gradf[x_,y_]=Grad[f[x,y],{x,y}];p0=
1
4
,π,0;​​tanvec=Appendgradf
1
4
,π,
gradf
1
4
,π.gradf
1
4
,π
//N;​​gradvec=Appendgradf
1
4
,π,0//N;​​Withp={x,y,f[x,y]}/.x
1
4
,yπ,q=gradvec,r=tanvec,​​ShowPlot3D[f[x,y],{x,0,2π},{y,0,2π},MeshFalse,PlotRangeAll,PlotStyleOpacity[.3],PlotRangeAll,AxesLabel->(Style[#,15,Blue]&/@{"X","Y","Z"}),BoxRatiosAutomatic,AxesOrigin{0,0,0},BoxedFalse,PlotLabelStyle[Framed["Directional derivative (black), tangent vector (red) and gradient (blue):"],16,Blue,BackgroundYellow]],Graphics3D[{Blue,Point[p0],Arrowheads[.05],Arrow[Tube[{p0,p0+q}]]}],Graphics3D[{Red,Point[p],Arrowheads[.05],Arrow[Tube[{p,p+r}]]}],Graphics3DBlack,Point[p],Arrowheads[.05],ArrowTubep,p+Cos[θ],Sin[θ],-
πCos[θ]
2
-
Sin[θ]
4
2
/.θ2π
Out[]=
Next, the values of t for which decrease the function, where the minimums exist:
In[]:=
gradLen=N
gradf
1
4
,π.gradf
1
4
,π

Out[]=
2.22846
In[]:=
u=-gradf
1
4
,πgradLen
Out[]=
{0.996849,0.0793267}
In[]:=
{x[t_],y[t_]}=
1
4
,π+tu
Out[]=

1
4
+0.996849t,π+0.0793267t
In[]:=
f[x[t],y[t]]//Simplify​​Plot[f[x[t],y[t]],{t,0,2π}]
Out[]=
Sin[0.785398-3.15152t-0.0790767
2
t
]
Out[]=
In[]:=
{x[t],y[t],f[x[t],y[t]]}/.t0.74
Out[]=
{-0.487668,3.08289,0.0673181}
In[]:=
{x[t],y[t],f[x[t],y[t]]}/.t2.58​​
Out[]=
{-2.32187,2.93693,0.859767}
In[]:=
{x[t],y[t],f[x[t],y[t]]}/.t4.31
Out[]=
{-4.04642,2.79969,0.327031}
Correspons to - ∇f, that is in the direction of decrease.
Next, the values of t for which the function increase, where the maximums exist:
u=gradf
1
4
,πgradLen
{-0.9968486721503291,-0.07932669684365853}​​
In[]:=
{x[t_],y[t_]}=
1
4
,π+tu
Out[]=

1
4
-0.996849t,π-0.0793267t
In[]:=
f[x[t],y[t]]//Simplify​​Plot[f[x[t],y[t]],{t,0,2π}]
Out[]=
Sin[0.785398+3.15152t-0.0790767
2
t
]
Out[]=
In[]:=
{x[t],y[t],f[x[t],y[t]]}/.t0.28
Out[]=
{-0.0291176,3.11938,0.995878}
In[]:=
{x[t],y[t],f[x[t],y[t]]}/.t2.41
Out[]=
{-2.15241,2.95042,0.997736}
Corresponds to ∇f, that is in the direction of increase.