Mathematica Labs | Denis Shubleka

Subject: Calculus

Topic: The Gradient; Directional Derivatives

Goal: Use Mathematica to compute gradient vectors and directional derivatives.

Task 1

In Mathematica we define a function f(x, y):

f[x_,y_]:=Cos[x^2+y^2];

To compute the gradient vector as a general rule, type and execute:

{(∂_x)f[x,y],(∂_y)f[x,y]}

Or alternatively:

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

To evaluate the gradient at a given point, let’s say (1, 3), use the substitution rule:

%/.{x->1,y->3}

If we plan to use this particular gradient vector for more computations, we can assign the result to a variable called ‘gradient’.

gradient=%;

Suppose that at the given point (1, 3) we want to compute the directional derivative in the direction of v = <-1, 5>. Recall that the directional derivative is the dot product between the gradient and the unit vector in the direction of v.

Dot[gradient, {-1,5}]/Norm[{-1,5}]

To plot a set of gradient vectors for a particular rectangle in the domain space (i.e. xy plane), type and execute the following. Can you identify the gradient vector that we computed above, at point (1,3)?

D[f[x,y],{{x,y}}]
VectorPlot[%,{x,-1,3},{y,1,5}]

Task 2

In Mathematica we define a function g(x, y, z):

g[x_,y_,z_]:=80/(1+x^2+2y^2+3z^2);

Suppose we want the gradient at the point (1, 1, -2). In Mathematica, type and execute:

D[g[x,y,z],{{x,y,z}}]/.{x->1,y->1,z->-2}

To compute the gradient vector as a general formula and a 3-dimensional plot of gradient vectors for a particular cuboid in the domain space (x-y-z), type:

D[g[x,y,z],{{x,y,z}}]
VectorPlot3D[%,{x,0,2},{y,0,2},{z,-3,-1}]

Related Exercises/Notes:

1.  Suppose f(x, y, z) = x sin(yz). Find the gradient of f and the directional derivative of f at the point (1, 3, 0) in the direction of v = <1, 2, -1>.

2.  Find the directional derivative of the function f(x, y)=(x^2)(y^3)-4y at the point (2, -1) in the direction of v = <2, 5>.

ap-calc.github.io