Basic Partial and Total Derivatives
of
z = f[x,y]

by
K. D.  Stroyan
University of Iowa

copyright 1997 by Academic Press, Inc. - All rights reserved.

Special Mathematica Functions used in this NoteBook

Off[General::spell];
Off[General::spell1];
Off[ParametricPlot3D::ppcom]
Needs["Graphics`Colors`"]

SetOptions[Graphics,Axes->False,AxesOrigin->{0,0},AspectRatio->Automatic];
SetOptions[Graphics3D,ViewPoint->{5,2,1},Boxed->False,AspectRatio->Automatic];
SetOptions[ParametricPlot3D,ViewPoint->{5,2,1},AspectRatio->Automatic];
SetOptions[Plot3D,ViewPoint->{5,2,1},BoxRatios->{1,1,1}];
SetOptions[ParametricPlot,AspectRatio->Automatic];
SetOptions[Plot,AspectRatio->Automatic];

colorQ[a_]:=(Head[a]===RGBColor);
grayQ[a_]:=(Head[a]===GrayLevel);
thickQ[a_]:=(Head[a]===Thickness);
absthickQ[a_]:=(Head[a]===AbsoluteThickness);
notruleQ[u_] := !(Head[u]===Rule);
ruleQ[u_] := (Head[u]===Rule);

Norm::usage = "Norm[expr] returns the Euclidean length of the
		vector, 'expr'.";

Norm[a_?VectorQ] := Sqrt[a.a];

Angle[a_?VectorQ,b_?VectorQ] := ArcCos[(a.b)/(Norm[a] Norm[b])]

Proj[a_?VectorQ,b_?VectorQ] := ((a.b)/(b.b))b

tol = .5*10^-6;

OrthUp[a_?VectorQ] :=
	Module[{b,c,d,f},
		d = N[a/Norm[a]];
		If[Chop[d[[3]]^2 - 1,tol] == 0, b={1,0,0} ,
		b = {0,0,1} - d[[3]] d; b = b/Norm[b];];
	Return[b]];
	
OrthSet[a_?VectorQ] :=
	Module[{b,c,d},
		d = N[a/Norm[a]];
		If[Chop[d[[1]],tol]==0,b={1,0,0},
			 If[Chop[d[[2]],tol]==0,b={0,1,0},
			  b={-d[[2]],d[[1]],0}]];
			b=b/Norm[b];c = Cross[d,b];
	Return[{b,c/Norm[c],d}]];

				
Rotate[a_?VectorQ,theta_] :=
	Module[{u,v,r},
		u = OrthSet[a];
		v = Transpose[u];
		r = N[{{Cos[theta],-Sin[theta],0},{Sin[theta],Cos[theta],0},{0,0,1}}];
	Return[v.r.u]];

Vector2D[tail_,tip_,opts___]:=
	Module[{ntail,ntip,sopts,Punit,up,arrow},
	ntail = N[tail];
	ntip = N[tip];
	
	sopts = Join[Select[{opts},colorQ],Select[{opts},
				thickQ],Select[{opts},absthickQ],Select[{opts},grayQ]];

If[Chop[ntail.ntail+ntip.ntip,tol] == 0,
			(*too small to plot or not number*)
			Return[Graphics[Point[{0,0}]]];
	,						
If[Chop[(ntip-ntail).(ntip-ntail),tol] == 0,
		Return[Graphics[{Join[sopts,{Line[{ntail,ntip}]}]}]];
	,
	Punit = (.075 Norm[ntip-ntail]/Norm[ntip-ntail]) (ntip-ntail) ;
	up = 0.15 {-Punit[[2]],Punit[[1]]};
	arrow = Line[{ntail,ntip,ntip-Punit+up,ntip,ntip-Punit-up}];
	]];
Return[Graphics[{Join[{AbsoluteThickness[2]},Join[sopts,{arrow}]]}]];
];

Vector3D[tail_,tip_,opts___]:=
	Module[{ntail,ntip,sopts,Punit,up,u2,u3,u4,rotmat,arrow},
	ntail = N[tail];
	ntip = N[tip];
	
	sopts = Join[Select[{opts},colorQ],Select[{opts},
				thickQ],Select[{opts},absthickQ],Select[{opts},grayQ]];

If[Chop[ntail.ntail+ntip.ntip,tol] == 0,
			(*too small to plot or not number*)
			Return[Graphics3D[Point[{0,0,0}]]];					
	,		
If[Chop[(ntip-ntail).(ntip-ntail),tol] == 0,
		Return[Graphics3D[{Join[sopts,{Line[{ntail,ntip}]}]}]];
	,
	Punit = (ntip-ntail);
	rotmat = Rotate[Punit,N[Pi/2]];
	up = OrthUp[Punit];
	Punit = .075 Punit;
	up = .15 Norm[Punit] up;
	u2 = rotmat.up;
	u3 = rotmat.u2;
	u4 = rotmat.u3;
	arrow = Line[{ntail,ntip,
				ntip - Punit + up,ntip,
				ntip - Punit + u2,ntip,
				ntip - Punit + u3,ntip,
				ntip - Punit + u4}];
	]];
Return[Graphics3D[{Join[{AbsoluteThickness[2]},Join[sopts,{arrow}]]}]];
];

aRCtAN[y_,x_] := Module[{v},
	If[ Chop[N[x],tol] == 0 ,If[ N[y] < 0, v = -N[Pi/2], v = N[Pi/2]],
		v = N[ArcTan[y/x]];
		If[ x < 0 && y < 0, v = N[Pi -  v],v]];
	Return[v]];

Arc2D[X_,Y_,opts___] := Graphics[{opts,
		Disk[{0,0},1,{Min[
							aRCtAN[
						N[X[[2]]],N[X[[1]]]
								]
							,
							aRCtAN[
							N[Y[[2]]],N[Y[[1]]]
								]
						]
						,
					Max[
							aRCtAN[
						N[X[[2]]],N[X[[1]]]
								]
							,
							aRCtAN[
							N[Y[[2]]],N[Y[[1]]]
								]
						]}
				]}
				];
Arc3D[X_,Y_] := 
	Module[{t,U,V},
		U[V_]:= N[(1/Sqrt[V.V]) V];
		Return[Graphics3D[
		Polygon[Append[Table[U[t X + (1-t) Y],{t,0,1,0.1}],{0,0,0}]]
				]
		]
	];

(*Zero Arrow causes trouble. Put in Vector?*)

Vector[tail_,tip_,opts___]:=
	If[Length[N[tail]]==2,Return[Vector2D[tail,tip,opts]],
		Return[Vector3D[tail,tip,opts]]];
		
Arrow[vec_,opts___]:= 
	Module[{a,b},
				If[Length[N[vec]]==2,
				a=ToExpression["Tail"]/.Select[{opts},ruleQ]/.
					{ToExpression["Tail"]->{0,0}};
				Return[Vector2D[a,a+vec,opts]],
				a=ToExpression["Tail"]/.Select[{opts},ruleQ]/.
					{ToExpression["Tail"]->{0,0,0}};
				Return[Vector3D[a,a+vec,opts]]]
				];

Frame2D[delta_] := 
      Graphics[{
            {Green,Line[{{-delta/4,0},{delta,0}}]},
                    Text["x",{1.01 delta,0}],
            {Green,Line[{{0,-delta/4},{0,delta}}]},
                    Text["y",{0, 1.01 delta}]}];

Frame3D[delta_] := 
      Graphics3D[{
            {Green,Line[{{-delta/4,0,0},{delta,0,0}}]},
                    Text["x",{1.01 delta,0,0}],
            {Green,Line[{{0,-delta/4,0},{0,delta,0}}]},
                    Text["y",{0, 1.01 delta,0}],
            {Green,Line[{{0,0,-delta/4},{0,0,delta}}]},
                    Text["z",{0,0,1.01 delta}]}];

View2D[stuff_] := Show[stuff,Frame2D[1]];
View3D[stuff_] := Show[stuff,Frame3D[1]];

sliceAt[f_,xCnt_,yCnt_,scale_,z_,zMIN_,opns___] :=
	Module[{surface,plane,contour,pLz,contLz,w},

	surface = Plot3D[ f[x,y] , {x,xCnt-scale,xCnt+scale},
						{y,yCnt-scale,yCnt+scale},opns,
				PlotPoints->30,BoxRatios->Automatic,
				AxesLabel->{"x","y","z"},
				ViewPoint->{1.149, -3.086, 0.779},
				DisplayFunction->Identity];


	plane[w_] := Plot3D[ w , {x,xCnt-scale,xCnt+scale},
						{y,yCnt-scale,yCnt+scale},
				PlotPoints->2,BoxRatios->Automatic,
				Ticks->None,DisplayFunction->Identity];
						
	contour[w_] := ContourPlot[ f[x,y] ,
		{x,xCnt-scale,xCnt+scale},{y,yCnt-scale,yCnt+scale},
		PlotPoints->30, Contours->{w},DisplayFunction->Identity];

				
	pLz = plane[z];

	contLz = Show[Evaluate[contour[z]],
			ContourShading->False,
			DisplayFunction -> Identity];

	contLz = First[Graphics[contLz]];
					
	contLz = Graphics3D[contLz /. Line[x_]:>
			Line[Map[Append[#,zMIN]&,x]]];

Show[surface,pLz,contLz,opns,
		DisplayFunction->$DisplayFunction]]

Notebook Overview

This NoteBook shows how to use Mathematica to compute symbolic partial derivatives.  The rules are the same as in Calculus 1, but there are more variables, so it takes practice to treat some letters as a constant while applying rules to others.

References to the Text

This NoteBook goes with Section 18.2 of the text.

Step-by-step

The D[.] operator

The derivative of an expression  "expr" with respect to a "variable" is denoted

D[ expr , variable ]

in Mathematica.  Compute the partials of  z = (x^2) + (y^3) by hand and compare your work to the result of <Enter>ing the next computation.

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

Practice Exercise

Use the idea above to calculate the partial derivatives of 
	
		f[x, y] = (x^2) (y^3)

Making the Partial Derivatives Functions

To make the partial derivatives functions of the same sort as  f[x,y], we need to Evaluate[.] the derivitave computed with D[.] as follows:

Clear[x, y, f, fX, fY]; \(f[x_, y_] := Exp[x\^2\ y\^3]; 


    fX[x_, y_]\  := \ Evaluate[D[\ f[x, y], x]]; 

    fY[x_, y_]\  := \ Evaluate[D[\ f[x, y], y]]; 


    Print["The partial derivatives of z =", f[x, y]]; 

    \(Print["Partial df/dx = ", fX[x, y]]; \)\)Print["Partial df/dy = ", fY[x, y]];

Practice Exercise

Use the idea above to define partial derivatives functions for

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

Making the Total Differential

To make the partial derivatives functions of the same sort as  f[x,y], we need to Evaluate[.] the derivitave computed with D[.] as follows:

Clear[x, y, f, fX, fY]; \(f[x_, y_] := Cos[x\^2 + y\^3]; 


    fX[x_, y_]\  := \ Evaluate[D[\ f[x, y], x]]; 

    fY[x_, y_]\  := \ Evaluate[D[\ f[x, y], y]]; 


    dz[dx_, dy_] := fX[x, y]\ dx + fY[x, y]\ dy; 

    \(Print["The total differential of z =", f[x, y]]; \)\)Print["dz = ", dz[dx, dy]];

Practice Exercise

Use the idea above to define the total differential of

f[x, y]  =  Log[(x^2) + (y^3)]

Using the Total Differential

The CHANGE in the function  f[x,y]  corresponding to a small change in x and y is approximately the differential with an error that is small compared with the change in the independent variable vector  {dx,dy},

f[x+dx,y+dy] - f[x,y] ≈ dz[dx,dy]

To use this approximation, we need to fix the values of  x  and  y and then compute with the differential:

Clear[x, y, f, fX, fY, dx, dy]; \(f[x_, y_] := Cos[x]\^2 + Sin[y\^3]; 


    fX[x_, y_]\  := \ Evaluate[D[\ f[x, y], x]]; 

    fY[x_, y_]\  := \ Evaluate[D[\ f[x, y], y]]; 


    dz[dx_, dy_] := fX[x, y]\ dx + fY[x, y]\ dy; 
\(x = \(-1.0\); \)\)y = 2.0; Print["The total differential of z =", f[x, y]]; Print["dz = ", dz[dx, dy]]; dx = 0.001; dy = -0.002; Print["Actual change = ", f[x + dx, y + dy] - f[x, y]]; Print["Differential approximation = ", dz[dx, dy]]; Print["Error/|dX| = ", 
      ((f[x + dx, y + dy] - f[x, y] - dz[dx, dy])/(Sqrt[(dx^2) + (dy^2)]))];

Practice Exercise

Use the idea above to approximate

f[x,y] = 60 x/y

for an initial point (x,y) = (1,1) and a change in (dx,dy) of (-2/60, 1/5280).

Solving Equations in Partials

Critical points of functions  y = f[x]  are places where the graph has a horizontal tangent.  For example, consider the following example:

\(Clear[f, x]; 
f[x_] := \ x^3\  - \ 3  x\  + \ 1; 

    Plot[f[x], {x, \(-2\), 2}, PlotStyle -> {Red}]; 

    Solve[\(f'\)[x]\  == \ 0, x]\)

The analogous thing for a function  z = f[x,y]  is to have a horizontal tangent plane.  This happens when the total derivative is zero,  dz = 0 dx + 0 dy, or, in other words when both partial derivatives are zero.  If the equations are algebraic or simple transcendental equations, Mathematica can solve them.  (If not, FindRoot[.] can still be used to approximate solutions.)  Here is an example:

\(Clear[x, y, f, fX, fY]; 
f[x_, y_] := \ x - x\^3/3 - x\ y\^2; 


    fX[x_, y_]\  := \ Evaluate[D[\ f[x, y], x]]; 

    fY[x_, y_]\  := \ Evaluate[D[\ f[x, y], y]]; 


    gradf[x_, y_] := {fX[x, y], fY[x, y]}; 

gradf[x, y]
\)solns  =  Solve[gradf[x, y]  ==  {0, 0}, {x, y}]

Now let's Plot and see what is happening at these points:

\(critPts\  = \ {x, y, f[x, y]} /. solns; 

    arrows\  = \ Map[Arrow[{0, 0, 1}, Tail -> #, Orange]&, critPts]; 

    critPts\  = \ Map[\(({0, 0,  .1} + #)\)&, critPts]; 

    criticalPts\  = \ 
      Map[Graphics3D[{PointSize[0.03], Red, Point[#]}]&, critPts]; 

    surf\  = \ 
      Plot3D[f[x, y], {x, \(-3\)/2, 3/2}, {y, \(-3\)/2, 3/2}, 
        AxesLabel -> {"x", "y", "z"}, 
        DisplayFunction -> Identity]; 

    Show[surf, criticalPts, arrows, ViewPoint -> {3, \ 1, \ 2}, 
      DisplayFunction -> $DisplayFunction]; \)

Exercises

Exercise 18.2.2

Check your partial differentiation of the functions from Exercise 18.2.1:

a)  z  = x (y^2) + (x^3) y

b)  z = Sin[x]^3 + Cos[y]^4

c)  z = Sin[x*y]*Cos[x*y]

d)  z = (Sqrt[1 - (x^2) - (y^2)]) Cos[2 π x y]

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

f) z = (1/(Sqrt[(x^2) + (y^2)]))

g)  z = (2 (x^2) + 3 (y^2)) Exp[-((x^2) + (y^2))]
	or  z = (2 (x^2) + 3 (y^2)) (e^(-((x^2) + (y^2))))

h)  z = 2 x y Exp[-(((x^2)/2) + ((y^2)/8))]

    or z = 2 x y (e^(-(((x^2)/2) + ((y^2)/8))))

i)  z = ArcTan[(x^2) (y^3)]

j)  z = Log[(x^2) + (y^4)]

Simply copy, paste and edit as needed.

Which (x,y)-points have both partials equal to zero?

Exercise 18.2.5  Basic Partial Derivatives with Different Letters

Calculate the partial derivatives of the following and make 3-D and contour plots of the functions.

f[x, y] = (x^2) + (y^2)

g[u, v] = ((((u/5)))^2) + ((((v/3)))^2)

h[x, y] = (x^2) (y^3)

k[x, y] = Sin[(x^2) + (y^2)]

h[u, v] = Log[(u^2) + (v^2)]

f[x,y] = Sin[x] + Cos[y]

g[u,v] = Sin[ u*v ]

h[x,y]=x Cos[2 π y]

Different variables are handled like this:

Clear[q, r, j]; j[q_, r_] := Exp[-((q^2) + (r^2))]; j[q, r]\(jQ[q_, r_]\  := \ Evaluate[D[j[q, r], q]]; 

    jR[q_, r_]\  := \ Evaluate[D[j[q, r], r]]; 
jQ[q, r]\)jR[q, r]