The Differential in Matlab

In this activity we present a visualization of the differential of a function `f:R^2\to R`. We begin by first reviewing the differential from single variable calculus.

The Differential in Single Variable Calculus

Newton's notation for the first derivative is `f'(x)`. Leibniz adopted the notation `dy//dx`. Up until this moment, the notation `dy//dx` did not represent the quotient of two quantities. Rather, the notation `dy//dx` was a single quantity representing the derivative of `y` with respect to `x`. With the definition of the differential, `dy` and `dx` become individual quantities in their own right.

We begin our definition of `dy` and `dx` with the image shown in Figure 1, where we see pictured a red secant line through the points `(x,f(x))` and `(x+Delta x, f(x + Delta x))`, and a green line tangent to the graph of `f` at the point `(x,f(x))`.

An image defining the differential.

Visualizing the differential of a single variable function.

As we move from the point `(x,f(x))` to the point `(x+Delta x, f(x+Delta x))`, the actual change in `x` is `Delta x` and the actual change in `y` is

`Delta y = f(x+Delta x)-f(x).`

Note that `Delta x` and `Delta y` measure the actual changes in `x` and `y`, respectively.

Defining the Differential

We begin by defining

`dx=Delta x`.

Now, how should we define `dy`? Note that if `dy` and `dx` are defined as individual quantities, then they would be the "rise" and the "run" defining the slope of the green tangent line in Figure 1. Thus, we would want their quotient to equal the slope of the tangent line; i.e.,

`frac(dy)(dx)=f'(x)`.

Thus, it is quite natural to define `dy` as follows:

`dy=f'(x)dx`

Thus, it is natural to make the following definition.

The Differential: Let `f:R\to R`. Then,

`dx=Delta x`,

and

`dy=f'(x)dx`.

Note that `dx` and `dy` are now defined as individual quantities.

The Differential in Multivariable Calculus

We are now going to define the differential for multivariable calculus. We are going work with the specific function

`z=f(x,y)=9-x^2-y^2`

to develop a visualization for purposes of defining what is meant by the differentials `dx`, `dy`, and `dz`.

We begin by drawing the surface defined by `f` over the domain `{(x,y): 1 le x,y le 2}`. First, set up a grid of `(x,y)` pairs.

[x,y]=meshgrid(1:0.05:2);

Calculate the `z`-values.

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

Plot the surface, returning a handle to the surface in the variable h.

h=surf(x,y,z);

We use Matlab's handle graphics to set the facecolor and a little transparency.

set(h,'FaceColor','magenta',...
    'FaceAlpha',0.5)

Now we will calculate the equation of the tangent plane to the surface at the point `(1,1)`. If you do not recall how to do this, work through the activity Tangent Planes in Matlab. First, we calculate the partial derivatives of `f` with respect to `x` and `y`.

$\begin{eqnarray} f_x(x,y)&=&-2x\\ f_y(x,y)&=&-2y \end{eqnarray}$

The equation of the tangent plane to the surface at the point `(1,1)` is

`L(x,y)=f(1,1)+f_x(1,1)(x-1)-=f_y(1,1)(y-1)=7-2(x-1)-2(y-1).`

Simplifying,

`L(x,y)=11-2x-2y.`

Hold the plot.

hold on

Calculate the linearization (the tangent plane).

L=11-2*x-2*y;

Plot the tangent plane, returning a handle in the variable g.

g=surf(x,y,L);

Now we use Matlab's handle graphics to change the facecolor of the tangent plane and add a bit of transparency.

set(g,'FaceColor',[0.9,0.9,0.9],...
    'FaceAlpha',0.5);

Recall that [0.9,0.9,0.9] is an RGB triple, representing a color mix that is 90% red, 90% green, and 90% blue. Equal amounts of red, green, and blue will produce a shade of gray. Experiment with these settings to obtain a color of your choice. Remember, that each component of an RGB triple must be a number between 0 and 1, inclusive.

Add a grid, turn on the box, and rotate the view into the standard orientation ued in class.

grid on
box on
view(140,20)

Add `x`- and `y`-axis labels.

xlabel('x-axis')
ylabel('y-axis')

The sequence of commands above produce the image of the surface and tangent plane produced in Figure 2.

Image of surface and tangent plane.

The surface and tangent plane at `(1,1,f(1,1))`.

Note how the plane is tangent ot the surface at the point `(1,1,f(1,1))=(1,1,7)`.

Auxiliary Lines

We are now going to add some auxiliary lines to the image in Figure 2 to help visualize the differentials `dx`, `dy`, and `dz`. We start by defining the `x`-, `y`-, and `z`-values of the points which will be connected with consecutive line segments.

x=[1,2,2,1,1]; y=[1,1,2,2,1]; z=[5,5,5,5,5];

We use Matlab's line command to connect consecutive points with line segments, returning a handle to the segments in the variable l1,

l1=line(x,y,z);

We use Matlab's handle graphics to thicken the linewidth and change its color to red.

set(l1,'linewidth',2,'color','r')

In similar fashion, we add another sequence of segments to our plot.

x=[1,2,2,1,1]; y=[1,1,2,2,1]; z=[3,3,3,3,3];
l2=line(x,y,z);
set(l2,'linewidth',2,'color','r')

Finally, we add some lighting to our plot.

camlight headlight
lighting phong

The above sequence of commands produces the image shown in Figure 3.

Adding auxiliary lines to the previous plot.

Adding auxiliary lines.

Now it's time to interpret the results.

Interpreting the Results

As we move from the point `(1,1)` to the point `(2,2)`, note that the change in `x` is 1 and the change in `y` is 1; i.e.,

`Delta x = Delta y = 1.`

We will define `dx=Delta x` and `dy=Delta y`.

Moreover, because `f(x,y)=9-x^2-y^2`, the actual change in `z` as we move from the point `(1,1)` in Figure 3 to the point `(2,2)` is

`Delta z = f(2,2)-f(1,1)=1-7=-6`.

We indicate this change in `z` in Figure 4 with a heavy blue line.

Showing the change in z.

Showing the change in `z`.

Again, the heavy blue line represents the change in `z`, that is, `Delta z = -6`.

The change in `z` on the tangent plane as we move from the point `(1,1)` to the point `(2,2)` is different, and we've highlighted it in Figure 5 in a dark red.

Showing the in z on the tangent plane.

Showing the change in `z` on the tangent plane.

Again, the heavy red line represents the change in `z` for the tangent plane as we move from the point `(1,1)` to the point `(2,2)`. Compare this drop with the drop in Figure 1. The drop in Figure 1 along the tangent line was defined as `dy`. Therefore, the change in `z` indicated by the heavy red line of Figure 5 is `dz`.

It is important to note that the drop in `z`, namely `dz`, indicated by the heavy red line of Figure 5, is the sum of two drops, one in the `x`-direction and a second in the `y`-direction.

Let's examine the change in the `x`-direction first, highlighted in blue in Figure 6.

Showing the in z in the x-direction.

Showing the in `z` in the `x`-direction.

Starting at the point `(1,1,7)` in Figure 6, move `dx` units in the `x`-direction, then drop `f_x(1,1)dx` units in the `z`-direction. This is the differential of single variable calculus since we are fixing the value of `y` along this edge of the tangent plane.

Now we repeat this effort in the `y`-direction in Figure 7.

Showing the in z in the y-direction.

Showing the in `z` in the `y`-direction.

Starting at the point `(1,1,7)` in Figure 6, move `dy` units in the `y`-direction, then drop `f_y(1,1)dy` units in the `z`-direction. This is the differential of single variable calculus since we are fixing the value of `x` along this edge of the tangent plane.

However, note that the triangle on the front face, also outlined in a thick blue line, is congruent to this triangle in the back, so the drop in `z` is the same for the triangle on the front face.

The conclusion is that the total drop on the tangent plane as we move from the point `(1,1)` to the point `(2,2)` is the sum of these two drops. That is, the heavy red line of Figure 5 is the sum of the drop in the `x`-direction and the drop in the `y`-direction. Because the heavy red line of Figure 5 represents `dz`, we have the following result:

`dz=f_x(1,1) dx+f_y(1,1) dy.`

Finally, because `f_x(x,y)=-2x`, `f_y(x,y)=-2y`, and `dx=dy=1`, we have

`dz=(-2)(1)+(-2)(1)=-4,`

which is precisely the change indicated by the heavy red line of Figure 5.

The argument posed above leads to the following definition.

If `f:R^2\to R`, then

`dx=Delta x` and `dy=Delta y`

and

`df=f_x(x,y)dx+f_y(x,y)dy`.

Using a different notation,

`df=frac(partial f)(partial x) dx + frac(partial y)(partial y) dy.`

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.

differential.m

The file differential.m is designed to be run in "cell mode." Open the file differential.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. Write a script to obtain the image in Figure 3. Obtain a printout. On your printout, use a pencil to mark `dx`, `dy`, `dz` and `Delta z`. Mark the changes in the `x`- and `y`-directions as in Figures 6 and 7, then argue That

    `dz=f_x(1,1)dx+f_y(1,1)dy`.

    Include the following with your submission:

    • All pencil and paper calculations needed to produce equations of the linearization.
    • A printout of the M-code that produced your figure.