Using R, compute partial derivatives of a multivariable function.
Obtain higher-order partial derivatives by hand.
Use partial derivatives to obtain a linear approximation of a multivariable function at a point.
1. Partial Derivatives in R
Objective. Using R, compute partial derivatives of a multivariable function.
The R command D(f(x,y) ~ x) returns fx, the partial derivative of f(x,y) with respect to the variable written immediately after the tilde, as a function of every input. The command D(f(x,y) ~ y) returns fy.
After dfx = D(f(x,y) ~ x), evaluate fx at (1,2) with dfx(x = 1, y = 2), or with dfx(1, 2), which takes the values in the order x, y. A call with an input left out stops with an error:
> dfx(1)Error in dfx(1) : argument "y" is missing, with no default
Example 1
Use R to compute fx and fy, then evaluate both at (1,2):
f(x,y)=xy2+x3.
ShowHide solution
Define f with makeFun(), compute each first partial derivative with D(), and evaluate both partial derivatives at (1,2). Used R.
f = makeFun(x*y^2 + x^3 ~ x & y)dfx = D(f(x,y) ~ x)dfy = D(f(x,y) ~ y)dfx(x = 1, y = 2)[1] 7dfy(x = 1, y = 2)[1] 4
R returns fx(1,2)=7 and fy(1,2)=4. As a check, the command dfx alone prints the formula R found, 3 * x^2 + y^2, which is fx(x,y)=3x2+y2 from the derivative rules. The command dfy alone prints 2 * x * y, which is fy(x,y)=2xy.
Example 2
Use R to compute fx(1,2) and fy(1,2):
f(x,y)=cos(xy)+x2y3.
ShowHide solution
Define f, compute fx and fy with D(), and evaluate both at (1,2). Used R.
f = makeFun(cos(x*y) + x^2*y^3 ~ x & y)dfx = D(f(x,y) ~ x)dfy = D(f(x,y) ~ y)dfx(x = 1, y = 2)[1] 14.18141dfy(x = 1, y = 2)[1] 11.0907
R returns fx(1,2)≈14.181 and fy(1,2)≈11.091.
To check by hand, treat y as a constant and apply the chain rule to cos(xy):
For f(x,y)=xy2+x3, plot fx in R for 0≤x≤2 with y held at 1, and determine whether f increases or decreases in x when y=1.
ShowHide solution
Define f and fx in R, then plot fx with y held at 1. Used R.
f = makeFun(x*y^2 + x^3 ~ x & y)
dfx = D(f(x,y) ~ x)
plotFun(dfx(x, y = 1) ~ x, xlim = range(0, 2))
In the formula dfx(x, y = 1) ~ x, the argument y = 1 fixes y at 1, and ~ x puts x on the horizontal axis.
By hand, treat y as a constant, differentiate, and then set y=1:
fxfx(x,1)=∂x∂[xy2+x3]=y2+3x2,=12+3x2=1+3x2.
Because 3x2≥0, the partial derivative satisfies fx(x,1)≥1 for every x, and the plot stays above the x-axis. The lowest point of the plot is (0,1), because 3x2=0 only at x=0, where fx(0,1)=1. The function f increases in x when y=1, because a positive rate of change means an increasing output (Lesson 25 Notes, section 4). As a check, the one-input function f(x,1)=x+x3 rises from f(0,1)=0 to f(2,1)=10.
Objective. Obtain higher-order partial derivatives by hand.
Definition 1 (Higher-order partial derivatives).
A second partial derivative is a partial derivative of a first partial derivative. A higher-order partial derivative is a partial derivative of a partial derivative, and the second partial derivatives are the higher-order partial derivatives of order 2. A two-variable function f(x,y) has four second partial derivatives:
The notation [fx]y means the partial derivative of fx with respect to y. The order of a partial derivative is the number of differentiations it takes: fx has order 1, fxy has order 2, and fxxy has order 3.
The first partial derivative fx is neither a second partial derivative nor a higher-order partial derivative, because it is a partial derivative of f itself, and f is not a partial derivative. Both definitions require a partial derivative of a partial derivative, such as fxx=[fx]x or fxxy=[fxx]y.
As in the textbook, each function in the examples is assumed to have partial derivatives of every order at the points where they are computed.
A mixed partial is a higher-order partial derivative that involves differentiating with respect to different input variables, such as fxy and fyx.
Order of differentiation. In subscript notation, find fxy by differentiating with respect to x first and then with respect to y. In Leibniz notation, differentiate first with respect to the rightmost variable in the denominator and work leftward:
∂y∂x∂2f=fxy.
The exponent on ∂ in the numerator is the order. For repeated differentiation with respect to one variable, write an exponent on that variable in the denominator:
∂x2∂2f=fxx,∂y2∂2f=fyy.
A multivariable function f(x,y) is smooth on an open region if and only if f(x,y) has continuous partials of all orders at every point of that region.
The common modeling functions are smooth on every open region inside their domains.
Theorem 2 (Clairaut’s theorem).
If f(x,y) is smooth on an open region, then fxy(x,y)=fyx(x,y) at every point of that region.
Example 4
For a function f(x,y,z), list the order of differentiation for each partial derivative:
fyzx,∂z∂y∂x∂3f.
ShowHide solution
In fyzx, the order is y, then z, then x, because subscripts are read left to right. In the partial derivative written in Leibniz notation, the order is x, then y, then z, because the Leibniz denominator is read from the rightmost variable leftward. In subscript notation, that partial derivative is fxyz.
Example 5
Compute all second partial derivatives of
f(x,y)=xy2+x3.
ShowHide solution
By the definition of higher-order partial derivatives, each second partial derivative is a partial derivative of a first one. Compute fx and differentiate it with respect to x and to y, then do the same with fy.
Differentiate f with respect to x, treating y as a constant:
fx=∂x∂[xy2+x3]=y2+3x2.
Differentiate fx with respect to x and then with respect to y:
fxx=∂x∂[y2+3x2]=6x,fxy=∂y∂[y2+3x2]=2y.
Differentiate f with respect to y, treating x as a constant:
fy=∂y∂[xy2+x3]=x(2y)+0=2xy.
Differentiate fy with respect to x and then with respect to y:
fyx=∂x∂[2xy]=2y,fyy=∂y∂[2xy]=2x.
The mixed partials fxy and fyx both equal 2y. Clairaut’s theorem predicts this equality, because a polynomial is smooth everywhere.
Example 6
Compute all second partial derivatives of
g(x,y)=x4sin(y).
ShowHide solution
As in Example 5, start with gx, where the factor sin(y) is a constant multiple, and differentiate it with respect to x and then y:
The mixed partials gxy and gyx are equal, as Clairaut’s theorem states for the smooth function x4sin(y).
Example 7
Compute all second partial derivatives of the function below, then compute the third partial derivative ∂y∂x2∂3q:
q(x,y)=x3e4y.
ShowHide solution
As in Example 5, start with qx, where the factor e4y is a constant multiple, and differentiate it with respect to x and then y. The computation of qxy uses ∂y∂[e4y]=4e4y from the chain rule:
The coefficient 16 of qyy is 4⋅4, one factor of 4 from each differentiation with respect to y.
The Leibniz denominator ∂y∂x2 read from the right gives the order x, x, then y. The third partial derivative is qxxy. Differentiate qxx with respect to y:
qxxy=∂y∂[6xe4y]=6x⋅4e4y=24xe4y.
Higher-order partial derivatives in R. List the variables of differentiation in order after the tilde, separated by &. The call D(f(x,y) ~
x & y) computes fxy. The call D(f(x,y) ~ x & x) computes fxx.
Example 8
The second partial derivatives of g(x,y)=x4sin(y) are
Define g, compute the four second partial derivatives with D() by listing the variables of differentiation in order, and evaluate each at (2,1). Used R.
g = makeFun(x^4*sin(y) ~ x & y)dgxx = D(g(x,y) ~ x & x)dgxy = D(g(x,y) ~ x & y)dgyx = D(g(x,y) ~ y & x)dgyy = D(g(x,y) ~ y & y)dgxx(x = 2, y = 1)[1] 40.39061dgxy(x = 2, y = 1)[1] 17.28967dgyx(x = 2, y = 1)[1] 17.28967dgyy(x = 2, y = 1)[1] -13.46354
The R values are gxx(2,1)≈40.391, gxy(2,1)=gyx(2,1)≈17.290, and gyy(2,1)≈−13.464.
Substitute (2,1) into the hand formulas. Used calculator.
Objective. Use partial derivatives to obtain a linear approximation of a multivariable function at a point.
The figure shows the surface z=f(x,y) for f(x,y)=x2+y2 and, in orange, the plane tangent to the surface at the point (1,1,2). The point lies on the surface because f(1,1)=12+12=2. From fx=2x and fy=2y, the plane’s slope is fx(1,1)=2(1)=2 in the x-direction and fy(1,1)=2(1)=2 in the y-direction. The plane has the equation
z=2+2(x−1)+2(y−1).
Around (1,1), the plane and the surface nearly coincide. The plane is the graph of the linear approximation, defined next.
Definition 3 (Linear approximation in two inputs).
Let f(x,y) be a smooth function around the point (a,b). The linear approximation of f(x,y) when (x,y)=(a,b) is given by
L(x,y)=f(a,b)+fx(a,b)⋅(x−a)+fy(a,b)⋅(y−b).
The function k(x,y)=∣x∣+y has no linear approximation when (x,y)=(0,0), because it is not smooth around (0,0): section 2 of the Lesson 37 Notes shows that kx(0,0) does not exist. The definition requires a smooth function, and the formula for L contains kx(0,0).
The graph of L(x,y) is a plane, the two-input counterpart of the tangent line of the single-variable linear approximation in section 1 of the Lesson 30 Notes. For inputs near (a,b), the linear approximation L(x,y) estimates f(x,y).
Finding a linear approximation.
Compute the first partial derivatives fx and fy.
Evaluate f, fx, and fy at (a,b).
Substitute f(a,b), fx(a,b), and fy(a,b) into L(x,y) and leave x and y as variables.
Example 9
Find the linear approximation of f(x,y) when (x,y)=(1,2), and use it to estimate f(1.1,1.9):
f(x,y)=xy+exy.
ShowHide solution
By the definition of the linear approximation, the linear approximation at (a,b)=(1,2) is
L(x,y)=f(1,2)+fx(1,2)⋅(x−1)+fy(1,2)⋅(y−2).
To write the approximation, find the output f(1,2) and the two slopes fx(1,2) and fy(1,2).
Compute the first partial derivatives. The chain rule gives ∂x∂[exy]=yexy and ∂y∂[exy]=xexy:
Use R to build the linear approximation of f(x,y)=xy+exy when (x,y)=(1,2), and compute the estimate of f(1.1,1.9).
ShowHide solution
Define f, compute fx and fy with D(), and evaluate all three at (1,2). Used R.
f = makeFun(x*y + exp(x*y) ~ x & y)dfx = D(f(x,y) ~ x)dfy = D(f(x,y) ~ y)f(x = 1, y = 2)[1] 9.389056dfx(x = 1, y = 2)[1] 16.77811dfy(x = 1, y = 2)[1] 8.389056
As a check by hand, fx=y+yexy and fy=x+xexy give f(1,2)=2+e2, fx(1,2)=2+2e2, and fy(1,2)=1+e2, which match R with e2≈7.389056. The linear approximation is
L(x,y)≈9.389+16.778(x−1)+8.389(y−2).
Build L from the unrounded R values and evaluate it at (1.1,1.9):
L = makeFun(f(1, 2) + dfx(1, 2)*(x - 1) +dfy(1, 2)*(y - 2) ~ x & y)L(x = 1.1, y = 1.9)[1] 10.22796
The estimate is f(1.1,1.9)≈L(1.1,1.9)≈10.228.
Example 11
A linear approximation of a function f(x,y) is
L(x,y)=5−2(x−3)+4(y+1).
Determine the point (a,b) at which L approximates f, the value of f at that point, and whether f increases or decreases in each input near that point.
ShowHide solution
Rewrite y+1 as y−(−1) and match the terms to the linear approximation formula:
The match gives a=3 and b=−1. The linear approximation is taken at (3,−1), where f(3,−1)=5. Near that point, f decreases in x because fx(3,−1)=−2 is negative. It increases in y because fy(3,−1)=4 is positive.
Example 12
A cylinder with radius r centimeters and height h centimeters has volume
Vol(r,h)=πr2h
cubic centimeters. Find the linear approximation of Vol when (r,h)=(2,5), and use it to estimate the volume when the radius is 2.1 centimeters and the height is 4.9 centimeters.
ShowHide solution
To write the linear approximation, find Vol(2,5), Volr(2,5), and Volh(2,5). Compute the first partial derivatives. With respect to r, the factor πh is a constant multiple. With respect to h, the factor πr2 is a constant multiple: