Chapter 14	 Differentiation in Several Variables

Useful Tip: If you are reading the electronic version of this publication formatted as a Mathematica Notebook, then it is possible to view 3-D plots generated by Mathematica from different perspectives.  First, place your screen cursor over the plot.  Then drag the mouse while pressing down on the left mouse button to rotate the plot.

14.1  Functions of Two or More Variables

Students should read Section 14.1 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

14.1.1 Plotting Level Curves using ContourPlot

We begin with plotting level curves f(x,y)=c of a function of two variables. The command to plot level curves is ContourPlot[f,{x,a,b},{y,c,d}].

Most of the options for ContourPlot are the same as those for Plot.  In the following example, we consider the option ImageSize.

Example 14.1.  Plot the level curves of f(x,y)=(x^2)+x y -(y^2) .

Solution:  Let us first plot the level curves using the default settings of Mathematica.

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

ContourPlot[f[x,y],{x,-5,5},{y,-5,5},ImageSize->{250}]

To get the level curves on the xy-plane without the shading, the colors, and the frame, but with the coordinate axes, we use the following options of ContourPlot.

ContourPlot[f[x,y],{x,-5,5},{y,-5,5},Frame->False,Axes->True,ContourShading->False,ImageSize->{250}]

Contours is an option of ContourPlot that can be used in two different ways: Contour->n displays n equally spaced contour curves while Contour->list plots level curves f(x,y)=c where c is an element of the list list.

To plot 15 level curves, we evaluate

ContourPlot[f[x,y],{x,-1,1},{y,-1,1}, Contours->15,ImageSize->{250} ]

Here is an example when list={-10,-5,-2, -1, 0,1, 2,  5, 10}.

ContourPlot[f[x,y],{x,-5,5},{y,-5,5},Contours->{-10,-5,-2,-1, 0, 1, 2, 5, 10},ImageSize->{250}]

14.1.2  Plotting Surfaces using Plot3D

Plot3D is the three-dimensional analog of the Plot command. Given an expression in two variables and the domain for the variables, Plot3D produces a surface plot.

The basic syntax to plot the graph of a function of two variables is Plot3D[ f,{x, a, b},{y, c, d}], where f is a function of x and y  with  a≤x≤b and c≤y≤d.

The command to plot the graphs of two or more functions on the same coordinate axes is Plot3D[{f, g, h, .... }, {x, a, b}, {y, c, d}], where f, g, h, ...  are the functions to be plotted.

We will begin with the default settings of plotting a graph of a function of two variables.

Example 14.2.  Plot f(x,y)=sin(x-cos y).

Solution:

Plot3D[Sin[x-  Cos[y]],{x,-3,3},{y,-3,3}]

Example 14.3.  Plot the graphs of f(x,y)=3x+4y-3 and g(x,y) =10 sin(xy) on the same axes.

Solution:  We will use red color for the graph of f and blue for that of g. This is given using the option PlotStyle.

Plot3D[{3x+4y-3,10Sin[x y]},{x,-5,5},{y,-5,5},PlotStyle->{Green,Blue},ImageSize->{250}]

NOTE: One of the most significant improvements of Mathematica 7.0 over the previous editions is its graphics capability.  Plot3D has many options.  Evaluate the command Options[Plot3D] to see the many options you have to plot a nice graph.

We will discuss some of these options below.

ViewPoint

In Mathematica 7.0, we can rotate the graph of a function of two variables by simply clicking on the graph and dragging the mouse around to see the graph from any point of view.

The option ViewPoint specifies the point in space from which the observer looks at a graphics object. The syntax for choosing a view point of a surface is Plot3D[f[x, y], {x, a, b}, {y, c, d}, ViewPoint->{A, B, C} ]. The default value for {A, B, C}  is {1.3,-2.4,2.0}. This may be changed by entering values directly.

To view a graph from directly in front {0, -2,0}; in front and up {0, -2,2}; in front and down {0, -2,-2}; left hand corner {  -2,-2,0};  directly above  {0, 0,2}.

Plot3D[ f[x, y], {x, a, b}, {y, c, d}, ViewPoint -> view ] produces a plot viewed from view. The possible values of view are Above (along positive z-axis), Below (along negative z-axis), Front (along negative y-axis), Back (along  positive y-axis), Left  (along the negative x-axis), and Right (along the positive x-axis).

Example 14.4.  Plot f(x,y)=cos x sin y using ViewPoint option to view the graph from various view points.

Solution: We leave it to the reader to navigate all of the above choices. We will consider a few of them.

Clear[f]
f[x_,y_]=Cos[x]  Sin[y]

Here is a plot of the graph using the default setting for ViewPoint:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi} ,PlotRange->All,
ImageSize->{250} ]

View from directly in front:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi},ViewPoint->Front ,
PlotRange->All ,ImageSize->{250}]

View from in front and up:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi},ViewPoint->{0,-2,2},
PlotRange->All,ImageSize->{250}]

View from in front and down:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi},ViewPoint->{0,-2,-2},
PlotRange->All,ImageSize->{250}]

View from directly above:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi},ViewPoint->Above,
PlotRange->All,Ticks->{Automatic,Automatic,{-1,0,1}},
ImageSize->{250}]

View from the right:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi},ViewPoint->Right,
PlotRange->All,ImageSize->{250}]

NOTE: As we pointed out earlier, we can also select different viewpoints by clicking on the graph and dragging the mouse around until we get the desired viewpoint.

Mesh, MeshStyle, MeshShading

The option Mesh specifies the type of mesh that should be drawn.

The option MeshStyle specifies the style in which a mesh should be drawn.

The option MeshShading is an option for specifying a list of colors to be used between mesh divisions.

We illustrate some uses of these options in the example below.

Example 14.5.  Plot f(x,y)=cos x sin y using various options involving Mesh.

Solution:

Clear[f]
f[x_,y_]=Cos[x]  Sin[y]

To plot a graph without a mesh we use the setting Mesh->None.

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi} , Mesh->None,
ImageSize->{250} ]

Mesh->n plots a surface with only n×n meshes.

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi} , Mesh->8 ,
ImageSize->{250}]

We can choose the color of the mesh using MeshStyle.

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi} , MeshStyle->
{Red,Black},ImageSize->{250} ]

Here is another use of MeshStyle:

Plot3D[ f[x,y],{x,-2Pi,2Pi},{y,-2Pi,2Pi} , MeshStyle->
{Dashing[0.01],None},ImageSize->{250} ]

To display a plot with selected colors between meshes we use MeshShading:

Plot3D[f[x,y],{x,-2Pi, 2Pi},{y,-2Pi,2Pi},MeshShading->{{Blue, Red, White},{Purple,Green,Black}},ImageSize->{250} ]

Here is a neat example in Mathematica 7.0:

Plot3D[(x^2-y^2)/(x^2+y^2)^2,{x,-1.5,1.5},{y,-1.5,1.5},BoxRatios->Automatic,PlotPoints->25,MeshFunctions->{#3&},MeshStyle->Purple,MeshShading->{None,Green,None,Yellow},ImageSize->{250}]

BoxRatios

The option BoxRatios specifies the ratio of the lengths of the sides of the box. This is analogous to specifying the AspectRatio of a two-dimensional plot.  For Plot3D, the default setting is BoxRatios->Automatic.

Example 14.6.  Plot f(x,y)=(e^(1-(x^2)-(y^2))) using the BoxRatio option.

Solution:

Clear[f]
f[x_,y_]= (E^(1-(x^2)- (y^2)))

Plot3D[ f[x,y],{x,-2,2},{y,-2,2},ImageSize->{250}]

Plot3D[ f[x,y],{x,-2,2},{y,-2,2},BoxRatios->{1,1,0.62`},
ImageSize->{250}]

AxesLabel

The option AxesLabel  is a command used to label the axes in plotting.

Example 14.7.  Plot f(x,y)=(Sqrt[9-(x^2)-(y^2)]) using the AxesLabel option.

Solution:

Clear[f]
f[x_,y_]= (Sqrt[9-(x^2)-(y^2)])

Plot3D[f[x,y],{x,-3,3},{y,-3,3},AxesLabel->{"x ","y ","z "},
ImageSize->{250},ImagePadding->{{15,15},{15,15}}]

NOTE: To label a graph, use the PlotLabel option as shown following:

Plot3D[f[x,y],{x,-3,3},{y,-3,3}, AxesLabel->{"x ","y ","z "},
PlotLabel->"Upper hemisphere",BoxRatios->Automatic,ImageSize->{250},
ImagePadding->{{15,15},{15,25}}]

ColorFunction

The option ColorFunction specifies a function to apply to the values of the function being plotted to determine the color to use for a particular region on the xy-plane. It is an option for Plot3D, ListPlot3D, DensityPlot, and ContourPlot.  The default setting for ColorFunction is ColorFunction->Automatic.  ColorFunction->Hue yields a range of colors.

Example 14.8.  Plot f(x,y)=sin((x^2)+(y^2)) +(e^(1- ((x )^2)-(y^2)))  in various colors using the ColorFunction option.

Solution:

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

Plot3D[f[x,y],{x,-Pi,Pi},{y,-Pi,Pi},ColorFunction->Hue,
ImageSize->{250}]

Here are other ways to use ColorFunction.

Plot3D[f[x,y],{x,-Pi,Pi},{y,-Pi,Pi},ColorFunction-> "Rainbow",
ImageSize->{250} ]

Plot3D[f[x,y],{x,-Pi,Pi},{y,-Pi,Pi},ColorFunction->
"BlueGreenYellow",ImageSize->{250}]

NOTE: We can use PlotStyle option to select color for graphs. The plot below uses this option.

Plot3D[f[x,y],{x,-Pi,Pi},{y,-Pi,Pi},PlotStyle-> Yellow,
ImageSize->{250} ]

RegionFunction

The option RegionFunction specifies the region to include in the plot drawn.

Example 14.9.  Plot f(x,y)={({{10sin(3x-y),, if  (x^2)+(y^2)<4;}, {(x^2)+(y^2)-5,    , otherwise}}).

Solution: We will use the command RegionFunction to specify the domain (x^2)+(y^2)<4 as follows.  Note that we have used Show to display the graphs.

Clear[plot1,plot2] 
plot1 =Plot3D[10Sin[3x-y],  {x,-4,4},{y,-4,4},PlotStyle->Blue,
 RegionFunction->Function[{x,y,z}, x^2+y^2<4]];
plot2 =Plot3D[  (x^2)+(y^2) -5,{x,-4,4},{y,-4,4},PlotStyle->Red, 
 RegionFunction->Function[{x,y,z}, x^2+y^2 ≥4]];
Show[plot1,plot2,ImageSize->{250}]

If we want to focus on a particular part of a surface defined by a function, we can use the option RegionFunction. The following example shows this point.

Example 14.10.  Plot the graph of f(x,y) =(x^2)-3x y-2(y^2)  and show the portion of the surface direclty above the unit circle centered at the origin.

Solution: We will use the option ViewPoint.

Clear[plot1,plot2,f, x,y]
f[x_,y_]=(x^2) -3 x y -2(y^2) 
plot1 =Plot3D[f[x,y],  {x,-4,4},{y,-4,4},PlotStyle->Blue,
 RegionFunction->Function[{x,y,z},x^2+y^2<1 ]];
plot2 =Plot3D[f[x,y] ,{x,-4,4},{y,-4,4},PlotStyle->Red,
RegionFunction->Function[{x,y,z}, x^2+y^2>1 ]]; 
Show[plot1,plot2 ,ViewPoint->Front,ImageSize->{250} ]

14.1.3  Plotting Parametric Surfaces using ParametricPlot3D

ParametricPlot3D is a direct analog of ParametricPlot.  Depending on the input, ParametricPlot3D  produces a space curve or a surface. ParametricPlot3D[{f, g, h}, {t, a, b }] produces a three-dimensional space curve parametrized by the variable t, which runs from  a to b. ParametricPlot3D[{f, g, h}, {t, a, b },{u, c, d}] produces a two-dimensional surface parametrized by t and u. Options are given to ParametricPlot3D the same way as for  Plot3D. Most of the options are the same.

Example 14.11.  Plot the curve that is parametrized by x=sin t, y=cos t and z=t/3 with 0≤t≤2π.

Solution:

ParametricPlot3D[{Sin[t],Cos[t],(t/3)},{t,0,2 π},ImageSize->{250},
ImagePadding->{{15,15},{15,15}}]

Example 14.12.  Plot the surface that is parametrized by  x = u cos u (4 + cos(u + v)), y = u sin u (4 + cos(u + v)), and z = u sin(u + v).

Solution:

ParametricPlot3D[{u Cos[u] (4+Cos[u+v]),u Sin[u] (4+Cos[u+v]),u Sin[u+v]},{u,0,4 π},{v,0,2 π},ImageSize->{250}]

14.1.4  Plotting Level Surfaces using ContourPlot3D

ContourPlot3D is the command used to plot level surfaces of functions of three variables.  Its syntax is ContourPlot3D[f,{x,a,b}, {y,c,d},{z,e,f}].  Most of the Options for ContourPlot3D are the same as those of Plot3D.  Below we will consider the option Contours of ContourPlot3D.

Example 14.13.  Plot level surfaces of f(x,y,z)=(x^2)+(y^2)+(z^2).

Clear[x,y,z,f]
f[x_,y_,z_]=(x^2)+(y^2)+(z^2)
ContourPlot3D[f[x,y,z],{x,-3,3},{y,-3,3},{z,-3,3},ImageSize->{250}]

The following displays five (5) equally spaced contour surfaces of f.

ContourPlot3D[f[x,y,z],{x,-3,3},{y,-3,3},{z,-3,3},
Contours->5,ImageSize->{250}]

The following  displays three level surfaces f(x,y,z)=c, where c=1, 4, 9.

ContourPlot3D[f[x,y,z],{x,-3,3},{y,-3,3},{z,-3,3},
Contours->{1,4,9},ImageSize->{250}]

Notice that we only see one sphere. The other two are enclosed in the sphere of radius 3 corresponding to c=9.  One way to remedy this is to plot the level surfaces one by one. For this we use the GraphicsArray command. First, let us define the level surfaces as function of c:

Clear[c,plot]
plot[c_]:=ContourPlot3D[f[x,y,z],{x,-3,3},{y,-3,3},{z,-3,3},
Contours->{c}]

Here are the three level surfaces corresponding to c=1, 4, 9.

Show[GraphicsArray[{plot[1],plot[4],plot[9]}]]

Exercises

In Exercises 1 through 4, plot the level curves and the graphs of the given functions.
1.  f(x,y)=x (y^5)-(x^5)y for -10≤x≤10, -10≤y≤10
2.  f(x,y)=(((x^2)+2y)/(1+(x^2)+(y^2))) for -10≤x≤10, -10≤y≤10
3.  f(x,y)=(sin y )(e^(cos x)) for -2π≤x≤2π, -2π≤y≤2π
4.  f(x,y)= sin(x+sin(y)) for -4π≤x≤4π, -4π≤y≤4π

In Exercises 5 through 7, use at least two nondefault options to plot the given functions.
5.  f(x,y)=sin (x-2y)(e^(1/(y-x))) for  -2π≤x≤2π, -2π≤y≤2π
6.  f(x,y)=4-3|x|-2|y| for -10≤x≤10, -10≤y≤10
7.  f(x,y)=(tanh^(-1))(x/y) for -5≤x≤5, -5≤y≤5

8.  Plot f(x,y)={({{(x^2)+(y^2)-4      if (x^2)+(y^2)<4}, {4-(x^2)+3(y^2)         otherwise }})

9.  Plot the portion of the helicoid (spiral ramp) that is defined by:
x = u cos v,  y = u sin v, z = v for 0 ≤ u ≤ 3  and  -2π ≤ v ≤2 π

10. Use ContourPlot3D to plot the level surfaces of the function f(x,y,z)=9-(x^2)-(y^2)-(z^2).

14.2  Limits and Continuity

Students should read Section 14.2 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

14.2.1  Limits

If f(x,y) is a function of x and y, and if the domain of f contains a circle around the point (a,b), we say that the limit of f at (a,b) is L if and only if f(x,y) can be arbitrarily close to L for all (x,y) arbitrarily close (a,b).

More precisely, for a given ϵ>0, there exists a δ>0 such that for every (x,y) is in the domain of f,

0<(Sqrt[(((x-a))^2)+(((y-b))^2)])<δ  ⟹ |f(x,y)-L|<ϵ

If this is the case, we write

(lim_((x,y)->(a,b)))f(x,y)=L

The Limit command of Mathematica is restricted to functions of one variable. However, we can use it twice to find the limit of function of two variables provided the limit exists.

Example 14.14.  Find (lim_((x,y)->(3,4)))((x^2)+(y^2)).

Solution: We can easliy determine that the limit exists. We can find the limit by evaluating

Limit[Limit[(x^2)+(y^2),x->3],y->4]

The plot following confirms this.

Clear[plot1,plot2]
plot1=Plot3D[(x^2)+(y^2),{x, 1,4},{y, 3, 5}];
plot2=Graphics3D[{Red,PointSize[.025],Point[{3,4,25}]}];
Show[plot1,plot2,ImageSize->{250},ImagePadding->{{15,15},{15,15}}]

Example 14.15.   Find (lim_((x,y)->(4,1)))((3x+(y^2))/(x-4y)).

Solution: We will evaluate the limit in two different orders. The limit in which we use limit with x first and then with y is

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

The limit in which we use limit with x first and then with y is

Limit[Limit[f[x,y],x->4],y->1]

The limit in which we use limit with y first and then with x is

Limit[Limit[f[x,y],y->1],x->4]

Here is the plot of the graph near the point (4,1). Observe that the graph of the function is in green and the point (4,1,0) is in red. For a better comaprison, we have colored the xy-plane light blue. You may need to rotate the graph to see the point (4,1,0) on the xy-plane and see how the graph behaves when (x,y) is close to (4,1).

Clear[plot1,plot2]
plot1=Plot3D[{f[x,y],0},{x,3,5},{y, 0, 2},PlotStyle->{Green, LightBlue},PlotPoints->100];
plot2=Graphics3D[{Red,PointSize[.025],Point[{4,1,0}]}];
Show[plot1,plot2,ImageSize->{250},
ImagePadding->{{15,15},{15,15}}]

Here is the animation with x as the animation parameter.

Important Note: If you are reading the printed version of this publication, then you will not be able to view any of the animations generated from the Animate command in this chapter.  If you are reading the electronic version of this publication formatted as a Mathematica Notebook, then evaluate each Animate command to view the corresponding animation.  Just click on the arrow button to start the animation.  To control the animation just click at various points on the sliding bar or else manually drag the bar.

Animate[Plot[f[x,y],{y, 0,3},PlotRange->{-20,20}],{x,3, 5}]

Example 14.16.  Find (lim_((x,y)->(0,0)))((sin x sin y)/(x y)).

Solution: We will evaluate the limit in two different orders.

Clear[f,x,y]
f[x_,y_]=((Sin[x  y])/(x y))

Limit[Limit[f[x,y],x->0],y->0]

Limit[Limit[f[x,y],y->0],x->0]

Here is the plot of the graph and the point (0,0,1).

Clear[plot1,plot2]
plot1=Plot3D[f[x,y],{x,-1,1},{y, -1, 1},PlotStyle-> Green];
plot2=Graphics3D[{Red,PointSize[.02],Point[{0,0,1}]}]; 
Show[plot1,plot2,ImageSize->{250}]

If we rotate this graph to a suitable position, we notice that the limit exists.  Here are animations with x and y as animation parameters, respectively.

Animate[Plot[f[x,y], {x, -2,2},PlotRange->{0,1}], {y, -2, 2}]

Animate[Plot[f[x,y], {y, -2,2},PlotRange->{0,1}], {x, -2, 2}]

Example 14.17.  Find (lim_((x,y)->(0,0))) x ln y.

Solution:

Clear[f,x,y]
f[x_,y_]=x Log[y]

Limit[Limit[f[x,y],x->0], y->0]

Limit[Limit[f[x,y],y->0], x->0]

Clear[plot1,plot2]
plot1=Plot3D[{f[x,y],0},{x,-1,1},{y, -1, 1},PlotStyle->{ Green,LightBlue}];
plot2=Graphics3D[{Red,PointSize[.025],Point[{0,0,0}]}];
Show[plot1,plot2,ImageSize->{250},
ImagePadding->{{15,15},{15,15}}]

Here is the animation with x as the animation parameter.

Animate[Plot[f[x,y], {y, -2,2},PlotRange->{-10,10}], {x, -2, 2}]

Example 14.18.  Consider the function f(x,y)=((x (y^2))/((x^2)+(y^4))). Show that (lim_((x,y)->(0,0)))f(x,y) does not exist.

Solution:

Clear[f,x,y]
f[x_,y_]=((x (y^2))/((x^2)+(y^4)))

Limit[Limit[f[x,y],x->0], y->0]

Limit[Limit[f[x,y],y->0], x->0]

Limit[Limit[f[x,y],y-> m x], x->0]

However, note that the limit along the curve y=(Sqrt[x]) is

Limit[Limit[f[x,y],y->  (Sqrt[x])], x->0]

Hence, the limit does not exist.  Here is the plot of the function:

Plot3D[f[x,y],{x,-1,1},{y, -1, 1},ImageSize->{250}]

14.2.2  Continiuty

Recall that a function f of two variables x and y is continuous at the point (a,b) if and only if (Subscript[limi, (x,y)->(a,b)])f(x,y)=f(a,b).

Example 14.19.  Let f(x,y)={({{1-(x^2)-(y^2), if (x^2)+(y^2)<1}, {0,                 if (x^2)+(y^2)≥1}}).  Is f continuous?

Solution: Clearly, f is continuous at all points inside and outside the circle of radius 1.  To check continuity on the unit circle, we let x=r cos t and y=r sin t. We then let r->1.

Clear[x,y,r,s,t,f] 
f[x_,y_]=1-(Sqrt[(x^2)+(y^2)])

x=r Cos[t]
y=r Sin[t]

Simplify[f[x,y]]

Limit[f[x,y],r->1]

The command below evaluates f on the circle.

Simplify[f[x,y]/.r->1]

Thus, the limit and the value of f are equal at all points on the unit circle. Hence, f is continuous everywhere.  Here is the graph.

Clear[plot1,plot2]
plot1=Plot3D[f[x,y],{x,-5,5},{y,-5,5},PlotStyle->Red, 
 RegionFunction->Function[{x,y,z},x^2+y^2<1],Mesh->None];
plot2=Plot3D[  0,{x,-5,5},{y,-5,5},PlotStyle->LightBlue,
 RegionFunction->Function[{x,y,z}, x^2+y^2 ≥1],Mesh->None];
Show[plot1,plot2,ImageSize->{250}]

Exercises

In Exercises 1 through 4, find the limit, if it exists.
1.  (lim_((x,y)->(1,-1)))(2(x^2)y +x (y^2))		2. (lim_((x,y)->(1,1)))((3(x^2)+(y^2))/((x^2)-y))
3.   (lim_((x,y)->(0,0)))((tan x sin y)/(x y))	 		4.  (lim_((x,y)->(0,0))) sin x ln y

5. Consider the function f(x,y)=(((x^2) +(y^2))/((x^2)+(y^4))). Show that (lim_((x,y)->(0,0)))f(x,y) does not exist.

6.  Let f(x,y)={({{(x^2)-(y^2), if x+y<0}, {2x+y,  if x+y≥0}}).
Is f continuous?

7.  Let f(x,y)=((x y)/((x^2)+(y^2))). The domain of f is the whole plane without the origin. Is it possible to define f(0,0) so that f is continuous everywhere? Plot the graph of f to support your conclusions.

8.  The domain of f(x,y)=((x y)/(x+y))  is the whole plane without the line y=-x.   Is it possible to define f(0,0) so that f is continuous everywhere? Plot the graph of f to support your conclusions.

14.3  Partial Derivatives

Students should read Section 14.3 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Recall that the Mathematica command for the partial derivative of a function f with respect to x is D[f, x], and D[f,{x,n}] gives the nth partial derivative of f with respect to x. The multiple (mixed) partial derivative of f with respect to (Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),... is obtained by D[f,(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),...].  We can access this command from  BasicMathInput. The symbols are (∂_□) □ and (∂_(□,□))□ .

Example 14.20.  Find the first partial derivatives of (x^3)+(y^2) with respect to x and (y^( )).

Solution:  We give two methods of input.

Method 1: We can type all the inputs and the command as follows:

Clear[x,y]
D[x^3+y^2,x]

D[x^3+y^2,y]

Method 2:  We can use the BasicInput palette to enter the inputs.

(∂_x) ((x^3)+(y^2))

(∂_y) ((x^3)+(y^2))

Example 14.21.  Find the four second partial derivatives of (x^3)sin(y)+(e^(x y)).

Solution: Let z=(x^3)sin y+(e^(x y)).  We again demonstrate two methods of input.

Method 1:

We can find (Subscript[z, xx]) by

Clear[x,y]
D[x^3*Sin[y]+E^(x*y),{x,2}]

We can find (Subscript[z, yy]) by

D[x^3*Sin[y]+E^(x*y),{y,2}]

We can find (Subscript[z, xy]) by

D[x^3*Sin[y]+E^(x*y),x,y]

(Subscript[z, yx])  is given by

D[x^3*Sin[y]+E^(x*y),y,x]

NOTE: Clairaut's Theorem states that if the mixed partial derivatives (Subscript[f, x y]) and (Subscript[f, y x]) are continuous at a point (x,y), then they are equal: (Subscript[f, x y])=(Subscript[f, y x]).  The last two outputs confirm Clairaut's Theorem for this particular example.

Method 2: Here is the input using the palette symbol (∂_(□,□))□:

Clear[x,y]
(∂_(x,x))((x^3)*Sin[y]+(E^(x*y)))   
(∂_(y,y))((x^3)*Sin[y]+(E^(x*y)))
(∂_(x,y))((x^3)*Sin[y]+(E^(x*y)))
(∂_(y,x))((x^3)*Sin[y]+(E^(x*y)))

Example 14.22.  Evaluate the first partial derivatives of x y+y (z^2)+x z at (-1,2,3).

Solution: Recall that Expr /.{(Subscript[x, 1])->(Subscript[a, 1,])(Subscript[x, 2])->(Subscript[a, 2,])(Subscript[x, 3])->(Subscript[a, 3,])... } is the command for substituting (Subscript[x, 1]) by (Subscript[a, 1]), (Subscript[x, 2]) by (Subscript[a, 2]), (Subscript[x, 3]) by (Subscript[a, 3]), .... , in Expr.

Clear[x,y,z]
D[x*y + y*z^2 + x*z,x]/.{x-> -1, y->2, z->3}

D[x*y + y*z^2 + x*z,y]/.{x->-1, y->2, z->3}

D[x*y+y*z^2+x*z,z]/.{x->-1, y->2, z->3}

Example 14.23.  Let f(x,y,z)=y (e^x)+x (e^(-y))ln z. Find   (Subscript[f, x x x]), (Subscript[f, x y z]), (Subscript[f, x z z]), (Subscript[f, z x z ]), and (Subscript[f, z z x]).

Solution: First, we define f(x,y,z) in Mathematica. We can use the (∂_(□,□))□ notation. Since the palette gives only two boxes for the variables, we need to add one more box. This can be done by using CTRL +, (comma), that is, hold the CONTROL key and press the COMMA button.  Note also that the command D[f[x,y,z],x,y,z] gives (Subscript[f, xyz]). We demonstrate both methods.

Clear[x,y,z,f]
f[x_,y_,z_]:=y*(E^x)+x *Log[z]*(E^(-y))

(∂_(x,x,x)) f[x,y,z]

(∂_(x,y,z)) f[x,y,z]

(∂_(x,z,z)) f[x,y,z]

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

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

Example 14.24.  Let f(x, y) = x y (((x^2)-(y^2))/((x^2)+(y^2))) if (x,y)≠(0,0) and f(0,0)=0. 
a) Find (Subscript[f, x])(x,y) and (Subscript[f, y])(x,y) for (x,y)≠(0,0).
b) Use the limit definition to find (Subscript[f, x])(0,0) and (Subscript[f, y])(0,0).
c) Find (Subscript[f, x y]) (x,y) and (Subscript[f, y x])(x,y) for (x,y)≠(0,0).
d) Use the limit definition to find (Subscript[f, x y])(0,0) and (Subscript[f, y x])(0,0).

Solution: We will first define f using the If command.

Clear[x,y,f,fx,fy,fxy,fyx]
f[x_,y_]=If[{x,y}≠{0,0}, x y (((x^2)-(y^2))/((x^2)+(y^2))),0]

a)  Let fx and fy denote the partial derivatives with respect to x and y, respectively. Then

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

If we use the FullSimplify command to simplify the preceding output, we get

FullSimplify[fx[x,y]]
FullSimplify[fy[x,y]]

Thus, (Subscript[f, x])(x,y)=((y((x^4)+4(x^3)(y^2)-(y^4)))/((((x^2)+(y^2)))^2)) and  (Subscript[f, y])(x,y)=((x((x^4)-4(x^2)(y^2)-(y^4)))/((((x^2)+(y^2)))^2)) if (x,y)≠(0,0).

b) We use the limit definition (Subscript[f, x])(0,0)=(lim_(h->0 ))((f(0+h,0)-f(0,0))/h)and (Subscript[f, y])(0,0)=(lim_(k->0 ))((f(0,0+k)-f(0,0))/k)  to find the partial derivatives at (0,0).

Clear[h,k]
Limit[((f[0+h,0]-f[0,0])/h),h->0]

Limit[((f[0,0+k]-f[0,0])/k),k->0]

Hence, (Subscript[f, x])(0,0)=0 and (Subscript[f, y])(0,0)=0.

c) To find the mixed second partial derivatives, we use fx and fy from the outputs in part a).  Note that the FullSimplify command is used to to get a simplified form of the mixed partial derivatives.

fxy[x_,y_]=FullSimplify[D[fx[x,y],y]]
fyx[x_,y_]=FullSimplify[D[fy[x,y],x]]

Thus, (Subscript[f, x y])=(((x-y)(x+y)((x^4)+10(x^2)(y^2)+(y^4)))/((((x^2)+(y^2)))^3)) and (Subscript[f, y x])=(((x-y)(x+y)((x^4)+10(x^2)(y^2)+(y^4)))/((((x^2)+(y^2)))^3)) for (x,y)≠(0,0). Note that these two functions are equal for (x,y)≠(0,0) in conformity with Clairaut's Theorem, since both are continuous when (x,y)≠(0,0).

d) We use the limit definition of a partial derivative to compute (Subscript[f, xy])(0,0) and (Subscript[f, yx])(0,0).  Recall that we have defined (Subscript[f, x]) as fx[x,y] and (Subscript[f, y]) as fy[x,y].

Then (Subscript[f, xy])(0,0) is given by

Limit[((fx[0 ,0+k]-fx[0,0])/k),k->0]

and (Subscript[f, yx])(0,0) is given by

Limit[((fy[0+h,0]-fy[0,0])/h),h->0]

Thus, (Subscript[f, xy])(0,0)=-1and (Subscript[f, yx])(0,0)=1.  Note that this implies that the mixed partial derivatives are not continuous at (x,y)=(0,0).  To see this graphically, first consider the following graph of f, which confirms that f has partial derivatives everywhere.

Plot3D[f[x,y],{x,-3,3},{y,-3,3},ImageSize->{250}]

Here are the graphs of (Subscript[f, x]) and (Subscript[f, y]), which now show why the second mixed partials at the origin are not equal.

Clear[plot1,plot2]
plot1= Plot3D[fx[x,y],{x,-3,3},{y,-3,3}, PlotStyle->Red,AxesLabel->{"Graph of z=(Subscript[f, x])", None, None}] ;
plot2= Plot3D[fy[x,y],{x,-3,3},{y,-3,3}, PlotStyle->Blue,AxesLabel->{"Graph of z=(Subscript[f, y])", None, None}] ;
Show[GraphicsArray[{plot1,plot2}],ImageSize->{420}]

In addition, the graphs of (Subscript[f, xy]) and (Subscript[f, yx]) show the mixed partials are not continuous at the origin. This is the main reason why the inequalities of the mixed partials at the origin does not contradict Clairaut's Theorem.

Clear[plot1,plot2]
plot1=Plot3D[fxy[x,y],{x,-3,3},{y,-3,3}, PlotStyle->Red, AxesLabel->{"Graph of z=(Subscript[f, xy])", None, None}];
plot2=Plot3D[fyx[x,y],{x,-3,3},{y,-3,3}, PlotStyle->Blue, AxesLabel->{"Graph of z=(Subscript[f, yx])", None, None}];
Show[GraphicsArray[{plot1,plot2}],ImageSize->{420}]

Exercises

1. Let f(x,y)=((((x-y))^2)/((x^2)+(y^2))).  Find:  
a. (Subscript[f, x]) (1,0)	b. (Subscript[f, y])(1,0)	c. (Subscript[f, xy])		d. (Subscript[f, yx])		e.  (Subscript[f, xxy])

2. Find the first partial derivatives of z=(x^3)(y^2) with respect to (x^( ))and y.

3. Find the four second partial derivatives of (x^2)cos(y) +tan(x (e^y)).

4. Evaluate the first partial derivatives of f(x,y,z)=(e^(-z))xy +(yz^2)+xz at (-1,2,3).

5. Let f(x,y,z)=(((x^4)(y^3))/((z^2)+sin x)).  Find (Subscript[f, xxx]), (Subscript[f, xyz]), (Subscript[f, xzz]), (Subscript[f, zxz ]), and (Subscript[f, zzx]).

6. Let f(x, y) = ((x (y^2))/((x^2)+(y^4))) if (x,y)≠(0,0) and f(0,0)=0. 
a.  Find (Subscript[f, x])(x,y) and (Subscript[f, y])(x,y) for (x,y)≠(0,0).
b.  Use the limit definition to find (Subscript[f, x])(0,0) and (Subscript[f, y])(0,0).
c.  Find (Subscript[f, xy]) (x,y) and (Subscript[f, yx])(x,y) for (x,y)≠(0,0).
d.  Use the limit definition to find (Subscript[f, xy])(0,0) and (Subscript[f, yx])(0,0).

14.4  Tangent Planes

Students should read Section 14.4 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Let z=f(x,y) be a function of two variables.  The equation of the tangent plane at the point (a,b,f(a,b)) is given by

z=(Subscript[f, x])(a,b)(x-a) +(Subscript[f, y])(a,b)(y-b)+f(a,b)

Example 14.25.  Let f(x, y) =  (x^2)+(y^2).
a) Find the equation of the tangent plane to the graph of f at the point (2,1,3).
b) Plot the graph of f and its tangent plane at (2,1,3).

Solution: Here, a=2, b=1.
a)

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

Thus, the equation the of the tangent plane is

A=(∂_x) f[x,y] /.{x->2,y->1};
 B=(∂_y) f[x,y] /.{x->2,y->1};
z=A (x-2) + B(y-1)+f[2,1];
Simplify[z]

b) Here is a plot of the graph of f:

plot1=Plot3D[{f[x,y],z},{x,-10,10},{y, -10, 10},PlotStyle-> {Blue,Green}];
plot2=ListPointPlot3D[{ {2,1,3}},PlotStyle-> {Red,PointSize[Large]}  ];
Show[plot1,plot2,ImageSize->{250},ImagePadding->{{15,15},{15,15}}]

Example 14.26.  Let f(x, y) =(x^2) y-6 x (y^2)+3y.  Find the points where  the tangent plane to the graph of f is parallel to the xy-plane.

Solution:  For the tangent plane to be parallel to the xy-plane, we must have (Subscript[f, x])=0 and (Subscript[f, y])=0.

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

A tangent plane is parallel to the xy-plane at

Solve[{ D[f[x,y],x] ==0,  D[f[x,y],y] ==0}]

Rotate the following graph to see the points of tangencies.

Plot3D[{f[x,y],f[-1,-1/3],f[1,1/3]},{x,-1,1},{y,-1,1},PlotStyle-> {LightBlue,Green,Red},PlotRange->All,ImageSize->{250},ImagePadding->{{15,15},{15,15}}]

Exercises

1. Let  f(x,y)=(x^(3 ))y +x (y^2)-3x +4.
a) Find a set of parametric equations of the normal line and an equation of the tangent plane to the surface at the point (1, 2).
b) Graph the surface, the normal line, and the tangent plane found in a).

2. Let f(x, y) =  (x^2)+(y^2).
a. Find the equation of the tangent plane to the graph of f at the point (2,1,5).
b. Plot the graph of f and its tangent plane at (2,1,5).

3.  Let f(x, y) =  (e^(-y/x)).
a. Find the equation of the tangent plane to the graph of f at the point (1,0,1).
b. Plot the graph of f and its tangent plane at (1,0,1).

4.  Let f(x, y) =cos (x y).  Find the points where  the tangent plane to the graph of f is parallel to the xy-plane.

14.5  Gradient and Directional Derivatives

Students should read Section 14.5 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Recall that the notation for a vector such as u=2i +5j -6k in Mathematica is {2,5,-6}.  The command for the dot product of two vectors u and v is obtained by typing u.v.

The gradient of f, denoted by  ∇f, at (a,b) can be obtained by evaluating ∇f(a,b)=⟨(∂_x)f(a,b), (∂_y)f(a,b)⟩.

The directional derivative of f at (a,b) in the direction of a unit vector u is given by (Subscript[D, u])f=∇f(a,b)·u.

Example 14.27.  Find the gradient and directional derivative of f(x,y)=(x^2)sin 2y at the point  (1,(π/2),0)   in the direction of v=⟨(3/5),-(4/5)⟩.

Solution:

Clear[f,v]
f[x_,y_]:=(x^2) *Sin[2y]
v={(3/5),((-4)/5)}

The gradient of f at (1,(π/2))  is

∇f ={(∂_x) f[x,y],(∂_y) f[x,y]}/.{x->1,y->(π/2)}

Since v is a unit vector, the directional derivative is given by

direcderiv=v.∇f

Example 14.28.  Find the gradient and directional derivative of  f(x,y,z)=x y+y z+x z at the point  (1, 1, 1)  in the direction of v=2i+j-k.

Solution:

Clear[x,y,z]
w=x*y+y*z+x*z
v={2,1,-1}

We normalize v:

unitvector=v/Norm[v]

The gradient of w=f(x,y,z) at (1,1,1) is

∇w={D[w,x],D[w,y],D[w,z]}/.{x->1,y->1,z->1}

Hence, the directional derivative is given by

direcderiv=unitvector.∇w

Example 14.29.  Plot the gradient vector field and the level curves of the function f(x,y)=(x^2)sin 2y.

Solution:

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

Thus, the gradient vector field is ∇f(x,y)=⟨ 2x-3y,1-3x-2y⟩.  To plot this vector field, we need to download the package VectorFieldPlots, which is done by evaluating

Needs["VectorFieldPlots`"]

Here is a plot of some level curves and the gradient field.

Clear[plot1,plot2]
plot1=ContourPlot[f[x,y],{x, -5,5},{y,-4,4},Axes->True,Frame->False,Contours->15, ColorFunction->Hue] ;
plot2=VectorFieldPlot[{fx,fy},{x, -5,5},{y,-4,4},Axes->True,Frame->False];
Show[plot1, plot2,ImageSize->{250}]

Example 14.30.  Let the temperature T at a point (x,y) on a metal plate be given by  T(x,y)=(x/((x^2)+(y^2))). 
a) Plot the graph of the temperature. 
b) Find the rate of change of temperature at (3,4), in the direction of v=i-2j.
c) Find the unit vector in the direction of which the temperature increases most rapidly at (3,4).
d) Find the maximum rate of increase in the temperature at (3,4).

Solution: 
a)  Here is the graph of T.

T[x_,y_]=(x/((x^2)+(y^2)))

graphofT=Plot3D[T[x,y],{x,-5,5},{y,-5,5},BoxRatios->{1,1,1},ImageSize->Small]

b) Let u=(v/(‖v‖)).  Then u is a unit vector and the rate of change in temperature at (3,4) in the direction of v is given by (Subscript[D, u])T(3,4)=∇f(3,4)·u.

∇T={D[T[x,y],x],D[T[x,y],y]}
v={1,-2}
u=(v/(Sqrt[v.v]))
u.∇T/.{x->3,y->4}//N

Thus, the rate of change at (3,4) in the direction v is 0.0393548.  NOTE: The command //N in the last line of the previous input converts the output to decimal form.

c) The unit vector in the direction of which the temperature increases most rapidly at (3,4) is given by

((∇T)/(Norm[∇T]))/.{x->3,y->4}

d) The maximum rate of increase in the temperature at (3,4) is the norm of the gradient at this point. This can be obtained by:

Norm[∇T]/.{x->3,y->4}

Exercises

1. Find the gradient and directional derivative of  f(x,y)=(sin^(-1))(x y) at the point  (1,1,(π/2))   in the direction of v=⟨1,-1⟩.

2. Let  T(x,y)=(e^(x y -(y^2))).
a. Find ∇ T(x, y).
b. Find the directional derivative of  T(x,y) at the point (3,5) in the dierection of u = 1/2 i  + (Sqrt[3])/2j.
c. Find the direction of greatest increase in T from the point (3,5).

3.  Plot the gradient vector field and the level curves of the function a f( x , y ) = cos x ((sin )^2)y.

4.  Find the gradient and directional derivative of  f(x,y,z) =  x y (e^( y z))+ sin (x z) at the point (1,1,0)  in the direction of v=i-j-k.

14.6  The Chain Rule

Students should read Section 14.6 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Example 14.31.  Let x=(t^2)+s ,y=t+(s^(2 )) and  z=x sin y.  Find the first partial derivatives of z with respect to s and t.

Solution:

Clear[x,y,z,s,t]
x=(t^2)+s
y=t+(s^2)
z=x Sin[y]

D[z,s]

D[z,t]

Example 14.32.  Find the partial derivatives of z with respect to x and y assuming that the equation (x^2)z-y (z^2)=x y defines z as a function of x and y.

Solution:

Clear[x,y,z,r,t,s]
eq= (x^2)z[x,y] -y ((z[x,y])^2)==x y

Solve[D[eq,x],D[z[x,y],x]]
Solve[D[eq,y],D[z[x,y],y]]

Example 14.33.  Let f(x,y,z)=F(r), where r=(Sqrt[(x^2)+(y^2)+(z^2)]) and F is a twice differentiable function of one variable. 
a) Show that ∇f=F'(r) (1/r)(x i +y j+z k).
b) Find the Laplacian of f.

Solution: 
a)

Clear[x,y,z,r,f,F]
f[x_,y_,z_]=F[r]

r=(Sqrt[(x^2)+(y^2)+(z^2)])

Here is the gradient of f:

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

With r=(Sqrt[(x^2)+(y^2)+(z^2)]), the preceding output becomes

∇f(x,y,z)=⟨  ((x F'(r))/r), ((y F'(r))/r),((z F'(r))/r)⟩=F'(r)(1/r)⟨ x ,y ,z ⟩

which proves part a).

b) Recall that the Laplacian of f, denoted by Δ f, is defined by Δ f=(Subscript[f, xx])+(Subscript[f, yy])+(Subscript[f, zz]).

D[f[x,y,z],{x,2}]+D[f[x,y,z],{y,2}]+D[f[x,y,z],{z,2}]

We simplify this to get

Simplify[%]

which is the same as (2/r)F'[r]+F''[r].

Exercises

1. Let x=(u^2)+sin v, y=u (e^(v/u)), and z=(y^3)ln x  .  Find the first partial derivatives of z with respect to u and v.

2. Find the partial derivatives of z with respect to x and y assuming that the equation (x^2)z-y (z^2)=x y defines z as a function of x and y.

3. Find an equation of the tangent plane to the surface x z +2 (x^2)y +(y^2)(z^3)=11 at (2,1,1).

14.7  Optimization

Students should read Section 14.7 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Second Derivative Test: Suppose (Subscript[f, x])(a,b)=0 and (Subscript[f, y])(a,b)=0.  Define

D(x,y)=(Subscript[f, x x])(Subscript[f, y y ])-((((Subscript[f, x y])))^2)

The function D is called the discriminant function.

i)   If D(a,,b)>0 and (Subscript[f, x x])(a,b)>0, then f(a,b) is a local minimum value.
ii)   If D(a,,b)>0 and (Subscript[f, x x])(a,b)<0, then f(a,b) is a local maximum value.
iii)  If D(a,,b)<0, then (a,b,f(a,b)) is a saddle point on the graph of f.
iv)  If D(a,b)=0, then no conclusion can be drawn about the the point (a,b).

Example 14.34.  Let f(x,y)=(x^4)-4 x y+2(y^2). 
a)  Find all critical points of f.
b)  Use the second derivative test to classify the critical points as local minimum, local maximum, saddle point, or neither.

Solution: Since D is used in Mathematica as the command for derivative, we will use disc for the discriminant function D.

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

a) The critical points are given by

cp=Solve[{D[f[x,y],x]==0,D[f[x,y],y]==0}]

b)

Clear[fxx,disc]
fxx[x_,y_]=D[f[x,y],{x,2}]
disc[x_,y_]=D[f[x,y],{x,2}]*D[f[x,y],{y,2}]-(((D[D[f[x,y],x],y]))^2)

TableForm[Table[{ cp[[k,2,2]],   cp[[k,1,2]] ,  disc[cp[[k,2,2]],cp[[k,1,2]]], fxx[cp[[k,2,2]],cp[[k,1,2]]],f[cp[[k,2,2]],cp[[k,1,2]]]},{k,1,Length[cp]}],
TableHeadings->{{},{"x ", "y  ",  "  D(x,y) ","  (Subscript[f, xx])  ","f(x,y)"}}]

By the second derivative test, we conclude that f has a local minimum value of -1 at (-1,-1) and (1,1), and a saddle point at (0,0).

Here is the graph of f and the relevant points.

Clear[plot1,plot2]
plot1=Plot3D[f[x,y],{x,-2,2},{y, -2,2},PlotStyle-> LightBlue,PlotRange-> 10];
plot2=Graphics3D[{PointSize[Large], Red, Point[Table[{ cp[[k,2,2]],   cp[[k,1,2]] ,  f[cp[[k,2,2]],cp[[k,1,2]]]},{k,1,Length[cp]}]]}, PlotRange->10];
Show[plot1,plot2,ImageSize->{250}]

Example 14.35.  Let f(x,y)=(x^3)+(y^4)-6x-2(y^2). 
a)  Find all critical points of f.
b)  Use the second derivative test to classify the critical points as local minimum, local maximum, saddle point, or neither.

Solution: Again, we will use disc to denote the discriminant function D since the letter D is used in Mathematica for the derivative command.

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

a) The critical points are given by

cp=Solve[{D[f[x,y],x]==0,D[f[x,y],y]==0}]

b)

Clear[fxx,disc]
fxx[x_,y_]=D[f[x,y],{x,2}]
disc[x_,y_]=D[f[x,y],{x,2}]*D[f[x,y],{y,2}]-(((D[D[f[x,y],x],y]))^2)

TableForm[Table[{ cp[[k,2,2]],   cp[[k,1,2]] ,  disc[cp[[k,2,2]],cp[[k,1,2]]], fxx[cp[[k,2,2]],cp[[k,1,2]]],f[cp[[k,2,2]],cp[[k,1,2]]]},{k,1,Length[cp]}],
TableHeadings->{{},{"x ", "y  ",  "  D(x,y) ","  (Subscript[f, xx])  ","f(x,y)"}}]

By the second derivative test we conclude that f has local maximum value of 4(Sqrt[2]) at (-(Sqrt[2]),0), local minimum value of -1-4(Sqrt[2]) at ((Sqrt[2]),-1) and ((Sqrt[2]),1), and saddle points at (-(Sqrt[2]),-1), ((Sqrt[2]),0), and  (-(Sqrt[2]),1).

Here is the graph of f and the relevant points.

Clear[plot1,plot2]
plot1=Plot3D[f[x,y],{x,-2.5,2.5},{y, -2.5, 2.5},PlotStyle-> LightBlue,PlotRange-> 10];
plot2=Graphics3D[{PointSize[Large], Red, Point[Table[{ cp[[k,2,2]],   cp[[k,1,2]] ,  f[cp[[k,2,2]],cp[[k,1,2]]]},{k,1,Length[cp]}]]}, PlotRange->10];
Show[plot1,plot2,ImageSize->{250}]

Example 14.36.  Let f(x,y)=2(x^2)-3x y-x+y+(y^2) and let R be the rectangle in the xy-plane whose vertices are at (0,0), (2,0), (2,2), and  (0,2). 
a) Find all relative extreme values of f inside R.
b) Find the maximum and minimum values of f on R.

Solution:

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

Solve[{(∂_x) f[x,y]==0,(∂_y) f[x,y]==0},{x,y}]

disc[x_,y_]= (∂_(x,x))f[x,y]*(∂_(y,y))f[x,y]-((((∂_(x,y))f[x,y]))^2)

(∂_(x,x))f[x,y]/.{x->1,y->1}
disc[x,y]/.{x->1,y->1}

Thus, (1,1) is the local minimum point of f inside R and its local minimum value is f(1,1)=5. Next, we find the extreme values of f on the boundary of the rectangle. This is done by considering f as a function of one variable corresponding to each side of R.  Let (Subscript[f, 1])=f(x,0), (Subscript[f, 2])=f(x,2), for x between 0 and 2, and (Subscript[f, 3])=f(0,y) and (Subscript[f, 4])=f(2,y), for y between 0 and 2.   We now proceed as follows:

Clear[f1,f2,f3,f4]
f1=f[x,0]
f2=f[x,2]
f3=f[0,y]
f4=f[2,y]

Solve[D[f1,x]==0 ]

Solve[D[f2,x]==0 ]

Solve[D[f3,y]==0 ]

Solve[D[f4,y]==0 ]

Thus, points on the boundary of R that are critical points of f are ((1/4),  0) and ((7/4), 2).  Observe that the points (0, -1/2) and (2,(5/2)) are outside the rectangle R. The four vertices of R at (0,0), (2,0), (0,2) and (2,2) are also critical points.  Can you explain why?  We now evaluate f at each of these points and at (1,1) (the relative minimum point found earlier) using the substitution command and compare the results.

f[x,y]/.{{x->(1/4),y->0},  {x->(7/4),y->2},
		{x->0,y->0},{x->2,y->0},{x->0,y->2},{x->2,y->2},{x->1,y->1}}

Thus, the minimum value of f is 39/8, which occurs at (1/4,0) and also at (7/4,2).  The maximum value of f is 6, which is attained at (2,0) and also at (0,2).  Here is the graph of f over the rectangle R.

Clear[plot1,plot2,plot3]
plot1=Plot3D[{f[x,y],0},{x,0,2},{y,0,2},PlotStyle->{Green,Blue},PlotRange->All];
plot2=Graphics3D[{PointSize[Large], Red , Point[{ {1/4,0,f[1/4,0]},{7/4,2,f[7/4,2]} }]}, PlotRange->All ];
plot3=Graphics3D[{PointSize[Large],Black , Point[{  {2,0,f[2,0]},{0,2,f[2,0]}}]}, PlotRange->All ];
Show[plot1,plot2 ,plot3,ImageSize->{250},ImagePadding->{{15,15},{15,15}}]

Exercises

1. Let f(x,y)=(x^4)-4 x y+2(y^2). 
a. Find all critical points of f.
b. Use the second derivative test to classify the critical points as local minimum, local maximum, saddle point, or neither.
c. Plot the graph of f and the local extreme points and saddle points, if any.

2. Let f(x,y)= (x+y)ln((x^2)+(y^2)), for (x,y)≠(0,0). 
a. Find all critical points of f.
b. Use the second derivative test to classify the critical points as local minimum, local maximum, saddle point, or neither.
c. Plot the graph of f and the local extreme points and saddle points, if any.

3. Let f(x,y)=2(x^2)-3x y-x+y+(y^2) and let R be the rectangle in the xy-plane whose vertices are at (0,0), (2,0), (2,2), and (0,2). 
a. Find all relative extreme values of f inside R.
b. Find the maximum and minimum values of f on  R.
c. Plot the graph of f and the local extreme points and saddle points, if any.

14.8  Lagrange Multipliers

Students should read Section 14.8 of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Example 14.37.   Let f(x,y)=x y and g(x,y)=(x^2)+(y^2)-4.
a) Plot the level curves of f and g as well as their gradient vectors. 
b) Find the maximum and minimum values of f subject to the constraint g(x,y)=0.

Solution: 
a) We will define f and g and compute their gradients. Recall that we need to evaluate the command Needs["`VectorFieldPlots`"] before we plot the gradient fields.

Clear[f,g,fx,fy,gx,gy,x,y]
f[x_,y_]=2x+3y
g[x_,y_]=(x^2)+(y^2)-4
fx=D[f[x,y],x]
fy=D[f[x,y],y]
gx=D[g[x,y],x]
gy=D[g[x,y],y]

Needs["VectorFieldPlots`"]

Clear[plot1,plot2,plot3,plot4]
plot1=ContourPlot[(x^2)+(y^2)-4,{x,-2,2},{y,-2,2},Frame->False,Axes->True, ContourShading->False,PlotRange->All];
plot2= ContourPlot[2x+3y,{x,-2,2},{y,-2,2},Frame->False,Axes->True, ContourShading->False,PlotRange->All];
plot3=VectorFieldPlot[{fx,fy},{x,-2,2},{y,-2,2},Axes->True,Frame->False, ColorFunction->Hue];
plot4=VectorFieldPlot[{gx,gy},{x,-2,2},{y,-2,2},Axes->True,Frame->False, ColorFunction->Hue];
 Show[plot1, plot2,plot3,plot4,ImageSize->{250}]

b) Let us use l for λ. To solve ∇f=l ∇g we compute

Solve[{fx==  l gx,fy==l gy, g[x,y]==0} ]

Thus, (-(4/(Sqrt[13])),-(6/(Sqrt[13]))) and ((4/(Sqrt[13])),(6/(Sqrt[13]))) are the critical points. We evaluate f at these points to determine the absolute maximum and the absolute minimum of f on the graph of g(x,y)=0.

f[ -(4/(Sqrt[13])),-(6/(Sqrt[13]))]
f[(4/(Sqrt[13])),(6/(Sqrt[13]))]

Hence, f attains its absolute minimum value of -2(Sqrt[13]) at (-(4/(Sqrt[13])),-(6/(Sqrt[13]))) and absolute maximum value of -2(Sqrt[13]) at ((4/(Sqrt[13])),(6/(Sqrt[13]))).

Here is a combined plot of the gradients of f (in black) and g (in red) at the critical points.

Clear[plot1,plot2,plot3, plot4,plot5,plot6]
plot1= ContourPlot[g[x,y],{x,-3,3}, {y, -3,3}, Contours->{0},Frame->False,Axes->True, ContourShading->False];
plot2=ListPlot[{{ -(4/(Sqrt[13])),-(6/(Sqrt[13]))}, {(4/(Sqrt[13])),(6/(Sqrt[13]))}}];

plot3=Graphics[Arrow[{{ -(4/(Sqrt[13])),-(6/(Sqrt[13]))},{ -(4/(Sqrt[13])),-(6/(Sqrt[13]))} +{fx,fy}/.{x->  ((-4)/(Sqrt[13])),y->((-6)/(Sqrt[13]))}  }]];

plot4=Graphics[Arrow[{ {(4/(Sqrt[13])),(6/(Sqrt[13]))} ,{(4/(Sqrt[13])),(6/(Sqrt[13]))}+{fx,fy}/.{x->  (4/(Sqrt[13])),y->(6/(Sqrt[13]))}  }]];

plot5=Graphics[{Red,Arrow[{{ -(4/(Sqrt[13])),-(6/(Sqrt[13]))} ,{ -(4/(Sqrt[13])),-(6/(Sqrt[13]))}+ {gx,gy}/.{x->  ((-4)/(Sqrt[13])),y->((-6)/(Sqrt[13]))} }]}];

plot6=Graphics[{Red,Arrow[{ {(4/(Sqrt[13])),(6/(Sqrt[13]))} ,{(4/(Sqrt[13])),(6/(Sqrt[13]))}+{gx,gy}/.{x->  (4/(Sqrt[13])),y->(6/(Sqrt[13]))} }]}];

Show[plot1,plot2,plot3,plot4, plot5,plot6,PlotRange->All,AspectRatio->Automatic,ImageSize->{250}]

Exercises

1.  Let f(x,y)=4(x^2)+9(y^2) and g(x,y)=x y-4.
a. Plot the level curves of f and g as well as their gradient vectors. 
b. Find the maximum and minimum values of f subject to g(x,y)=0.

2.  Find the maximum and minimum values of f(x,y,z)=(x^3)-3(y^2)+4z subject to the constraint g(x,y,z)=x +y z-4=0.

3.  Find the maximum area of a rectangle that can be inscribed in the ellipse ((x^2)/(a^2))+((y^2)/(b^2))=1.

4.  Find the maximum volume of a box that can be inscribed in the sphere (x^2)+(y^2)+(z^2)=4.