Sec13.5VisualGradients.mws

Visualizing Directional Derivatives and Gradients

Worksheet by Mike May, S.J. - maymk@slu.edu

As we have been looking at directional dericatives and gradients, it seems worthwhile to look at a Maple visualization of everything we have been doing.

First want to load the appropriate commands. We will need commands from the packages linalg, plottools, and plots. (Restart cleans up in case we were already running a Maple worksheet.)

> restart: with(linalg): with(plottools): with(plots):

Warning, the protected names norm and trace have been redefined and unprotected

Warning, the name changecoords has been redefined

Directional derivatives:

For preliminaries we want to define the function, the point in the x-y plane we are evaluating at and the direction vector we are interested in taking a derivative in..

> f := (x,y) -> x^2 + 2*y^2;
xval :=2: yval:= 3:
dirvec := [3, 4]:

f := proc (x, y) options operator, arrow; x^2+2*y^2...

Next we would like to compute a unit vector in the desired direction and the z-coordinate of the point of interest.

> uvec := convert(
evalm(dirvec/sqrt(dotprod(dirvec,dirvec))), list);
zval := f(xval, yval);

uvec := [3/5, 4/5]

zval := 22

To visualize what we are doing, we look at the surface together with a plane containing the direction vector.

> surf:= plot3d(f(x,y), x=xval-2..xval+2, y=yval-2..yval+2,
axes=boxed, color=red, style = patchnogrid):
pathc :=spacecurve(
[xval+t*uvec[1], yval+t*uvec[2], f(xval+t*uvec[1], yval+t*uvec[2])],
t=-2..2, color=blue, thickness=2):
cutplane := polygon([[xval+2*uvec[1], yval+2*uvec[2],zval+5],
[xval+2*uvec[1], xval+2*uvec[2],zval-5], [xval-2*uvec[1],
yval-2*uvec[2],zval-5], [xval-2*uvec[1], yval-2*uvec[2],zval+5]],
color=yellow):
display3d({cutplane, surf, pathc});

[Maple Plot]

The surface is in red, the plane that cuts the cross section in the desired direction is in yellow, and the curve in the cross section is in blue.

We can get a better view if we look at the function as a function of one variable in the cutting plane.

> plot(f(xval+t*uvec[1],yval+t*uvec[2]), t=-2..2);

[Maple Plot]

We can compute the directional derivative from the definition.

> dderiv := limit((f(xval+t*uvec[1], yval+t*uvec[2]) - f(xval, yval))/t, t=0);

dderiv := 12

This can be used to plot a tangent line to the curve.

> plot({f(xval+t*uvec[1],yval+t*uvec[2]), zval+dderiv*t}, t=-2..2);

[Maple Plot]

We can also put this line back on the 3D graph of the surface.

> tanline :=spacecurve([xval+t*uvec[1], yval+t*uvec[2], zval+t*dderiv],
t=-2..2, color=green, thickness=3):
display3d({cutplane, surf, pathc, tanline});

[Maple Plot]

>

For convenience sake we can put the stuff together into one block of code.

> f := (x,y) -> x^2 + 2*y^2;
xval :=2: yval:= 3:
dirvec := [3, 4]:
uvec := convert(
evalm(dirvec/sqrt(dotprod(dirvec,dirvec))),list);
zval := f(xval,yval);
dderiv := limit((f(xval+t*uvec[1], yval+t*uvec[2]) - f(xval, yval))/t, t=0);
plot({f(xval+t*uvec[1],yval+t*uvec[2]), zval+dderiv*t}, t=-2..2);
surf:= plot3d(f(x,y), x=xval-2..xval+2, y=yval-2..yval+2, axes=boxed,
color=red, style = patchnogrid):
pathc :=spacecurve([xval+t*uvec[1], yval+t*uvec[2], f(xval+t*uvec[1],
yval+t*uvec[2])], t=-2..2, color=blue, thickness=2):
cutplane := polygon([[xval+2*uvec[1], yval+2*uvec[2],zval+5],
[xval+2*uvec[1], yval+2*uvec[2],zval-5],
[xval-2*uvec[1], yval-2*uvec[2],zval-5],
[xval-2*uvec[1], yval-2*uvec[2],zval+5]], color=yellow):
tanline :=spacecurve([xval+t*uvec[1], yval+t*uvec[2], zval+t*dderiv],
t=-2..2, color=green, thickness=3):
display3d({cutplane, surf, pathc, tanline});

f := proc (x, y) options operator, arrow; x^2+2*y^2...

uvec := [3/5, 4/5]

zval := 22

dderiv := 12

[Maple Plot]

[Maple Plot]

>

Exercise:.

1) Let g(x,y) = sin(x^2+y^2) . Modify the code above to plot the graph of g(x,y) in a region around the point (3, 4) together with a line tangent in the direction (5, 12).

>

A second look at directional derivatives

The alternate way to produce the directional derivative is to compute the partial derivatives, evaluate them at our point, and then take the sum of the partials times the appropriate component of the unit direction vector.

> f := (x,y) -> x^2 + 2*y^2:
xval :=2: yval:= 3:
dirvec := [3, 4]:
fx := diff(f(x,y),x);
xslope := subs({x=xval, y=yval}, fx);
fy := diff(f(x,y), y);
yslope := subs({x=xval, y=yval}, fy);
dirderiv := xslope*uvec[1]+yslope*uvec[2];

fx := 2*x

xslope := 4

fy := 4*y

yslope := 12

dirderiv := 12

Notice that we do get the same answer for the directional derivative that we got with the formal definition above.

This second method is more roundabout, but it has the advantage of being easier to do when we don't have Maple handy. In the second method we only have to take limits for two cross sections.

>

Exercise:.

2) Let g(x,y) = sin(x^2+y^2) . Find the directional derivative of g(x,y) at the point (3, 4) in the direction (5, 12) by the method that uses the partials at the point.

>

Comparing the two methods of finding directionalderivatives

It is worthwhile varifying that the two methods of computing derectional derivative give the same answer for any direction.

(To be more precise, that these two methods with produce the same answer in every direction is the definition of the function being differentiable at a point.)

We recall computations made above to get the x-slope and y-slope at a point.

> f := (x,y) -> x^2 + 2*y^2:
xval :=2: yval:= 3:
dirvec := [3, 4]:
zval := f(xval,yval);
fx := diff(f(x,y),x):
xslope := subs({x=xval, y=yval},fx):
fy := diff(f(x,y), y):
yslope := subs({x=xval, y=yval}, fy):

zval := 22

We recall that a unit vector in direction theta has coordinates (cos(theta), sin(theta)). We then find a directional derivative in direction theta by using the definition of directional derivative.

> ddera := theta -> limit((f(xval+t*cos(theta), yval+t*sin(theta))-f(xval, yval))/t, t=0);

ddera := proc (theta) options operator, arrow; limi...

We now compute the directioanl derivative by the method that uses x-slope and y-slope.

> dderb := theta -> cos(theta)*xslope + sin(theta)*yslope;

dderb := proc (theta) options operator, arrow; cos(...

It is now a simple matter to graphically compare the values obtained by the two methods for all angles theta.

> plot({ddera(theta)- dderb(theta)}, theta=0..2*Pi, axes=boxed);

[Maple Plot]

If we plot the ring of points (a+cos(theta), b+sin(theta), f(a,b)+ddera(theta)) we get a ring in the tangent plane.

> surf:= plot3d(f(x,y), x=xval-2..xval+2, y=yval-2..yval+2,
axes=boxed, color=red, style = patchnogrid):
tanplane:= plot3d(zval+xslope*(x-xval)+yslope*(y-yval), x=xval-2..xval+2, y=yval-2..yval+2,
axes=boxed, color=yellow, style = patchnogrid):
tanring :=spacecurve([xval+cos(theta), yval+sin(theta), f(xval,yval)+ddera(theta)],
theta=0..2*Pi, color=blue, thickness=2):
display3d({surf, tanplane, tanring});

[Maple Plot]

>

Exercise:.

3) Let g(x,y) = sin(x^2+y^2) . Show that g(x,y) is differentiable at (3, 4) by showing that the 2 methods of computing directional derivatives give the same answer in all directions.

>

Gradients:

The gradient of a function can be thought of in a number of ways. The easiest description is symbolic. The gradient of f(x,y) is defined to be the vector with components equal to the partial derivatives. In the terminology we have been using, grad(f) = [x-slope, y-slope]. Notice that the gradient has as many components as the input vector, rather than the number of coordiantes in a point in the graph.

> f := (x,y) -> x^2 + 2*y^2;
xval :=2: yval:= 3:
fx := diff(f(x,y),x);
xslope := subs({x=xval, y=yval},fx);
fy := diff(f(x,y), y);
yslope := subs({x=xval, y=yval}, fy);
gradient := [xslope,yslope];

f := proc (x, y) options operator, arrow; x^2+2*y^2...

fx := 2*x

xslope := 4

fy := 4*y

yslope := 12

gradient := [4, 12]

We are also told that the gradient should be the direction that is perpendicular to the contour of the graph at that point. To see that we want to put the contourplot and the gradient on the same graph.

> cplot := contourplot(f(x,y),x=xval-2..xval+2, y=yval-2..yval+2):
gradv := line([xval,yval], [xval+xslope, yval+yslope], linestyle=1,
thickness=3, color=green):
display({cplot, gradv},scaling=constrained);

[Maple Plot]

>

This relationship can be seen more clearly with the Maple command gradplot that plots a field of scaled gradient vectors.

> gplot := gradplot(f(x,y),x=xval-2..xval+2, y=yval-2..yval+2):
cplot := contourplot(f(x,y),x=xval-2..xval+2, y=yval-2..yval+2):
display({cplot, gplot}, scaling=constrained);

[Maple Plot]

>

Tied into the idea that the gradient is perpendicular to the contours is that it is the direction of steepest ascent.

This means that we can visually check to see if we have computed the gradient correctly. If we take the cross section in the direction of the gradient it should be the direction that is rising most steeply.

> f := (x,y) -> x^2 + 2*y^2;
xval :=2: yval:= 3:
zval := f(xval,yval):
fx := diff(f(x,y),x):
mx := subs({x=xval, y=yval},fx):
fy := diff(f(x,y), y):
my := subs({x=xval, y=yval}, fy):
gradvec := [mx, my];
unitgradient := convert(evalm(gradvec/sqrt(dotprod(gradvec,gradvec))),list);
gradientderiv := mx*unitgradient[1]+my*unitgradient[2];
surf:= plot3d(f(x,y), x=xval-2..xval+2, y=yval-2..yval+2,
axes=boxed, color=red, style = patchnogrid):
gradientpath :=spacecurve([xval+t*unitgradient[1], yval+t*unitgradient[2],
f(xval+t*unitgradient[1], yval+t*unitgradient[2])], t=-2..2, color=blue, thickness=2):
cutplane := polygon([[xval+2*unitgradient[1], yval+2*unitgradient[2],zval+5],
[xval+2*unitgradient[1], yval+2*unitgradient[2],zval-5],
[xval-2*unitgradient[1], yval-2*unitgradient[2],zval-5],
[xval-2*unitgradient[1], yval-2*unitgradient[2],zval+5]], color=yellow):
gradline :=spacecurve(
[xval+t*unitgradient[1], yval+t*unitgradient[2], zval+t*gradientderiv],
t=0..1, color=green, thickness=3):
display3d({cutplane, surf, gradientpath, gradline});

f := proc (x, y) options operator, arrow; x^2+2*y^2...

gradvec := [4, 12]

unitgradient := [1/10*sqrt(10), 3/10*sqrt(10)]

gradientderiv := 4*sqrt(10)

[Maple Plot]

>

>

Exercise:

4a) Let g(x,y) = x^2+2*x+3*y^2-4*y . Estimate the gradient of g(x,y) at (3, 4) by plotting a contouplot and finding the direction perpendicular to the contours.

>

4b) Estimate the gradient of g(x,y) at (3, 4) by plotting the surface for a region around (3,4) and estimating the direction of steepest increase.

>

4c) Calculate the gradient of g(x,y) at (3, 4) symbolically.

>

>