Laboratory 8
Directional Derivatives

Introduction

    In this laboratory, we shall discuss the directional derivatives of a function. The directional derivative can be used to find the directions in which a function is increasing or decreasing most rapidly, as well as to find the directions in which a function remains constant.

Example 8.1, Introduction to Directional Derivatives

    Consider the function f(x, y) = 2x y^2 - x^3 - y^4

f[x_, y_] := 2x y^2 - x^3 - y^4 Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1}, BoxRatios {1, 1, 1}] ;

[Graphics:HTMLFiles/index_3.gif]

We know from our study of partial derivatives that if we hold x constant, say x = x_0, and consider the function yf(x_0, y), then the derivative of this function at the point y = y_0 is the partial derivative ∂f/∂y (x_ (0,) y_0). We can interpret this as the derivative of the function f(x, y) at the point (x_0, y_0) in the direction of the y-axis, or equivalently in the direction of the unit vector (0, 1). On the other hand, if we want the derivative of the function in the direction of the x-axis, or equivalently in the direction of the unit vector (1, 0), we can find that by calculating the partial derivative ∂f/∂x (x_ (0,) y_0).
    Now suppose that we want to find the derivative of the function f in some other direction, say Overscript[n, ⇀] = (n_1, n_2). For simplicity, we require that the vector n be a unit vector so that || Overscript[n, ⇀] || = (n_1^2 + n_2^2)^(1/2) = 1. If we start out at the point FormBox[Cell[TextData[Cell[BoxData[(x , y )]]]], TraditionalForm]                                      0   0 and move in the direction Overscript[n, ⇀] = (n_1, n_2), then we would be following the parametric curve
    (x(t), y(t)) = (x_0 + n_1t, y_0 + n_2t).
The function f(x, y) on this curve would then be a function of t given by
    tf(x(t), y(t))
and we would like to know what the derivative of this function is with respect to t at the time when t = 0. We can find this derivative by using the chain rule;
    d/(d t) f(x(t), y(t)) = ∂f/∂x (d x)/(d t) + ∂f/∂y (d y)/(d t).
From the definition of (x(t), y(t)), we can then calculate (d x)/(d t) and (d y)/(d t) to see that
    d/(d t) f(x(t), y(t)) = n_1∂f/∂x + n_2∂f/∂y.
We can rewrite this expression with the aid of the gradient vector  ∇f = (∂f/∂x, ∂f/∂y) as
    d/(d t) f (x(t), y(t)) = ∇f · Overscript[n, ⇀].
    
    Let us see how this works in the context of our original example. Suppose that we want to find the derivative of f(x, y) = 2 x y^2 - x^3 - y^4 at the point (1/2, 0) in the direction towards the point (1, 1). First, we let Overscript[v, ⇀] be the vector from (1/2, 0) to (1, 1).FormBox[Cell[], TraditionalForm]

v = {1, 1} - {1/2, 0}

{1/2, 1}

Because Overscript[v, ⇀] does not have unit length, we let Overscript[n, ⇀] = Overscript[v, ⇀]/|| Overscript[v, ⇀] ||, which has length 1, and still points in the same direction as Overscript[v, ⇀]. To do this, we use the fact that || Overscript[v, ⇀] ||^2 = Overscript[v, ⇀] · Overscript[v, ⇀].

n = v/Sqrt[v . v]

{1/5^(1/2), 2/5^(1/2)}

Set (x(t), y(t)) = (x_0 + n_1t, y_0 + n_2t) which gives us the parametric form of a line starting at (x_0, y_0) and heading in the direction Overscript[n, ⇀]. We then calculate d/(d t) f(x(t), y(t)) when t = 0. For simplicity, we shall write g(t) = f(x(t), y(t)). Remember that, in this problem we want the derivative at the point (1/2, 0) so that (x_0, y_0) = (1/2, 0).

x[t_] := 1/2 + n[[1]] * t y[t_] := 0 + n[[2]] * t g[t_] := f[x[t], y[t]]

We then calculate g ' (0)

g '[0]

-3/(4 5^(1/2))

We claim that the derivative of f(x, y) at (1/2, 0) in the direction towards (1, 1) is -3/(45^(1/2)).
    To understand what this means graphically, let us graph the function g(t), and compare it to the graph of f(x, y) where the vertical plane through (1/2, 0) and (1, 1) is shown.
    We shall do so with the aid of a module called Slice that we define below. The first input to SliceFormBox[Cell[], TraditionalForm] is the function itself; the next two pairs of inputs are the coordinates of two points in the x y-plane; the module returns a graph of f and the vertical plane that contains these two points. The last inputs are the x-range, y-range and z-range of the graph.

Slice[f_, {xa_, ya_}, {xb_, yb_}, {x, x0_, x1_}, {y, y0_, y1_}, {z, z0_, z1_}] := Module[{a, b ... Range {{x0, x1}, {y0, y1}, {z0, z1}}, DisplayFunction$DisplayFunction] ; ]

General :: spell1 : Possible spelling error: new symbol name \"Slice\" is similar to existing symbol \"Splice\".  More…

We ignore the warning; we are not interested in Mathematica's built in command Splice. We execute the module Slice where the x-range and the y-range are chosen so that the points of interest (1/2, 0) and (1, 1) are on opposite corners of a rectangle, and the z-range is chosen to give a good picture.FormBox[Cell[], TraditionalForm]

Slice[f, {0.5, 0}, {1, 1}, {x, 0.5, 1}, {y, 0, 1}, {z, -0.5, 0.2}]

[Graphics:HTMLFiles/index_83.gif]

Let us also plot the graph of the function g(t). Recall that we have chosen the function g(t) so that one unit of distance in the three dimensional graph corresponds to one unit of distance for the independent variable t. Because the distance from (1/2, 0) to (1, 1) is ((1 - 1/2)^2 + (1 - 0)^2)^(1/2) = 5^(1/2)/2, we plot the graph of g(t) for 0≤t≤5^(1/2)/2.

Plot[g[t], {t, 0, 5^(1/2)/2}] ;

[Graphics:HTMLFiles/index_93.gif]

We can see that the graph of g agrees with the portion of the graph of f that lies on the vertical plane containing (1/2, 0) and (1, 1). As a consequence, we see that the derivative of f at (1/2, 0) in the direction towards (1, 1) is the same as the derivative of g(t) when t = 0, namely -3/(45^(1/2)).

    We saw above however, that there is another method we could use to calculate this directional derivative; we could calculate the gradient of f, and then find ∇f · Overscript[n, ⇀]. We know that the gradient vector is given by ∇f = (f_x, f_y).

gradf = {D[f[x, y], x], D[f[x, y], y]}

{f^(1, 0)[x, y], f^(0, 1)[x, y]}

We can then calculate ∇f · Overscript[n, ⇀]

gradf . n

(-3 x^2 + 2 y^2)/5^(1/2) + (2 (4 x y - 4 y^3))/5^(1/2)

Substitute x = 1/2 and y = 0 to obtain

gradf . n /. {x1/2, y0}

-3/(4 5^(1/2))

which agrees with our original answer.
    We remark that Mathematica has a command Grad which is part of the Calculus`VectorAnalysis` package, but we shall not discuss it.

Experiment 8.1

    Find the directional derivative of f(x, y) at the indicated point in the direction toward the second indicated point. Calculate the derivative in two ways, as d/(d t) f(x(t), y(t)) and as ∇f · Overscript[n, ⇀], and include graphs of the function f(x(t), y(t)) as well as a graph of the function f(x, y) together with the vertical plane containing the starting point and ending point.
    
    1. f(x, y) = x^2sin2y at (1, π/2) towards (3, -4).
    
    2. f(x, y) = 3x^2 - 2y^2 at (-3/4, 0) towards (0, 1).
    
    3. f(x, y) = e^(-x) cos y at (-1, -2) towards (0, -1).
    
    4. f(x, y) = x^2 - y^2 at (-2, -2) towards (1, 0).
    
    5. f(x, y) = tan^(-1)(x y) at (-1, -2) towards (1, 1)

Example 8.2 Direction of Fastest Change; Level Surfaces

    Consider once again the function f(x, y) = 2x y^2 - x^3 - y^4

f[x_, y_] := 2x y^2 - x^3 - y^4 Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1}, BoxRatios {1, 1, 1}] ;

[Graphics:HTMLFiles/index_138.gif]

In the previous example, we learned how to find the derivative of this function in any direction; in particular if Overscript[n, ⇀] = (n_1, n_2) is a vector of length 1, then the derivative of f at the point (x, y) in the direction of Overscript[n, ⇀] is exactly ∇f · Overscript[n, ⇀].
    Now suppose that we wanted to know in which direction the function increases most rapidly. Because the directional derivative of f is ∇f · Overscript[n, ⇀], we conclude that the directional derivative is largest when ∇f points in the same direction as Overscript[n, ⇀]; in particular, the function increases most rapidly when Overscript[n, ⇀] = ∇f/|| ∇f ||. Similarly, the function decreases most rapidly when Overscript[n, ⇀] points in the direction opposite to ∇f, so that Overscript[n, ⇀] = -∇f/|| ∇f ||.
    For example, to find the direction in which f(x, y) increases most rapidly at the point (1, 2), we proceed as follows.

gradf = {D[f[x, y], x], D[f[x, y], y]} gradf /. {x1, y2}

{6 x, -4 y}

{6, -8}

gradf/Sqrt[gradf . gradf] /.{x1, y2}

{3/5, -4/5}

The function f increases most rapidly in the direction (5/601^(1/2), -24/601^(1/2)), and decreases most rapidly in the direction (-5/601^(1/2), 24/601^(1/2)).
    Next, note that if ∇f is perpendicular to Overscript[n, ⇀], then the directional derivative of f in the direction of Overscript[n, ⇀] is identically zero. In two dimensions, we can find a vector perpendicular to a given vector Overscript[v, ⇀] = (v_1, v_2) by setting Overscript[v, ⇀]^⊥ = (-v_2, v_1). Thus, given a function f(x, y), we know that f is constant in the direction Overscript[n, ⇀] = (∇f)^⊥/|| (∇f)^⊥ || as well as in the direction -(∇f)^⊥/|| (∇f)^⊥ ||. In the case of our example function f(x, y) = 2x y^2 - x^3 - y^4, if we want to find in which directions f remains constant at the point (1, 2), we can proceed as follows.

gradfperp = {-D[f[x, y], y], D[f[x, y], x]} gradfperp /. {x1, y2}

{4 y, 6 x}

{8, 6}

gradfperp/Sqrt[gradfperp . gradfperp] /.{x1, y2}

{4/5, 3/5}

Thus we see that the function remains constant in the direction (24/601^(1/2), 5/601^(1/2)) and (-24/601^(1/2), -5/601^(1/2)) at the point (1, 2).

    To better understand the relationship between the gradient and the function, let us take a look at the gradient vector field superimposed on the contour graph of the function. To do so, we shall use the module ContourVectorPlot that is defined below. It takes as input a function f(x, y) as well as a viewing box.

<<Graphics`ImplicitPlot` <<Graphics`PlotField` ContourVectorPlot[f_, {x, x0_, x1_} ... 0}], PlotRange {{x0, x1}, {y0, y1}}, DisplayFunction$DisplayFunction] ; ]

ContourVectorPlot[f, {x, -1, 1}, {y, -1, 1}, {z, -1.5, 1}]

[Graphics:HTMLFiles/index_186.gif]

[Graphics:HTMLFiles/index_187.gif]

The gradient vector at each point is represented by an arrow of the same length and direction starting at that point. At points where the length of the gradient vector is small, all that is seen is simply the head of the vector.
    From the contour graph overlaid by the gradient vector field, we can see that the gradient vectors are always perpendicular to the level curves of the surface, and moreover that they point in the direction in which the function is increasing most rapidly.

Experiment 8.2

    For each of the following functions at the indicated points, find the direction in which the function increases most rapidly, and the directions along which the function is constant. Include a graph of the function as well as a graph of the gradient field overlaid on the contour graph. On the printout of the graph of the gradient field overlaid on the contour graph, draw in by hand the direction vectors that you found, and label them.

    1. f(x, y) = 9 - x^2 - 2y^2 at (1, 2).

    2. f(x, y) = 3x y e^(-x^2/2 - y^2) at (1/2, 1/2).

    3. f(x, y) = 1/4x^4 - 1/3y^4 - 4x y at (1, 3/2).
    
    4. FormBox[RowBox[{f(x, y), =, RowBox[{RowBox[{Cell[TextData[Cell[BoxData[x]]]], RowBox[{cos, (,  ... Data[x]]]], Cell[TextData[Cell[BoxData[y]]]]}], )}]}], +, 3 (1 - sin(x + y) }]}], TraditionalForm]at (π/3, π/4).

    5.                                                                                                ... ormBox[Cell[TextData[Cell[BoxData[x ]]]], TraditionalForm]]]]]}], -,  , y ^2}]}], TraditionalForm] at (2, -3).

Credits

These laboratories were created by Raouf Boules, Geoff Goodson, Ohoe Kim and Mike O'Leary for use in the Calculus courses of Towson University. Commercial use is prohibited without permission of the authors. Non-commercial use is permitted, provided this credit section is retained.


Created by Mathematica  (August 20, 2004)