Lab 3B - Directional Derivatives, Gradients, and Vector Fields

Math 2374 - University of Minnesota

http://www.math.umn.edu/math2374

Questions to: rogness@math.umn.edu

Introduction

This week's lab is sort of a mish-mash of a few different topics which are all interrelated: directional derivatives, gradients, and vector fields. These topics are all covered in your book, but Mathematica can help you visualize them in ways not possible on a blackboard or on a written page.

When you started this class, you already knew about the plain-old single variable calculus derivative, i.e. the slope of the tangent line. Since then you've learned about two multivariable generalizations of the derivative, the "partial derivative" and the "total derivative" (which is also called the "derivative matrix" or "Jacobian matrix"). These two concepts are intertwined, of course, because the derivative matrix is composed of a bunch of partial derivatives.

In this lab, we're going to deal with variants of both of these ideas. The directional derivative is a generalization of a partial derivative. The gradient, meanwhile, is a special case of the Jacobian matrix. In what might seem to be either a bizarre coincidence or a stroke of good luck, the gradient is very useful for calculating the directional derivative. It also turns out that the gradient provides an example of a vector field, which will be an important new kind of function for the rest of the course.

If you're getting confused trying to keep track of all these connections, don't worry. You know what? This stuff is confusing! But the more you work with these ideas, the easier it gets. This lab can help, too, by showing you some visual examples, and explaining things that you've also learned about in lecture. Sometimes it can really help to read or hear a second explanation about a topic, even if the first one was very good!

With that in mind, this lab includes very few problems, but a lot of reading and a number of pictures. Above all, resist the temptation to scan over a few sentences here, gloss over this paragraph there, and just skim through the material about this or that. Gradients and vector fields, in particular, will be absolutely fundamental for the rest of the course, so it's important to learn these ideas. (That's why they're being covered in both lecture and lab.) If a sentence or paragraph is really confusing, read it over again, slowly, a few times. If you're still not sure what it says, ask your TA for help.

Directional Derivatives and Gradients

Let's go back to partial derivatives for a minute. Recall that if you're on a surface above (or below) a point , then partial derivative of f with respect to x tells you how steep the surface is in the positive x-direction at that point. If it's positive, then the surface goes "uphill" in that direction. If it's negative, then the surface goes "downhill." The partial derivative with respect to y tells you the same sort of information about what's happening in the positive y-direction.

That's all fine and good, but what about any of the other directions? To carry the previous metaphor a bit further, if you're a mountain climber on a 24,000ft mountain (the surface of which could be the graph of a function), you want to know what's going on in more than just the positive x- and y-direction.

(Let's think of these directions as east and north, respectively; imagine you're looking down at the xy-plane, and think of the axes as lying on a compass. The x-axis points east, to the right, and the y-axis points up, to the north.)

Back on the mountain you're climbing, you want to know: what about northeast? Or southsouthwest? Or any of the infinitely many directions in-between? That's where the directional derivative comes in. It can tell you how steep a surface is in any given direction, and whether that "steepness" goes up or down.

Take a look at the following graph, which could represent a mountain and a valley, side-by-side in an otherwise nearly flat plain.

In[242]:=

Out[242]=

Look at the point (1,0,0), which is almost exactly in the middle of the graph. A mountain climber at that point on the surface could walk either uphill or downhill -- or even in a path which keeps her flat, at the same height! Go to the following web page to see an interactive demonstration of the directional derivative using this surface:

http://www.math.umn.edu/~rogness/multivar/dirderiv.shtml

(In this online version the demonstration is included here:)

Here's what the example shows you. The green dot represents the mountain climber's position. As you change the bearing by sliding the blue dot around, you get an interactive look at the "cross section" of the surface through that point at the bearing. Now imagine fitting a tangent line to the curve representing the cross section. That's the red arrow, where the direction of the arrow shows you which direction the climber is facing.

If the arrow is pointing up, then the directional derivative in that direction is positive. If the arrow is pointing down, then the directional derivative is negative. An arrow which is pointing just ever so slightly up would indicate a small (but positive) value for the directional derivative, say 0.01. If the arrow is tilted more upward, the derivative has a much higher positive value.

As you can see, it's positive as you move from the east, through the north, to the west. From the west, to the south, and back to the east, it's negative.

Calculating Direction Derivatives

So how do we calculate direction derivatives? Well, first it's important to understand when we can even talk about them. We can only use directional derivatives for a function with one output; that is, a function . The reason for this is that a directional derivative measures the change in that one output per unit change in some given direction on the input side of things. (In English, using the mountain climbing metaphor: the directional derivative measures the slope of the mountain, i.e. the instantaneous change in height per change in (x,y) position, where the (x,y) position is changing in some given direction.)

In most cases, we'll only work with directional derivatives for functions , in other words, functions like . But you could use the same ideas to calculate a directional derivative for a function , or .

Like all derivatives, the directional derivative has a limit definition, which you can find in your textbook. It's got vectors in it, so it's a little messier than your normal, everyday limit definition of in single variable calculus, and we're going to forget about it here. Instead we'll concentrate on a different way to calculate the directional derivative.

Suppose we've got a function , and we're interested in the point . In the example above, this was the point (1,0), but it could be anything. Now suppose we want to look in a certain direction, whether it's east, west, or 8.29 degrees west of southsouthwest. We specify a direction by choosing a vector. For example, we'd say "in the direction of the vector (3,4)." In that case, we can easily find the directional derivative of at the point .

Theorem: The directional derivative of at the point , in the direction of a vector , is given by

,

where is a unit vector in the direction of , given by , and is the gradient of evaluated at the point a.

One thing in that theorem requires some explanation, namely the "gradient of f at the point a. For a function with just one output, the gradient of f is:

In most cases, we'll only work with two or three dimensions, in which case, or , respectively.

Note how similar the gradient is to the Jacobian matrix. Write down the definitions for the Jacobian matrix of a function . The only real difference is whether we think of it as a vector or a 1-column matrix; the entries themselves are exactly the same!

Why, you might ask, do we have two names for the identical thing? That's a good question, and probably has more to do with traditions -- and the difficulty of changing traditions -- than anything else.

Mathematica can compute gradients using the Grad function:

In[244]:=

Out[244]=

Out[245]=

There's a catch, unfortunately; Mathematica only works with three dimensional gradients. Notice how it tacks on an extra zero at the end of the following gradient.

In[246]:=

Out[246]=

Out[247]=

We've defined a special command called Grad2D to let you compute two dimensional gradients:

In[248]:=

Out[248]=

Example

Let's use the same function as before, and find the directional derivative at the point (1,0) in the direction of the vector (3,4).

In[249]:=

Out[249]=

From our handy-dandy theorem, we know that:

First we should compute , which is a unit vector in the direction of the vector =(3,4). We can use the formula , where the double bars mean "length of" a vector, which is also called the "norm" of a vector.

In[250]:=

Out[250]=

We can do that a little more automatically by defining a function like this, which will take a vector and return a unit vector in that direction.

In[251]:=

Out[251]=

Out[252]=

We also need the gradient of f, and then we're ready to compute:

In[253]:=

Out[253]=

Out[254]=

Out[255]=

Out[256]=

So the slope in that direction is about 8/(5e), or about 0.59. If you look back at the interactive example, does that seem correct?

From the picture it seems the steepest the incline can be is when the bearing is about northnorthwest, or (roughly) in the direction of the vector (-1,2). In that direction, the directional derivative is:

In[257]:=

Out[257]=

Out[258]=

Out[259]=

Our eyes weren't deceiving us -- it really is steeper in that direction. If you go in the direction of (-1,-1) instead, you'll go downhill:

In[260]:=

Out[260]=

Out[261]=

Out[262]=

Exercise 1

Suppose there is a spaceship in three-dimensional space, with our sun located at the origin, (0,0,0). The temperature near the sun is extremely hot, of course -- about 6000K -- and it drops rapidly as one moves away. Suppose the temperate at any point is space is given by , where x, y, and z measured in units of 10 million miles. (Here we're assuming the spaceship will never get close enough to other stars to notice their heat, so effectively our sun is the only heat-producer in the universe.)

Suppose now that the spaceship is located on the Earth, about 93 million miles away, at the point (9,2,1). What is the directional derivative (change in K per change in position) if the spaceship were to move in the direction of the vector (-4,-3,5)?

(If you're interested in how to find the surface temperature of a star, look at

http://zebu.uoregon.edu/~soper/Stars/color.html., or any of the other pages you can find on Google by searching for "Star Temperature Color" or something similar.)

Exercise 2

Using the same setup as in Example 1, suppose the spaceship is situated at the point (4,-2,8). If the ship were to fly in the direction of the point (-4,3,7), would the initial change in temperature be an increase or a decrease?

Vector Fields

A Vector Field is one of the most fundamental concepts in Multivariable Calculus and Vector Analysis. One of the main reason this course exists is to teach you four specific theorems: the Fundamental Theorem of Line Integrals, Green's Theorem, Stokes' Theorem, and the Divergence Theorem. Every single one of these theorems involves vector fields! If you take a little time now to understand what a vector field is, you'll save yourself some time and grief later on.

In lecture you've already talked about linear transformations, so you should hopefully be comfortable with the idea of a function . This notation means that takes two inputs and has two outputs. You've also learned that a linear transformation with two inputs and outputs can be represented by a 2 by 2 matrix.

A (two-dimensional) vector field is simply a function , but there are two key differences: (1) is not necessarily a linear function, so it might or might not be possible to represent it using a matrix, and (2) we interpret the inputs and outputs in a different way.

Graphing a Vector Field

Let's look at an example. Traditionally we represent vector fields with capital letters, so we'll call our first field instead of .

In[263]:=

Out[263]=

Here's the key to understanding vector fields: we think of the inputs as points, and the outputs as vectors. For example, is the vector (-1,2):

In[264]:=

Out[264]=

To draw a picture of the vector field, we simply draw the vector (-1,2) -- the output -- beginning at the point (2,1) -- the input. In other words, we draw a vector which goes from (2,1) to (1,3). (Check my arithmetic here!)

Here are a whole bunch of values of the vector field:

In[265]:=

Out[265]=

Out[266]=

Out[267]=

Out[268]=

Out[269]=

Out[270]=

Out[271]=

Out[272]=

Out[273]=

And here's a picture where we've drawn in each of these vectors, starting at their respective points. (You don't have to understand this command, although if you look at it long enough you can probably kind of figure out what's going on.)

In[274]:=

While that's somewhat interesting, it's awfully tedious, and it would take forever to tell what the "big picture" is. Fortunately Mathematica includes a command to graph vector fields for you. Here's a picture of

In[275]:=

Out[275]=

Wow! Imagine having to draw each of those arrows by hand. This is definitely a case where a computer can make your life easier, BUT... you need to be careful, because Mathematica is hiding some of the details from you. Look above at the picture we constructed "manually." At the point (1,1) you can see the vector (-1,1), which has length . But if you look at the computer generated picture, the vector starting at (1,1) is so short that you can only see the arrow head! Mathematica is scaling the vectors, trying to make the picture easier to look at; while that's a noble goal, it means you do lose some information -- namely, exactly how long those vectors are.

Evaluate the next cell to show the vectors at full length. We' added the ScaleFactor->None option to turn off the scaling.

In[276]:=

Out[276]=

That's a much different picture! So now you can see that Mathematica was really distorting your view of what's going on. However, you can also see what the designers of Mathematica were thinking; if you show the whole vectors, it can get very crowded. You can use the ScaleFactor option to fiddle around with a picture to suit your own tastes. You've already seen what setting this option to None can do; if you set it to a number instead, then Mathematica will rescale the longest vector in the picture to have that length. For this particular vector field (with this range of x and y) it seems like scaling the vectors to have length 2 or less is a nice compromise.

In[277]:=

Out[277]=

Why Vector Fields are Useful

The last picture looks like it represents some sort of circular motion, and that's a clue about why vector fields are important. Very often we'll think of vector fields as representing fluid flow, in which case this vector field would represent some kind of whirlpool or drain.

Sometimes we'll also think of a vector field as a force field; in other words, each vector represents some kind of force, whether from an explosion, or gravity, etc.

Here are a few examples of vector fields for you took look at. Look at the functions and try to understand why the picture looks the way it does. Also play around with the ScaleFactor option to see how it affects the pictures. Which works better, the Automatic or None setting?

The "Blowup" or "Super Nova" Vector Field

In[278]:=

Out[278]=

Out[279]=

The "Black Hole" Vector Field

Can you explain what happens when you set ScaleFactor to None here?

In[280]:=

Out[280]=

Out[281]=

The "Important" Vector Field

This vector field looks a lot like the very first example we worked with. See if you can tell how it's different. This particular vector field has some very unique characteristics, which you'll learn about in lecture. That's why we called it "important" here; it also has a tendency to show up in textbooks and on exams.

In[282]:=

Out[282]=

Out[283]=

The "Curving River"

In[284]:=

Out[284]=

Out[285]=

Vector Fields in 3D

Many -- maybe most -- of the vector fields you'll work with this semester are three-dimensional. That means we have a function instead of . Everything else you've learned goes the same way: the three inputs are the (x,y,z) coordinates of a point, and the output is a three dimensional vector which you draw beginning at the input point (x,y,z).

Mathematica can draw 3D vector fields, but you'll quickly see why we tend to concentrate on 2D examples when we're talking about these ideas. It just gets too messy when you look at a three dimensional picture. Look at this example of a 3D vector field, which is almost the same as the first 2D vector field we considered above:

In[286]:=

Out[286]=

Out[287]=

Mathematica is trying valiantly to keep things from getting too messy. It's scaling the vectors, as before, and it's also removed the arrows at the end of the vectors. While the missing arrows make things a little less cluttered, it's a big loss of information because you have no way of knowing which directions things are heading in.

If you'd like to put the arrows back in, you can use the VectorHeads option. We've done that in the next command here, along with turning the scaling off.

In[288]:=

Out[288]=

Out[289]=

Now you can see why, although we work with 3D vector fields all the time, we very rarely draw pictures of them!

Gravity

One 3D vector field that can be drawn fairly easily is the gravitational force field on the surface of the earth. Of course, the only reason we can draw it is because it's such a simple force field. Look at the function and see if you can guess what the picture will look like before evaluating the cell.

In[290]:=

Out[290]=

Out[291]=

The Gradient Revisited; Tying it all Together

Now let's combine the two previous sections. If we have a function , we can take its gradient, which is a function . Read that again -- it's tricky. f takes two inputs, and its gradient takes the same two inputs, and gives you two outputs back -- namely, the two partial derivatives.

But wait! If the gradient is a function , then we can think of the gradient of a function as a vector field! And thus the idea of a "gradient vector field" is born. You'll run across that term fairly often. It just means a vector field which happens to be the gradient of some (possibly unknown!) function.

In[292]:=

Out[292]=

Out[293]=

Out[294]=

Gradient vector fields are important because they have a property called "path independence," which you'll learn about later in this course. For today we'll concentrate on a different idea, the fact that gradients are perpendicular (or "orthogonal," or "normal," all of which mean the same thing) to level sets of a function. This will help us tie a few loose ends with the directional derivative, too.

Remember that, for a function , a level set is the set of all points (x,y) such that , where c is some constant number you've already chosen. For example, with our function that we just used,

is the same as y=1/x, which is a hyperbola,

is the same as y=2/x, which is a hyperbola,

is the same as y=3/x, which is a hyperbola,

and so on. Essentially we're finding all of the points where the height of the surface is equal to 1 (or 2, or 3). We could plot these together:

In[295]:=

Out[295]=

But wait! If we're looking for points where the height has a certain values, then we're really looking for contour lines. Remember ContourPlot? It can generate a nice picture of this function. (Go look at Lab 1B if you don't remember this command.) It shows us the various "elevation lines" of the graph, including those for negative elevation.

In[296]:=

Out[296]=

Now let's look at the contour plot and the gradient field together. There's an extra option here to make the arrows blue in the picture of the vector field; you don't need to worry about figuring that out. Also, the DisplayFunction stuff prevents Mathematica from drawing a picture of the vector field until we show both pictures together.

In[297]:=

Notice that the blue vectors in the picture are perpendicular to the level curves (i.e. the contour lines). This is no accident! In fact, it's always true that

If the point (x,y) is on the level curve , then will be perpendicular to the level curve.

(More technically, if you draw the tangent line which touches the curve at the point (x,y), then the gradient vector at that point will be perpendicular to the tangent line.)

The same is true in any dimension. If you have a function , then the level sets will be level surfaces, and the gradient will be perpendicular to the level surfaces. (That is, if is on some level surface, then the gradient will be perpendicular -- or "normal" -- to the tangent plane which touches the surface at (x,y,z).

You won't be asked to use this fact about gradients and level sets for the rest of this lab, but it will be very important in next week's lab.

There's a little more to be said about the picture above. Remember that ContourPlot shades the low areas with dark colors, and the high areas with bright colors. Notice how the gradient vectors always seem to point straight uphill. Once again, that's no accident. Remember,

and if we combine that with the definition of a dot product, along with the fact that is a unit vector (and therefore has length one):

where θ is the angle between the gradient vector and the vector , i.e. the angle between the gradient and the direction that we're going. The largest this can be is when cos(θ)=1, which happens for θ=0. In other words,

"The largest the directional derivative can be is when the angle between the gradient and our direction is 0 -- in other words, when we're looking in the direction of the gradient vector."

Rephrased one more time:

The largest value of the directional derivative is in the direction of the gradient; equivalently, the gradient points in the direction of the largest increase.

Exercise 3

Recall the setup for Exercises 1 and 2:

Suppose there is a spaceship in three-dimensional space, with our sun located at the origin, (0,0,0). The temperature near the sun is extremely hot, of course -- about 6000K -- and it drops rapidly as one moves away. Suppose the temperate at any point is space is given by , where x, y, and z measured in units of 10 million miles. (Here we're assuming the spaceship will never get close enough to other stars to notice their heat, so effectively our sun is the only heat-producer in the universe.)

Let's assume again that the spaceship is located on the Earth, about 93 million miles away, at the point (9,2,1). In what direction is the largest increase in temperature? Why is this not the least bit surprising? In which direction could the spaceship go to remain at the same temperature? (This is much harder than the first question in this paragraph.)

(If you're assigned this problem, it's not worthy of a separate writeup. You should include your answer as an "interesting aside" in your answer to Exercise 1.)

Exercise 4

Assuming the spaceship is at the point (4,-2,8). In which direction should the spacecraft move in order to decrease the temperature as quickly as possible. What direction could the spacecraft go to keep the temperature constant?

If you're assigned this problem, you should include your answers as part of your writeup to Exercise 2, instead of doing a separate problem.

Credits

Created by Mathematica (November 6, 2004)