> restart;

>

Gradients, Directional Derivatives and Rates of Change

Maple Syntax for Vectors and Gradients

Vectors are entered in Maple using the command vector . For example, to define the vector v=2 i +3 j -4 k in three-dimensional space, use the syntax

> v1:=vector([2,3,-4]);

[Maple Math]

>

The output of the vector command looks like a list [2,3,-4]. Mathematically speaking, as long as you remain within a fixed coordinate system, a vector can be identified with a list of its components. Nevertheless, to Maple, a list and a vector are two different objects. In most commands that we are going to use below, you can enter vectors as lists, and Maple will understand what you mean. Later on, however, when we need more advanced commands involving vectors, the distinction between lists and vectors will become important. It is of crucial importance in Maple programming. Should you try to program something yourself, you have to carefully distinguish between vectors and lists. Incidentally, you can ask Maple what it considers a given object to be by using the type command. Let's see what v1 and [2,3,-4] are according to Maple.

> type(v1,vector);

[Maple Math]

> type(v1,list);

[Maple Math]

> type([2,3,-4],list); type([2,3,-4],vector);

[Maple Math]

[Maple Math]

>

As you see, Maple considers v1 to be a vector but not a list, and [2,3,-4] to be a list but not a vector.

Most commands to manipulate vectors are contained in the linalg package ("linalg" for "linear algebra"). Let's load the linalg package. If you are curious about its content, replace the colon by a semicolon on the line below before re-executing it.

> with(linalg):

Warning, new definition for norm

Warning, new definition for trace

>

The warnings tell us that the commands norm and trace have a new meaning after the package is loaded. We shall use only the norm command to calculate the magnitude of a vector. There are several possible definitions of the norm of a vector, each useful in its own right. To obtain the magnitude as we understand it, use the syntax

> norm(v1,2);

[Maple Math]

>

The "2" within the norm command stands for the root of order two in our definition of the magnitude.

Two of the commands contained in the package that are important to us are the dot product and the cross product. Let's define another vector v2 and calculate the dot product and the cross product of v1 and v2.

> v2:=vector([1/2,3,-1]);

[Maple Math]

> dotprod(v1,v2);

[Maple Math]

> crossprod(v1,v2);

[Maple Math]

>

All of the above commands work fine if you enter vectors as lists. For example:

> dotprod([2,0,1],[1,1,3]);

[Maple Math]

>

To add properly defined vectors , or to multiply them by scalars use the command evalm . Don't use this command when dealing with lists . For example:

> evalm(3*v1-(1/2)*v2);

[Maple Math]

>

If you are trying to get away with vectors defined as lists, the syntax for algebraic operations is simpler. For example:

> 3*[1,2,3]-2*[0,1,1];

[Maple Math]

The useful command normalize returns a unit vector with the same direction as a given vector. In other words, divides a nonzero vector by its magnitude. For example:

> normalize(v1);

[Maple Math]

>

Another handy command is angle . The command calculates the angle between two vectors.

> angle(v1,v2); evalf(%);

[Maple Math]

[Maple Math]

>

The value of the angle is returned in radians.

The very important command grad that calculates the gradient of a given function of two or more variables is also contained in the linalg package. It works as follows:

> grad(x^2+y^3,[x,y]);

[Maple Math]

or

> h:=(x,y,z)->exp(2*x)*sin(z*x*y^2);

[Maple Math]

> gradh:=grad(h(x,y,z),[x,y,z]);

[Maple Math]

>

The command grad returns the gradient of a given function as a vector of the appropriate length in terms of independent variables. To extract a component of the gradient vector, use a similar syntax as for lists. For example, to extract the third component of the gradient, use the command

> gradh[3];

[Maple Math]

>

Since [Maple Math] , the third component of the gradient is equal to the partial derivative of h with respect to z. Indeed:

> diff(h(x,y,z),z);

[Maple Math]

>

Gradients of Functions of Two Variables

>

You know that for a given function f, say a function of two variables, f= [Maple Math] , its gradient at a given point (a,b), gradf(a,b) , is a vector pointing in the direction of the greatest rate of change of f at (a,b), and the magnitude of the gradient is equal to that maximum rate of change. You also know that for any unit vector [Maple Math] , the directional derivative of f at (a,b) in the direction u , [Maple Math] , represents the rate of change of f in the direction u , and is given as the dot product of the vector u with the gradient gradf(a,b), that is:

[Maple Math] = [Maple Math] .

>

Because the gradient represents the direction of the greatest rate of change, gradient vectors gradf(x,y) are perpendicular to the contours of f. Calculating gradients, directional derivatives, as well as illustrating the perpendicularity of gradients to contours can be easily done in Maple.

>

Example 1. A square flat metal plate, 2 by 2 meters, is positioned on the xy-plane in such a way that the x and y coordinates of points on the plate are between 0 and 2. The plate is heated so that the temperature of each point (x,y) on the plate, in degrees Fahrenheit, is given by

[Maple Math] .

(a) Plot the 3d-graph of the function T to have some idea how the temperature distribution in the plate looks.

>

We define the function T in Maple.

> T:=(x,y)->10*sin((x-1)*(y-1))+100;

[Maple Math]

Below, we plot the graph T=T(x,y) in the (x,y,T)-space. By rotating the graph, we have found an orientation that gives a good view of the graph. We have locked the orientation in on the command line. Still, you should rotate the graph yourself to get a good idea of its shape.

> plot3d(T(x,y),x=0..2,y=0..2,axes=framed,orientation=[15,80]);

[Maple Plot]

>

We see that the temperature is the highest at the vertices (2,2), (0,0), the lowest at (2,0), (0,2).

(b) Plot the contour diagram and the field of gradients for the temperature distribution T(x,y) in one coordinate system. What do you observe?

To plot the contour diagram, we need to load the package plots that contains the command contourplot , as well as the commands fieldplot and display that we use below.

> with(plots):

>

Now we shall create the contour diagram of T(x,y) throughout the plate. We shall label the diagram c, and end the command with a colon as we don't want the graph displayed at this point. Observe that we use the entry contours=15 under the contourplot command. The entry increases the number of contours shown. The default setting of 8 contours often gives graphs that are not clear enough.

> c:=contourplot(T(x,y),x=0..2,y=0..2,contours=15,filled=true,coloring=[white,yellow],scaling=constrained):

>

The next step is to create a plot of the field of gradients for the function T(x,y). This can be accomplished in several ways. For example, you could use the command gradplot that plots the field of gradients without actually calculuating them. Since we are going to need the explicit form for the gradient of T(x,y) below, we shall calculate the gradient, and use the fieldplot command. The command plots any given field of vectors.

> gradT:=grad(T(x,y),[x,y]);

[Maple Math]

>

Maple has calculated the gradient gradT at any point (x,y). Now we shall create a plot of the field of gradients and label it g. We do not want to display it at this point, hence, we end the command with a colon.

> g:=fieldplot(gradT,x=0..2,y=0..2,scaling=constrained,arrows=thick):

>

Note. If you want the field of gradients or the contourplot above displayed separately, replace colons with semicolons, and remove the labels c and g. If you do not remove the labels, you will see a screen full of gibberish rather than a plot!

We now display both the field of gradients and the contour diagram in one coordinate system.

> display([c,g]);

[Maple Plot]

>

What do we observe? The gradients are perpendicular to the contours. They point to the direction of increase of the function T(x,y). (Remember that darker shades of yellow correspond to higher temperatures in our example.) We also notice that longer gradient vectors correspond to contours being closer together, shorter to contours being farther apart. This is not surprising as contours close to each other signify the fast change in a function and hence a large magnitude of the gradient.

Note: In order to see perpendicularity of gradient vectors and contours you have to use scaling=constrained option!

Note. The gradient arrows displayed by Maple are scaled in length. The length of each arrow represents a relative magnitude of the gradient and not the real magnitude corresponding to units in a given coordinate system. The scaling is necessary in order to obtain a picture that shows the qualitative behavior of the gradient field rather than a bunch of long vectors, scrambled together.

>

(c) Find the temperature at the point p=(.5,.5), that is, at the point x=.5, y=.5. Find the gradient gradT(.5,.5) and its magnitude at the point p. What do the gradient and its magnitude represent?

We have calculated gradT at any point (x,y) above. All we need to do is to substitute our point p into the expression for the gradient. Because gradT is a vector, we have to apply the subs and evalf commands to each component separately. Since we want our answer to be a vector, we use the command vector . We denote gradT(.5,.5) as gradTp.

> gradTp:=vector([evalf(subs(x=0.5,y=0.5,gradT[1])),evalf(subs(x=0.5,y=0.5,gradT[2]))]);

[Maple Math]

>

The vector gradTp shows the direction from the point p in which the function increases fastest. How fast does the function increase in this direction? This is given by the magnitute of the vector.

> norm(gradTp,2);

[Maple Math]

>

If you move from the point (.5,.5) in the direction of the vector gradTp, the temperature increases at the instantaneous rate of approximately [Maple Math] F/m. The temperature at the point p is:

> T(0.5,0.5);

[Maple Math]

>

degrees Fahrenheit.

>

(d) Plot the contour diagram and the field of gradients for the function T(x,y) in a small region around the point p=(.5,.5). What do you observe?

Again, we shall create and label the two plots, and then display them in one coordinate system.

> c1:=contourplot(T(x,y),x=0.4..0.6,y=0.4..0.6,scaling=constrained,color=blue):

> g1:=fieldplot(gradT,x=0.4..0.6,y=0.4..0.6,scaling=constrained,arrows=thick):

> display([c1,g1]);

[Maple Plot]

>

We observe that in the small square of the size .2 by .2 around the point p=(.5,.5), the contours are almost parallel, equally spaced lines, with gradient vectors clearly perpendicular to them. That is because locally, near p, the fraph of the function T(x,y) is almost planar; it almost coincides with its tangent plane at that point.

>

(f) Heat Flow. The flow of heat in a temperature field takes place in the direction of maximum decrease of temperature. Physical experiments show that the rate of flow v is proportional to the gradient of the temperature, v=-KgradT, where K is a positive constant representing the thermal conductivity of the body. For the plate in Example 1, plot the field of vectors that qualitatively describes heat flow in the plate.

The direction of heat flow at any point of the plate (x,y) coincides with the direction of maximum decrease in temperature, that is, with the direction -gradT. The rate is proportional to that vector. Since the lengths of vectors displayed by the fieldplot command is scaled anyway, we don't have to worry about the constant K. We shall obtain a good picture describing heat flow in the plate by simply plottting the field of vectors -gradT.

> fieldplot(evalm(-gradT),x=0..2,y=0..2,arrows=thick,color=red,scaling=constrained);

[Maple Plot]

>

We see heat is flowing from the warm edges of the plate (2,2) and (0,0) toward the cooler parts.

>

Gradients of Functions of Three Variables

The formulas for the gradient and directional derivatives, as well as Maple syntax for calculating them, are very similar as in the case of two variables. Visualization of the concepts is, of course, much more difficult. We can plot fields of gradients for functions of three variables and plot them in the 3d-space together with a couple of level surfaces. However, because of our optical limitations, it is only in simple cases that we can actually see the perpendicularity of gradients to level surfaces.

Example 2. Plot a level surface and the field of gradients for the function

[Maple Math] .

> F:=(x,y,z)->2*x^2+y^2+z^2;

[Maple Math]

> gradF:=grad(F(x,y,z),[x,y,z]);

[Maple Math]

> G:=fieldplot3d(gradF,x=-1..1,y=-1..1,z=-1..1,scaling=constrained,axes=framed,color=red):

> L:=implicitplot3d(F(x,y,z)=1,x=-1..1,y=-1..1,z=-1..1,axes=framed,scaling=constrained):

> display([G,L]);

[Maple Plot]

>

Rotate the above graph. Can you see the perpendicularity of the gradients to the level surface?

The gradient of a function of three variables has the same interpretation in terms of rates of change as in the case of two variables.

>

Example 3. Suppose that a large rectangular tank of water occupies the region 0<x<4, 0<y<6, 0<z<2 (all measurements in meters). A block of salt is placed at the center of the bottom of the tank, and begins to dissolve and diffuse throughout the tank. Under appropriate conditions and at a certain fixed time, the salt concentration C(x,y,z) at any given point in the tank can be approximated by

[Maple Math]

grams per liter.

(a) Find and interpret the gradient of C at the point p=(0,5,1), gradC(0,5,1).

>

We first define the function C in Maple and then obtain its gradient.

> C:=(x,y,z)->10*exp(-(sqrt((x-2)^2+(y-3)^2+z^2)/10));

[Maple Math]

> gradC:=grad(C(x,y,z),[x,y,z]);

[Maple Math]
[Maple Math]

>

The formula for the gradient isn't very inviting. All we need to do, however, is to evaluate the gradient at the point p= (0,5,1). Let's label the result gradCp. Since we have three coordinates into which we have to substitute x=0, y=5, z=1, we shall use the command seq .

> gradCp:=vector([seq(evalf(subs(x=0,y=5,z=1,gradC[i])),i=1..3)]);

[Maple Math]

>

The vector gradCp gives the direction from (0,5,1) in which the rate of increase of the concentration of salt is largest. Physically, we would expect the direction to be toward the lump of salt. Is it so? The coordinates of the lump are (2,3,0). (Remember, the salt is located in the middle of the bottom of the tank.) Let's look at the displacement vector from (0,5,1) to (2,3,0).

> d:=vector([2,3,0]-[0,5,1]);

[Maple Math]

>

Are gradCp and d parallel? One way to check is to calculate the angle between gradCp and d.

> angle(gradCp,d); evalf(%);

[Maple Math]

[Maple Math]

>

They are, indeed, parallel. Thus, the concentration increases fastest when we move in the direction of the lump of salt. What is this maximal rate of increase in concentration? This is given, of course, by the magnitude of gradCp.

> norm(gradCp,2);

[Maple Math]

>

The concentration is increasing at the rate of, approximately, .741 g/l per meter.

(b) Find the rate of change in the concentration if we begin to move from (0,5,1) in the direction toward (2,6,1).

Note that the movement is in the direction of the displacement vector from (0,5,1) to (2,6,1), that is, [2,1,0]. We calculate the corresponding unit vector u in the direction of [2,1,0]. The dot product of u with the gradient gradCp gives us the desired rate of change.

> u:=normalize([2,1,0]);

[Maple Math]

> dotprod(gradCp,u); evalf(%);

[Maple Math]

[Maple Math]

>

When we move in the direction [2,1,0] from the point (0,5,1), the concentration increases at the rate of, approximately, .221 g/l per meter.

>

Homework Problems

Problem 1.

(a) Consider the function [Maple Math] . Display both its contour diagram and its field of gradients on the same plot. Comment on the direction in which you should move from a point (x,y) to obtain the maximum and minimum rates of change of f.

(b) Looking back at part (a) and remembering graphs and contour diagrams of functions [Maple Math] and [Maple Math] that you have seen before, what can you say about contour diagrams and gradient fields of functions of the special form [Maple Math] , where [Maple Math] ? What characteristics of [Maple Math] will determine the directions that give maximum and minimum rates of change and magnitudes of those rates?

>

Problem 2. The display below shows both the contour diagram and the gradient field for a certain function H=H(x,y). (Contours are shown in red). Find a formula for a function whose corresponding display is identical or very similar.

[Maple Plot]

>

Problem 3. A room occupies the region 0<x<15, 0<y<20, 0<z<8 (all distances measured in feet). Its only light source is a single bulb in the corner (15,20,8). As a result, the light intensity at any given point (x,y,z) in the rooom is given approximately by

[Maple Math] ,

where K is a constant which, for convenience, we may take as having numerical value equal to one, K=1.

(a) If a light intensity meter is located at (5,5,5) and is moved in the direction toward (6,6,5), what is the rate of change of the measured intensity?

(b) From (5,5,5), in what direction should it be moved to give the maximum rate of increase of its reading? Explain why your latter answer may match up with intuition in the setting of this problem.

>

Problem 4. In a room that occupies the region 0<x<20, 0<y<30, 0<z<10 (all measurements in feet), a temporary source of heat is located at the floor center (10,15,0). Ignoring all effects of moving air and taking into account heat being lost from the room's various surfaces, it turns out that a rough approximation of the temperature, in degrees Fahrenheit, at a given point (x,y,z) in the room at time t minutes after observations begin, is given by

[Maple Math] .

(a) Find the gradient of U in the four variables x,y,z,t at x=15, y=20, z=5, t=10.

(b) The gradient in (a) gives the "direction of motion" in the xyzt-space that maximizes the rate of increase of U. Comment on the relevance of your answer in (a) in terms of a real object actually moving within the room towards a warmer location.

(c) If we fix t=10 and do not allow it to change, what direction of motion will give the largest rate of decrease of U? Comment on how this answer may match your intuition for this problem setting.

>

This MTH 243 Maple Worksheet written by D. Abrahamson, B. Kaskosz, and L. Pakula, Copyright 1999.

>