Survey of Mathematics

5.2 Directional derivatives, partial derivatives and the gradient

To differentiate a function of many variables, pick a direction and ask the one-variable question along that line. The answers for the coordinate directions are the partial derivatives; collected into a vector they form the gradient, which points uphill and is perpendicular to the level sets. But having a derivative in every direction does not even make a function continuous.

Taught in 375/376Apostol, not taughtBeyond the courseApostol Vol. 2, 8.6 to 8.10, 8.12 and 8.14Spagnolie, Lectures 20 and 21 and Piazza note @105Fourier, Théorie analytique de la chaleur (1822)

In one variable the derivative answers: how fast does ff change when xx moves? In the plane or in space there are many ways to move, and the answer depends on which way. This chapter fixes one direction and asks the one-variable question along that line; then it collects the answers for all directions into a single vector, the gradient, which points the way uphill. The chapter ends with a warning that explains why the next one is needed: a function can have a derivative in every direction at a point and still fail to be continuous there.

Before you start

1.4: the derivative of a function of one variable. 5.1: scalar fields, level sets, and why a point must be interior to the domain (a ball fits around it). 2.9: the dot product and the Cauchy–Schwarz inequality.

Quick checkLet g(t)=(3+2t)2g(t) = (3 + 2t)^2. What is g′(0)g'(0)?
Quick checkFor a vector gg and a unit vector uu, how large can g⋅ug \cdot u be?
Quick checkWhat is the angle between two vectors with dot product 00?

The picture: the slope of a slice

Left is the plane with the point aa (red) and a line through it in a chosen direction (purple). Right is the slice: the graph of ff above that line, a curve in one variable tt. Its slope at t=0t = 0 is the number this chapter is about. Things to do:

  1. Take 3x2+y23x^2 + y^2 and point the direction along the xx-axis (angle 0). The slope is 6x6x at your point: the ordinary derivative of 3x23x^2 with yy held fixed. Now point along the yy-axis: the slope is 2y2y.
  2. Turn the direction slowly. The slope changes smoothly, positive in some directions, negative in the opposite ones, and there are two directions in which it is 00 (the direction of the level curve through the point).
  3. Switch to the last function, with the point at the origin. Its slice has a slope in every direction, but near the vertical direction the slope jumps wildly. This is the warning of the last section.

The idea, in words

Fix a point aa and a vector yy. The line through aa with direction yy is a+hya + hy, where hh is a real number: at h=0h = 0 we are at aa, at h=1h = 1 at a+ya + y. Walk along it and record the value of ff: this is the one-variable function g(h)=f(a+hy)g(h) = f(a + hy), the slice. The directional derivative is the number g′(0)g'(0), written as the limit of the difference quotient. Everything else in this chapter is a consequence of that sentence: we reuse the one-variable derivative of 1.4 along lines.

Why· Why the length of y matters

The distance from aa to a+hya + hy is ∣h∣ ∥y∥|h|\,\lVert y \rVert. If you want the rate of change per unit distance, you want ∥y∥=1\lVert y \rVert = 1. If you double yy, you walk twice as fast along the same line, so the rate of change per unit of hh doubles. That is why Apostol allows any vector yy in the definition (“the derivative with respect to yy”) and reserves the word “directional” for unit vectors. When n=1n = 1 and y=1y = 1 the definition is the derivative of 1.4.

Definitions

Definition T

Directional derivative

Let f:S→Rf: S \to \mathbb R be a scalar field, let aa be an interior point of SS (some ball B(a,r)B(a, r) lies in SS, so the points a+hya + hy with ∣h∣ ∥y∥<r|h|\,\lVert y \rVert < r are in SS) and let y∈Rny \in \mathbb R^n. The derivative of ff at aa with respect to yy is

f′(a;y)=lim⁡h→0f(a+hy)−f(a)h,f'(a; y) = \lim_{h \to 0}\frac{f(a + hy) - f(a)}{h},

when the limit exists. If ∥y∥=1\lVert y \rVert = 1 it is called the directional derivative of ff at aa in the direction of yy. The fraction is the average rate of change over the segment from aa to a+hya + hy. (Apostol 8.6 and 8.7.)

The limit is two-sided: h→0h \to 0 from both signs, so hh may be negative and then we walk backwards from aa. Some books use only h→0+h \to 0^+ (a one-sided derivative). Apostol, Spagnolie and this book use the two-sided one, and the difference matters: for f(x)=∥x∥f(x) = \lVert x \rVert at a=Oa = O we get ∣h∣ ∥y∥h=±∥y∥\frac{|h|\,\lVert y \rVert}{h} = \pm\lVert y \rVert, which has a one-sided limit but no two-sided limit.

In one lineFor a scalar field ff, a point aa and a vector yy: f′(a;y)=lim⁡h→0f(a+hy)−f(a)hf'(a; y) = \lim_{h \to 0} \frac{f(a + hy) - f(a)}{h}, the rate of change of ff as you walk from aa in the direction yy; it is called the directional derivative when ∥y∥=1\lVert y \rVert = 1.

Why it existsIt reduces 'how does f change here?' to the one-variable question, one direction at a time, and for a unit vector it is the change per unit distance.

Lemma T new (the idea of Apostol 8.9 #22)

Scaling a direction scales the derivative

If f′(a;y)f'(a; y) exists, then f′(a;cy)f'(a; cy) exists and equals c f′(a;y)c\,f'(a; y) for every real number cc.

In one lineIf f′(a;y)f'(a; y) exists then f′(a;cy)=c f′(a;y)f'(a; cy) = c\,f'(a; y) for every real cc.

Why it is trueWalking cc times as fast along the same line gives cc times the rate; in particular walking backwards (c=−1c = -1) reverses the sign.

Proofnew
The idea: Change the variable in the difference quotient from h to ch.
If c=0c = 0 both sides are 00.
For c=0c = 0: f(a+h⋅0)−f(a)=0f(a + h \cdot 0) - f(a) = 0 for every hh.
If c≠0c \ne 0: f(a+h cy)−f(a)h=c⋅f(a+(hc) y)−f(a)hc\dfrac{f(a + h\,cy) - f(a)}{h} = c\cdot\dfrac{f(a + (hc)\,y) - f(a)}{hc}.
Multiply and divide by cc. This is allowed since c≠0c \ne 0.
As h→0h \to 0 the right side tends to c f′(a;y)c\,f'(a; y). ■\blacksquare
k=hc→0k = hc \to 0 exactly when h→0h \to 0, and the quotient in kk tends to f′(a;y)f'(a; y).
Theorem T Apostol Thm 8.3

The derivative of a slice

Let g(t)=f(a+ty)g(t) = f(a + ty). If one of g′(t)g'(t) and f′(a+ty;y)f'(a + ty; y) exists, so does the other, and g′(t)=f′(a+ty;y)g'(t) = f'(a + ty; y). In particular g′(0)=f′(a;y)g'(0) = f'(a; y).

In one lineFor g(t)=f(a+ty)g(t) = f(a + ty), the derivative g′(t)g'(t) exists exactly when f′(a+ty;y)f'(a + ty; y) exists, and then g′(t)=f′(a+ty;y)g'(t) = f'(a + ty; y).

Why it is trueIt is the bridge to Unit 1: every directional derivative is an ordinary derivative of a one-variable function, so all of 1.4 (Fermat, the mean value theorem) applies to it.

ProofApostol 8.3
The idea: Write the two difference quotients and see that they are the same expression.
g(t+h)−g(t)h=f(a+ty+hy)−f(a+ty)h\dfrac{g(t + h) - g(t)}{h} = \dfrac{f(a + ty + hy) - f(a + ty)}{h}.
Definition of gg.
So the left side has a limit as h→0h \to 0 exactly when f′(a+ty;y)f'(a + ty; y) exists, and then the two are equal. ■\blacksquare
The right side is the difference quotient defining f′(b;y)f'(b; y) at the point b=a+tyb = a + ty.
Definition T

Partial derivative

The partial derivative of ff with respect to the kk-th variable at aa is the derivative with respect to the kk-th unit vector eke_k:

Dkf(a)=f′(a;ek)=lim⁡h→0f(a1,…,ak+h,…,an)−f(a1,…,an)h.D_k f(a) = f'(a; e_k) = \lim_{h \to 0}\frac{f(a_1, \dots, a_k + h, \dots, a_n) - f(a_1, \dots, a_n)}{h}.

Other notations: ∂f∂xk(a)\dfrac{\partial f}{\partial x_k}(a) and fxk(a)f_{x_k}(a); in the plane ∂f∂x\frac{\partial f}{\partial x} and ∂f∂y\frac{\partial f}{\partial y}, or fxf_x and fyf_y. In practice: hold all the other variables constant and differentiate with the rules you know. That is the JEE method, and this definition is why it is correct.

In one lineThe partial derivative Dkf(a)=f′(a;ek)D_k f(a) = f'(a; e_k) is the derivative of ff along the kk-th coordinate axis: differentiate in xkx_k with every other variable held fixed.

Why it existsIt is the cheapest directional derivative to compute, by the ordinary rules of 1.4, and the gradient is built from them.

Definition T

Gradient

If all the partial derivatives of ff exist at aa, the gradient of ff at aa is the vector

∇f(a)=(D1f(a),D2f(a),…,Dnf(a)).\nabla f(a) = \big(D_1 f(a), D_2 f(a), \dots, D_n f(a)\big).

Also written grad⁡f(a)\operatorname{grad} f(a); the symbol ∇\nabla is read “del” or “nabla”. Where it exists at every point of a set, ∇f\nabla f is a vector field. In the plane, ∇f=(∂f∂x,∂f∂y)\nabla f = \big(\frac{\partial f}{\partial x}, \frac{\partial f}{\partial y}\big). The gradient lives in the domain space Rn\mathbb R^n (it has one entry per input variable), not in the space of outputs.

In one lineThe gradient ∇f(a)=(D1f(a),…,Dnf(a))\nabla f(a) = (D_1 f(a), \dots, D_n f(a)) is the vector of all the partial derivatives of ff at aa.

Why it existsIt packs the rates of change along all the axes into one vector, and the next sections show that this one vector knows the rate of change in every direction and the direction of steepest climb.

Where this came from· The steepness of temperature, and the harp

Joseph Fourier’s treatise on heat (1822) rests on a statement about the quantity we now write ∇T\nabla T: heat flows from hot to cold, and the rate at which it crosses a surface is proportional to the steepness of the temperature, in the direction of steepest drop. He had no vector notation; the gradient was born as a physical idea first. The symbol ∇\nabla has its own history: William Rowan Hamilton introduced the operator in 1837 (he drew it as a triangle lying on its side), Peter Guthrie Tait used the upright upside-down triangle in his 1867 book on quaternions, and in 1870 William Robertson Smith suggested to Tait the name “nabla”, after a Greek word for a harp, which the symbol resembles. Spagnolie mentioned the harp in Lecture 21. The question both men were answering is the one of this chapter: how to speak, with one object, about the way a quantity varies in all directions at once.

Quick checkFor a scalar field f:R3→Rf: \mathbb R^3 \to \mathbb R, in which space does the vector ∇f(a)\nabla f(a) live?

Computing partial derivatives

Worked example Lecture 20
For F(x,y,z)=xysin⁡(z2)F(x, y, z) = xy\sin(z^2), find the three partial derivatives at a=(1,2,3)a = (1, 2, 3) and the second partials FxxF_{xx}, FxyF_{xy}, FzzF_{zz}.

Solution.

  1. xx-partial. Hold y,zy, z fixed: Fx=ysin⁡(z2)F_x = y\sin(z^2). At aa: 2sin⁡92\sin 9. xysin⁡(z2)=x⋅(constant)xy\sin(z^2) = x \cdot (\text{constant}) as a function of xx.
  2. yy-partial. Fy=xsin⁡(z2)F_y = x\sin(z^2). At aa: sin⁡9\sin 9. Same, now yy varies.
  3. zz-partial. Fz=xycos⁡(z2)⋅2z=2xyzcos⁡(z2)F_z = xy\cos(z^2)\cdot 2z = 2xyz\cos(z^2). At aa: 12cos⁡912\cos 9. Chain rule of 1.4 for sin⁡(z2)\sin(z^2), the inner function z2z^2 having derivative 2z2z.
  4. Second partials. Fxx=0F_{xx} = 0, since Fx=ysin⁡(z2)F_x = y\sin(z^2) does not contain xx. Fxy=sin⁡(z2)F_{xy} = \sin(z^2), the yy-derivative of FxF_x. Fzz=2xy(cos⁡(z2)−2z2sin⁡(z2))F_{zz} = 2xy\big(\cos(z^2) - 2z^2\sin(z^2)\big) by the product rule on 2xyzcos⁡(z2)2xyz\cos(z^2). Each partial is again a scalar field, so we can differentiate it again.

Answer. ∇F(a)=(2sin⁡9, sin⁡9, 12cos⁡9)≈(0.824, 0.412, −10.93)\nabla F(a) = (2\sin 9,\ \sin 9,\ 12\cos 9) \approx (0.824,\ 0.412,\ -10.93), and the second partials are as above. (The values are numbers: the derivative at a fixed point has no xx, yy or zz left in it, as Spagnolie reminded the class.)

Your answerFor f(x,y)=x2+y2sin⁡(xy)f(x, y) = x^2 + y^2\sin(xy), find ∂f∂y\dfrac{\partial f}{\partial y} at (1,π/2)(1, \pi/2).
Worked example MATH 375 HW 11 (Apostol 8.9 #1 and #8)
Compute f′(x;y)f'(x; y) and all partial derivatives of f(x)=a⋅xf(x) = a \cdot x on Rn\mathbb R^n, for a fixed vector aa.

Solution.

  1. Build the slice. g(t)=f(x+ty)=a⋅(x+ty)=a⋅x+t (a⋅y)g(t) = f(x + ty) = a\cdot(x + ty) = a\cdot x + t\,(a\cdot y). The dot product is linear in its second slot.
  2. Differentiate it. g′(0)=a⋅yg'(0) = a\cdot y, so f′(x;y)=a⋅yf'(x; y) = a\cdot y for every xx and yy. The slice is a straight line in tt with slope a⋅ya\cdot y.
  3. Partials. Take y=eiy = e_i: Dif(x)=a⋅ei=aiD_i f(x) = a\cdot e_i = a_i. A partial derivative is a derivative with respect to a unit coordinate vector.

Answer. f′(x;y)=a⋅yf'(x; y) = a\cdot y (independent of xx), ∂f∂xi=ai\dfrac{\partial f}{\partial x_i} = a_i, so ∇f=a\nabla f = a. For a linear field the gradient is a constant vector; compare 3.8, where ∇(aTx)=a\nabla(a^{\mathsf T}x) = a.

Worked example MATH 375 HW 11 (Apostol 8.9 #2a, #2b)
Find f′(a;y)f'(a; y) for f(x)=∥x∥4f(x) = \lVert x \rVert^4 on Rn\mathbb R^n, and for n=2n = 2 find all y=(p,q)y = (p, q) with f′((2,3);(p,q))=6f'((2, 3); (p, q)) = 6.

Solution.

  1. Build the slice. ∥a+ty∥2=∥a∥2+2t a⋅y+t2∥y∥2\lVert a + ty \rVert^2 = \lVert a \rVert^2 + 2t\,a\cdot y + t^2\lVert y \rVert^2, so g(t)=f(a+ty)=(∥a∥2+2t a⋅y+t2∥y∥2)2g(t) = f(a + ty) = \big(\lVert a \rVert^2 + 2t\,a\cdot y + t^2\lVert y \rVert^2\big)^2. Expand (a+ty)⋅(a+ty)(a + ty)\cdot(a + ty) using bilinearity.
  2. Differentiate at t=0t = 0. Chain rule: g′(t)=2(…)⋅(2a⋅y+2t∥y∥2)g'(t) = 2\big(\ldots\big)\cdot\big(2a\cdot y + 2t\lVert y \rVert^2\big), so g′(0)=2∥a∥2⋅2 a⋅y=4∥a∥2 (a⋅y)g'(0) = 2\lVert a \rVert^2\cdot 2\,a\cdot y = 4\lVert a \rVert^2\,(a\cdot y). The bracket at t=0t = 0 is ∥a∥2\lVert a \rVert^2.
  3. Read the gradient. f′(a;y)=4∥a∥2 (a⋅y)f'(a; y) = 4\lVert a \rVert^2\,(a\cdot y) means the partials are 4∥a∥2ai4\lVert a \rVert^2 a_i, so ∇f(a)=4∥a∥2a\nabla f(a) = 4\lVert a \rVert^2 a, a vector (as it should be). Take y=eiy = e_i.
  4. The case n=2n = 2. For a=(2,3)a = (2, 3): ∥a∥2=13\lVert a \rVert^2 = 13, and f′(a;(p,q))=4⋅13 (2p+3q)=52(2p+3q)f'(a; (p, q)) = 4\cdot 13\,(2p + 3q) = 52(2p + 3q). Set it equal to 66: 2p+3q=652=3262p + 3q = \dfrac{6}{52} = \dfrac3{26}. One linear equation in (p,q)(p, q).

Answer. f′(a;y)=4∥a∥2(a⋅y)f'(a; y) = 4\lVert a \rVert^2(a\cdot y); the required vectors lie on the line 2p+3q=3262p + 3q = \frac3{26}.

Worked example MATH 375 HW 11 (Apostol 8.9 #9)
Find all partial derivatives of the quadratic form f(x)=∑i,jaijxixjf(x) = \sum_{i,j} a_{ij}x_ix_j when aij=ajia_{ij} = a_{ji}.

Solution.

  1. Differentiate a typical term. ∂∂xk(xixj)=δikxj+xiδjk\dfrac{\partial}{\partial x_k}(x_ix_j) = \delta_{ik}x_j + x_i\delta_{jk}, where δik\delta_{ik} is 11 if i=ki = k and 00 otherwise. Product rule; ∂xi∂xk=1\frac{\partial x_i}{\partial x_k} = 1 if i=ki = k and 00 otherwise, since the variables are independent.
  2. Sum over all i,ji, j. ∂f∂xk=∑jakjxj+∑iaikxi\dfrac{\partial f}{\partial x_k} = \sum_j a_{kj}x_j + \sum_i a_{ik}x_i. Only the terms with i=ki = k (first sum) or j=kj = k (second sum) survive.
  3. Use symmetry. aik=akia_{ik} = a_{ki}, so both sums equal ∑jakjxj=(Ax)k\sum_j a_{kj}x_j = (Ax)_k. Rename the summation index in the second sum.

Answer. ∂f∂xk=2(Ax)k\dfrac{\partial f}{\partial x_k} = 2(Ax)_k, that is ∇(xTAx)=2Ax\nabla(x^{\mathsf T}Ax) = 2Ax for symmetric AA. This is the 2Ax2Ax of 3.8; the general rule (A+AT)x(A + A^{\mathsf T})x for non-symmetric AA is the same computation without step 3.

Worked example MATH 375 HW 11 (Apostol 8.9 #5); Lecture 20
Find all partial derivatives of f(x,y)=x2+y2f(x, y) = \sqrt{x^2 + y^2}, and decide whether they exist at the origin.

Solution.

  1. Away from the origin. fx=xx2+y2f_x = \dfrac{x}{\sqrt{x^2 + y^2}} and fy=yx2+y2f_y = \dfrac{y}{\sqrt{x^2 + y^2}}. Chain rule: ddxu=12ududx\frac{d}{dx}\sqrt u = \frac{1}{2\sqrt u}\frac{du}{dx} with u=x2+y2u = x^2 + y^2 and dudx=2x\frac{du}{dx} = 2x.
  2. In any dimension. For f(x)=∥x∥f(x) = \lVert x \rVert, ∂f∂xk=xk∥x∥\dfrac{\partial f}{\partial x_k} = \dfrac{x_k}{\lVert x \rVert}, so ∇∥x∥=x∥x∥\nabla\lVert x \rVert = \dfrac{x}{\lVert x \rVert} is the unit vector pointing away from the origin. Same computation with nn terms under the root.
  3. At the origin. f(h,0)=h2=∣h∣f(h, 0) = \sqrt{h^2} = |h|, and ∣h∣−0h\dfrac{|h| - 0}{h} is 11 for h>0h > 0 and −1-1 for h<0h < 0. So fx(0,0)f_x(0, 0) does not exist (likewise fyf_y). This is the corner of ∣x∣|x| from 1.4: the graph is a cone with its point at the origin.

Answer. The gradient is the radial unit vector (x,y)/x2+y2(x, y)/\sqrt{x^2 + y^2} at every point except the origin, where the partial derivatives do not exist.

Worked example MATH 375 HW 11 (Apostol 8.9 #11)
For f(x,y)=log⁡(x2+y2)f(x, y) = \log(x^2 + y^2) on (x,y)≠(0,0)(x, y) \ne (0, 0), compute the first partials and check that the two mixed second partials agree.

Solution.

  1. First partials. D1f=2xx2+y2D_1 f = \dfrac{2x}{x^2 + y^2}, D2f=2yx2+y2D_2 f = \dfrac{2y}{x^2 + y^2}. Chain rule for log⁡u\log u, derivative 1ududx\frac{1}{u}\frac{du}{dx}.
  2. Differentiate D2fD_2 f in xx. D1(D2f)=−2y⋅2x(x2+y2)2=−4xy(x2+y2)2D_1(D_2 f) = \dfrac{-2y\cdot 2x}{(x^2 + y^2)^2} = \dfrac{-4xy}{(x^2 + y^2)^2}. Quotient rule on 2y (x2+y2)−12y\,(x^2 + y^2)^{-1} with yy constant.
  3. Differentiate D1fD_1 f in yy. D2(D1f)=−2x⋅2y(x2+y2)2=−4xy(x2+y2)2D_2(D_1 f) = \dfrac{-2x\cdot 2y}{(x^2 + y^2)^2} = \dfrac{-4xy}{(x^2 + y^2)^2}. Same computation with the roles swapped.

Answer. D1(D2f)=D2(D1f)=−4xy(x2+y2)2D_1(D_2 f) = D_2(D_1 f) = \dfrac{-4xy}{(x^2 + y^2)^2}. The mixed partials are equal here; the next box says when to expect that.

Watch out· Mixed partials: notation, and when the order does not matter

D1(D2f)D_1(D_2 f) means: differentiate in the second variable first, then the first. Books write it ∂2f∂x ∂y\frac{\partial^2 f}{\partial x\,\partial y} (Apostol) or fyxf_{yx}, and Spagnolie’s notes write FxyF_{xy} for ”xx first, then yy”. The conventions differ, so read the definition in the source. Fortunately it rarely matters: if the second partials are continuous near the point, the two mixed partials are equal (Apostol 8.23; proved in a later chapter of this unit). Continuity is needed: see the stretch problem for a function whose two mixed partials at the origin are +1+1 and −1-1.

Worked example MATH 375 HW 11 (Apostol 8.9 #18)
Find the constant nn for which v(r,t)=tne−r2/(4t)v(r, t) = t^n e^{-r^2/(4t)} satisfies ∂v∂t=1r2∂∂r(r2∂v∂r)\dfrac{\partial v}{\partial t} = \dfrac{1}{r^2}\dfrac{\partial}{\partial r}\Big(r^2\dfrac{\partial v}{\partial r}\Big).

Solution.

  1. tt-derivative. vt=ntn−1e−r2/4t+tne−r2/4t⋅r24t2=e−r2/4t(ntn−1+14r2tn−2)v_t = nt^{n-1}e^{-r^2/4t} + t^n e^{-r^2/4t}\cdot\dfrac{r^2}{4t^2} = e^{-r^2/4t}\Big(nt^{n-1} + \tfrac14r^2t^{n-2}\Big). Product rule; ddt(−r24t)=r24t2\frac{d}{dt}\big(-\frac{r^2}{4t}\big) = \frac{r^2}{4t^2}.
  2. rr-derivative. vr=−r2t tne−r2/4tv_r = -\dfrac{r}{2t}\,t^ne^{-r^2/4t}, so r2vr=−12r3tn−1e−r2/4tr^2v_r = -\tfrac12r^3t^{n-1}e^{-r^2/4t}. Chain rule on the exponent, then multiply by r2r^2.
  3. Differentiate again. ∂∂r(r2vr)=−32r2tn−1e−r2/4t+14r4tn−2e−r2/4t\dfrac{\partial}{\partial r}(r^2v_r) = -\tfrac32r^2t^{n-1}e^{-r^2/4t} + \tfrac14r^4t^{n-2}e^{-r^2/4t}. Divide by r2r^2: −32tn−1e−r2/4t+14r2tn−2e−r2/4t-\tfrac32t^{n-1}e^{-r^2/4t} + \tfrac14r^2t^{n-2}e^{-r^2/4t}. Product rule again: ddr(−12r3)=−32r2\frac{d}{dr}\big(-\frac12r^3\big) = -\frac32r^2, and ddre−r2/4t=−r2te−r2/4t\frac{d}{dr}e^{-r^2/4t} = -\frac{r}{2t}e^{-r^2/4t}.
  4. Compare coefficients. The r2tn−2r^2t^{n-2} terms agree (14\frac14 on both sides). The tn−1t^{n-1} terms need n=−32n = -\frac32. Both sides are e−r2/4te^{-r^2/4t} times a combination of tn−1t^{n-1} and r2tn−2r^2t^{n-2}.

Answer. n=−32n = -\tfrac32. This vv is the temperature spreading from a point source: the right side is the heat equation for a spherically symmetric temperature in three dimensions (a diffusivity of 1). The numbers t−3/2t^{-3/2} and e−r2/4te^{-r^2/4t} say that the heat cloud has width about t\sqrt t and height about t−3/2t^{-3/2}, the total heat being constant.

The gradient points uphill

So far the gradient is only a bookkeeping device: a list of partials. It becomes geometry through one fact, proved in the next chapter.

A preview· What we borrow from 5.3

If ff is differentiable at aa (the total derivative of 5.3: ff is well approximated by a linear function near aa), then for every vector yy

f′(a;y)=∇f(a)⋅y.f'(a; y) = \nabla f(a)\cdot y.

This is Theorem “Directional derivative as a dot product with the gradient”. It holds for every field in this chapter’s examples, because their partials are continuous (a sufficient condition proved in 5.3). The last section of this chapter shows that it can fail without differentiability. Everything in this section assumes it, and says so.

Theorem T Apostol 8.12

The gradient is the direction of steepest ascent

Suppose f′(a;y)=g⋅yf'(a; y) = g\cdot y for every yy, where g=∇f(a)≠Og = \nabla f(a) \ne O. For unit vectors uu:

(i) −∥g∥≤f′(a;u)≤∥g∥-\lVert g \rVert \le f'(a; u) \le \lVert g \rVert.

(ii) f′(a;u)=∥g∥f'(a; u) = \lVert g \rVert if and only if u=g/∥g∥u = g/\lVert g \rVert; f′(a;u)=−∥g∥f'(a; u) = -\lVert g \rVert if and only if u=−g/∥g∥u = -g/\lVert g \rVert.

(iii) f′(a;u)=0f'(a; u) = 0 if and only if u⊥gu \perp g.

In one lineIf f′(a;y)=∇f(a)⋅yf'(a; y) = \nabla f(a)\cdot y for all yy and ∇f(a)≠O\nabla f(a) \ne O, then among unit vectors uu the rate f′(a;u)f'(a; u) is largest, equal to ∥∇f(a)∥\lVert\nabla f(a)\rVert, for u=∇f(a)/∥∇f(a)∥u = \nabla f(a)/\lVert\nabla f(a)\rVert, smallest for u=−∇f(a)/∥∇f(a)∥u = -\nabla f(a)/\lVert\nabla f(a)\rVert, and 00 for uu perpendicular to ∇f(a)\nabla f(a).

Why it is trueIt turns a list of partials into a compass: the gradient tells you which way is uphill and how steep it is, in one vector.

ProofApostol 8.12 (via Cauchy–Schwarz); this proof avoids angles
The idea: Compare u with the unit vector ĝ along g by expanding ‖u − ĝ‖², which is never negative.
Let g^=g/∥g∥\hat g = g/\lVert g \rVert, so ∥g^∥=1\lVert\hat g\rVert = 1 and g⋅u=∥g∥ (g^⋅u)g\cdot u = \lVert g \rVert\,(\hat g\cdot u).
Name the unit vector along gg.
∥u−g^∥2=1−2 g^⋅u+1=2−2 g^⋅u\lVert u - \hat g \rVert^2 = 1 - 2\,\hat g\cdot u + 1 = 2 - 2\,\hat g\cdot u.
Expand (u−g^)⋅(u−g^)(u - \hat g)\cdot(u - \hat g) using ∥u∥=∥g^∥=1\lVert u \rVert = \lVert\hat g\rVert = 1.
So g^⋅u≤1\hat g\cdot u \le 1, with equality if and only if u=g^u = \hat g.
A squared length is ≥0\ge 0, and it is 00 exactly when u=g^u = \hat g.
Likewise −g^⋅u≤1-\hat g\cdot u \le 1, with equality if and only if u=−g^u = -\hat g. Together ∣g^⋅u∣≤1|\hat g\cdot u| \le 1.
Replace uu by −u-u in the previous step.
This proves (i) and (ii).
Multiply by ∥g∥>0\lVert g \rVert > 0 and use f′(a;u)=∥g∥ (g^⋅u)f'(a; u) = \lVert g \rVert\,(\hat g\cdot u).
(iii) follows. ■\blacksquare
f′(a;u)=g⋅uf'(a; u) = g\cdot u, which is 00 exactly when u⊥gu \perp g (definition of orthogonal, 2.9).

In the picture: the red arrow is ∇f\nabla f at your dot, the orange curve is the level curve through it, and the dashed line is the tangent to that level curve. Slide the direction uu (green) around: the number ∇f⋅u\nabla f\cdot u is largest when uu lies along the red arrow, zero when uu lies along the dashed line, and most negative on the opposite side. The red arrow always crosses the level curve at a right angle. That is the next theorem.

Quick checkYou walk along a contour line (a level curve) of a hill. What is your rate of climb, and how does the gradient sit relative to your path?
Proof workshopGive the reason for each line of the steepest-ascent proofApostol 8.12

Each line of the proof is shown in order. Choose the reason that justifies it, then press Check.

  1. Let g^=g/∥g∥\hat g = g/\lVert g \rVert, a unit vector, so g⋅u=∥g∥ (g^⋅u)g \cdot u = \lVert g \rVert\,(\hat g \cdot u).
  2. ∥u−g^∥2=∥u∥2−2 g^⋅u+∥g^∥2=2−2 g^⋅u\lVert u - \hat g \rVert^2 = \lVert u \rVert^2 - 2\,\hat g \cdot u + \lVert \hat g \rVert^2 = 2 - 2\,\hat g \cdot u.
  3. Hence g^⋅u≤1\hat g \cdot u \le 1, with equality if and only if u=g^u = \hat g.
  4. Replacing uu by −u-u: − g^⋅u≤1-\,\hat g \cdot u \le 1, with equality if and only if u=−g^u = -\hat g.
  5. So −∥g∥≤f′(a;u)≤∥g∥-\lVert g \rVert \le f'(a; u) \le \lVert g \rVert, with the extremes at u=±g^u = \pm\hat g.
Why· The perpendicular to level sets (proved in 5.3)

Walk along the level curve through aa: ff does not change, so the rate of change along the curve is 00. The rate along a direction uu is ∇f(a)⋅u\nabla f(a)\cdot u, so the direction of the curve is perpendicular to ∇f(a)\nabla f(a). That is the theorem of 5.3, which needs the chain rule along a curve (not only along straight lines) to be stated correctly. Check it now on an example: f=x2+y2f = x^2 + y^2, level curve the circle through (1,0)(1, 0). Its tangent there is vertical, direction (0,1)(0, 1), and ∇f(1,0)=(2,0)\nabla f(1, 0) = (2, 0); the dot product is 00.

Worked example MATH 375 HW 12 (Apostol 8.14 #3)
At which points (x,y)(x, y) of the circle x2+y2=1x^2 + y^2 = 1, and in which directions, is the directional derivative of f(x,y)=3x2+y2f(x, y) = 3x^2 + y^2 largest?

Solution.

  1. The gradient. ∇f=(6x,2y)\nabla f = (6x, 2y). Partial derivatives.
  2. Fix a point; choose the best direction. At a point pp the largest directional derivative is ∥∇f(p)∥\lVert\nabla f(p)\rVert, reached in the direction ∇f(p)/∥∇f(p)∥\nabla f(p)/\lVert\nabla f(p)\rVert. Steepest ascent theorem (we may use it: the partials are polynomials, so ff is differentiable by 5.3).
  3. Now choose the best point on the circle. ∥∇f∥2=36x2+4y2=4+32x2\lVert\nabla f\rVert^2 = 36x^2 + 4y^2 = 4 + 32x^2 when x2+y2=1x^2 + y^2 = 1. Replace y2y^2 by 1−x21 - x^2.
  4. Maximise. This is largest when x2=1x^2 = 1 (the biggest x2x^2 can be), giving ∥∇f∥2=36\lVert\nabla f\rVert^2 = 36 and ∥∇f∥=6\lVert\nabla f\rVert = 6. x2≤1x^2 \le 1 on the circle.
  5. The direction. At (1,0)(1, 0): ∇f=(6,0)\nabla f = (6, 0), direction (1,0)(1, 0). At (−1,0)(-1, 0): ∇f=(−6,0)\nabla f = (-6, 0), direction (−1,0)(-1, 0). Normalise the gradient.

Answer. The largest directional derivative on the circle is 66, attained at (1,0)(1, 0) in the direction i\mathbf i and at (−1,0)(-1, 0) in the direction −i-\mathbf i. The level curves of ff are the ellipses 3x2+y2=c3x^2 + y^2 = c; they are closest together along the xx-axis, where ff climbs fastest.

Your answerAt a point where ∇f=(3,4)\nabla f = (3, 4), what is the largest possible value of the directional derivative f′(a;u)f'(a; u) over unit vectors uu?
Worked example MATH 375 HW 12 (Apostol 8.14 #1b, #1e)
Find the gradients of f=excos⁡yf = e^x\cos y and of f=log⁡(x2+2y2−3z2)f = \log(x^2 + 2y^2 - 3z^2).

Solution.

  1. First field. fx=excos⁡yf_x = e^x\cos y and fy=−exsin⁡yf_y = -e^x\sin y. Hold yy fixed, then xx fixed.
  2. Second field. With u=x2+2y2−3z2u = x^2 + 2y^2 - 3z^2: fx=2xuf_x = \dfrac{2x}{u}, fy=4yuf_y = \dfrac{4y}{u}, fz=−6zuf_z = \dfrac{-6z}{u}. Chain rule for log⁡u\log u.

Answer. ∇(excos⁡y)=(excos⁡y, −exsin⁡y)\nabla(e^x\cos y) = (e^x\cos y,\ -e^x\sin y) and ∇log⁡(x2+2y2−3z2)=(2x, 4y, −6z)x2+2y2−3z2\nabla\log(x^2 + 2y^2 - 3z^2) = \dfrac{(2x,\ 4y,\ -6z)}{x^2 + 2y^2 - 3z^2}, where the argument of log⁡\log is positive. Both match Apostol’s answers.

Worked example Apostol 8.14 #2
Evaluate the directional derivatives of f(x,y,z)=x2+2y2+3z2f(x, y, z) = x^2 + 2y^2 + 3z^2 at (1,1,0)(1, 1, 0) in the direction of i−j+2k\mathbf i - \mathbf j + 2\mathbf k, and of f=(x/y)zf = (x/y)^z at (1,1,1)(1, 1, 1) in the direction of 2i+j−k2\mathbf i + \mathbf j - \mathbf k.

Solution.

  1. Gradient of the first. ∇f=(2x,4y,6z)\nabla f = (2x, 4y, 6z), which is (2,4,0)(2, 4, 0) at (1,1,0)(1, 1, 0). Partials.
  2. Make the direction a unit vector. (1,−1,2)(1, -1, 2) has length 6\sqrt6, so u=(1,−1,2)6u = \dfrac{(1, -1, 2)}{\sqrt6}. A directional derivative uses a unit vector; otherwise the answer is scaled by ∥y∥\lVert y \rVert.
  3. Dot. f′=(2,4,0)⋅u=2−4+06=−26f' = (2, 4, 0)\cdot u = \dfrac{2 - 4 + 0}{\sqrt6} = -\dfrac{2}{\sqrt6}.
  4. Second field. ∇f=(z xz−1y−z, −z xzy−z−1, (x/y)zln⁡(x/y))\nabla f = \big(z\,x^{z-1}y^{-z},\ -z\,x^zy^{-z-1},\ (x/y)^z\ln(x/y)\big), which is (1,−1,0)(1, -1, 0) at (1,1,1)(1, 1, 1). ln⁡(1)=0\ln(1) = 0 kills the last entry.
  5. Direction and dot. u=(2,1,−1)/6u = (2, 1, -1)/\sqrt6, so f′=2−1+06=16f' = \dfrac{2 - 1 + 0}{\sqrt6} = \dfrac{1}{\sqrt6}.

Answer. −26-\dfrac{2}{\sqrt6} and 16\dfrac{1}{\sqrt6}, matching Apostol’s answers. Forgetting to divide by 6\sqrt6 is the usual slip.

Worked example Apostol 8.14 #4
A differentiable scalar field ff has, at (1,2)(1, 2), directional derivative +2+2 toward (2,2)(2, 2) and −2-2 toward (1,1)(1, 1). Find ∇f(1,2)\nabla f(1, 2) and the directional derivative toward (4,6)(4, 6).

Solution.

  1. Directions as unit vectors. Toward (2,2)(2, 2) from (1,2)(1, 2) is (1,0)=e1(1, 0) = e_1. Toward (1,1)(1, 1) is (0,−1)=−e2(0, -1) = -e_2. Subtract the points and normalise; both vectors already have length 1.
  2. Translate into partials. f′(a;e1)=fx=2f'(a; e_1) = f_x = 2. f′(a;−e2)=−fy=−2f'(a; -e_2) = -f_y = -2, so fy=2f_y = 2. Scaling lemma with c=−1c = -1.
  3. The gradient. ∇f(1,2)=(2,2)\nabla f(1, 2) = (2, 2).
  4. The new direction. Toward (4,6)(4, 6) is (3,4)(3, 4), of length 55, so u=(3,4)/5u = (3, 4)/5. Unit vector.
  5. Use the formula. f′(a;u)=(2,2)⋅(3,4)5=145f'(a; u) = (2, 2)\cdot\frac{(3, 4)}{5} = \frac{14}{5}. Directional derivative equals gradient dotted with a unit vector (differentiability assumed).

Answer. ∇f(1,2)=2i+2j\nabla f(1, 2) = 2\mathbf i + 2\mathbf j and the derivative toward (4,6)(4, 6) is 145\frac{14}{5}.

Worked example Apostol 8.14 #5, with new numbers
Find constants a,b,ca, b, c so that the directional derivative of f(x,y,z)=ax2y+byz+cz3f(x, y, z) = ax^2y + byz + cz^3 at (1,1,1)(1, 1, 1) has the largest possible value 88 in a direction parallel to the xx-axis.

Solution.

  1. The gradient at the point. ∇f=(2axy, ax2+bz, by+3cz2)\nabla f = (2axy,\ ax^2 + bz,\ by + 3cz^2), which at (1,1,1)(1, 1, 1) is (2a, a+b, b+3c)(2a,\ a + b,\ b + 3c). Partial derivatives, then substitute the point.
  2. Translate the wording. “Largest possible value” means ∥∇f∥=8\lVert\nabla f\rVert = 8, and the direction where it is reached is ∇f/∥∇f∥\nabla f/\lVert\nabla f\rVert. “Parallel to the xx-axis” means that direction is ±e1\pm e_1, so ∇f\nabla f is a multiple of e1e_1: its last two entries are 00. Steepest ascent theorem: the largest rate is the length of the gradient, in the direction of the gradient.
  3. Write the equations. a+b=0a + b = 0 and b+3c=0b + 3c = 0, and the first entry is ±8\pm8, so 2a=±82a = \pm8. The length of (2a,0,0)(2a, 0, 0) is ∣2a∣|2a|.
  4. Solve. With 2a=82a = 8: a=4a = 4, b=−4b = -4, c=43c = \frac43 (direction +e1+e_1). With 2a=−82a = -8: a=−4a = -4, b=4b = 4, c=−43c = -\frac43 (direction −e1-e_1). Back-substitute: b=−ab = -a, then c=−b3c = -\frac b3.

Answer. (a,b,c)=(4,−4,43)(a, b, c) = (4, -4, \frac43) or (−4,4,−43)(-4, 4, -\frac43). Both give a maximum rate of 88, one pointing along +e1+e_1 and the other along −e1-e_1. The same method, with other numbers, gives Apostol’s answer (6,24,−8)(6, 24, -8) or its negative.

Where the derivative must vanish

Theorem T Apostol 8.14 #10(b); Lecture 24

Partial derivatives vanish at an interior extremum

Let ff be defined on a ball around aa, with f(x)≤f(a)f(x) \le f(a) for all xx in the ball (or f(x)≥f(a)f(x) \ge f(a) for all xx). If Dkf(a)D_kf(a) exists then Dkf(a)=0D_kf(a) = 0. Consequently, if ∇f(a)\nabla f(a) exists then ∇f(a)=O\nabla f(a) = O.

In one lineIf ff has a local maximum or minimum at an interior point aa and a partial derivative Dkf(a)D_kf(a) exists, then Dkf(a)=0D_kf(a) = 0; so ∇f(a)=O\nabla f(a) = O when all partials exist.

Why it is trueIt is the many-variable Fermat theorem: it finds the only places where an extremum can hide, and it is how the normal equations of least squares come out of calculus.

ProofApostol 8.14 #10(b)
The idea: Restrict to the k-th coordinate line. The restriction is a one-variable function with an interior maximum at t = 0, so its derivative there is 0 by Fermat's theorem of 1.4.
Let g(t)=f(a+tek)g(t) = f(a + te_k) for small ∣t∣|t|.
The slice along eke_k.
gg has a maximum (or minimum) at the interior point t=0t = 0.
g(t)=f(a+tek)≤f(a)=g(0)g(t) = f(a + te_k) \le f(a) = g(0), as a+teka + te_k lies in the ball.
g′(0)g'(0) exists.
Derivative of a slice: g′(0)=f′(a;ek)=Dkf(a)g'(0) = f'(a; e_k) = D_kf(a).
So g′(0)=0g'(0) = 0, that is Dkf(a)=0D_kf(a) = 0. ■\blacksquare
Fermat's theorem (1.4).
Watch out· Necessary, not sufficient

f(x,y)=x2−y2f(x, y) = x^2 - y^2 has ∇f(0,0)=(0,0)\nabla f(0, 0) = (0, 0), but the origin is not an extremum: f>0f > 0 along the xx-axis and f<0f < 0 along the yy-axis. A point where ∇f=O\nabla f = O is a candidate; telling a maximum from a minimum from a saddle takes the second-derivative test of 5.6 (eigenvalues of the Hessian, using 3.2). And an extremum on a boundary need not have ∇f=O\nabla f = O at all: “check the boundary separately” (Lecture 24).

Worked example Practice Final 2013, problem 8
Find the constants a,ba, b minimising I(a,b)=∫01(ax+b−x2)2 dxI(a, b) = \int_0^1 (ax + b - x^2)^2\,dx.

Solution.

  1. Differentiate under the integral. Ia=2∫01x(ax+b−x2) dx=2(a3+b2−14)I_a = 2\int_0^1 x(ax + b - x^2)\,dx = 2\big(\tfrac a3 + \tfrac b2 - \tfrac14\big) and Ib=2∫01(ax+b−x2) dx=2(a2+b−13)I_b = 2\int_0^1(ax + b - x^2)\,dx = 2\big(\tfrac a2 + b - \tfrac13\big). Chain rule on the square: the derivative of (ax+b−x2)2(ax + b - x^2)^2 in aa is 2(…)⋅x2(\ldots)\cdot x, in bb is 2(…)⋅12(\ldots)\cdot 1. We may differentiate inside the integral since the integrand is a polynomial in a,ba, b.
  2. Set both to zero. a3+b2=14\frac a3 + \frac b2 = \frac14 and a2+b=13\frac a2 + b = \frac13. At a minimum (an interior point of the plane) the gradient is zero, by the theorem.
  3. Solve. From the second, b=13−a2b = \frac13 - \frac a2. Substituting: a3+16−a4=14\frac a3 + \frac16 - \frac a4 = \frac14, so a12=112\frac a{12} = \frac1{12}, a=1a = 1, b=−16b = -\frac16. Linear elimination.
  4. Show it is a minimum. ∫01(ax+b−x2)2dx=∥(ax+b)−x2∥2\int_0^1(ax + b - x^2)^2dx = \lVert(ax + b) - x^2\rVert^2 is the squared distance from x2x^2 to the line-polynomial ax+bax + b. By the best approximation theorem of 2.9, the closest polynomial in span⁡{1,x}\operatorname{span}\{1, x\} is the orthogonal projection of x2x^2, and it is unique. Our critical point is the only candidate, so it is that projection. The theorem says a critical point is a candidate only; here geometry tells us the minimum exists and is unique.

Answer. a=1a = 1, b=−16b = -\frac16: the best straight-line fit to x2x^2 on [0,1][0, 1] in the least-squares sense is x−16x - \frac16. Setting two partial derivatives to zero here is the same as the normal equations of 2.10.

A warning: derivatives in every direction are not enough

In one variable, a derivative at aa forces continuity at aa. One might hope the same holds for directional derivatives: if they exist in every direction, is ff continuous? It is not.

Worked example Lecture 20; Piazza @105; Apostol 8.10
Let f(x,y)=xy2x2+y4f(x, y) = \dfrac{xy^2}{x^2 + y^4} for x≠0x \ne 0 and f(0,y)=0f(0, y) = 0. Show that f′(0;y)f'(0; y) exists for every direction yy, that ff is not continuous at the origin, and that f′(0;y)≠∇f(0)⋅yf'(0; y) \ne \nabla f(0)\cdot y.

Solution.

  1. A direction y=(p,q)y = (p, q) with p≠0p \ne 0. f(hp,hq)=hp⋅h2q2h2p2+h4q4=hpq2p2+h2q4f(hp, hq) = \dfrac{hp\cdot h^2q^2}{h^2p^2 + h^4q^4} = \dfrac{hpq^2}{p^2 + h^2q^4}. So f(hp,hq)−f(0)h=pq2p2+h2q4→q2p\dfrac{f(hp, hq) - f(0)}{h} = \dfrac{pq^2}{p^2 + h^2q^4} \to \dfrac{q^2}{p} as h→0h \to 0. Cancel h2h^2 from numerator and denominator, then divide by hh.
  2. A direction y=(0,q)y = (0, q). f(0,hq)=0f(0, hq) = 0 for every hh, since f(0,y)=0f(0, y) = 0 by definition. So f′(0;(0,q))=0f'(0; (0, q)) = 0. The line is the yy-axis, where ff is defined to be 00.
  3. Conclusion for directions. f′(0;y)f'(0; y) exists for every yy, equal to q2/pq^2/p if p≠0p \ne 0 and 00 if p=0p = 0. Piazza @105: “the directional derivative exists for every direction”.
  4. The gradient. ∇f(0)=(f′(0;e1),f′(0;e2))=(0,0)\nabla f(0) = (f'(0; e_1), f'(0; e_2)) = (0, 0), using q=0q = 0, p=1p = 1 and then p=0p = 0, q=1q = 1. So ∇f(0)⋅y=0\nabla f(0)\cdot y = 0 for all yy. But f′(0;(1,1))=1≠0f'(0; (1, 1)) = 1 \ne 0. The formula f′(a;y)=∇f(a)⋅yf'(a; y) = \nabla f(a)\cdot y fails here; also y↦f′(0;y)y \mapsto f'(0; y) is not even a linear function of yy.
  5. Discontinuity. Along the parabola x=y2x = y^2 (with y≠0y \ne 0): f(y2,y)=y2⋅y2y4+y4=12f(y^2, y) = \dfrac{y^2\cdot y^2}{y^4 + y^4} = \dfrac12. Along x=−y2x = -y^2: −12-\frac12. But along every straight line through the origin f→0f \to 0 (step 1 shows f(hp,hq)=O(h)f(hp, hq) = O(h)). Points with f=12f = \frac12 lie arbitrarily close to the origin while f(0,0)=0f(0, 0) = 0.

Answer. All directional derivatives exist at the origin, yet ff is not continuous there. Spagnolie’s moral (Piazza @105): what goes wrong is that the directional derivative q2/pq^2/p is not itself a continuous function of the direction: it explodes as p→0p \to 0, while at p=0p = 0 it is 00.

Your answerFor the function above, what is the directional derivative f′(0;y)f'(0; y) in the direction y=(2,3)y = (2, 3)?
Quick checkSuppose f′(a;y)f'(a; y) exists for every direction yy. Which conclusion is justified?

Select the last function above (the point starts at the origin). The direction is (p,q)=(cos⁡θ,sin⁡θ)(p, q) = (\cos\theta, \sin\theta), so the slope is q2p=sin⁡2θcos⁡θ\frac{q^2}{p} = \frac{\sin^2\theta}{\cos\theta}. Turn the direction towards the vertical from the right and watch the slope grow without bound; from the left it becomes very negative. Press the button “straight up” to see the slope at exactly the vertical, where the formula does not apply and the slope is 00.

Python labFinite-difference gradients, and the field that fools them
import numpy as np

def f(p):
    x, y, z = p
    return x * y * np.sin(z**2)

def grad_formula(p):
    x, y, z = p
    return np.array([y * np.sin(z**2), x * np.sin(z**2), 2 * x * y * z * np.cos(z**2)])

a = np.array([1.0, 2.0, 3.0])
exact = grad_formula(a)
print("formula gradient:", exact.round(6))
print("   h        forward error   central error")
for k in range(1, 14):
    h = 10.0**-k
    fw = np.array([(f(a + h * e) - f(a)) / h for e in np.eye(3)])
    ce = np.array([(f(a + h * e) - f(a - h * e)) / (2 * h) for e in np.eye(3)])
    print(f"{h:8.0e}   {abs(fw - exact).max():12.2e}   {abs(ce - exact).max():12.2e}")

# A field that is not differentiable at the origin, but has a "gradient" there
def g(x, y):
    return 0.0 if x == 0 else x * y**2 / (x**2 + y**4)
h = 1e-6
print("partials of g at 0:", (g(h, 0) - g(0, 0)) / h, (g(0, h) - g(0, 0)) / h)
print("g'(0; (1,1)) =", (g(h, h) - g(0, 0)) / h, "but grad . (1,1) = 0")

Try this. Run it. The forward difference is most accurate near h = 1e-8, and the central difference near h = 1e-5 or 1e-6: below that, rounding error in f(a + h) - f(a) takes over (the subtraction of two nearly equal numbers loses digits). Change f to np.sin(x) * np.exp(y) * z and update grad_formula to match. In the second block, g has both partials 0 at the origin, so a finite-difference 'gradient check' there would pass, yet the derivative along (1,1) is 1: gradient checking is only meaningful for differentiable fields.

Where it is used

Used inside maths· Inside maths: calculus on a segment, and optimisation

The mean value theorem of 1.4 extends to scalar fields by applying it to a slice (the proof is in the problems): if f′(a+ty;y)f'(a + ty; y) exists for 0≤t≤10 \le t \le 1 then f(a+y)−f(a)=f′(z;y)f(a + y) - f(a) = f'(z; y) for some zz on the segment (Apostol 8.4; Lecture 20). From it follows that a field whose derivative vanishes in every direction on a ball is constant there. The vanishing theorem above is the first step of all many-variable optimisation: critical points (5.6), constrained optimisation with Lagrange multipliers (5.8), and the gradient descent of 5.10 all start from ∇f\nabla f.

Used in the real world· Physics and AI: heat flow, and checking a gradient

Fourier’s law says the heat flux through a material is q=−k∇T\mathbf q = -k\nabla T, with kk the thermal conductivity. Pure copper has k≈400 W/(m K)k \approx 400\ \text{W/(m K)}. Take a copper rod 0.2 m long with its ends held at 100 °C and 20 °C. The temperature drops uniformly, so ∇T\nabla T has length 800.2=400 K/m\frac{80}{0.2} = 400\ \text{K/m} and the flux is 400×400=160,000 W/m2400 \times 400 = 160{,}000\ \text{W/m}^2, flowing from hot to cold, that is against ∇T\nabla T (a rod of cross-section 1 cm21\ \text{cm}^2 would carry 16 W). The negative sign in the law is the steepest-descent direction of the theorem above.

In machine learning, a hand-written or automatic gradient is routinely checked by comparing it with central differences, f(w+hek)−f(w−hek)2h\dfrac{f(w + he_k) - f(w - he_k)}{2h} for each weight wkw_k, with hh around 10−510^{-5} (the lab above shows why). This costs two evaluations of the loss per weight, which for a model with a million weights is two million evaluations. That is why it is used only to test, and training uses the backward pass of 3.8, which delivers all million partials for roughly the price of one evaluation.

Traps

Watch out· Derivative with respect to y depends on the length of y

f′(a;2y)=2f′(a;y)f'(a; 2y) = 2f'(a; y). For “the rate of change per unit distance” use a unit vector. Forgetting to divide by ∥y∥\lVert y \rVert was the usual slip in Apostol 8.14 #2.

Watch out· Existing partials do not make a field differentiable, nor continuous

All the directional derivatives of xy2x2+y4\frac{xy^2}{x^2+y^4} exist at the origin, and it is not continuous there. Do not conclude continuity (or f′(a;y)=∇f⋅yf'(a; y) = \nabla f\cdot y) from the existence of derivatives. Chapter 5.3 gives the condition that works.

Watch out· The formula f′(a; y) = ∇f(a) · y is a theorem with a hypothesis

It needs differentiability at aa. For the example above, ∇f(0)=O\nabla f(0) = O yet f′(0;(1,1))=1f'(0; (1, 1)) = 1.

Watch out· The gradient lives in the input space

For f:R2→Rf: \mathbb R^2 \to \mathbb R, ∇f\nabla f is a vector in the (x,y)(x, y) plane, drawn on the contour map, pointing across the level curves. It is not a vector pointing out of the surface. “Steepest ascent” means: steepest climb of the height, expressed as a direction in the plane below.

Watch out· Partials of a cone at its tip

x2+y2\sqrt{x^2 + y^2} has partials everywhere except the origin: the cone’s tip is a corner. A field can have a gradient formula that looks fine and still fail to have a partial derivative at one point. Check the point separately with the definition.

Watch out· The quotient rule has a sign

∇(f/g)=g∇f−f∇gg2\nabla(f/g) = \dfrac{g\nabla f - f\nabla g}{g^2}. The printed homework key for Apostol 8.14 #7(e) has it upside down. Compare with the one-variable rule (f/g)′=f′g−fg′g2(f/g)' = \frac{f'g - fg'}{g^2}.

Problems

checkMATH 375 HW 12 (Apostol 8.14 #1)

Compute the gradient of f(x,y,z)=x2y3z4f(x, y, z) = x^2y^3z^4.

Solution

∇f=(2xy3z4, 3x2y2z4, 4x2y3z3)\nabla f = (2xy^3z^4,\ 3x^2y^2z^4,\ 4x^2y^3z^3).

checkMATH 375 HW 11 (Apostol 8.9 #10)

For f(x,y)=x4+y4−4x2y2f(x, y) = x^4 + y^4 - 4x^2y^2 find ∇f\nabla f and verify D1(D2f)=D2(D1f)D_1(D_2f) = D_2(D_1f).

Solution

D1f=4x3−8xy2D_1f = 4x^3 - 8xy^2 and D2f=4y3−8x2yD_2f = 4y^3 - 8x^2y. Then D1(D2f)=−16xyD_1(D_2f) = -16xy and D2(D1f)=−16xyD_2(D_1f) = -16xy.

check

Does ∂∂xx2+y2\dfrac{\partial}{\partial x}\sqrt{x^2 + y^2} exist at (0,0)(0, 0)? At (3,4)(3, 4), and what is the gradient there?

Solution

At (0,0)(0, 0): no, f(h,0)=∣h∣f(h, 0) = |h| has no derivative at 00. At (3,4)(3, 4): ∇f=(3,4)5\nabla f = \frac{(3, 4)}{5}, a unit vector pointing away from the origin.

check

If f′(a;y)=7f'(a; y) = 7 for y=(1,2)y = (1, 2), what is f′(a;(−3,−6))f'(a; (-3, -6))?

Solution

By the scaling lemma with c=−3c = -3 ((−3,−6)=−3 (1,2)(-3, -6) = -3\,(1, 2)): f′(a;(−3,−6))=−21f'(a; (-3, -6)) = -21.

useApostol 8.14 #6; MATH 375 HW 12 style

A scalar field ff is differentiable at a point aa in R2\mathbb R^2, with f′(a;y)=1f'(a; y) = 1 and f′(a;z)=2f'(a; z) = 2 where y=2i+3jy = 2\mathbf i + 3\mathbf j and z=i+jz = \mathbf i + \mathbf j. Find ∇f(a)\nabla f(a) and describe all (x,y)(x, y) with f′(a;xi+yj)=6f'(a; x\mathbf i + y\mathbf j) = 6.

Solution

Write ∇f(a)=(p,q)\nabla f(a) = (p, q). Then 2p+3q=12p + 3q = 1 and p+q=2p + q = 2. From the second q=2−pq = 2 - p, so 2p+6−3p=12p + 6 - 3p = 1, p=5p = 5, q=−3q = -3. So ∇f(a)=5i−3j\nabla f(a) = 5\mathbf i - 3\mathbf j, and f′(a;xi+yj)=5x−3y=6f'(a; x\mathbf i + y\mathbf j) = 5x - 3y = 6 is the line through (65,0)(\frac65, 0) and (0,−2)(0, -2).

useApostol 8.14 #5

Find constants a,b,ca, b, c such that the directional derivative of f(x,y,z)=axy2+byz+cz2x3f(x, y, z) = axy^2 + byz + cz^2x^3 at (1,2,−1)(1, 2, -1) has the maximum value 6464 in a direction parallel to the zz-axis.

Solution

∇f=(ay2+3cz2x2, 2axy+bz, by+2czx3)\nabla f = (ay^2 + 3cz^2x^2,\ 2axy + bz,\ by + 2czx^3), which at (1,2,−1)(1, 2, -1) is (4a+3c, 4a−b, 2b−2c)(4a + 3c,\ 4a - b,\ 2b - 2c). The maximum rate is ∥∇f∥\lVert\nabla f\rVert, attained in the direction of ∇f\nabla f. For that direction to be parallel to the zz-axis, the first two entries vanish: 4a+3c=04a + 3c = 0 and 4a=b4a = b. The third entry is ±64\pm 64. Taking +64+64 (direction +k+\mathbf k): b=4ab = 4a, c=−4a3c = -\frac{4a}{3}, 2b−2c=8a+8a3=32a3=642b - 2c = 8a + \frac{8a}{3} = \frac{32a}{3} = 64, so a=6a = 6, b=24b = 24, c=−8c = -8. Taking −64-64 gives (a,b,c)=(−6,−24,8)(a, b, c) = (-6, -24, 8) (maximum 64 in the direction −k-\mathbf k). Apostol lists both.

usePractice Final 2013, problem 2(a)

ff is differentiable with f(1,2)=1f(1, 2) = 1, fx(1,2)=2f_x(1, 2) = 2, fy(1,2)=−1f_y(1, 2) = -1. Estimate f(43,53)f(\frac43, \frac53) by walking along the straight line from (1,2)(1, 2).

Solution

y=(13,−13)y = (\frac13, -\frac13) and f(a+y)≈f(a)+∇f(a)⋅y=1+23+13=2f(a + y) \approx f(a) + \nabla f(a)\cdot y = 1 + \frac23 + \frac13 = 2. The reason: the slice g(t)=f(a+ty)g(t) = f(a + ty) has g(0)=f(a)g(0) = f(a) and slope g′(0)=∇f(a)⋅yg'(0) = \nabla f(a)\cdot y (differentiability), and g(1)≈g(0)+g′(0)g(1) \approx g(0) + g'(0) is the tangent line of the slice. This is the linearization of 5.3, worked there as an example; the improvement in part (b) of that exam uses the second derivatives of 5.6.

use

Find the maximum and minimum of f(x,y)=x2+y2−2xf(x, y) = x^2 + y^2 - 2x over all of R2\mathbb R^2 using the vanishing theorem, and explain why the critical point is a minimum.

Solution

∇f=(2x−2,2y)=O\nabla f = (2x - 2, 2y) = O at (1,0)(1, 0), with f(1,0)=−1f(1, 0) = -1. Completing the square, f=(x−1)2+y2−1≥−1f = (x - 1)^2 + y^2 - 1 \ge -1, so (1,0)(1, 0) is the global minimum. There is no maximum: f→∞f \to \infty as ∥(x,y)∥→∞\lVert(x, y)\rVert \to \infty.

proveMATH 375 HW 12 (Apostol 8.14 #7d, #7e)

If f,gf, g are scalar fields with gradients, prove ∇(fg)=f∇g+g∇f\nabla(fg) = f\nabla g + g\nabla f and, where g≠0g \ne 0, ∇(f/g)=g∇f−f∇gg2\nabla(f/g) = \dfrac{g\nabla f - f\nabla g}{g^2}.

Solution

Plan. Work one coordinate at a time with the one-variable rules.

The kk-th partial of fgfg is a one-variable derivative with the other variables held fixed, so the product rule of 1.4 gives Dk(fg)=(Dkf)g+f(Dkg)D_k(fg) = (D_kf)g + f(D_kg). Collecting the components: ∇(fg)=g∇f+f∇g\nabla(fg) = g\nabla f + f\nabla g. Likewise the quotient rule of 1.4 gives Dk(f/g)=g Dkf−f Dkgg2D_k(f/g) = \dfrac{g\,D_kf - f\,D_kg}{g^2}, so ∇(f/g)=g∇f−f∇gg2\nabla(f/g) = \dfrac{g\nabla f - f\nabla g}{g^2}. ■\blacksquare (The printed key had the numerator reversed.)

proveApostol 8.14 #8

Let r=(x,y,z)\mathbf r = (x, y, z) and r=∥r∥r = \lVert\mathbf r\rVert. Show ∇r=r/r\nabla r = \mathbf r/r for r≠O\mathbf r \ne O, and ∇(rn)=nrn−2r\nabla(r^n) = nr^{n-2}\mathbf r. Is the second formula valid for negative or zero nn? Find a scalar field ff with ∇f=r\nabla f = \mathbf r.

Solution

Plan. Differentiate rn=(x2+y2+z2)n/2r^n = (x^2 + y^2 + z^2)^{n/2} by the chain rule.

∂r∂x=xr\dfrac{\partial r}{\partial x} = \dfrac{x}{r}, and similarly for y,zy, z: ∇r=r/r\nabla r = \mathbf r/r (a unit vector). For rn=(x2+y2+z2)n/2r^n = (x^2 + y^2 + z^2)^{n/2}: ∂∂xrn=n2(x2+y2+z2)n/2−1⋅2x=nrn−2x\dfrac{\partial}{\partial x}r^n = \frac n2(x^2 + y^2 + z^2)^{n/2 - 1}\cdot 2x = nr^{n-2}x, so ∇(rn)=nrn−2r\nabla(r^n) = nr^{n-2}\mathbf r. The computation is valid for every real nn at points where r≠0r \ne 0 (for n≤0n \le 0 the origin is excluded; for n=0n = 0 the gradient of the constant 11 is OO, which the formula gives). For ∇f=r\nabla f = \mathbf r take n=2n = 2 and divide by 2: f=12r2=12(x2+y2+z2)f = \frac12r^2 = \frac12(x^2 + y^2 + z^2). ■\blacksquare

proveApostol 8.4 (Theorem) and 8.9 #20(a)

(a) Prove the mean value theorem for scalar fields: if f′(a+ty;y)f'(a + ty; y) exists for every t∈[0,1]t \in [0, 1], then f(a+y)−f(a)=f′(a+θy;y)f(a + y) - f(a) = f'(a + \theta y; y) for some θ∈(0,1)\theta \in (0, 1). (b) Deduce: if f′(x;y)=0f'(x; y) = 0 for every xx in a ball B(a,r)B(a, r) and every vector yy, then ff is constant on the ball.

Solution

Plan. Apply the one-variable mean value theorem to the slice.

(a) Let g(t)=f(a+ty)g(t) = f(a + ty). By the derivative-of-a-slice theorem, g′(t)=f′(a+ty;y)g'(t) = f'(a + ty; y) exists on [0,1][0, 1]. Then gg is differentiable on [0,1][0, 1] hence continuous, and the mean value theorem of 1.4 gives θ∈(0,1)\theta \in (0, 1) with g(1)−g(0)=g′(θ)g(1) - g(0) = g'(\theta), that is f(a+y)−f(a)=f′(a+θy;y)f(a + y) - f(a) = f'(a + \theta y; y). (b) Let x∈B(a,r)x \in B(a, r) and y=x−ay = x - a. The segment from aa to xx lies in the ball (each point a+tya + ty has ∥ty∥≤∥y∥<r\lVert ty \rVert \le \lVert y \rVert < r). By (a), f(x)−f(a)=f′(z;y)=0f(x) - f(a) = f'(z; y) = 0 for some zz on the segment. So f(x)=f(a)f(x) = f(a) for every xx in the ball. ■\blacksquare

proveApostol 8.9 #22

(a) Prove there is no scalar field ff such that f′(a;y)>0f'(a; y) > 0 for a fixed aa and every nonzero yy. (b) Give a scalar field with f′(x;y)>0f'(x; y) > 0 for a fixed nonzero yy and every xx.

Solution

(a) If f′(a;y)f'(a; y) exists and is positive, the scaling lemma with c=−1c = -1 gives f′(a;−y)=−f′(a;y)<0f'(a; -y) = -f'(a; y) < 0, which contradicts positivity in the direction −y-y. (b) f(x)=x⋅yf(x) = x\cdot y: by the worked example, f′(x;y)=y⋅y=∥y∥2>0f'(x; y) = y\cdot y = \lVert y \rVert^2 > 0 for all xx. ■\blacksquare

proveMATH 375 Final Review, 8.8 to 8.13

Prove that for the field f(x,y)=xyx2+y2f(x, y) = \dfrac{xy}{x^2 + y^2}, f(0,0)=0f(0, 0) = 0, both partial derivatives exist at the origin and are 00, but the directional derivative in the direction (1,1)(1, 1) does not exist.

Solution

Along the axes, f=0f = 0, so D1f(0)=D2f(0)=0D_1f(0) = D_2f(0) = 0. For y=(1,1)y = (1, 1): f(h,h)−f(0)h=12−0h=12h\dfrac{f(h, h) - f(0)}{h} = \dfrac{\frac12 - 0}{h} = \dfrac1{2h}, which has no limit as h→0h \to 0 (it tends to +∞+\infty from the right and −∞-\infty from the left). So f′(0;(1,1))f'(0; (1, 1)) does not exist, although ∇f(0)=O\nabla f(0) = O does. ■\blacksquare

stretchApostol 8.23; Schwarz and Peano

Let f(x,y)=xy(x2−y2)x2+y2f(x, y) = \dfrac{xy(x^2 - y^2)}{x^2 + y^2} for (x,y)≠(0,0)(x, y) \ne (0, 0) and f(0,0)=0f(0, 0) = 0. Compute D1f(0,y)D_1f(0, y) and D2f(x,0)D_2f(x, 0), and deduce that D2(D1f)(0,0)D_2(D_1f)(0, 0) and D1(D2f)(0,0)D_1(D_2f)(0, 0) are different.

Solution

For y≠0y \ne 0: f(h,y)−f(0,y)h=y(h2−y2)h2+y2→−y\dfrac{f(h, y) - f(0, y)}{h} = \dfrac{y(h^2 - y^2)}{h^2 + y^2} \to -y as h→0h \to 0, so D1f(0,y)=−yD_1f(0, y) = -y (also true at y=0y = 0). For x≠0x \ne 0: f(x,h)−f(x,0)h=x(x2−h2)x2+h2→x\dfrac{f(x, h) - f(x, 0)}{h} = \dfrac{x(x^2 - h^2)}{x^2 + h^2} \to x, so D2f(x,0)=xD_2f(x, 0) = x. Then D2(D1f)(0,0)=ddy(−y)∣0=−1D_2(D_1f)(0, 0) = \frac{d}{dy}(-y)\big|_0 = -1 and D1(D2f)(0,0)=ddx(x)∣0=+1D_1(D_2f)(0, 0) = \frac{d}{dx}(x)\big|_0 = +1. The mixed partials differ, so the second partials of this ff cannot be continuous at the origin: the equality theorem needs continuity.

stretchApostol 8.17 #6, previewed

Let f(x,y)=∣xy∣f(x, y) = \sqrt{|xy|}. Show that both partial derivatives are 00 at the origin, but that f′(0;(1,1))f'(0; (1, 1)) does not exist. What does this say about a tangent plane to the surface z=f(x,y)z = f(x, y) at the origin?

Solution

On the axes f=0f = 0, so both partials are 00. Along (1,1)(1, 1): f(h,h)−0h=∣h∣h\dfrac{f(h, h) - 0}{h} = \dfrac{|h|}{h}, which is 11 for h>0h > 0 and −1-1 for h<0h < 0: no limit. Cutting the surface by the plane x=yx = y gives z=∣x∣z = |x|, a V with a corner at the origin. A tangent plane would have to contain the tangent line to every smooth curve on the surface through the origin; no line fits a corner. So the surface has no tangent plane at the origin, although both partials exist (and are zero). Chapter 5.3 explains: the partials alone do not determine a tangent plane.

Summary

  • The derivative of ff at aa with respect to yy is f′(a;y)=lim⁡h→0f(a+hy)−f(a)h=g′(0)f'(a; y) = \lim_{h \to 0}\frac{f(a + hy) - f(a)}{h} = g'(0) for the slice g(t)=f(a+ty)g(t) = f(a + ty). For a unit vector yy it is the directional derivative: the rate of change per unit distance. Scaling yy by cc scales it by cc.
  • A partial derivative Dkf=f′(⋅;ek)D_kf = f'(\cdot; e_k) is computed by holding the other variables fixed. The gradient ∇f=(D1f,…,Dnf)\nabla f = (D_1f, \dots, D_nf) lists them; it lives in the input space.
  • If ff is differentiable (5.3), then f′(a;y)=∇f(a)⋅yf'(a; y) = \nabla f(a)\cdot y for all yy. Then ∇f\nabla f points in the direction of steepest ascent, its length is the steepest rate, and it is perpendicular to the level set through the point.
  • At an interior maximum or minimum every partial derivative that exists is 00 (Fermat in many variables). This is a necessary condition only (x2−y2x^2 - y^2 at the origin), and the boundary must be checked separately.
  • Warning: a field can have a directional derivative in every direction at a point and still be discontinuous there (xy2x2+y4\frac{xy^2}{x^2+y^4}). The map y↦f′(a;y)y \mapsto f'(a; y) need not be linear, and f′(a;y)=∇f⋅yf'(a; y) = \nabla f\cdot y can fail. That gap is closed by the total derivative of 5.3.
  • Mixed partials are equal when the second partials are continuous; without continuity they can differ (+1+1 against −1-1 for Schwarz and Peano’s example).

Watch and read

The same ideas explained by someone else. Use these when an explanation here does not land.

Where this sits

Comes from
1.4 The derivative as best linear approximation
5.1 Scalar and vector fields, limits and continuity
2.9 Inner products, orthogonality, Gram–Schmidt, projection
Leads to
5.3 The total derivative, the Jacobian matrix and tangent planes
Same idea elsewhere
MATH 375: Lecture 20 and Apostol 8.6 to 8.10, and homework 8.9 and 8.14 (gradients, directional derivatives, the circle problem). The sneaky function x y² / (x² + y⁴) of Piazza @105 is worked in full.
MATH 340: The gradient points in the direction of steepest ascent, so steepest descent moves against it. Every optimisation method in 340 and 5.10 starts from this theorem.
MATH 521: 521 distinguishes the Gateaux derivative (a derivative in each direction, this chapter) from the Fréchet derivative (the total derivative of 5.3). The Fréchet derivative is the one with a chain rule.
AI: Gradient descent, gradient checking and every optimiser (SGD, Adam) are built on the gradient of the loss.