close

Function Repository Resource:

DirectionalD

Source Notebook

Compute the directional derivative of a function

Contributed by: Wolfram Staff (original content by Alfred Gray)

ResourceFunction["DirectionalD"][f,r,vars]

computes the derivative of a function f in the direction r with variables vars.

Details and Options

The directional derivative of a function f at the point p along an arbitrary vector Image represents the instantaneous rate of change of the function at that direction.
ResourceFunction["DirectionalD"] is a generalization of a partial derivative in which the rate of change is taken with respect to one of the variables, considering the rest as constant.
The directional derivative is formally defined as Image, where Image is the vector that indicates the direction.
An alternative definition of the directional derivative is taken to be with respect to a normalized arbitrary nonzero vector Image.
Other notations for the directional derivative are Image, Image, Image, Image, Image, Image, Image and Image.

Examples

Basic Examples (2) 

Directional derivative of a function of two variables:

In[1]:=
ResourceFunction["DirectionalD"][3 x^2 y, {1, 1/2}, {x, y}]
Out[1]=
Image

Directional derivative of a function of three variables:

In[2]:=
ResourceFunction["DirectionalD"][
 Sin[x] Cos[y] Exp[z], {1, 2, 3}, {x, y, z}]
Out[2]=
Image

Scope (6) 

Directional derivative with a zero component in one direction:

In[3]:=
ResourceFunction["DirectionalD"][3 x^2 y, {0, 1}, {x, y}]
Out[3]=
Image

A unit vector along direction (3/2,1):

In[4]:=
ResourceFunction["DirectionalD"][ x Sin[y], Normalize[{3/2, 1}], {x, y}]
Out[4]=
Image

Without normalization:

In[5]:=
ResourceFunction["DirectionalD"][ x Sin[y], {3/2, 1}, {x, y}]
Out[5]=
Image

Evaluated at the point p=(3/2,1):

In[6]:=
{%, %%} /. {x -> -2, y -> \[Pi]}
Out[6]=
Image

Define a function:

In[7]:=
g[x_, y_, z_] := x y z

Compute the directional derivative:

In[8]:=
ResourceFunction["DirectionalD"][g[x, y, z], {1, 2, 3}, {x, y, z}]
Out[8]=
Image

Directional derivative in the direction (-1,-1/2) evaluated at the point (-1,3/2):

In[9]:=
ResourceFunction["DirectionalD"][
  3 x^2 y, {-1, -1/2}, {x, y}] /. {x -> -1, y -> 3/2}
Out[9]=
Image

A plot of the directional derivative:

In[10]:=
With[{p = {x, y, 3 x^2 y} /. {x -> -1, y -> 3/2}}, Show[Plot3D[3 x^2 y, {x, -2, 0}, {y, 0, 2}, Mesh -> None, PlotStyle -> Opacity[.5]], Graphics3D[{Red, Point[p], Arrowheads[.005], Arrow[{p, p + .5 {-1, -(1/2), 15/2}}]}]]]
Out[10]=
Image

Visualize directional derivatives over a surface:

In[11]:=
arrows = With[{d = {.5, 1}}, Graphics3D[{Arrowheads[.025], Table[Arrow[{{x0, y0, Sin[x0] Cos[y0]}, {x0, y0, Sin[x0] Cos[y0]} + .5 Normalize[{d[[1]], d[[2]], ResourceFunction["DirectionalD"][Sin[x] Cos[y], d, {x, y}] /. {x -> x0, y -> y0}}]}], {x0, 0, 5, .5}, {y0, 0, 5, .5}]}]];
Show[Plot3D[Sin[x] Cos[y], {x, 0, 5.5}, {y, 0, 5.5}, Mesh -> None, PlotStyle -> Opacity[.5]], arrows]
Out[12]=
Image

The directional derivative (red) and the gradient (blue):

In[13]:=
Manipulate[
 With[{pt2 = Append[pt, 2 x^2 - y^2] /. Thread[{x, y} -> pt]}, Show[Plot3D[2 x^2 - y^2, {x, -5, 5}, {y, -5, 5}, Mesh -> None, PerformanceGoal -> "Quality", PlotStyle -> Opacity[.5], PlotRange -> {{-5, 5}, {-5, 5}, {-26, 51}}], Graphics3D[{Arrowheads[.005], Blue, Arrow[{pt2, pt2 + {1, 1, 4 x - 2 y}}], Red, Arrow[{pt2, pt2 + {a, b, Normalize[
           ResourceFunction["DirectionalD"][
            2 x^2 - y^2, {a, b}, {x, y}]]}}]} /. Thread[{x, y} -> pt]]]],
 {{pt, {2, -2}}, {-4, -4}, {4, 4}}, {a, -1, 1}, {b, -1, 1}]
Out[13]=
Image

Publisher

Enrique Zeleny

Version History

  • 2.0.0 – 01 October 2020
  • 1.0.0 – 15 June 2020

Source Metadata

Related Resources

License Information