Mathematical Foundations

III.7 · Calculus

How does a surface change when only one input moves?

Move a point across a contour map. Two slices through it — one along x, one along y — are ordinary curves, and their slopes are the two partial derivatives.

Hold one input fixed and differentiate the slice

Contour map of f(x, y) = x² − y²

Level curves of f(x, y) = x² − y² at heights -4, -3.5, -3, -2.5, -2, -1.5…. The point (1, 0.5) has height 0.75. The solid blue line holds y = 0.5; the dashed green line holds x = 1.

Slice y = 0.5: t ↦ f(t, 0.5)

Holding y fixed, the slice is the one-variable polynomial t² − 0.25. Its tangent at t = 1 has slope 2 = ∂f/∂x.

Slice x = 1: t ↦ f(1, t)

Holding x fixed, the slice is the one-variable polynomial −t² + 1. Its tangent at t = 0.5 has slope −1 = ∂f/∂y.

Input x
1
Input y
0.5
Height
0.75
Rate along x, y held fixed
2
Rate along y, x held fixed
−1
Secant along x (h = 0.25)
2.25
Secant along y (h = 0.25)
−1.25

Exact values use =; binary64 approximations use ≈. Undefined values state their reason.

Moving along x with y fixed changes f at rate 2; moving along y with x fixed changes it at rate −1. Two different questions, two different slices of the same surface.

f(x,y)=x2−y2,f(1,0.5)=0.75f(x,y)=x^{2}-y^{2},\qquad f(1,0.5)=0.75
fx(1,0.5)=2t∣t=1=2f_x(1,0.5)=2t\Big|_{t=1}=2
fy(1,0.5)=−2t∣t=0.5=−1f_y(1,0.5)=-2t\Big|_{t=0.5}=-1
∂f∂x=2x,∂f∂y=−2y\frac{\partial f}{\partial x}=2x,\qquad \frac{\partial f}{\partial y}=-2y

Hold y fixed

Set y = 0.5. What remains is a function of x alone.

f(t,0.5)=t2−0.25f(t,0.5)=t^{2}-0.25

from −2.5 to 2.5, up to 2 decimal places

from −2.5 to 2.5, up to 2 decimal places

Drag the point on the contour map (arrow keys move it in x and y). The blue line fixes y, the dashed green line fixes x; the two lower planes are those slices with their tangent lines.

On the saddle x2−y2x^2-y^2 at (1,12)(1,\tfrac12), the slice along xx rises with slope 22 while the slice along yy falls with slope −1-1. Drag the point to the origin: both slices are flat, although the surface is a saddle, not a bowl.

Intuition: one knob at a time

A function f(x,y)f(x,y) assigns a height to every point of the plane. Its contour map draws the curves f=cf=c at several heights, like an elevation map. Standing at a point, you can ask how the height changes if you walk east (change xx only) or north (change yy only). Each question fixes one input and leaves an ordinary function of one variable, whose derivative you already know.

Definition

The partial derivatives of ff at (a,b)(a,b) are

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

Equivalently, ∂f/∂x\partial f/\partial x is the derivative of the slice t↦f(t,b)t\mapsto f(t,b) at t=at=a. Other common notations are fxf_x, ∂xf\partial_x f and D1fD_1f.

Mechanics: what the explorer computes

The surfaces are polynomials with rational coefficients, so the height, both slices and both partial derivatives are exact. The contour lines are a drawing aid computed numerically on a grid; no value is read from them. Each slice plane shows the one-variable polynomial obtained by fixing the other input, and the inspector adds an exact secant along each axis with h=0.25h=0.25 as evidence.

Derivation: differentiate the slice

For f(x,y)=x2−y2f(x,y)=x^2-y^2 at (1,12)(1,\tfrac12), fix y=12y=\tfrac12:

f(t,12)=t2−14,f(t,\tfrac12)=t^2-\tfrac14 ,

whose derivative 2t2t is 22 at t=1t=1. Fix x=1x=1 instead:

f(1,t)=1−t2,f(1,t)=1-t^2 ,

whose derivative −2t-2t is −1-1 at t=12t=\tfrac12. In practice we write this as a rule: differentiate with respect to one variable, treating the others as constants. So ∂f/∂x=2x\partial f/\partial x=2x and ∂f/∂y=−2y\partial f/\partial y=-2y.

Worked example

Take f(x,y)=xyf(x,y)=xy at (1,12)(1,\tfrac12). The xx-slice is f(t,12)=12tf(t,\tfrac12)=\tfrac12t, a line of slope 12\tfrac12; the yy-slice is f(1,t)=tf(1,t)=t, slope 11. Hence ∂f/∂x=y=12\partial f/\partial x=y=\tfrac12 and ∂f/∂y=x=1\partial f/\partial y=x=1. Because ff is linear in each variable separately, every secant along an axis equals the partial derivative exactly.

Replay the worked derivation

Hold y fixed

Set y = 0.5. What remains is a function of x alone.

f(t,0.5)=t2−0.25f(t,0.5)=t^{2}-0.25
Replay: hold y fixed and differentiate, then hold x fixed and differentiate.

What partial derivatives do not say

Two partial derivatives describe change along two directions only. A function can have both partial derivatives at a point and still behave badly along a diagonal; and zero partial derivatives do not identify a minimum (the saddle at the origin). III.8 combines the partials into the gradient, which describes change in every direction for differentiable functions, and III.11 uses second derivatives to classify flat points.

Executable reference code

The pedagogical reference computes partial derivatives of an exact polynomial by differentiating slices with Fraction and checks them against exact secant limits. The NumPy version samples a surface on a grid and compares numpy.gradient with the analytic partials.

code/python/calculus_partials.py
"""Partial derivatives as derivatives of slices, with exact Fractions (III.7)."""
from fractions import Fraction as F

# f(x, y) = sum c * x**i * y**j, stored as {(i, j): c}
f = {(2, 0): F(1), (0, 2): F(-1)}            # x**2 - y**2


def value(p, x, y):
    return sum(c * x**i * y**j for (i, j), c in p.items())


def d_dx(p):
    return {(i - 1, j): c * i for (i, j), c in p.items() if i > 0}


def d_dy(p):
    return {(i, j - 1): c * j for (i, j), c in p.items() if j > 0}


x, y = F(1), F(1, 2)
assert (value(d_dx(f), x, y), value(d_dy(f), x, y)) == (2, -1)

# The same numbers as limits of exact secants along each axis: m(h) is a polynomial in h, so 2 nonzero steps fix it.
def axis_slope(axis, h):
    step = (h, 0) if axis == 'x' else (0, h)
    return (value(f, x + step[0], y + step[1]) - value(f, x, y)) / h


for axis, expected in (('x', 2), ('y', -1)):
    m1, m2 = axis_slope(axis, F(1, 4)), axis_slope(axis, F(1, 2))
    # linear in h: extrapolate to h = 0
    assert m1 - (m2 - m1) == expected
code/python/calculus_partials_numpy.py
"""Grid partial derivatives with numpy.gradient versus the analytic ones (III.7)."""
import numpy as np

x = np.linspace(-2, 2, 81)
y = np.linspace(-2, 2, 81)
X, Y = np.meshgrid(x, y, indexing="ij")
Z = X**2 * Y + 3 * Y
dZdx, dZdy = np.gradient(Z, x, y)
# Central differences are exact for this quadratic in x and linear in y in the interior.
interior = (slice(1, -1), slice(1, -1))
assert np.allclose(dZdx[interior], (2 * X * Y)[interior], atol=1e-12)
assert np.allclose(dZdy[interior], (X**2 + 3)[interior], atol=1e-12)

Explore

On Tilted plane the partials are the same everywhere: a plane has constant slopes. On Valley, compare how much faster the height changes along yy than along xx — the contours are squeezed in the yy direction. Move the point along a contour line and notice that the height row stays fixed while the partials change.

Common mistakes

Holding the wrong variable fixed, or forgetting to treat it as a constant (∂(x2y)/∂x=2xy\partial(x^2y)/\partial x=2xy, not 2x2x). Reading a partial derivative as the total change when both inputs move. Assuming zero partial derivatives mean a minimum.

Numerical considerations

Grid-based partial derivatives (as in numpy.gradient) are finite differences with spacing errors; at boundaries they become one-sided and less accurate. Contour plots depend on the grid resolution and chosen levels and can miss narrow features. Exact slices avoid both issues for polynomials.

Engineering and ML connections

A model’s loss depends on many parameters at once. Each partial derivative measures sensitivity to one parameter while all others are fixed; together they form the gradient used by optimisation (VI.4). Partial derivatives also describe sensitivities of physical models — temperature to position, cost to each resource.

Exercises

calculation

Two partial derivatives

f(x, y) = x²y + 3y. Give ∂f/∂x and ∂f/∂y at (1, 2).

calculation

Write the slice

For f(x, y) = x² − y², write the slice f(x, 1) as a polynomial and give its slope at x = 2.

calculation

A secant along one axis

f(x, y) = xy at (1, 2). Compute [f(1 + h, 2) − f(1, 2)]/h for h = 0.5.

prediction

What does “partial” hold?

What does ∂f/∂x measure?

Choose one answer

prediction

Two zero partials

At the origin, f = x² − y² has ∂f/∂x = ∂f/∂y = 0. Is the origin a minimum?

Choose one answer

Summary and checkpoints

A partial derivative is the derivative of a one-variable slice, holding the other inputs fixed. Two partials give rates along the coordinate axes; they are not yet a full description of change. Next, III.8 assembles them into the gradient and measures change in any direction.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available