Laboratory 5
Partial Derivatives and Directional Derivatives

Introduction

In this lab we examine the notion of the partial derivatives of a function. We also use partial derivatives to calculate the equations of tangent planes to graphs of functions of two variables and to illustrate the notion of differentiability of a function of two variables.

5.1 A Graphical Introduction to Partial Derivatives

Let us consider a function f(x,y), say

f[x_,y_]:=4-x+y-(x^2)-(y^2)

We obtain the graph of this function on the region -3≤x≤3, -3≤y≤3 in the usual fashion.

Plot3D[f[x,y],{x,-3,3},{y,-3,3}]

Now suppose that we choose a specific value for y, say y=0. We can then look at the function of one variable x->f(x,0). The graph of this function is the intersection of the three-dimensional graph and the plane y=0. To obtain these graphs, we have defined a new command called ySlice.

ySlice[f_,ys_,{x,x0_,x1_},{y,y0_,y1_}]:=Module[{g,p1,p2,p3,p4,dz,z0,z1},
g[x_,y_]=f[x,y];
z1=Maximize[{g[x,y],x0≤x≤x1,y0≤y≤y1},{x,y}][[1]];
z0=Minimize[{g[x,y],x0≤x≤x1,y0≤y≤y1},{x,y}][[1]];
dz=10^Floor[Log10[z1-z0]]; z0=z0-0.5dz;z1=z1+0.5dz;
p1=Plot3D[g[x,y],{x,x0,x1},{y,y0,y1},PlotRange->{z0,z1},
AxesLabel->{"x","y","z"},ClippingStyle->None];
p2=ParametricPlot3D[{x,ys,z},{x,x0,x1},{z,z0,z1},
PlotStyle->Opacity[.75],Mesh->None];
p3=Show[p1,p2,PlotRange->{{x0,x1},{y0,y1},{z0,z1}}];
p4=Plot[g[x,ys],{x,x0,x1},PlotRange->{z0,z1},
Axes->False,FrameLabel->{"x","z"},Frame->True,PlotStyle->Blue];
Rasterize[Show[GraphicsRow[{p3,p4}],ImageSize->600]]]

This command takes four inputs. The first input is the name of the function we wish to graph, the second input is the fixed value of y we want to investigate: in our example y=0. The remaining parameters are the x- and y-ranges of the plot. We now apply this command to the previous function f.

ySlice[f,0,{x,-3,3},{y,-3,3}]

On the left we have the three-dimensional graph of f(x,y) together with the plane y=0. On the right, we have the graph of the function z=f(x,0). Looking at the function on the right, we might ask what is the derivative of this function that corresponds to a single value of x, say x=0. To estimate the slope of this function from the graph at that point, we can zoom in at that point. To do so, we again call the function ySlice, changing repeatedly the range of values for x and y as follows.

ySlice[f,0,{x,-0.5,0.5},{y,-0.5,0.5}]

We notice that the two-dimensional curve appears somewhat “flatter” and thus it is easier to estimate its slope near x=0. We would like to zoom further in, perhaps several times, and to do so we combine the command ySlice with Mathematica’s dynamic command Manipulate. The code below allows us to view the output of 
	ySlice[f, 0, {x,-h,h}, {y,-k,k}]
for any choice of h in the range 0.001≤h≤0.5 and k in the range 0.001≤k≤0.5, starting with (h,k)=(0.5,0.5).

Manipulate[ySlice[f,0,{x,-h,h},{y,-k,k}],{{h,0.5},0.001,0.5},{{k,0.5},0.001,0.5}]

When we sellect values of h and k near 0, we see that the two-dimenstional curve appears to be a short line segment and we can easily estimate its slope to be approximately -1. We can then estimate the slope of the function z=f(x,0) at the point x=0 also to be about -1. Thus, we say that the partial derivative of f with respect to x at the point x=0, y=0 is approximately -1:
	((∂f)/(∂x))(0,0)=(Subscript[f, x])(0,0)≈-1.

Suppose that instead we want to find the partial derivative of f with respect to x at the point x=-0.5, y=-1. We first draw the graph of the function along the slice y=-1 as before.

ySlice[f,-1,{x,-3,3},{y,-3,3}]

We then zoom in repeatedly near the point x=-0.5, y=-1.

Manipulate[ySlice[f,-1,{x,-0.5-h,-0.5+h},{y,-1-k,-1+k}],{{h,0.5},0.001,0.5},{{k,0.5},0.001,0.5}]

After some experimentation, we conclude that the partial derivative of f with respect to x at the point x=-0.5, y=-1 is approximately zero.

Now we might ask if we can go through the same sort of process, but holding the x-variable fixed and letting the y-variable vary. To do just that, we have the following modification of the command ySlice, which we call xSlice.

xSlice[f_,xs_,{x,x0_,x1_},{y,y0_,y1_}]:=Module[{g,p1,p2,p3,p4,dz,z0,z1},
g[x_,y_]=f[x,y];
z1=Maximize[{g[x,y],x0≤x≤x1,y0≤y≤y1},{x,y}][[1]];
z0=Minimize[{g[x,y],x0≤x≤x1,y0≤y≤y1},{x,y}][[1]];
dz=10^Floor[Log10[z1-z0]]; z0=z0-0.5dz;z1=z1+0.5dz;
p1=Plot3D[g[x,y],{x,x0,x1},{y,y0,y1},PlotRange->{z0,z1},
AxesLabel->{"x","y","z"},ClippingStyle->None];
p2=ParametricPlot3D[{xs,y,z},{y,y0,y1},{z,z0,z1},
PlotStyle->Opacity[.75],Mesh->None];
p3=Show[p1,p2,PlotRange->{{x0,x1},{y0,y1},{z0,z1}}];
p4=Plot[g[xs,y],{y,y0,y1},PlotRange->{z0,z1},
Axes->False,FrameLabel->{"y","z"},Frame->True,PlotStyle->Blue];
Rasterize[Show[GraphicsRow[{p3,p4}],ImageSize->600]]]

It is called in the same fashion as ySlice but now it takes as input an x-value, and returns the corresponding x-slice.

xSlice[f,0,{x,-3,3},{y,-3,3}]

On the left is a plot of the function z=f(x,y) together with the plane x=0. On the right is a plot of the function z=f(0,y). Since this is a function of one variable, we can ask what the derivative of this function is at some point, say y=1. To find this, we can zoom in as we did before near the point (0,1).

Manipulate[xSlice[f,0,{x,-h,h},{y,1-k,1+k}],{{h,0.5},0.001,0.5},{{k,0.5},0.001,0.5}]

From the two-dimensional graphs on the right, we conclude that the slope of the function z=f(0,y) at the point y=1 is approximately -1. Thus,
	((∂f)/(∂y))(0,1)=(Subscript[f, y])(0,1)≈-1.

5.2 Tangent Planes

One of the things that you may have noticed in Section 4.1 is that, as we zoomed in to a function more and more closely, the graph of the function looked more and more like a plane. This is similar to our experience with single-variable functions:  under repeated zooming, the graph of a differentiable function looks more and more like a straight line. We remember from Calculus I that this line is in fact the tangent line to the graph of the function. For the same reason, we say that the plane that we approach under repeated zooming is the tangent plane to the graph of our function of two variables. 

We can use the partial derivatives of a function to find the equation of its tangent plane. Indeed, given a function f(x,y), then the tangent plane to this function at the point (Subscript[z, 0])=f((Subscript[x, 0]),(Subscript[y, 0])) is the plane
	z-(Subscript[z, 0])=[((∂f)/(∂x))((Subscript[x, 0,])(Subscript[y, 0]))](x-(Subscript[x, 0]))+[((∂f)/(∂y))((Subscript[x, 0,])(Subscript[y, 0]))](y-(Subscript[y, 0])).
	
Let us look once again at the function

f[x_,y_]:=4-x+y-(x^2)-(y^2)

and suppose that we want to find the tangent plane to the graph of this function at the point (2,1). We begin by calculating (Subscript[f, x])(2,1) and (Subscript[f, y])(2,1).

fx=D[f[x,1],x] /. x->2
fy=D[f[2,y],y] /. y->1

We then calculate (Subscript[z, 0])=f((Subscript[x, 0]),(Subscript[y, 0])).

f[2,1]

Finally, combining these data, we find that the tangent plane is z=-2-5(x-2)-(y-1). We can verify that this is the case by plotting both the function and this plane on the same plot. In order to see clearer that the two surfaces are tangent at (2,1), we use the option PlotStyle->Opacity[0.5] when plotting the plane; we also use the option Mesh->{{2},{1}} to change the appearance of the plot meshes from the default grids to the grids consisting only of the lines x=2 and y=1

Plot3D[{f[x,y],-2-5(x-2)-(y-1)},{x,0,3},{y,0,3},PlotStyle->{{},Opacity[0.5]},Mesh->{{2},{1}}]

Let us examine what happens to the function and its tangent plane as we zoom in towards the point at which we found the tangent plane; in this case towards the point (2,1).

Plot3D[{f[x,y],-2-5(x-2)-(y-1)},{x,1.8,2.2},{y,0.8,1.2},PlotStyle->{{},Opacity[0.5]},Mesh->{{2},{1}}]

We see that the tangent plane is looking more and more like the graph of the original function.

5.3  Directional Derivatives

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

Clear[f]
f[x_,y_]:=2 x (y^2)-(x^3)-(y^4)
Plot3D[f[x,y],{x,-1,1},{y,-1,1}]

We know from our study of partial derivatives that if we hold x constant, say x=(Subscript[x, 0]), and consider the function y->f((Subscript[x, 0]),y), then the derivative of this function at the point y=(Subscript[y, 0]) is the partial derivative ((∂f)/(∂y))((Subscript[x, 0,])(Subscript[y, 0])). We can interpret this as the derivative of the function f(x,y) at the point ((Subscript[x, 0]),(Subscript[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))((Subscript[x, 0,])(Subscript[y, 0])).

Now suppose that we want to find the derivative of the function f in some other direction, say n=⟨(Subscript[n, 1]),(Subscript[n, 2])⟩. For simplicity, we require that the vector n be a unit vector so that ||n||=(Sqrt[(Subscript[n, 1]^2)+(Subscript[n, 2]^2)])=1. By definition, the directional derivative is the ordinary derivative of the single-variable function g(t) obtained by restricting f(x,y) to the line through ((Subscript[x, 0]),(Subscript[y, 0])) and parallel to n: g(t)=f(x(t),y(t)). We recall from the lectures that the derivative of this function is

	((d g)/(d t))=(Subscript[n, 1])((∂f)/(∂x))+(Subscript[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·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 v be the vector from (1/2,0) to (1,1).

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

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

n=v/Norm[v]

Set ⟨x(t),y(t)⟩=⟨(Subscript[x, 0])+(Subscript[n, 1])t,(Subscript[y, 0])+(Subscript[n, 2])t⟩ which gives us the parametric form of a line starting at ((Subscript[x, 0]),(Subscript[y, 0])) and heading in the direction 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 ((Subscript[x, 0]),(Subscript[y, 0]))=(1/2,0).

Clear[x,y,g]
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]

We claim that the derivative of f(x,y) at (1/2,0) in the direction towards (1,1) is ((-3)/(4(Sqrt[5]))). 

Now, let us try to calculate this directional derivative by means of the gradient of f. We know that the gradient vector is given by ∇f=⟨(Subscript[f, x]),(Subscript[f, y])⟩.

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

We can then calculate ∇f·(OverVector[n])

gradf.n

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

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

which agrees with our original answer.

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 Slice is the function itself; the next two pairs of inputs are the coordinates of two points in the x y-plane; the module returns two plots: a graph of f and the vertical plane that contains these two points; and a graph of g(t) for 0≤t≤1. 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_}]:=
Module[{g,p1,p2,p3,p4,dz,z0,z1},
g[x_,y_]=f[x,y];
z1=Maximize[{g[x,y],x0≤x≤x1,y0≤y≤y1},{x,y}][[1]];
z0=Minimize[{g[x,y],x0≤x≤x1,y0≤y≤y1},{x,y}][[1]];
dz=10^Floor[Log10[z1-z0]]; z0=z0-0.5dz;z1=z1+0.5dz;
p1=Plot3D[g[x,y],{x,x0,x1},{y,y0,y1},PlotRange->{z0,z1},
AxesLabel->{"x","y","z"},ClippingStyle->None];
p2=ParametricPlot3D[{xa+t (xb-xa),ya+t (yb-ya),z},{t,0,1},{z,z0,z1},
PlotStyle->Opacity[.75],Mesh->None];
p3=Show[p1,p2,PlotRange->{{x0,x1},{y0,y1},{z0,z1}}];
p4=Plot[g[xa+t (xb-xa),ya+t (yb-ya)],{t,0,1},PlotRange->{z0,z1},
Axes->False,FrameLabel->{"t","z"},Frame->True,PlotStyle->Blue];
Show[GraphicsRow[{p3,p4}],ImageSize->600]]

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.

slice[f,{0.5,0},{1,1},{x,0.5,1},{y,0,1}]

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, that is, the slope of the blue curve at its left endpoint.

Exercises

Exercise 1

For each of the functions below, use either xSlice or ySlice to graph the appropriate intersection curve. Then estimate the partial derivative in question graphically by zooming in about a suitable point.

(a) f(x,y)=(Sqrt[25-(x^2)-(y^2)]), ((∂f)/(∂y))(2,-1)		(b) f(x,y)=sin(((x^2)+(y^2))/6), (Subscript[f, y])(3,3)

Exercise 2

Find the equation of the tangent plane to each function at the indicated point, and give a graph showing both the function and the tangent plane. Choose the ranges of the variables so that the graph of the function and the tangent plane are nearly identical.

(a) f(x,y)=sin(Sqrt[(x^2)+2(y^2)]) at (-1,1) 		(b) f(x,y)=sin(((x^2)+(y^2))/6)at(3,3)

Exercise 3

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·n. Use the module Slice to obtain plots of the graph of g(t)=f(x(t),y(t)) as the graph of a single-variable function of t and as the intersection curve of the graph of f(x,y) and a vertical plane.
	
	1. f(x,y)=(e^(-x))cos y at (-1,-2) towards (0,-1).
	
	2. f(x,y)=arctan xy at (-1,-2) towards (1,1).
	
	3. f(x,y)=(x^2)sin2y at (1,π/2) towards (3,-4).

Credits

This lab is based on the laboratories created by Raouf Boules, Geoffrey Goodson, Ohoe Kim, Alexei Kolesnikov and Michael O'Leary for use in the Calculus courses of Towson University. Commercial use is prohibited without permission of those authors. Non-commercial use is permitted, provided this credit section is retained.