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 f change when x 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)2. What is g′(0)?
g(t)=9+12t+4t2, so g′(0)=12. (By the chain rule, 2(3+2t)⋅2 at t=0 is 12.) This is exactly what a directional derivative will be: the ordinary derivative of a function of t built by walking along a line.Review: the derivative as a limit
Quick checkFor a vector g and a unit vector u, how large can g⋅u be?
Cauchy–Schwarz: ∣g⋅u∣≤∥g∥∥u∥=∥g∥. The bound is attained for u=g/∥g∥. This is the whole content of 'the gradient points uphill'.Review: Cauchy–Schwarz
Quick checkWhat is the angle between two vectors with dot product 0?
Left is the plane with the point a (red) and a line through it in a chosen direction (purple). Right is the slice: the graph of f above that line, a curve in one variable t. Its slope at t=0 is the number this chapter is about. Things to do:
Take 3x2+y2 and point the direction along the x-axis (angle 0). The slope is 6x at your point: the ordinary derivative of 3x2 with y held fixed. Now point along the y-axis: the slope is 2y.
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 0 (the direction of the level curve through the point).
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 a and a vector y. The line through a with direction y is a+hy, where h is a real number: at h=0 we are at a, at h=1 at a+y. Walk along it and record the value of f: this is the one-variable function g(h)=f(a+hy), the slice. The directional derivative is the number 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 a to a+hy is ∣h∣∥y∥. If you want the rate of change per unit distance, you want ∥y∥=1. If you double y, you walk twice as fast along the same line, so the rate of change per unit of h doubles. That is why Apostol allows any vector y in the definition (“the derivative with respect to y”) and reserves the word “directional” for unit vectors. When n=1 and y=1 the definition is the derivative of 1.4.
Definitions
Definition T
Directional derivative
Let f:S→R be a scalar field, let a be an interior point of S (some ball B(a,r) lies in S, so the points a+hy with ∣h∣∥y∥<r are in S) and let y∈Rn. The derivative of f at a with respect to y is
f′(a;y)=h→0limhf(a+hy)−f(a),
when the limit exists. If ∥y∥=1 it is called the directional derivative of f at a in the direction of y. The fraction is the average rate of change over the segment from a to a+hy. (Apostol 8.6 and 8.7.)
The limit is two-sided: h→0 from both signs, so h may be negative and then we walk backwards from a. Some books use only h→0+ (a one-sided derivative). Apostol, Spagnolie and this book use the two-sided one, and the difference matters: for f(x)=∥x∥ at a=O we get h∣h∣∥y∥=±∥y∥, which has a one-sided limit but no two-sided limit.
In one lineFor a scalar field f, a point a and a vector y: f′(a;y)=limh→0hf(a+hy)−f(a), the rate of change of f as you walk from a in the direction y; it is called the directional derivative when ∥y∥=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 Tnew (the idea of Apostol 8.9 #22)
Scaling a direction scales the derivative
If f′(a;y) exists, then f′(a;cy) exists and equals cf′(a;y) for every real number c.
In one lineIf f′(a;y) exists then f′(a;cy)=cf′(a;y) for every real c.
Why it is trueWalking c times as fast along the same line gives c times the rate; in particular walking backwards (c=−1) reverses the sign.
Proofnew
The idea: Change the variable in the difference quotient from h to ch.
If c=0 both sides are 0.
For c=0: f(a+h⋅0)−f(a)=0 for every h.
If c=0: hf(a+hcy)−f(a)=c⋅hcf(a+(hc)y)−f(a).
Multiply and divide by c. This is allowed since c=0.
As h→0 the right side tends to cf′(a;y). ■
k=hc→0 exactly when h→0, and the quotient in k tends to f′(a;y).
Theorem TApostol Thm 8.3
The derivative of a slice
Let g(t)=f(a+ty). If one of g′(t) and f′(a+ty;y) exists, so does the other, and g′(t)=f′(a+ty;y). In particular g′(0)=f′(a;y).
In one lineFor g(t)=f(a+ty), the derivative g′(t) exists exactly when f′(a+ty;y) exists, and then 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.
hg(t+h)−g(t)=hf(a+ty+hy)−f(a+ty).
Definition of g.
So the left side has a limit as h→0 exactly when f′(a+ty;y) exists, and then the two are equal. ■
The right side is the difference quotient defining f′(b;y) at the point b=a+ty.
Definition T
Partial derivative
The partial derivative of f with respect to the k-th variable at a is the derivative with respect to the k-th unit vector ek:
Other notations: ∂xk∂f(a) and fxk(a); in the plane ∂x∂f and ∂y∂f, or fx and fy. 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) is the derivative of f along the k-th coordinate axis: differentiate in xk 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 f exist at a, the gradient of f at a is the vector
∇f(a)=(D1f(a),D2f(a),…,Dnf(a)).
Also written gradf(a); the symbol ∇ is read “del” or “nabla”. Where it exists at every point of a set, ∇f is a vector field. In the plane, ∇f=(∂x∂f,∂y∂f). The gradient lives in the domain space Rn (it has one entry per input variable), not in the space of outputs.
In one lineThe gradient ∇f(a)=(D1f(a),…,Dnf(a)) is the vector of all the partial derivatives of f at a.
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: 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 ∇ 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→R, in which space does the vector ∇f(a) live?
The gradient lists the n partial derivatives, one for each input direction, so it is a vector in the domain Rn. On a contour map it is an arrow drawn in the plane of the map, not a vector sticking out of the surface.
Computing partial derivatives
Worked example Lecture 20
For F(x,y,z)=xysin(z2), find the three partial derivatives at a=(1,2,3) and the second partials Fxx, Fxy, Fzz.
Solution.
x-partial. Hold y,z fixed: Fx=ysin(z2). At a: 2sin9. xysin(z2)=x⋅(constant) as a function of x.
y-partial.Fy=xsin(z2). At a: sin9. Same, now y varies.
z-partial.Fz=xycos(z2)⋅2z=2xyzcos(z2). At a: 12cos9. Chain rule of 1.4 for sin(z2), the inner function z2 having derivative 2z.
Second partials.Fxx=0, since Fx=ysin(z2) does not contain x. Fxy=sin(z2), the y-derivative of Fx. Fzz=2xy(cos(z2)−2z2sin(z2)) by the product rule on 2xyzcos(z2). Each partial is again a scalar field, so we can differentiate it again.
Answer.∇F(a)=(2sin9,sin9,12cos9)≈(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 x, y or z left in it, as Spagnolie reminded the class.)
Your answerFor f(x,y)=x2+y2sin(xy), find ∂y∂f at (1,π/2).
Product rule on y2sin(xy), chain rule on sin(xy) with x fixed.
fy=2ysin(xy)+y2cos(xy)⋅x=2ysin(xy)+xy2cos(xy). At (1,π/2): 2⋅2π⋅1+1⋅4π2⋅0=π.
Worked example MATH 375 HW 11 (Apostol 8.9 #1 and #8)
Compute f′(x;y) and all partial derivatives of f(x)=a⋅x on Rn, for a fixed vector a.
Solution.
Build the slice.g(t)=f(x+ty)=a⋅(x+ty)=a⋅x+t(a⋅y). The dot product is linear in its second slot.
Differentiate it.g′(0)=a⋅y, so f′(x;y)=a⋅y for every x and y. The slice is a straight line in t with slope a⋅y.
Partials. Take y=ei: Dif(x)=a⋅ei=ai. A partial derivative is a derivative with respect to a unit coordinate vector.
Answer.f′(x;y)=a⋅y (independent of x), ∂xi∂f=ai, so ∇f=a. For a linear field the gradient is a constant vector; compare 3.8, where ∇(aTx)=a.
Worked example MATH 375 HW 11 (Apostol 8.9 #2a, #2b)
Find f′(a;y) for f(x)=∥x∥4 on Rn, and for n=2 find all y=(p,q) with f′((2,3);(p,q))=6.
Solution.
Build the slice.∥a+ty∥2=∥a∥2+2ta⋅y+t2∥y∥2, so g(t)=f(a+ty)=(∥a∥2+2ta⋅y+t2∥y∥2)2. Expand (a+ty)⋅(a+ty) using bilinearity.
Differentiate at t=0. Chain rule: g′(t)=2(…)⋅(2a⋅y+2t∥y∥2), so g′(0)=2∥a∥2⋅2a⋅y=4∥a∥2(a⋅y). The bracket at t=0 is ∥a∥2.
Read the gradient.f′(a;y)=4∥a∥2(a⋅y) means the partials are 4∥a∥2ai, so ∇f(a)=4∥a∥2a, a vector (as it should be). Take y=ei.
The case n=2. For a=(2,3): ∥a∥2=13, and f′(a;(p,q))=4⋅13(2p+3q)=52(2p+3q). Set it equal to 6: 2p+3q=526=263. One linear equation in (p,q).
Answer.f′(a;y)=4∥a∥2(a⋅y); the required vectors lie on the line 2p+3q=263.
Worked example MATH 375 HW 11 (Apostol 8.9 #9)
Find all partial derivatives of the quadratic form f(x)=∑i,jaijxixj when aij=aji.
Solution.
Differentiate a typical term.∂xk∂(xixj)=δikxj+xiδjk, where δik is 1 if i=k and 0 otherwise. Product rule; ∂xk∂xi=1 if i=k and 0 otherwise, since the variables are independent.
Sum over all i,j.∂xk∂f=∑jakjxj+∑iaikxi. Only the terms with i=k (first sum) or j=k (second sum) survive.
Use symmetry.aik=aki, so both sums equal ∑jakjxj=(Ax)k. Rename the summation index in the second sum.
Answer.∂xk∂f=2(Ax)k, that is ∇(xTAx)=2Ax for symmetric A. This is the 2Ax of 3.8; the general rule (A+AT)x for non-symmetric A 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+y2, and decide whether they exist at the origin.
Solution.
Away from the origin.fx=x2+y2x and fy=x2+y2y. Chain rule: dxdu=2u1dxdu with u=x2+y2 and dxdu=2x.
In any dimension. For f(x)=∥x∥, ∂xk∂f=∥x∥xk, so ∇∥x∥=∥x∥x is the unit vector pointing away from the origin. Same computation with n terms under the root.
At the origin.f(h,0)=h2=∣h∣, and h∣h∣−0 is 1 for h>0 and −1 for h<0. So fx(0,0) does not exist (likewise fy). This is the corner of ∣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 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) on (x,y)=(0,0), compute the first partials and check that the two mixed second partials agree.
Solution.
First partials.D1f=x2+y22x, D2f=x2+y22y. Chain rule for logu, derivative u1dxdu.
Differentiate D2f in x.D1(D2f)=(x2+y2)2−2y⋅2x=(x2+y2)2−4xy. Quotient rule on 2y(x2+y2)−1 with y constant.
Differentiate D1f in y.D2(D1f)=(x2+y2)2−2x⋅2y=(x2+y2)2−4xy. Same computation with the roles swapped.
Answer.D1(D2f)=D2(D1f)=(x2+y2)2−4xy. 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) means: differentiate in the second variable first, then the first. Books write it ∂x∂y∂2f (Apostol) or fyx, and Spagnolie’s notes write Fxy for ”x first, then y”. 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 and −1.
Worked example MATH 375 HW 11 (Apostol 8.9 #18)
Find the constant n for which v(r,t)=tne−r2/(4t) satisfies ∂t∂v=r21∂r∂(r2∂r∂v).
r-derivative.vr=−2trtne−r2/4t, so r2vr=−21r3tn−1e−r2/4t. Chain rule on the exponent, then multiply by r2.
Differentiate again.∂r∂(r2vr)=−23r2tn−1e−r2/4t+41r4tn−2e−r2/4t. Divide by r2: −23tn−1e−r2/4t+41r2tn−2e−r2/4t. Product rule again: drd(−21r3)=−23r2, and drde−r2/4t=−2tre−r2/4t.
Compare coefficients. The r2tn−2 terms agree (41 on both sides). The tn−1 terms need n=−23. Both sides are e−r2/4t times a combination of tn−1 and r2tn−2.
Answer.n=−23. This v 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/2 and e−r2/4t say that the heat cloud has width about t and height about t−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 f is differentiable at a (the total derivative of 5.3: f is well approximated by a linear function near a), then for every vector y
f′(a;y)=∇f(a)⋅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 TApostol 8.12
The gradient is the direction of steepest ascent
Suppose f′(a;y)=g⋅y for every y, where g=∇f(a)=O. For unit vectors u:
(i) −∥g∥≤f′(a;u)≤∥g∥.
(ii) f′(a;u)=∥g∥ if and only if u=g/∥g∥; f′(a;u)=−∥g∥ if and only if u=−g/∥g∥.
(iii) f′(a;u)=0 if and only if u⊥g.
In one lineIf f′(a;y)=∇f(a)⋅y for all y and ∇f(a)=O, then among unit vectors u the rate f′(a;u) is largest, equal to ∥∇f(a)∥, for u=∇f(a)/∥∇f(a)∥, smallest for u=−∇f(a)/∥∇f(a)∥, and 0 for u perpendicular to ∇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∥, so ∥g^∥=1 and g⋅u=∥g∥(g^⋅u).
Name the unit vector along g.
∥u−g^∥2=1−2g^⋅u+1=2−2g^⋅u.
Expand (u−g^)⋅(u−g^) using ∥u∥=∥g^∥=1.
So g^⋅u≤1, with equality if and only if u=g^.
A squared length is ≥0, and it is 0 exactly when u=g^.
Likewise −g^⋅u≤1, with equality if and only if u=−g^. Together ∣g^⋅u∣≤1.
Replace u by −u in the previous step.
This proves (i) and (ii).
Multiply by ∥g∥>0 and use f′(a;u)=∥g∥(g^⋅u).
(iii) follows. ■
f′(a;u)=g⋅u, which is 0 exactly when u⊥g (definition of orthogonal, 2.9).
In the picture: the red arrow is ∇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 u (green) around: the number ∇f⋅u is largest when u lies along the red arrow, zero when u 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?
Along a level curve f does not change, so the rate of climb is 0. Since the rate in a direction u is ∇f⋅u, that means u⊥∇f. The gradient points straight up the slope, across the contour lines.
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.
Let g^=g/∥g∥, a unit vector, so g⋅u=∥g∥(g^⋅u).
∥u−g^∥2=∥u∥2−2g^⋅u+∥g^∥2=2−2g^⋅u.
Hence g^⋅u≤1, with equality if and only if u=g^.
Replacing u by −u: −g^⋅u≤1, with equality if and only if u=−g^.
So −∥g∥≤f′(a;u)≤∥g∥, with the extremes at u=±g^.
Why· The perpendicular to level sets (proved in 5.3)
Walk along the level curve through a: f does not change, so the rate of change along the curve is 0. The rate along a direction u is ∇f(a)⋅u, so the direction of the curve is perpendicular to ∇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+y2, level curve the circle through (1,0). Its tangent there is vertical, direction (0,1), and ∇f(1,0)=(2,0); the dot product is 0.
Worked example MATH 375 HW 12 (Apostol 8.14 #3)
At which points (x,y) of the circle x2+y2=1, and in which directions, is the directional derivative of f(x,y)=3x2+y2 largest?
Solution.
The gradient.∇f=(6x,2y). Partial derivatives.
Fix a point; choose the best direction. At a point p the largest directional derivative is ∥∇f(p)∥, reached in the direction ∇f(p)/∥∇f(p)∥. Steepest ascent theorem (we may use it: the partials are polynomials, so f is differentiable by 5.3).
Now choose the best point on the circle.∥∇f∥2=36x2+4y2=4+32x2 when x2+y2=1. Replace y2 by 1−x2.
Maximise. This is largest when x2=1 (the biggest x2 can be), giving ∥∇f∥2=36 and ∥∇f∥=6. x2≤1 on the circle.
The direction. At (1,0): ∇f=(6,0), direction (1,0). At (−1,0): ∇f=(−6,0), direction (−1,0). Normalise the gradient.
Answer. The largest directional derivative on the circle is 6, attained at (1,0) in the direction i and at (−1,0) in the direction −i. The level curves of f are the ellipses 3x2+y2=c; they are closest together along the x-axis, where f climbs fastest.
Your answerAt a point where ∇f=(3,4), what is the largest possible value of the directional derivative f′(a;u) over unit vectors u?
∥(3,4)∥=9+16=5, attained for u=(3,4)/5. Direction (4,−3)/5 would give 0.
Worked example MATH 375 HW 12 (Apostol 8.14 #1b, #1e)
Find the gradients of f=excosy and of f=log(x2+2y2−3z2).
Solution.
First field.fx=excosy and fy=−exsiny. Hold y fixed, then x fixed.
Second field. With u=x2+2y2−3z2: fx=u2x, fy=u4y, fz=u−6z. Chain rule for logu.
Answer.∇(excosy)=(excosy,−exsiny) and ∇log(x2+2y2−3z2)=x2+2y2−3z2(2x,4y,−6z), where the argument of 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+3z2 at (1,1,0) in the direction of i−j+2k, and of f=(x/y)z at (1,1,1) in the direction of 2i+j−k.
Solution.
Gradient of the first.∇f=(2x,4y,6z), which is (2,4,0) at (1,1,0). Partials.
Make the direction a unit vector.(1,−1,2) has length 6, so u=6(1,−1,2). A directional derivative uses a unit vector; otherwise the answer is scaled by ∥y∥.
Dot.f′=(2,4,0)⋅u=62−4+0=−62.
Second field.∇f=(zxz−1y−z,−zxzy−z−1,(x/y)zln(x/y)), which is (1,−1,0) at (1,1,1). ln(1)=0 kills the last entry.
Direction and dot.u=(2,1,−1)/6, so f′=62−1+0=61.
Answer.−62 and 61, matching Apostol’s answers. Forgetting to divide by 6 is the usual slip.
Worked example Apostol 8.14 #4
A differentiable scalar field f has, at (1,2), directional derivative +2 toward (2,2) and −2 toward (1,1). Find ∇f(1,2) and the directional derivative toward (4,6).
Solution.
Directions as unit vectors. Toward (2,2) from (1,2) is (1,0)=e1. Toward (1,1) is (0,−1)=−e2. Subtract the points and normalise; both vectors already have length 1.
Translate into partials.f′(a;e1)=fx=2. f′(a;−e2)=−fy=−2, so fy=2. Scaling lemma with c=−1.
The gradient.∇f(1,2)=(2,2).
The new direction. Toward (4,6) is (3,4), of length 5, so u=(3,4)/5. Unit vector.
Use the formula.f′(a;u)=(2,2)⋅5(3,4)=514. Directional derivative equals gradient dotted with a unit vector (differentiability assumed).
Answer.∇f(1,2)=2i+2j and the derivative toward (4,6) is 514.
Worked example Apostol 8.14 #5, with new numbers
Find constants a,b,c so that the directional derivative of f(x,y,z)=ax2y+byz+cz3 at (1,1,1) has the largest possible value 8 in a direction parallel to the x-axis.
Solution.
The gradient at the point.∇f=(2axy,ax2+bz,by+3cz2), which at (1,1,1) is (2a,a+b,b+3c). Partial derivatives, then substitute the point.
Translate the wording. “Largest possible value” means ∥∇f∥=8, and the direction where it is reached is ∇f/∥∇f∥. “Parallel to the x-axis” means that direction is ±e1, so ∇f is a multiple of e1: its last two entries are 0. Steepest ascent theorem: the largest rate is the length of the gradient, in the direction of the gradient.
Write the equations.a+b=0 and b+3c=0, and the first entry is ±8, so 2a=±8. The length of (2a,0,0) is ∣2a∣.
Solve. With 2a=8: a=4, b=−4, c=34 (direction +e1). With 2a=−8: a=−4, b=4, c=−34 (direction −e1). Back-substitute: b=−a, then c=−3b.
Answer.(a,b,c)=(4,−4,34) or (−4,4,−34). Both give a maximum rate of 8, one pointing along +e1 and the other along −e1. The same method, with other numbers, gives Apostol’s answer (6,24,−8) or its negative.
Where the derivative must vanish
Theorem TApostol 8.14 #10(b); Lecture 24
Partial derivatives vanish at an interior extremum
Let f be defined on a ball around a, with f(x)≤f(a) for all x in the ball (or f(x)≥f(a) for all x). If Dkf(a) exists then Dkf(a)=0. Consequently, if ∇f(a) exists then ∇f(a)=O.
In one lineIf f has a local maximum or minimum at an interior point a and a partial derivative Dkf(a) exists, then Dkf(a)=0; so ∇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) for small ∣t∣.
The slice along ek.
g has a maximum (or minimum) at the interior point t=0.
g(t)=f(a+tek)≤f(a)=g(0), as a+tek lies in the ball.
g′(0) exists.
Derivative of a slice: g′(0)=f′(a;ek)=Dkf(a).
So g′(0)=0, that is Dkf(a)=0. ■
Fermat's theorem (1.4).
Watch out· Necessary, not sufficient
f(x,y)=x2−y2 has ∇f(0,0)=(0,0), but the origin is not an extremum: f>0 along the x-axis and f<0 along the y-axis. A point where ∇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 at all: “check the boundary separately” (Lecture 24).
Worked example Practice Final 2013, problem 8
Find the constants a,b minimising I(a,b)=∫01(ax+b−x2)2dx.
Solution.
Differentiate under the integral.Ia=2∫01x(ax+b−x2)dx=2(3a+2b−41) and Ib=2∫01(ax+b−x2)dx=2(2a+b−31). Chain rule on the square: the derivative of (ax+b−x2)2 in a is 2(…)⋅x, in b is 2(…)⋅1. We may differentiate inside the integral since the integrand is a polynomial in a,b.
Set both to zero.3a+2b=41 and 2a+b=31. At a minimum (an interior point of the plane) the gradient is zero, by the theorem.
Solve. From the second, b=31−2a. Substituting: 3a+61−4a=41, so 12a=121, a=1, b=−61. Linear elimination.
Show it is a minimum.∫01(ax+b−x2)2dx=∥(ax+b)−x2∥2 is the squared distance from x2 to the line-polynomial ax+b. By the best approximation theorem of 2.9, the closest polynomial in span{1,x} is the orthogonal projection of x2, 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=1, b=−61: the best straight-line fit to x2 on [0,1] in the least-squares sense is x−61. 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 a forces continuity at a. One might hope the same holds for directional derivatives: if they exist in every direction, is f continuous? It is not.
Worked example Lecture 20; Piazza @105; Apostol 8.10
Let f(x,y)=x2+y4xy2 for x=0 and f(0,y)=0. Show that f′(0;y) exists for every direction y, that f is not continuous at the origin, and that f′(0;y)=∇f(0)⋅y.
Solution.
A direction y=(p,q) with p=0.f(hp,hq)=h2p2+h4q4hp⋅h2q2=p2+h2q4hpq2. So hf(hp,hq)−f(0)=p2+h2q4pq2→pq2 as h→0. Cancel h2 from numerator and denominator, then divide by h.
A direction y=(0,q).f(0,hq)=0 for every h, since f(0,y)=0 by definition. So f′(0;(0,q))=0. The line is the y-axis, where f is defined to be 0.
Conclusion for directions.f′(0;y) exists for every y, equal to q2/p if p=0 and 0 if p=0. Piazza @105: “the directional derivative exists for every direction”.
The gradient.∇f(0)=(f′(0;e1),f′(0;e2))=(0,0), using q=0, p=1 and then p=0, q=1. So ∇f(0)⋅y=0 for all y. But f′(0;(1,1))=1=0. The formula f′(a;y)=∇f(a)⋅y fails here; also y↦f′(0;y) is not even a linear function of y.
Discontinuity. Along the parabola x=y2 (with y=0): f(y2,y)=y4+y4y2⋅y2=21. Along x=−y2: −21. But along every straight line through the origin f→0 (step 1 shows f(hp,hq)=O(h)). Points with f=21 lie arbitrarily close to the origin while f(0,0)=0.
Answer. All directional derivatives exist at the origin, yet f is not continuous there. Spagnolie’s moral (Piazza @105): what goes wrong is that the directional derivative q2/p is not itself a continuous function of the direction: it explodes as p→0, while at p=0 it is 0.
Your answerFor the function above, what is the directional derivative f′(0;y) in the direction y=(2,3)?
q2/p=32/2=29. (Not a unit vector, so this is the derivative with respect to y in Apostol's sense.)
Quick checkSuppose f′(a;y) exists for every direction y. Which conclusion is justified?
The example above has all the directional derivatives and is discontinuous. What guarantees continuity is a stronger property, differentiability, which is the topic of 5.3.
Select the last function above (the point starts at the origin). The direction is (p,q)=(cosθ,sinθ), so the slope is pq2=cosθsin2θ. 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 0.
Python labFinite-difference gradients, and the field that fools them
import numpy as npdef 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" theredef g(x, y): return 0.0 if x == 0 else x * y**2 / (x**2 + y**4)h = 1e-6print("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) exists for 0≤t≤1 then f(a+y)−f(a)=f′(z;y) for some z 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.
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, with k the thermal conductivity. Pure copper has k≈400W/(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 has length 0.280=400K/m and the flux is 400×400=160,000W/m2, flowing from hot to cold, that is against ∇T (a rod of cross-section 1cm2 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, 2hf(w+hek)−f(w−hek) for each weight wk, with h around 10−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). For “the rate of change per unit distance” use a unit vector. Forgetting to divide by ∥y∥ 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 x2+y4xy2 exist at the origin, and it is not continuous there. Do not conclude continuity (or f′(a;y)=∇f⋅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 a. For the example above, ∇f(0)=O yet f′(0;(1,1))=1.
Watch out· The gradient lives in the input space
For f:R2→R, ∇f is a vector in the (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 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)=g2g∇f−f∇g. The printed homework key for Apostol 8.14 #7(e) has it upside down. Compare with the one-variable rule (f/g)′=g2f′g−fg′.
Problems
checkMATH 375 HW 12 (Apostol 8.14 #1)
Compute the gradient of f(x,y,z)=x2y3z4.
Solution
∇f=(2xy3z4,3x2y2z4,4x2y3z3).
checkMATH 375 HW 11 (Apostol 8.9 #10)
For f(x,y)=x4+y4−4x2y2 find ∇f and verify D1(D2f)=D2(D1f).
Solution
D1f=4x3−8xy2 and D2f=4y3−8x2y. Then D1(D2f)=−16xy and D2(D1f)=−16xy.
check
Does ∂x∂x2+y2 exist at (0,0)? At (3,4), and what is the gradient there?
Solution
At (0,0): no, f(h,0)=∣h∣ has no derivative at 0. At (3,4): ∇f=5(3,4), a unit vector pointing away from the origin.
check
If f′(a;y)=7 for y=(1,2), what is f′(a;(−3,−6))?
Solution
By the scaling lemma with c=−3 ((−3,−6)=−3(1,2)): f′(a;(−3,−6))=−21.
useApostol 8.14 #6; MATH 375 HW 12 style
A scalar field f is differentiable at a point a in R2, with f′(a;y)=1 and f′(a;z)=2 where y=2i+3j and z=i+j. Find ∇f(a) and describe all (x,y) with f′(a;xi+yj)=6.
Solution
Write ∇f(a)=(p,q). Then 2p+3q=1 and p+q=2. From the second q=2−p, so 2p+6−3p=1, p=5, q=−3. So ∇f(a)=5i−3j, and f′(a;xi+yj)=5x−3y=6 is the line through (56,0) and (0,−2).
useApostol 8.14 #5
Find constants a,b,c such that the directional derivative of f(x,y,z)=axy2+byz+cz2x3 at (1,2,−1) has the maximum value 64 in a direction parallel to the z-axis.
Solution
∇f=(ay2+3cz2x2,2axy+bz,by+2czx3), which at (1,2,−1) is (4a+3c,4a−b,2b−2c). The maximum rate is ∥∇f∥, attained in the direction of ∇f. For that direction to be parallel to the z-axis, the first two entries vanish: 4a+3c=0 and 4a=b. The third entry is ±64. Taking +64 (direction +k): b=4a, c=−34a, 2b−2c=8a+38a=332a=64, so a=6, b=24, c=−8. Taking −64 gives (a,b,c)=(−6,−24,8) (maximum 64 in the direction −k). Apostol lists both.
usePractice Final 2013, problem 2(a)
f is differentiable with f(1,2)=1, fx(1,2)=2, fy(1,2)=−1. Estimate f(34,35) by walking along the straight line from (1,2).
Solution
y=(31,−31) and f(a+y)≈f(a)+∇f(a)⋅y=1+32+31=2. The reason: the slice g(t)=f(a+ty) has g(0)=f(a) and slope g′(0)=∇f(a)⋅y (differentiability), and g(1)≈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−2x over all of R2 using the vanishing theorem, and explain why the critical point is a minimum.
Solution
∇f=(2x−2,2y)=O at (1,0), with f(1,0)=−1. Completing the square, f=(x−1)2+y2−1≥−1, so (1,0) is the global minimum. There is no maximum: f→∞ as ∥(x,y)∥→∞.
proveMATH 375 HW 12 (Apostol 8.14 #7d, #7e)
If f,g are scalar fields with gradients, prove ∇(fg)=f∇g+g∇f and, where g=0, ∇(f/g)=g2g∇f−f∇g.
Solution
Plan. Work one coordinate at a time with the one-variable rules.
The k-th partial of fg 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). Collecting the components: ∇(fg)=g∇f+f∇g. Likewise the quotient rule of 1.4 gives Dk(f/g)=g2gDkf−fDkg, so ∇(f/g)=g2g∇f−f∇g. ■ (The printed key had the numerator reversed.)
proveApostol 8.14 #8
Let r=(x,y,z) and r=∥r∥. Show ∇r=r/r for r=O, and ∇(rn)=nrn−2r. Is the second formula valid for negative or zero n? Find a scalar field f with ∇f=r.
Solution
Plan. Differentiate rn=(x2+y2+z2)n/2 by the chain rule.
∂x∂r=rx, and similarly for y,z: ∇r=r/r (a unit vector). For rn=(x2+y2+z2)n/2: ∂x∂rn=2n(x2+y2+z2)n/2−1⋅2x=nrn−2x, so ∇(rn)=nrn−2r. The computation is valid for every real n at points where r=0 (for n≤0 the origin is excluded; for n=0 the gradient of the constant 1 is O, which the formula gives). For ∇f=r take n=2 and divide by 2: f=21r2=21(x2+y2+z2). ■
proveApostol 8.4 (Theorem) and 8.9 #20(a)
(a) Prove the mean value theorem for scalar fields: if f′(a+ty;y) exists for every t∈[0,1], then f(a+y)−f(a)=f′(a+θy;y) for some θ∈(0,1). (b) Deduce: if f′(x;y)=0 for every x in a ball B(a,r) and every vector y, then f is constant on the ball.
Solution
Plan. Apply the one-variable mean value theorem to the slice.
(a) Let g(t)=f(a+ty). By the derivative-of-a-slice theorem, g′(t)=f′(a+ty;y) exists on [0,1]. Then g is differentiable on [0,1] hence continuous, and the mean value theorem of 1.4 gives θ∈(0,1) with g(1)−g(0)=g′(θ), that is f(a+y)−f(a)=f′(a+θy;y). (b) Let x∈B(a,r) and y=x−a. The segment from a to x lies in the ball (each point a+ty has ∥ty∥≤∥y∥<r). By (a), f(x)−f(a)=f′(z;y)=0 for some z on the segment. So f(x)=f(a) for every x in the ball. ■
proveApostol 8.9 #22
(a) Prove there is no scalar field f such that f′(a;y)>0 for a fixed a and every nonzero y. (b) Give a scalar field with f′(x;y)>0 for a fixed nonzero y and every x.
Solution
(a) If f′(a;y) exists and is positive, the scaling lemma with c=−1 gives f′(a;−y)=−f′(a;y)<0, which contradicts positivity in the direction −y. (b) f(x)=x⋅y: by the worked example, f′(x;y)=y⋅y=∥y∥2>0 for all x. ■
proveMATH 375 Final Review, 8.8 to 8.13
Prove that for the field f(x,y)=x2+y2xy, f(0,0)=0, both partial derivatives exist at the origin and are 0, but the directional derivative in the direction (1,1) does not exist.
Solution
Along the axes, f=0, so D1f(0)=D2f(0)=0. For y=(1,1): hf(h,h)−f(0)=h21−0=2h1, which has no limit as h→0 (it tends to +∞ from the right and −∞ from the left). So f′(0;(1,1)) does not exist, although ∇f(0)=O does. ■
stretchApostol 8.23; Schwarz and Peano
Let f(x,y)=x2+y2xy(x2−y2) for (x,y)=(0,0) and f(0,0)=0. Compute D1f(0,y) and D2f(x,0), and deduce that D2(D1f)(0,0) and D1(D2f)(0,0) are different.
Solution
For y=0: hf(h,y)−f(0,y)=h2+y2y(h2−y2)→−y as h→0, so D1f(0,y)=−y (also true at y=0). For x=0: hf(x,h)−f(x,0)=x2+h2x(x2−h2)→x, so D2f(x,0)=x. Then D2(D1f)(0,0)=dyd(−y)0=−1 and D1(D2f)(0,0)=dxd(x)0=+1. The mixed partials differ, so the second partials of this f cannot be continuous at the origin: the equality theorem needs continuity.
stretchApostol 8.17 #6, previewed
Let f(x,y)=∣xy∣. Show that both partial derivatives are 0 at the origin, but that f′(0;(1,1)) does not exist. What does this say about a tangent plane to the surface z=f(x,y) at the origin?
Solution
On the axes f=0, so both partials are 0. Along (1,1): hf(h,h)−0=h∣h∣, which is 1 for h>0 and −1 for h<0: no limit. Cutting the surface by the plane x=y gives 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 f at a with respect to y is f′(a;y)=limh→0hf(a+hy)−f(a)=g′(0) for the slice g(t)=f(a+ty). For a unit vector y it is the directional derivative: the rate of change per unit distance. Scaling y by c scales it by c.
A partial derivativeDkf=f′(⋅;ek) is computed by holding the other variables fixed. The gradient∇f=(D1f,…,Dnf) lists them; it lives in the input space.
If f is differentiable (5.3), then f′(a;y)=∇f(a)⋅y for all y. Then ∇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 0 (Fermat in many variables). This is a necessary condition only (x2−y2 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 (x2+y4xy2). The map y↦f′(a;y) need not be linear, and f′(a;y)=∇f⋅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 against −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.
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.