Flow Lab

Syllabus / Module 2: Multivariable & Vector Calculus

Lesson 2.1

Functions of Several Variables & Partial Derivatives

Surfaces, contour plots and heatmaps; partial derivatives as slopes along coordinate directions; higher and mixed partials.

Written byClaude Opus

🌊
Why this matters for simulation: Temperature, pressure and density are scalar fields; a PDE relates their partial derivatives.

By the end of this lesson you can…

  • Picture a scalar field f(x, y) three ways (surface, contour map and heatmap) and move between them.
  • Explain a partial derivative as the slope of a slice, and compute first partials by hand by freezing the other variables.
  • Compute second-order and mixed partial derivatives, and use Clairaut's theorem to check your work.
  • Estimate partial derivatives from grid samples with forward and central differences, and say which is more accurate.
  • Read PDE notation such as u_t, u_x and u_xx, and verify that a given function solves the heat or wave equation.
On this page
·
  1. Scalar fields: one number at every point
  2. Three ways to see f(x,y)f(x, y)f(x,y)
  3. Partial derivatives: slopes of slices
  4. Notation
  5. Computing partials: treat the others as constants
  6. Higher-order and mixed partials
  7. Partial derivatives on a grid
  8. Exercise: derivatives of a sampled field
  9. PDE notation: equations about partial derivatives
  10. Where this goes next
  11. Key Takeaways

Open any fluid simulator and look at what it stores: a pressure value in every cell, a temperature in every cell, a density in every cell. Each of these is a number that depends on where you are (and usually when). In Calc I and II every function had one input, y=f(x)y = f(x). A temperature field T(x,y,z,t)T(x, y, z, t) has four. This lesson is your first step into multivariable calculus, the language in which every law of fluid motion is written. The good news is that the first big idea, the partial derivative, is an ordinary derivative in disguise: freeze all inputs but one and take the slope you already know how to take. In 1.1 Vectors, Linear Combinations & Span we put a vector at every grid cell. Now we put a number at every point and ask how fast it changes.

▶ Video Three views of a scalar field, and partial derivatives as slopes of slicescaptions only
Transcript
  1. Welcome to Module Two. Until now, every function you have differentiated had one input. Out in the physical world, almost nothing does.
  2. Think of the temperature across a metal plate, or the pressure around a wing. At every point x comma y there is one number. A function like that, f of x and y, is called a scalar field.
  3. The first way to see it is a heatmap. Color each point by its value: bright means high, dark means low. This is exactly how a fluid simulator shows you a pressure field.
  4. The second way is a contour map. Connect all the points that share the same value, and you get level curves, just like the contour lines on a hiking map.
  5. Where the contours crowd together, the function changes quickly. Where they spread out, the landscape is nearly flat.
  6. The third view uses a third dimension. Make the value a height, z equals f of x and y, and the field becomes a surface: a landscape of hills and valleys.
  7. Every contour line is a horizontal slice of this landscape at one fixed height. Same function, three pictures, and you should get used to switching between them.
  8. Now the big question of calculus: how fast does f change? With two inputs, the answer depends on which way you walk.
  9. So we do the simplest thing possible. Freeze y at one value, and walk only in the x direction. That means slicing the surface with the vertical plane y equals a constant.
  10. The slice cuts out an ordinary curve: f as a function of x alone. And an ordinary curve has an ordinary slope.
  11. That slope is the partial derivative of f with respect to x. Walk along the slice and watch the tangent line tilt. Uphill it is positive, at the top it is zero, and downhill it is negative.
  12. Written as a limit, it is the difference quotient you already know, f at x plus h, minus f at x, over h, with y held fixed the whole time.
  13. Now freeze x instead, and walk in the y direction. A different slice gives a different curve, and a different slope: the partial derivative of f with respect to y.
  14. Watch the y slope change sign as we cross the ridge. Two slices, two slopes, one surface.
  15. At one point, the two partial derivatives can be completely different. Here, at x equals zero and y equals zero point six, the surface climbs steeply in the x direction, but is almost level in the y direction.
  16. Each partial derivative only sees one direction. Combining them into a full picture of how the surface tilts is the job of the next few lessons.
  17. So a partial derivative is just an ordinary derivative along a slice. Freeze every input but one, and take the slope.
  18. Next, we will compute partial derivatives by hand, take them twice, and teach a computer to estimate them on a grid.

Scalar fields: one number at every point

A function of two variables f(x,y)f(x, y) takes a point of the plane and returns one real number. A function of three variables f(x,y,z)f(x, y, z) does the same for a point in space. Physicists call such functions scalar fields (“scalar” because the output is a single number, as opposed to the vector fields of 2.6 Vector Fields, Flow Lines & Streamlines). Examples:

  • the temperature T(x,y)T(x, y) across a heated metal plate;
  • the pressure p(x,y,z)p(x, y, z) around an aircraft wing;
  • the height h(x,y)h(x, y) of terrain above sea level;
  • the density ρ(x,y,z,t)\rho(x, y, z, t) of smoke in a room, which also changes with time.

The set of allowed inputs is the domain. For f(x,y)=1−x2−y2f(x, y) = \sqrt{1 - x^2 - y^2} it’s the disk x2+y2≤1x^2 + y^2 \le 1. For most fields in this course, it’s simply the region your simulation covers.

Three ways to see f(x,y)f(x, y)

You can’t draw a function of two variables as a curve, so we use three pictures. You need all three, and you should be able to switch between them at will.

1. The graph (a surface). Plot the value as a height: the points (x,y,z)(x, y, z) with z=f(x,y)z = f(x, y) form a surface over the xyxy-plane. Hills are where ff is large and valleys where it is small. This is the most intuitive picture, but it needs a 3D view, and it is useless for functions of three variables (you’d need four dimensions).

2. The contour map (level curves). For a constant cc, the level curve f(x,y)=cf(x, y) = c is the set of points where the function has the value cc. Draw level curves for evenly spaced values cc and you get a hiking map. Each level curve is the “shoreline” you’d see if you flooded the surface up to height cc. Where the curves crowd together, the surface is steep; where they spread out, it is flat. For f(x,y,z)f(x, y, z) the same idea gives level surfaces (isosurfaces). Those are what volume renderers draw when they show you a smoke plume.

3. The heatmap. Color each point by its value with a colormap (dark is low, bright is high). It’s a flattened graph. It is how simulators display a 2D field, since it is literally an image with one pixel per grid cell.

Worked example: a paraboloid. Take f(x,y)=4−x2−2y2f(x, y) = 4 - x^2 - 2y^2.

  • Graph: an upside-down bowl with its top at (0,0,4)(0, 0, 4).
  • Level curves: 4−x2−2y2=c4 - x^2 - 2y^2 = c means x2+2y2=4−cx^2 + 2y^2 = 4 - c. For c<4c \lt 4 this is an ellipse with semi-axes 4−c\sqrt{4-c} along xx and (4−c)/2\sqrt{(4-c)/2} along yy. For c=4c = 4 it is a single point, and for c>4c \gt 4 the level set is empty. The ellipses are squashed in the yy direction, so the contours are closer together along yy, which tells you the bowl is steeper in yy than in xx. We’ll confirm that with derivatives in a moment.
  • Heatmap: a bright elliptical spot in the middle fading to dark at the edges.
✅ Concept check

On a contour map with evenly spaced levels, the contours near a point P are packed tightly in the east–west ($x$) direction but far apart in the north–south ($y$) direction. What does that say about $f$ near P?

Partial derivatives: slopes of slices

How fast does f(x,y)f(x, y) change? With one input, there was only one way to move. With two, you can walk in any direction, and the rate of change depends on the direction. We start with the two simplest directions: along the xx-axis and along the yy-axis.

Freeze yy. Fix y=by = b. The vertical plane y=by = b slices the surface along a curve, namely the graph of the one-variable function

g(x)=f(x,b).g(x) = f(x, b).

This is an ordinary function of one variable, and its ordinary derivative at x=ax = a is the slope of the slice. That slope is the partial derivative of ff with respect to xx at (a,b)(a, b):

∂f∂x(a,b)=g′(a)=lim⁡h→0f(a+h, b)−f(a, b)h.\frac{\partial f}{\partial x}(a, b) = g'(a) = \lim_{h \to 0} \frac{f(a + h,\, b) - f(a,\, b)}{h}.

Freeze xx. Likewise, fixing x=ax = a slices the surface with the plane x=ax = a, and the slope of that slice is

∂f∂y(a,b)=lim⁡h→0f(a, b+h)−f(a, b)h.\frac{\partial f}{\partial y}(a, b) = \lim_{h \to 0} \frac{f(a,\, b + h) - f(a,\, b)}{h}.

The curly ∂\partial (“partial”, or “del”) warns you that ff has other inputs that are being held fixed. Geometrically, fx(a,b)f_x(a, b) is the slope of the surface if you stand at (a,b)(a, b) and face the +x+x direction; fy(a,b)f_y(a, b) is the slope facing +y+y.

Notation

You will meet all of these, and they mean the same thing:

∂f∂x  =  fx  =  ∂xf  =  Dxf,∂f∂y  =  fy  =  ∂yf.\frac{\partial f}{\partial x} \;=\; f_x \;=\; \partial_x f \;=\; D_x f, \qquad \frac{\partial f}{\partial y} \;=\; f_y \;=\; \partial_y f .

For a field u(x,t)u(x, t) that depends on position and time, ut=∂u/∂tu_t = \partial u / \partial t is the rate of change at a fixed location, and ux=∂u/∂xu_x = \partial u / \partial x is the rate of change in space at a fixed instant. Subscripts are the dialect of PDEs, and you’ll be reading them constantly from now on.

Computing partials: treat the others as constants

No new rules are needed. To compute fxf_x, pretend yy is a number like 7 and differentiate with respect to xx using everything you know from Calc I: power rule, product rule, chain rule.

Worked example. Let f(x,y)=x2y3+sin⁡x−4yf(x, y) = x^2 y^3 + \sin x - 4y.

  • For fxf_x, yy is a constant. Then x2y3x^2 y^3 is (y3y^3) times x2x^2, so its derivative is 2x y32x\,y^3. The term sin⁡x\sin x gives cos⁡x\cos x, and −4y-4y is a constant, so it gives 00:
fx=2xy3+cos⁡x.f_x = 2xy^3 + \cos x .
  • For fyf_y, xx is a constant. Then x2y3x^2 y^3 is (x2x^2) times y3y^3, giving 3x2y23x^2 y^2. The term sin⁡x\sin x is a constant, so it gives 00, and −4y-4y gives −4-4:
fy=3x2y2−4.f_y = 3x^2 y^2 - 4 .

At the point (1,2)(1, 2): fx(1,2)=2⋅1⋅8+cos⁡1≈16.540f_x(1, 2) = 2\cdot 1 \cdot 8 + \cos 1 \approx 16.540 and fy(1,2)=3⋅1⋅4−4=8f_y(1, 2) = 3 \cdot 1 \cdot 4 - 4 = 8.

Worked example: the paraboloid again. For f(x,y)=4−x2−2y2f(x, y) = 4 - x^2 - 2y^2 we get fx=−2xf_x = -2x and fy=−4yf_y = -4y. At (1,1)(1, 1), where f=1f = 1:

  • The slice y=1y = 1 is the parabola z=2−x2z = 2 - x^2. Its slope at x=1x = 1 is −2-2, which matches fx(1,1)=−2f_x(1,1) = -2.
  • The slice x=1x = 1 is the parabola z=3−2y2z = 3 - 2y^2. Its slope at y=1y = 1 is −4-4, which matches fy(1,1)=−4f_y(1,1) = -4.

Walking in +y+y you descend twice as fast as walking in +x+x, just as the squashed ellipses predicted.

Three variables work the same way. For a temperature blob T(x,y,z)=20+80 e−(x2+y2+z2)T(x, y, z) = 20 + 80\,e^{-(x^2 + y^2 + z^2)}, the chain rule gives Tx=−160 x e−(x2+y2+z2)T_x = -160\,x\,e^{-(x^2+y^2+z^2)}, and by symmetry TyT_y and TzT_z have yy and zz in place of xx. At (1,0,0)(1, 0, 0), Tx=−160/e≈−58.9T_x = -160/e \approx -58.9 degrees per unit length: moving outward along xx, it gets colder fast.

✅ Concept check

For $f(x, y) = x^2 y^3$, compute $f_y(2, 1)$.

✅ Concept check

Geometrically, $f_x(a, b)$ is…

Now explore it yourself. The probe shows the heatmap with contours on the left. The amber line is the slice y=y0y = y_0, and the cyan line is the slice x=x0x = x_0. On the right are the two slice graphs, each with its tangent line in red.

◆ Interactive Probe a scalar field: slices, slopes and finite differences

Loading simulation…

Try these:

  1. Drag the probe to the top of the tall hill. Both tangent lines go flat: fx=fy=0f_x = f_y = 0 at a peak.
  2. Put the probe on the left flank of the tall hill, around (0,0.6)(0, 0.6). The amber slice climbs steeply and the cyan slice is almost level: a large fxf_x and a tiny fyf_y at the same point.
  3. Switch to the saddle. At the origin both partials are zero, yet it is neither a peak nor a valley: the xx-slice is a smile and the yy-slice is a frown. (Lesson 2.3 Gradient & Directional Derivatives classifies such points.)
  4. Turn on Finite-difference estimate and slide hh from large to small. Compare forward with central. We’ll explain what you see in the section on grids below.
✏️ Pen & paperFirst partials●○○

Compute the indicated partial derivatives.

  1. f(x,y)=x3y2−4xy+e2yf(x, y) = x^3 y^2 - 4xy + e^{2y}: find fxf_x and fyf_y.
  2. g(x,y,z)=xyz2+sin⁡(yz)g(x, y, z) = x y z^2 + \sin(yz): find gxg_x, gyg_y and gzg_z.
  3. h(x,y)=xx2+y2h(x, y) = \dfrac{x}{x^2 + y^2}: find hxh_x and hyh_y (for (x,y)≠(0,0)(x, y) \neq (0,0)).
Show worked solution
  1. Freezing yy: fx=3x2y2−4yf_x = 3x^2 y^2 - 4y (the term e2ye^{2y} is a constant). Freezing xx: fy=2x3y−4x+2e2yf_y = 2x^3 y - 4x + 2e^{2y} (chain rule on e2ye^{2y}).
  2. gx=yz2g_x = y z^2 (both sin⁡(yz)\sin(yz) and the factor yz2yz^2 are constants in xx). For gyg_y: xz2+zcos⁡(yz)x z^2 + z\cos(yz), by the chain rule with inner derivative ∂(yz)/∂y=z\partial(yz)/\partial y = z. For gzg_z: 2xyz+ycos⁡(yz)2xyz + y\cos(yz).
  3. Quotient rule with yy frozen: hx=(x2+y2)⋅1−x⋅2x(x2+y2)2=y2−x2(x2+y2)2h_x = \dfrac{(x^2+y^2)\cdot 1 - x \cdot 2x}{(x^2+y^2)^2} = \dfrac{y^2 - x^2}{(x^2+y^2)^2}. With xx frozen the numerator xx is a constant: hy=x⋅−2y(x2+y2)2=−2xy(x2+y2)2h_y = x \cdot \dfrac{-2y}{(x^2+y^2)^2} = \dfrac{-2xy}{(x^2+y^2)^2}.

Higher-order and mixed partials

The partial derivatives fxf_x and fyf_y are themselves functions of (x,y)(x, y), so we can differentiate them again. There are four second-order partials:

fxx=∂2f∂x2,fyy=∂2f∂y2,fxy=(fx)y=∂2f∂y ∂x,fyx=(fy)x=∂2f∂x ∂y.f_{xx} = \frac{\partial^2 f}{\partial x^2}, \qquad f_{yy} = \frac{\partial^2 f}{\partial y^2}, \qquad f_{xy} = (f_x)_y = \frac{\partial^2 f}{\partial y\,\partial x}, \qquad f_{yx} = (f_y)_x = \frac{\partial^2 f}{\partial x\,\partial y}.

Mind the ordering conventions. In subscript notation, you differentiate left to right: fxyf_{xy} means “first xx, then yy”. In fraction notation you read right to left: ∂2f/∂y ∂x\partial^2 f / \partial y\,\partial x means “first xx, then yy”. The good news is that for the functions in this course the order won’t matter, as we’ll see in a moment.

fxxf_{xx} is the concavity of the xx-slice: positive where the slice curves up like a smile, negative where it curves down. The mixed partial fxyf_{xy} measures twist: how the xx-slope changes as you step in the yy direction.

Worked example. Continue with f(x,y)=x2y3+sin⁡x−4yf(x, y) = x^2 y^3 + \sin x - 4y, where fx=2xy3+cos⁡xf_x = 2xy^3 + \cos x and fy=3x2y2−4f_y = 3x^2 y^2 - 4:

fxx=2y3−sin⁡x,fyy=6x2y,fxy=∂∂y(2xy3+cos⁡x)=6xy2,fyx=∂∂x(3x2y2−4)=6xy2.f_{xx} = 2y^3 - \sin x, \qquad f_{yy} = 6x^2 y, \qquad f_{xy} = \frac{\partial}{\partial y}\left(2xy^3 + \cos x\right) = 6xy^2, \qquad f_{yx} = \frac{\partial}{\partial x}\left(3x^2y^2 - 4\right) = 6xy^2 .

The two mixed partials came out equal, even though we computed them along completely different routes. That is not a coincidence.

Worked example. Let f(x,y)=x exyf(x, y) = x\,e^{xy}. With the product and chain rules,

fx=exy+xy exy=(1+xy) exy,fy=x2exy.f_x = e^{xy} + xy\,e^{xy} = (1 + xy)\,e^{xy}, \qquad f_y = x^2 e^{xy}.

Then

fxy=∂∂y[(1+xy)exy]=x exy+(1+xy) x exy=(2x+x2y) exy,f_{xy} = \frac{\partial}{\partial y}\big[(1+xy)e^{xy}\big] = x\,e^{xy} + (1+xy)\,x\,e^{xy} = (2x + x^2 y)\,e^{xy}, fyx=∂∂x[x2exy]=2x exy+x2y exy=(2x+x2y) exy.f_{yx} = \frac{\partial}{\partial x}\big[x^2 e^{xy}\big] = 2x\,e^{xy} + x^2 y\,e^{xy} = (2x + x^2 y)\,e^{xy}.

Equal again.

Why should it be true? Look at the four corners of a tiny square with side hh and form the “double difference”

Δ=f(a+h,b+h)−f(a+h,b)−f(a,b+h)+f(a,b).\Delta = f(a+h, b+h) - f(a+h, b) - f(a, b+h) + f(a, b).

Group it as [f(a+h,b+h)−f(a,b+h)]−[f(a+h,b)−f(a,b)][f(a+h, b+h) - f(a, b+h)] - [f(a+h, b) - f(a, b)]. That is the change in the xx-differences as you step up in yy, so Δ≈h2fxy\Delta \approx h^2 f_{xy}. Group it the other way, [f(a+h,b+h)−f(a+h,b)]−[f(a,b+h)−f(a,b)][f(a+h, b+h) - f(a+h, b)] - [f(a, b+h) - f(a, b)]. That is the change in the yy-differences as you step right in xx, so Δ≈h2fyx\Delta \approx h^2 f_{yx}. It’s the same number Δ\Delta either way. Dividing by h2h^2 and letting h→0h \to 0 gives fxy=fyxf_{xy} = f_{yx}. Continuity is exactly what makes both approximations valid in the limit. The formal proof applies the mean value theorem twice.

✅ Concept check

Let $f(x, y) = e^x \sin y + x^3 y$. What is $f_{xy}$?

✏️ Pen & paperSecond partials and Clairaut●○○

For f(x,y)=x3y2−4xy+e2yf(x, y) = x^3 y^2 - 4xy + e^{2y} (from the first problem), compute fxxf_{xx}, fyyf_{yy}, fxyf_{xy} and fyxf_{yx}, and confirm that Clairaut’s theorem holds.

Show worked solution

From fx=3x2y2−4yf_x = 3x^2 y^2 - 4y and fy=2x3y−4x+2e2yf_y = 2x^3 y - 4x + 2e^{2y}:

  • fxx=6xy2f_{xx} = 6xy^2
  • fyy=2x3+4e2yf_{yy} = 2x^3 + 4e^{2y}
  • fxy=(fx)y=6x2y−4f_{xy} = (f_x)_y = 6x^2 y - 4
  • fyx=(fy)x=6x2y−4f_{yx} = (f_y)_x = 6x^2 y - 4

The mixed partials agree everywhere, as they must: ff is built from polynomials and exponentials, so all of its partials are continuous.

Partial derivatives on a grid

A simulator never has the formula for ff. It has samples on a grid: fi,j≈f(xi,yj)f_{i,j} \approx f(x_i, y_j) with xi=x0+i hx_i = x_0 + i\,h and yj=y0+j hy_j = y_0 + j\,h. To get derivatives, it replaces the limit h→0h \to 0 in the definition with a small but finite hh, the grid spacing. This is the finite-difference method, and you’ll build whole PDE solvers out of it in 10.1 Finite Differences: Discretizing Derivatives.

For ∂f/∂x\partial f/\partial x at cell (i,j)(i, j), only the ii index moves:

forward:∂f∂x∣i,j≈fi+1,j−fi,jh,central:∂f∂x∣i,j≈fi+1,j−fi−1,j2h.\text{forward:}\quad \frac{\partial f}{\partial x}\Big|_{i,j} \approx \frac{f_{i+1,j} - f_{i,j}}{h}, \qquad \text{central:}\quad \frac{\partial f}{\partial x}\Big|_{i,j} \approx \frac{f_{i+1,j} - f_{i-1,j}}{2h}.

For ∂f/∂y\partial f/\partial y, only the jj index moves: (fi,j+1−fi,j−1)/(2h)(f_{i,j+1} - f_{i,j-1})/(2h). Freezing a variable on a grid means keeping its index fixed.

Which is better? Expand in a Taylor series along the xx-slice (all derivatives evaluated at (x,y)(x, y)):

f(x±h,y)=f±hfx+h22fxx±h36fxxx+⋯f(x \pm h, y) = f \pm h f_x + \tfrac{h^2}{2} f_{xx} \pm \tfrac{h^3}{6} f_{xxx} + \cdots

Using only the plus sign,

f(x+h,y)−f(x,y)h=fx+h2fxx+⋯ ,\frac{f(x+h,y) - f(x,y)}{h} = f_x + \tfrac{h}{2} f_{xx} + \cdots ,

so the forward difference has error proportional to hh (first order). Subtracting the minus expansion from the plus one cancels the even terms:

f(x+h,y)−f(x−h,y)2h=fx+h26fxxx+⋯ ,\frac{f(x+h,y) - f(x-h,y)}{2h} = f_x + \tfrac{h^2}{6} f_{xxx} + \cdots ,

so the central difference has error proportional to h2h^2 (second order). Halve hh, and the forward error halves while the central error drops by a factor of four. Adding the two expansions instead cancels the odd terms and gives the second-derivative stencil

fxx≈fi+1,j−2fi,j+fi−1,jh2,f_{xx} \approx \frac{f_{i+1,j} - 2f_{i,j} + f_{i-1,j}}{h^2},

also second order. Add the xx and yy versions and you have the five-point Laplacian fxx+fyyf_{xx} + f_{yy}, the most important stencil in fluid simulation (2.11 The Laplacian & Vector Identities). A mixed partial uses the four diagonal neighbors: it’s the double difference Δ\Delta from the Clairaut argument, centered:

fxy≈fi+1,j+1−fi+1,j−1−fi−1,j+1+fi−1,j−14h2.f_{xy} \approx \frac{f_{i+1,j+1} - f_{i+1,j-1} - f_{i-1,j+1} + f_{i-1,j-1}}{4h^2}.

In the probe sim, turn on the finite-difference estimate and watch the error readout as you shrink hh. With central differences, each tenfold reduction of hh cuts the error about a hundredfold, until hh gets so tiny (around 10−310^{-3} is fine, but far smaller is not) that floating-point round-off in f(x+h)−f(x−h)f(x+h) - f(x-h) starts to win.

Here’s the same experiment in code, as a log-log plot:

🐍 PythonForward vs central differences: error vs h
⌘/Ctrl + Enter
✅ Concept check

A central-difference estimate of $f_x$ has error $4 \times 10^{-4}$ with spacing $h = 0.1$. Roughly what error do you expect with $h = 0.05$?

Exercise: derivatives of a sampled field

In NumPy, a 2D field sampled with X, Y = np.meshgrid(x, y) is stored as F[j, i] = f(x[i], y[j]). Axis 0 is yy (rows) and axis 1 is xx (columns). Mixing them up is the single most common bug in grid codes, so keep this in mind. NumPy’s np.gradient(F, h, axis=...) computes central differences in the interior and one-sided differences at the edges. You’ll write your own and check it against both np.gradient and the exact partials, then verify Clairaut numerically.

🧪 Coding exerciseExercise: central differences on a grid, and Clairaut
⌘/Ctrl + Enter

Notice that the discrete fxyf_{xy} and fyxf_{yx} agree to round-off, not just approximately. Differencing along rows and differencing along columns are operations that commute, which is a discrete Clairaut theorem. You can check the symbolic version too:

🐍 PythonSymbolic partial derivatives with SymPy
⌘/Ctrl + Enter
✏️ Pen & paperPartials from a table of samples●●○

A temperature field is sampled on a grid with spacing h=0.5h = 0.5:

x=0.5x = 0.5x=1.0x = 1.0x=1.5x = 1.5
y=2.5y = 2.510.62512.515.625
y=2.0y = 2.010.512.014.5
y=1.5y = 1.510.37511.513.375
  1. Estimate TxT_x and TyT_y at (1,2)(1, 2) with central differences.
  2. Estimate Tx(1,2)T_x(1, 2) with a forward difference.
  3. Estimate Txx(1,2)T_{xx}(1, 2) and Txy(1,2)T_{xy}(1, 2).
  4. The data actually come from T=x2y+10T = x^2 y + 10. Compare with the exact values, and explain why the central estimates are exact here while the forward one is not.
Show worked solution
  1. Tx≈T(1.5,2)−T(0.5,2)2(0.5)=14.5−10.51=4T_x \approx \dfrac{T(1.5, 2) - T(0.5, 2)}{2(0.5)} = \dfrac{14.5 - 10.5}{1} = 4, and Ty≈T(1,2.5)−T(1,1.5)1=12.5−11.51=1T_y \approx \dfrac{T(1, 2.5) - T(1, 1.5)}{1} = \dfrac{12.5 - 11.5}{1} = 1.
  2. Tx≈T(1.5,2)−T(1,2)0.5=14.5−120.5=5T_x \approx \dfrac{T(1.5, 2) - T(1, 2)}{0.5} = \dfrac{14.5 - 12}{0.5} = 5.
  3. Txx≈14.5−2(12)+10.50.25=10.25=4T_{xx} \approx \dfrac{14.5 - 2(12) + 10.5}{0.25} = \dfrac{1}{0.25} = 4. For the mixed partial, use the four corners: Txy≈15.625−13.375−10.625+10.3754(0.25)=21=2T_{xy} \approx \dfrac{15.625 - 13.375 - 10.625 + 10.375}{4(0.25)} = \dfrac{2}{1} = 2.
  4. Exactly: Tx=2xy=4T_x = 2xy = 4, Ty=x2=1T_y = x^2 = 1, Txx=2y=4T_{xx} = 2y = 4, Txy=2x=2T_{xy} = 2x = 2. All the central estimates are exact. The central-difference error starts with h26Txxx\tfrac{h^2}{6}T_{xxx}, and Txxx=0T_{xxx} = 0 for a function that is quadratic in xx. (Similarly TT is linear in yy.) The forward error starts with h2Txx=0.52⋅4=1\tfrac{h}{2}T_{xx} = \tfrac{0.5}{2}\cdot 4 = 1, which is exactly the gap between 5 and 4.

PDE notation: equations about partial derivatives

An ordinary differential equation relates a function of one variable to its derivatives. A partial differential equation (PDE) relates a function of several variables to its partial derivatives. You can now read the three classics:

ut=α uxx(heat / diffusion),utt=c2uxx(wave),uxx+uyy=0(Laplace).u_t = \alpha\, u_{xx} \quad \text{(heat / diffusion)}, \qquad u_{tt} = c^2 u_{xx} \quad \text{(wave)}, \qquad u_{xx} + u_{yy} = 0 \quad \text{(Laplace)}.

Each says something you can now picture. Take the heat equation: u(x,t)u(x, t) is the temperature along a rod. At a fixed location, the temperature rises (ut>0u_t \gt 0) wherever the profile is concave up (uxx>0u_{xx} \gt 0, a local dip that gets filled in) and falls wherever it is concave down (a hot bump that melts away). The constant α\alpha is the diffusivity. The second video animates exactly this.

▶ Video Computing partials, Clairaut's theorem, finite differences on a grid, and PDE notationcaptions only
Transcript
  1. Let's compute partial derivatives, first with a pencil, and then the way a simulator does it, on a grid.
  2. Computing a partial derivative needs no new rules. Treat every other variable as a constant, and differentiate exactly as in Calc One.
  3. For the partial with respect to x, y is frozen. So y cubed is just a constant multiplier, and minus four y is a constant that disappears. We get two x y cubed, plus cosine x.
  4. For the partial with respect to y, x is frozen instead. Now x squared is the constant, sine x vanishes, and we get three x squared y squared, minus four.
  5. The notation varies. You will see the curly d fraction, or a subscript: f sub x. In physics, u sub t is the rate of change in time, and u sub x x is the second derivative in space.
  6. A partial derivative is itself a function of x and y, so we can differentiate it again. That gives f x x, f y y, and two mixed partials, f x y and f y x.
  7. Look: the two mixed partials agree. That is no accident. Clairaut's theorem says that if the second partials are continuous, the order of differentiation does not matter.
  8. Now to simulation. A computer never stores the formula. It stores samples on a grid, one number per cell, with spacing h between them.
  9. To estimate the x partial at this cell, look at its left and right neighbors. Their difference, divided by two h, is the central difference.
  10. For the y partial, use the neighbors above and below instead. It is the same idea, rotated by ninety degrees.
  11. Second derivatives work the same way: neighbor plus neighbor, minus twice the center, over h squared. Add the x and y versions, and you have the five point Laplacian, the workhorse of every fluid solver.
  12. How good is the estimate? On a single slice, the central difference is the slope of the amber line through two neighbors. The red line is the true tangent.
  13. Shrink h, and the amber line rotates onto the tangent. Halve h, and the error drops by about a factor of four. We say the central difference is second order accurate.
  14. A partial differential equation ties these derivatives together. The heat equation says u sub t equals alpha times u sub x x.
  15. Read it out loud: temperature rises wherever the profile curves upward, and falls wherever it curves downward. Bumps melt away.
  16. Every fluid simulation in this course is, at its core, an equation like this one, discretized on a grid with exactly the stencils you just saw.
  17. To recap: freeze the other inputs and differentiate. Mixed partials agree for smooth functions. And on a grid, neighbors give you derivatives.
  18. Next time, we combine the two partial derivatives into a tangent plane, the best linear approximation of a surface, and meet the multivariable chain rule.

Checking a solution is pure partial differentiation. Is u(x,t)=e−απ2tsin⁡(πx)u(x, t) = e^{-\alpha \pi^2 t}\sin(\pi x) a solution of the heat equation? With xx frozen, ut=−απ2e−απ2tsin⁡(πx)u_t = -\alpha\pi^2 e^{-\alpha\pi^2 t}\sin(\pi x). With tt frozen, ux=πe−απ2tcos⁡(πx)u_x = \pi e^{-\alpha\pi^2 t}\cos(\pi x) and uxx=−π2e−απ2tsin⁡(πx)u_{xx} = -\pi^2 e^{-\alpha\pi^2 t}\sin(\pi x). So αuxx=−απ2e−απ2tsin⁡(πx)=ut\alpha u_{xx} = -\alpha\pi^2 e^{-\alpha\pi^2 t}\sin(\pi x) = u_t. ✓ It’s a sine-shaped temperature profile that decays exponentially in time, and the wigglier the profile, the faster it decays. That’s the seed of Fourier’s method in 9.3 Solving the Heat Equation: Separation of Variables.

✅ Concept check

In the equation $u_t = 0.1\, u_{xx}$, what does $u_t$ mean?

✏️ Pen & paperVerifying PDE solutions●●○
  1. Show that u(x,t)=sin⁡(x−ct)u(x, t) = \sin(x - ct) satisfies the wave equation utt=c2uxxu_{tt} = c^2 u_{xx}. What does the solution look like as time passes?
  2. For which constant kk does u(x,t)=e−ktsin⁡(3x)u(x, t) = e^{-kt}\sin(3x) satisfy ut=2uxxu_t = 2u_{xx}?
  3. Show that f(x,y)=x2−y2f(x, y) = x^2 - y^2 and g(x,y)=excos⁡yg(x, y) = e^x \cos y both satisfy Laplace’s equation fxx+fyy=0f_{xx} + f_{yy} = 0.
Show worked solution
  1. ut=−ccos⁡(x−ct)u_t = -c\cos(x - ct) and utt=−c2sin⁡(x−ct)u_{tt} = -c^2\sin(x - ct). Also ux=cos⁡(x−ct)u_x = \cos(x - ct) and uxx=−sin⁡(x−ct)u_{xx} = -\sin(x - ct). So c2uxx=−c2sin⁡(x−ct)=uttc^2 u_{xx} = -c^2 \sin(x-ct) = u_{tt}. ✓ The profile at time tt is the initial sine wave shifted right by ctct: a wave traveling at speed cc without changing shape.
  2. ut=−ke−ktsin⁡3xu_t = -k e^{-kt}\sin 3x and uxx=−9e−ktsin⁡3xu_{xx} = -9e^{-kt}\sin 3x. The equation requires −k=2(−9)-k = 2(-9), so k=18k = 18. A profile with spatial frequency 3 decays at rate α⋅32\alpha \cdot 3^2.
  3. For ff: fxx=2f_{xx} = 2 and fyy=−2f_{yy} = -2, which sum to 00. ✓ For gg: gxx=excos⁡yg_{xx} = e^x\cos y and gyy=−excos⁡yg_{yy} = -e^x\cos y, which sum to 00. ✓ Notice that in both cases, where the xx-slice is concave up the yy-slice is concave down by the same amount. Harmonic functions are “saddle-like” everywhere and have no interior peaks.
✏️ Pen & paperWhen Clairaut fails●●●

Let

f(x,y)={xy(x2−y2)x2+y2,(x,y)≠(0,0)0,(x,y)=(0,0).f(x, y) = \begin{cases} \dfrac{xy(x^2 - y^2)}{x^2 + y^2}, & (x, y) \neq (0, 0) \\ 0, & (x, y) = (0, 0). \end{cases}
  1. Using the limit definition, show that fx(0,y)=−yf_x(0, y) = -y for all yy and fy(x,0)=xf_y(x, 0) = x for all xx.
  2. Conclude that fxy(0,0)=−1f_{xy}(0,0) = -1 while fyx(0,0)=1f_{yx}(0,0) = 1.
  3. Which hypothesis of Clairaut’s theorem fails?
Show worked solution
  1. For y≠0y \neq 0: fx(0,y)=lim⁡h→0f(h,y)−f(0,y)hf_x(0, y) = \lim_{h\to 0} \dfrac{f(h, y) - f(0, y)}{h}. Since f(0,y)=0f(0, y) = 0 and f(h,y)=hy(h2−y2)h2+y2f(h, y) = \dfrac{h y (h^2 - y^2)}{h^2 + y^2}, the quotient is y(h2−y2)h2+y2→y(−y2)y2=−y\dfrac{y(h^2 - y^2)}{h^2 + y^2} \to \dfrac{y(-y^2)}{y^2} = -y. For y=0y = 0: f(h,0)=0f(h, 0) = 0, so fx(0,0)=0=−0f_x(0,0) = 0 = -0. ✓ Similarly, fy(x,0)=lim⁡k→0f(x,k)k=lim⁡x(x2−k2)x2+k2=xf_y(x, 0) = \lim_{k\to 0}\dfrac{f(x, k)}{k} = \lim \dfrac{x(x^2 - k^2)}{x^2 + k^2} = x (and 00 at x=0x = 0).
  2. fxy(0,0)=∂∂yfx(0,y)∣y=0=ddy(−y)=−1f_{xy}(0,0) = \dfrac{\partial}{\partial y} f_x(0, y)\Big|_{y=0} = \dfrac{d}{dy}(-y) = -1, while fyx(0,0)=∂∂xfy(x,0)∣x=0=ddx(x)=1f_{yx}(0,0) = \dfrac{\partial}{\partial x} f_y(x, 0)\Big|_{x=0} = \dfrac{d}{dx}(x) = 1.
  3. The mixed partials exist everywhere but are not continuous at the origin. Away from the origin, a computation shows fxy=fyx=x6+9x4y2−9x2y4−y6(x2+y2)3f_{xy} = f_{yx} = \dfrac{x^6 + 9x^4y^2 - 9x^2y^4 - y^6}{(x^2+y^2)^3}. Along the xx-axis this equals 11 and along the yy-axis it equals −1-1, so it has no limit at (0,0)(0,0).

Where this goes next

Partial derivatives only look along the coordinate axes. In 2.2 Linearization, the Chain Rule & the Jacobian we combine fxf_x and fyf_y into a tangent plane, the best linear approximation of a surface, and develop the multivariable chain rule. That gives the material derivative ∂/∂t+u⃗⋅∇\partial/\partial t + \vec u\cdot\nabla that every fluid equation uses. In 2.3 Gradient & Directional Derivatives the pair (fx,fy)(f_x, f_y) becomes the gradient vector, which points uphill. The PDEs you just read are derived properly in 9.2 The Heat (Diffusion) Equation, and the Navier–Stokes equations (11.4 Viscosity & the Navier–Stokes Equations) are just a larger system of the same kind of statement.

Key Takeaways

  • A scalar field f(x,y)f(x, y) assigns one number to each point. See it as a surface z=f(x,y)z = f(x,y), a contour map of level curves f=cf = c, or a heatmap. Crowded contours mean steep change.
  • The partial derivative fx(a,b)f_x(a, b) is the slope of the slice y=by = b at x=ax = a: freeze every other input and differentiate as usual. fyf_y freezes xx instead.
  • Notation: ∂f/∂x=fx=∂xf\partial f/\partial x = f_x = \partial_x f. In PDEs, utu_t is the rate of change in time at a fixed place, and uxxu_{xx} is the spatial concavity.
  • Second partials fxx,fyy,fxy,fyxf_{xx}, f_{yy}, f_{xy}, f_{yx} measure the concavity of slices and the twist between them. Clairaut’s theorem: fxy=fyxf_{xy} = f_{yx} whenever these are continuous, which is a free check on hand computations.
  • On a grid, freezing a variable means fixing its index. The central difference (fi+1,j−fi−1,j)/2h(f_{i+1,j} - f_{i-1,j})/2h is second-order accurate, while the forward difference is only first order. The stencil (fi+1−2fi+fi−1)/h2(f_{i+1} - 2f_i + f_{i-1})/h^2 gives fxxf_{xx}.
  • In NumPy’s meshgrid layout, axis 0 is yy and axis 1 is xx, and every fluid-solver operator (pressure gradient, divergence, Laplacian) is built from these stencils.
  • A PDE is an equation relating partial derivatives. Checking a proposed solution is just computing partials and substituting.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: