Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 39 · The Gradient and Directional Derivatives

The Gradient

Define the gradient of a function of several variables, compute it, see why it points uphill, and draw gradient fields over contour plots with Python.

Partial derivatives give the slope along each axis. The gradient bundles them into one vector that tells you, at each point, which way the function increases fastest and how steeply. It is one of the most important objects in all of applied mathematics, from physics to machine learning.

Definition

For f(x, y) the gradient is

Text
∇f = ⟨f_x, f_y⟩ = f_x i + f_y j

and for f(x, y, z), ∇f = ⟨f_x, f_y, f_z⟩. The symbol ∇ is called "nabla" or "del". (Some texts write grad f.) The gradient is a vector field: it assigns a vector to every point.

Worked example 1: f(x, y) = x² y + sin y. Then ∇f = ⟨2xy, x² + cos y⟩. At (1, 0): ∇f = ⟨0, 2⟩.

Worked example 2: f(x, y, z) = x e^(yz). Then ∇f = ⟨e^(yz), x z e^(yz), x y e^(yz)⟩. At (1, 0, 3): ∇f = ⟨1, 3, 0⟩.

Python Compute gradients symbolically

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why the gradient matters

The gradient obeys two fundamental facts, proved in the next lessons:

  1. The rate of change of f at a point in direction of the unit vector u is ∇f · u.
  2. Therefore ∇f points in the direction of steepest increase, and its length |∇f| is the steepest rate. The opposite direction -∇f is the direction of steepest decrease.

Also, ∇f is perpendicular to the level curve (or level surface) through the point. If you stand on a contour line of a hill, the steepest uphill direction is at a right angle to the contour line.

The gradient points

Steepest uphill The direction of fastest increase
Its length The steepest slope
Level curves Perpendicular to them
1 / 2

Seeing the gradient field

Plot the contour lines of f(x, y) = x² + 2y² together with arrows for ∇f = ⟨2x, 4y⟩. The arrows should cross the ellipses at right angles and point outward (increasing f).

Python Gradient arrows over a contour plot

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Gradient rules

The gradient is linear and obeys product and chain rules:

Text
∇(f + g) = ∇f + ∇g
∇(c f)  = c ∇f
∇(f g)  = f ∇g + g ∇f
∇(h(f)) = h'(f) ∇f

Worked example: for f = x² + y² + z² (the squared distance from the origin) we get ∇f = ⟨2x, 2y, 2z⟩ = 2r, where r is the position vector. For ρ = √(x² + y² + z²) we get ∇ρ = r/ρ, the unit vector pointing away from the origin: the distance increases fastest radially at unit rate.

The gradient and zero

At a point where ∇f = 0 (all partials vanish) the tangent plane is horizontal. These critical points are candidates for maxima, minima and saddles; you will study them in the optimization chapter. Here is a quick search for such points numerically.

Python Find where the gradient vanishes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The gradient is a vector in the input space (the xy-plane for f(x, y)), not in the three-dimensional graph. It tells you which way to walk on the map to gain height fastest.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is ∇f for f(x, y) = x² + 3xy?

    f_x = 2x + 3y and f_y = 3x.
  2. 2. The gradient of f at a point points in the direction of

    ∇f points toward the greatest rate of increase; -∇f toward the greatest decrease.
  3. 3. The gradient at a point is ____ to the level curve through that point.

    Along a level curve f does not change, so the gradient is orthogonal to it.
  4. 4. If ∇f = 0 at a point, the tangent plane there is

    Both slopes are zero, so the plane z = f(a, b) is horizontal.

Summary

  • ∇f = ⟨f_x, f_y, f_z⟩ collects the partial derivatives into a vector field.
  • It points in the direction of steepest ascent, with length equal to the steepest rate, and is perpendicular to level sets.
  • Where ∇f = 0, the tangent plane is horizontal: a critical point.

Next: Directional Derivatives