Partial Derivatives  and their Applications

This lab looks at partial derivatives and shows how they can be used to find extrema for functions of the form  z = f(x, y).  Some examples are done below.  Read the text and examples carefully to understand the commands, then move your cursor to the end of the first input line (the bold face lines are input lines).  To execute the commands press the ENTER  key over on the right side of the keyboard with the numeric keyboard.

Examples

Partial Derivatives

If  z = f(x, y) is a function of  x  and  y  we can find the partial derivatives by first defining the function and then using  D[f[x,y], x]  for the partial derivative of  f  with respect to  x  and  D[f[x,y], y]  for the partial derivative of  f  with respect to  y.

f[x_, y_] = 2 (x^3) + Sin[x] Sin[y]

D[f[x, y], x]

D[f[x, y], y]

In order to evaluate these two functions at various points, it will be handy to have names for them.

fx[x_, y_] = D[f[x, y], x]
fy[x_, y_] = D[f[x, y], y]

Now we can easily evaluate these partial derivatives at some point.

fx[2, -5]

Unfortunately, this is probably  not the form that we would like to see for an answer.  To obtain a numerical approximation, we can either enter N[%]  where  "%"  stands for the last output or we could have initially written the command as  fx[2,-5]//N.

N[%]

fx[2, -5] // N

We can also find higher-order partial derivatives if we wish.  A function of two variable has four second-order partial derivatives.  Notice that the first two below are the same.  That is usually true.

D[f[x, y], x, y]

D[f[x, y], y, x]

D[f[x, y], x, x]

D[f[x, y], y, y]

Contour Plots

A contour plot is a view from directly overhead, looking down on the surface.  On any given level curve, all the points are at the same height just like a topographical map.  Let's start by looking at the graph of the function f(x,y) = 3x - (x^3)  -  2  x (y^2)  +  2  y.

Clear[f]
f[x_, y_] = 3 x - (x^3) - 2 x (y^2) + 2 y

Plot3D[f[x, y],  {x,  -2, 2}, {y, -2, 2}, 
        ViewPoint -> {3, 1, 1}, 
        AxesLabel -> {x, y, z}];

The contour plot shows the high areas as light and the low areas as dark. The command Contours->30 specifies how many contours are to be displayed.  The default is 10 contours.

ContourPlot[
      3 x - (x^3) - 2 x (y^2) + 2 y, {x, -2, 2}, {y, -2, 2}, 
      Contours -> 30];

If we add ContourShading->False to the command, it will speed up the printing.

ContourPlot[
      3 x - (x^3) - 2 x (y^2) + 2 y, {x, -2, 2}, {y, -2, 2}, 
      Contours -> 30, ContourShading -> False];

If we want Mathematica to only draw certain level curves, we can specify them.  For example, if want only the contour lines at z = 0, 2, -1 try the following line

ContourPlot[
      3 x - (x^3) - 2 x (y^2) + 2 y, {x, -2, 2}, {y, -2, 2}, 
      Contours -> {0, 2, -1}, 
      ContourShading -> False];

Constructing Straight  Lines Parallel to a Vector

If we are given the vector  v = a i + b j then the line parallel to  v, through the point  ((Subscript[x, 0]),  (Subscript[y, 0])),   and going the same direction as  v  is given to us in parametric form  by  x(t) = (Subscript[x, 0]) + a t  and  y(t) = (Subscript[y, 0])+ b t  where  t ≥ 0.  Since the gradient ∇f is defined by   ∇f  =  (Subscript[f, x])(x,  y) i  + (Subscript[f, y])(x,  y) j it is a vector.  If we take the function that we were looking at before,  f(x,y) = 3x - (x^3)  -  2  x (y^2)  +  2  y and construct the straight line parallel to ∇f  and passing through the point (-1, 0.8) we can use the following lines.

Clear[f]
f[x_, y_] = 3 x - (x^3) - 2 x (y^2) + 2 y;

fx[x_, y_] = D[f[x, y], x];
fy[x_, y_] = D[f[x, y], y];

x[t_] = -1 +  t fx[-1, 0.8]

y[t_] = 0.8 + t fy[-1, 0.8]

ParametricPlot[{x[t], y[t]}, {t, 0, 0.1}, 
      PlotRange -> {{-2, 2}, {-2, 2}}];

The graph above only plots that part of the line for which 0 ≤ t ≤ 0.1.  You can try changing the t-interval or the PlotRange option to display more of the line.

If we want to see two graphs together, we can give each graph a name and then use the Show[ ] command to display them.  We must be careful that the two graphs have the same domain and the same dimension.

A = 
        ContourPlot[
          3 x - (x^3) - 2 x (y^2) + 2 y, {x, -2, 2}, {y, -2, 2}, 
          Contours -> 80, ContourShading -> False];

B = 
        ParametricPlot[{x[t], y[t]}, {t, 0, 0.1}, 
          PlotRange -> {{-2, 2}, {-2, 2}}];

Show[A, B];

Finding Extrema

In Calculus I we saw that functions had local extrema at critical points or endpoints and that possible critical points occurred when f '(x) = 0.  If  z  = f(x, y)  it too may have extreme values at critical points . A point (a, b) is now said to be a critical point if (Subscript[f, x])(a, b) = 0 =  (Subscript[f, y])(a, b).

If we look back at the last function and its contour lines, it appears that we may have a minimum near the point (-1, -0.5).  Let's try to find it using two different methods.  First, we can try  entering the following lines.

Solve[{fx[x, y] == 0, fy[x, y] == 0}, {x, y}] // 
      N

This function appears to have four critical points and one of them is at the point  (-0.888074, -0.563016)

f[-0.888074, -0.563016]

If we apply  the second derivative test, we look at  D = (Subscript[f, xx])(a,  b) (Subscript[f, yy])(a, 
                b) - (([(Subscript[f, xy])(a,  b)])^2)

fxx[x_, y_] = D[f[x, y], x, x];
fyy[x_, y_] = D[f[x, y], y, y];
fxy[x_, y_] = D[f[x, y], x, y];

fxx[-0.888074, -0.563016] fyy[-0.888074, -0.563016] - 
      fxy[-0.888074, -0.563016]^2

Since D > 0, then f (-0.888074,-0.563016) is a local minimum and the minimum value is -2.52684.   There is a second method for finding extrema; it is an iterative method and is worth looking at.  It is called "The Method of Steepest Descent."

The Method of Steepest Descent

We will try to apply  this method to the function f(x,y) = 3x - (x^3)  -  2  x (y^2)  +  2  y and the minimum that we just looked at.  The first step is to make an initial estimate for the local minimum; our initial estimate was (-1, -0.5).

Clear[x, y]

x[t_] = -1 -  t fx[-1, -0.5]

y[t_] = -0.5 - t fy[-1, -0.5]

The portion of the surface f(x,y) = 3x - (x^3)  -  2  x (y^2)  +  2  y which lies directly above this straight line has z-coordinates given by  u(t) =  f((Subscript[x, 0]) - t (Subscript[f, x])(x,  y),  (Subscript[y, 0])- t (Subscript[f, y])(x,  y) )

u[t_] = f[-01 + 0.5 t, -0.5]

Together[%]

We now have a function of one variable and if we plot u(t) we see that it does have a local minimum near t = 0.

Plot[u[t], {t, -1, 5}]

Let's try to find the minimum for u(t) using Calculus I and then substitute the value back into x  and  y.

FindRoot[u'[t] == 0, {t, 0}]

x[0.174258]

y[0.1742588]

We now have a second estimate for the minimum at (-0.912871, -0.5).  We can repeat the process again and again until we achieve whatever degree of accuracy  that we want.  This method is very similar to what we did when we studied Newton's Method.

Exercises

1.  Use Mathematica to find the indicated partial derivatives for the following functions.
     a)  f(x, y) = sin (x - 2y),  find  D[f[x ,y], y]
     
     b)  f(x, y) = ((e^xy)) (ln y), find  D[f[x, y], x]
     
     c)  f(x, y) = ((((x^2)  +  x  -  y))^3), find  D[f[x, y], y, y]
     
     d)  f(x, y) = 1/(Sqrt[(x^2)  +  (y^2)]),  find  both  D[f[x ,y], x, y]  and  D[f[x, y], y, x]

2.  Obtain a printout that shows contour plots for each of the following and estimate the x and y coordinates of those points that are local maximum and minimum.
     a)  f(x, y) = 4 - (x^2)  -  (y^2)
     
     b)  f(x, y) = ((y^2)/6)  +  4 (e^(-((x^2)  +  
                      (y^2))/4)) 
     
     c)  f(x, y) = (y^2)  -  x  +  
        xy  +  ln(
            (x^2)  +  (y^2)  +  1) (e^(-((x^2)  +  (y^2))))

3.  Obtain a three-dimensional printout and a contour plot  that shows the following functions.   Find all critical points and classify them as maximum, minimum, or saddle points.
     a)  f(x, y) = (2  (x^2)  -  5  x  - 
          6  (y^(3 ))  +  2  (y^2)  +  
          3  y) (e^(-((x^2)  +  (y^2))))
     
     b)  f(x, y) = (2y  -  x  +  
            (e^((1 - (x^2)  -  (y^2)) )))/(1  +  
            (x^2)  +  (y^2))

4.  Obtain a printout of a contour plot and use the Method  of Steepest Descent to find a local minimum in the first quadrant, accurate to four decimal places, for f(x, y) = sin(x)sin(y - x) + (1/(1  +  (x^2))).  Submit copies of the initial and final applications of this technique that you used.

5.  Obtain a printout of  a contour plot and use the Method  of Steepest Descent to find a local maximum near the point (1, 1), accurate to four decimal places, for f(x, y) = sin(x)sin(y - x) + (1/(1  +  (x^2))).  Submit copies of the initial and final applications of this technique that you used.  You will need to change the equations that we used in this lab.  Recall the direction of greatest increase will be in the same direction as the gradient, ∇f .