## Partial Derivatives in Matlab

Suppose that we have a function f:R^2\to R defined by

f(x,y)=9-x^2-y^2.

Let's use Matlab to draw the surface represented by the function f over the domain {(x,y): -2 le x,y le 2}. We begin by creating a grid of (x,y) pairs.

[x,y]=meshgrid(-2:.25:2);


When used in this form, Matlab uses the entries in the vector -2:.25:2 for both x and y.

Next, calculate the value of z at each point (x,y) in the grid.

z=9-x.^2-y.^2;


Next, we plot the surface, storing a handle to the surface in h1.

h1=surf(x,y,z);


We use the handle to set some property-value pairs.

set(h1,'FaceColor','magenta',...
'FaceAlpha',0.5,...
'EdgeColor','k')


Some comments of explanation are in order:

1. h1 contains a "handle." It is a numerical value associated with the surface created with the surf command. If you type get(h1) at the Matlab prompt, you will get a list of the current properties and their values for the surface in Figure 1.
2. You use Matlab's set command to change or "set" the value of a property. In this case, we set values for three properties:
• We set the 'Facecolor' to mangenta ('m').
• We set the 'FaceAlpha' to 0.5. This is the amount of transparency we want. The value must be a number between 0 and 1, with 0 being fully transparent and 1 being completely opaque (unable to see through the surface).
• We also set the 'EdgeColor' to black ('k'). These are the gridlines that flow through the surface.

We turn on the grid and rotate the view into the standard orientation we use in class.

grid on
view(150,20)


Finally, we add some appropriate annotations.

xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
title('The surface defined by f(x,y) = 9 - x^2 - y^2.')


The result of this sequence of commands is the surface shown in Figure 1.

### The Partial Derivative with Respect to x.

The partial derivative of f with respect to x is defined as follows.

f_x(x,y)=lim_(h\to 0)frac(f(x+h,y)-f(x,y))(h)

Note how y is "fixed" while x varies from x to x+h. This is an important observation.

Suppose, for example, that we wish to calculate the partial derivative of f with respect to x at the point (1,1). The definition then becomes:

f(1,1)=lim_(h\to 0)frac(f(1+h,1)-f(1,1))(h).

Because f(x,y)=9-x^2-y^2,

$\begin{eqnarray} f_x(1,1) &=&\lim_{h\to 0}\frac{[9-(1+h)^2-(1)^2]-[9-(1)^2-(1)^2]}{h}\\ &=&\lim_{h\to 0}\frac{[8-(1+h)^2]-7}{h}\\ &=&\lim_{h\to 0}\frac{8-1-2h-h^2-7}{h}\\ &=&\lim_{h\to 0}\frac{-2h-h^2}{h}\\ &=&\lim_{h\to 0}(-2-h)\\ &=&-2\end{eqnarray}$

Geometrical Interpretation: One question remains: how do we interpret the result f_x(1,1)=-2? From single variable calculus, we know that the first derivative f'(1) gives the slope of the tangent line at x=1. It is therefore completely natural to think that f_x(1,1)=-2 gives us the slope of the tangent line to the surface at the point (1,1) in the x-direction.

We can visualize this interpretation of f_x(1,1)=-2 by first noting that y=1 is "fixed." This leads us to add the plane y=1 to the plot in Figure 1. Drawing a vertical plane in Matlab is a bit tricky. The first thing to realize is the fact that y is a function of x and z. True, y=1 is constant on its domain, but it is still a function of x and z. Thus, the first step is to choose a good domain over which to draw the plane represented by y=1.

In Figure 1, note that the x-values run from -2 to 2; i.e., -2 le x le 2. Also, note that the z-values run from 1 to 9; i.e., 1le z le 9. Hence, lets create a grid of (x,z) points on this domain.

[x,z]=meshgrid(-2:.25:2,1:0.5:9);


Now for the tricky part. We need y=1 at each of these (x,z) pairs. We do this with Matlab's ones command.

y=ones(size(x));


Hold the previous plot.

hold on


Now we draw the plane and save a handle to the plane in the variable h2.

h2=surf(x,y,z);


Finally, we set the face color to a shade of gray and turn off edge plotting.

h2=surf(x,y,z);
set(h2,'FaceColor',[0.7,0.7,0.7],...
'EdgeColor','none')


The result is the image shown in Figure 2.

Adding a Tangent Line: . The fact that f_x(1,1)=-2 tells us that the slope of the tangent line to the surface at the point (1,1,f(1,1)) is -2. Because f(x,y)=9-x^2-y^2, note that

(1,1,f(1,1))=(1,1,7),

so the point of tangency is the point P(1,1,7). To find the equation of the tangent line through this point in the direction of x, we need to find a vector pointing in the direction of the tangent line.

As we move in the direction of x, the y-coordinate must remain fixed. However, a slope of -2 indicates that for each positive unit moved in the x-direction, z must drop 2 units. If we start at the point P(1,1,7), then move one unit in the positive x-direction, followed by a negative 2 units in the z-direction, we would arrive at the point Q(2,1,5). Thus, the vector vec(PQ) will point in the direction of the line tangent to the surface at the point P(1,1,7) in the direction of x.

vec(PQ)=langle 2-1,1-1,5-7 rangle=langle 1,0,-2 rangle

If we let X(x,y,z) be an arbitrary point on the tangent line, then the equation of the tangent line is:

vec(PX)=t vec(PQ).

With X(x,y,z), P(1,1,7), and vec(PQ)=langle 1,0,-2 rangle, this becomes

langle x-1,y-1,z-7 rangle=t langle 1,0,-2 rangle.

Therefore, the parametric equations of the line tangent to the surface at P(1,1,7) in the direction of x is:

$\begin{eqnarray} x&=&1+t\\ y&=&1\\ z&=&7-2t. \end{eqnarray}$

We will now add this tangent line to the image in Figure 2. The first task is to determine reasonable values of the parameter t. We could just guess values for the parameter t, but the image in Figure 2 provides clues. Note that -2le x le 2. Thus,

-2 le 1+t le 2.

Solving for t,

-3 le t le 1.

But Figure 2 also requires that 1le z le 9; that is,

1 le 7-2t le 9.

Solving for t,

-1 le t le 3.

We need both requirements on t, namely -3 le t le 1 and -1 le t le 3. This means that we should choose -1 le t le 1. Thus, we should sketch the parametric equations of the line on the domain [-1,1].

t=linspace(-1,1);
x=1+t;
y=ones(size(t));
z=7-2*t;
line(x,y,z,...
'color','blue',...
'linewidth',2)


Note that you can add property-value pairs directly in the line command. We chose the color 'blue' and made the linewidth a bit thicker. If you wished, you could use instead h3=line(x,y,z), then follow with set(h3,'color','blue','linewidth',2). If you use this approach, you can capture a complete list of possible properties for the line command with the command get(h3).

The final result is shown in Figure 3.

The key thing to note is the fact that the blue line in Figure 3 is tangent to the graph of f(x,y)=9-x^2-y^2 at the point P(1,1,7) in the direction of x.

### Matlab Files

Although the following file features advanced use of Matlab, we include it here for those interested in discovering how we generated the images for this activity. You can download the Matlab file at the following link. Download the file to a directory or folder on your system.

pderiv.m

The file pderiv.m is designed to be run in "cell mode." Open the file pderiv.m in the Matlab editor, then enable cell mode from the Cell Menu. After that, use the entries on the Cell Menu or the icons on the toolbar to execute the code in the cells provided in the file. There are options for executing both single and multiple cells. After executing a cell, examine the contents of your folder and note that a PNG file was generated by executing the cell.

### Exercises

1. The partial derivative of f in the direction of y is defined by

f_y(x,y)=lim_(h\to 0) frac(f(x,y+h)-f(x,y))(h).

Perform each of the following tasks.

• Again define f(x,y)=9-x^2-y^2. Use the definition above to find the partial derivative of f with respect to y at the point (1,1).
• As in the activity, sketch the surface f.
• Add the plane x=1 to your image.
• Add the tangent line to the surface at the point P(1,1,7) in the direction of y. Include pencil and paper work needed to develop the parametric equations of the tangent line.
• Include a printout of the M-code that produced your image.