Mathematical Foundations

III.4 · Calculus

How far can a tangent line predict?

Read the derivative as a velocity, then use the tangent line to predict a nearby value. Move the prediction farther away and watch its error grow — faster than the distance itself near the point, without bound far away.

Use the tangent line as a local model

Tangent model of f(x) = (1/6)x³ − x at x₀ = 1

Graph of f(x) = (1/6)x³ − x, tangent line at x₀ = 1 with slope −0.5. At x₀ + Δx = 2 the model gives −4/3 and the function gives −2/3; the caution segment is the error 2/3.

Model error e(Δx) = f(x₀ + Δx) − L(Δx)

The error curve touches zero at Δx = 0 and is flat there: it shrinks faster than Δx. The dashed parabola (f″(x₀)/2)Δx² with f″(x₀) 1 matches it near 0. Current error 2/3 at Δx = 1.

Expansion point
1
Displacement
1
Value
−5/6−0.833333…
Instantaneous rate
−0.5
Average rate over [x₀, x₀ + Δx]
1/60.166667…
Linear model at x₀ + Δx
−4/3−1.333333…
True value
−2/3−0.666667…
Model error f − L
2/30.666667…
Error per unit displacement
2/30.666667…
Curvature coefficient
0.5

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

Near x₀ the model is close: error 2/3 at Δx = 1. Halving Δx roughly quarters the error when f″(x₀) is nonzero — it shrinks faster than Δx.

L(Δx)=f(1)+f′(1) ΔxL(\Delta x)=f(1)+f'(1)\,\Delta x
L(1)=−56+(−0.5)⋅1L(1)=-\frac{5}{6}+(-0.5)\cdot1
L(1)=−43L(1)=-\frac{4}{3}
f(1+1)=−23f(1+1)=-\frac{2}{3}
e=f−L=23e=f-L=\frac{2}{3}
mˉ=f(x0+Δx)−f(x0)Δx=16\bar m=\frac{f(x_0+\Delta x)-f(x_0)}{\Delta x}=\frac{1}{6}
f′(1)=−0.5 (instantaneous)f'(1)=-0.5\ \text{(instantaneous)}
Shrinking Δx: the error shrinks faster than Δx itself (error/Δx → 0)
Δxerror ee / Δxe / Δx²
12/32/32/3
0.57/487/247/12
0.2513/38413/9613/24
0.12525/307225/38425/48
0.062549/2457649/153649/96
0.0312597/19660897/614497/192
0.015625193/1572864193/24576193/384

Δx = 1

Model −4/3, true value −2/3, error 2/3, error/Δx 2/3.

e(1)=23e(1)=\frac{2}{3}

from −2.5 to 2.5, up to 2 decimal places

from −3 to 3, up to 2 decimal places

Drag the expansion point x₀ or the displaced point x₀ + Δx. The caution segment is the model error; the right plane draws the error for every Δx.

The tangent line at x0=1x_0=1 predicts f(2)f(2) with error 2/32/3. Drag the displaced point toward x0x_0: the error shrinks, and the error per unit of displacement shrinks too. Then choose Far away: at Δx=2.5\Delta x=2.5 the error is 275/48≈5.7275/48\approx5.7. A tangent line knows only what happens at one point.

Intuition: rate and prediction

If s(t)s(t) is a position, the secant slope s(t+Δt)−s(t)Δt\frac{s(t+\Delta t)-s(t)}{\Delta t} is the average velocity over the interval, and the derivative s′(t)s'(t) is the instantaneous velocity. Knowing where you are and how fast you are going, you can predict where you will be shortly:

s(t+Δt)≈s(t)+s′(t) Δt.s(t+\Delta t)\approx s(t)+s'(t)\,\Delta t .

The same reading applies to any quantity: a cost per extra unit, a temperature change per second, a loss change per parameter step.

Definition

The local linear model (linearization, tangent-line approximation) of a differentiable ff at x0x_0 is

L(Δx)=f(x0)+f′(x0) Δx.L(\Delta x)=f(x_0)+f'(x_0)\,\Delta x .

Its error is e(Δx)=f(x0+Δx)−L(Δx)e(\Delta x)=f(x_0+\Delta x)-L(\Delta x). Differentiability at x0x_0 means exactly that the error is small compared with the displacement:

lim⁡Δx→0e(Δx)Δx=0.\lim_{\Delta x\to0}\frac{e(\Delta x)}{\Delta x}=0 .

Mechanics: what the explorer computes

For the cubic f(x)=x3/6−xf(x)=x^3/6-x every quantity is exact: f(x0)f(x_0), the derivative, the model value, the true value and the error. For sin⁡x\sin x and exe^x the values are binary64 and marked ≈\approx. The error plane draws e(Δx)e(\Delta x) from the same model, together with the dashed parabola 12f′′(x0)Δx2\tfrac12f''(x_0)\Delta x^2 that describes it near 00. The table halves Δx\Delta x and lists ee, e/Δxe/\Delta x and e/Δx2e/\Delta x^2.

Derivation: why the error is second order

For a polynomial, expanding around x0x_0 gives exactly

f(x0+Δx)=f(x0)+f′(x0)Δx+12f′′(x0)Δx2+⋯\begin{aligned}f(x_0+\Delta x)={}&f(x_0)+f'(x_0)\Delta x\\ &+\tfrac12f''(x_0)\Delta x^2+\cdots\end{aligned}

so the error starts with 12f′′(x0)Δx2\tfrac12f''(x_0)\Delta x^2. Dividing by Δx\Delta x leaves 12f′′(x0)Δx+⋯\tfrac12f''(x_0)\Delta x+\cdots, which tends to 00. Halving Δx\Delta x therefore roughly quarters the error when f′′(x0)≠0f''(x_0)\ne0. When f′′(x0)=0f''(x_0)=0 (an inflection) the error is even smaller, of order Δx3\Delta x^3. Far from x0x_0 the higher terms dominate and nothing keeps the error small.

Worked example

Take f(x)=x3/6−xf(x)=x^3/6-x, x0=1x_0=1, Δx=1\Delta x=1. Then f(1)=−5/6f(1)=-5/6 and f′(1)=−1/2f'(1)=-1/2, so the model predicts

L(1)=−56−12=−43.L(1)=-\tfrac56-\tfrac12=-\tfrac43 .

The true value is f(2)=8/6−2=−2/3f(2)=8/6-2=-2/3, so e(1)=−2/3+4/3=2/3e(1)=-2/3+4/3=2/3. With Δx=1/2\Delta x=1/2, e=7/48e=7/48; with Δx=1/4\Delta x=1/4, e=13/384e=13/384. The ratio e/Δx2e/\Delta x^2 approaches 12f′′(1)=12\tfrac12f''(1)=\tfrac12.

Replay the worked derivation

Δx = 1

Model −4/3, true value −2/3, error 2/3, error/Δx 2/3.

e(1)=23e(1)=\frac{2}{3}
Replay the shrinking displacements and their exact model errors.

When there is no linear model

At a corner, such as ∣x∣|x| at 00, no single slope works on both sides. Any line through the origin misses one side by an amount proportional to ∣Δx∣|\Delta x|, so e/Δxe/\Delta x cannot tend to 00. The explorer reports the one-sided slopes and refuses to draw a model.

Executable reference code

The reference computes exact model errors of x2x^2 with Fraction and shows that e(Δx)=Δx2e(\Delta x)=\Delta x^2 exactly. The NumPy version linearizes sin⁡\sin and checks that e/Δx2→−12sin⁡x0e/\Delta x^2\to-\tfrac12\sin x_0.

code/python/calculus_linear_model.py
"""The tangent-line model and its exact error (III.4)."""
from fractions import Fraction as F


def linear_model(f, fprime, x0, dx):
    return f(x0) + fprime(x0) * dx


f = lambda x: x * x
fprime = lambda x: 2 * x

x0 = F(3)
for dx in [F(1), F(1, 10), F(-1, 100)]:
    error = f(x0 + dx) - linear_model(f, fprime, x0, dx)
    assert error == dx * dx            # for x**2 the error is exactly dx**2

assert linear_model(f, fprime, x0, F(1, 10)) == F(96, 10)

# error/dx -> 0: the model is accurate to first order.
ratios = [(f(x0 + d) - linear_model(f, fprime, x0, d)) / d for d in (F(1, 2**k) for k in range(10))]
assert ratios == [F(1, 2**k) for k in range(10)]
code/python/calculus_linear_model_numpy.py
"""Linearize sin at x0 and watch e/dx**2 approach f''(x0)/2 = -sin(x0)/2 (III.4)."""
import numpy as np

x0 = 0.5
dx = 2.0 ** -np.arange(1, 12)
error = np.sin(x0 + dx) - (np.sin(x0) + np.cos(x0) * dx)
ratio = error / dx**2
assert abs(ratio[-1] + np.sin(x0) / 2) < 1e-3
# Far away the model is poor: at dx = 3 the error is larger than 1.
assert abs(np.sin(x0 + 3) - (np.sin(x0) + np.cos(x0) * 3)) > 1

Explore

Put x0x_0 at an inflection (x0=0x_0=0 for the cubic) and compare the error curve with the flat dashed parabola. On exe^x at 00, the model 1+Δx1+\Delta x always underestimates. Make Δx\Delta x negative: the model also predicts the past.

Common mistakes

Evaluating the derivative at x0+Δxx_0+\Delta x instead of x0x_0 mixes two different models. An average rate over a long interval is not the instantaneous rate. “Small Δx\Delta x” is not a fixed number: it is small relative to how quickly the slope changes (f′′f'').

Numerical considerations

The model is often used in binary64 to predict tiny changes, and the error of the prediction can be much smaller than the rounding error of computing f(x0+Δx)−f(x0)f(x_0+\Delta x)-f(x_0) directly. That is why linearized error propagation (VIII.3) is a practical tool, and why finite differences (III.14) need care.

Engineering and ML connections

Gradient descent (VI.4) trusts the local linear model to choose a downhill step; a too-large learning rate leaves the region where the model is accurate, exactly like Far away. Sensitivity analysis, uncertainty propagation and Newton’s method all start from f(x0)+f′(x0)Δxf(x_0)+f'(x_0)\Delta x.

Exercises

calculation

Use the tangent line

f(x) = x², x₀ = 3, Δx = 0.1. Give the linear estimate f(3) + f′(3)Δx.

calculation

Measure the error

For f(x) = x² at x₀ = 3, the model error is e(Δx) = f(3 + Δx) − 9 − 6Δx. Give e(0.1).

calculation

Average versus instantaneous

Position s(t) = t². Give the average velocity on [1, 3] and the instantaneous velocity at t = 1.

prediction

How far does it work?

Why does the tangent estimate of x³/6 − x at x₀ = 1 fail at Δx = 2.5?

Choose one answer

prediction

A corner has no linear model

Why is there no tangent model of |x| at 0?

Choose one answer

Summary and checkpoints

The derivative is an instantaneous rate and the slope of the best local linear model. Its error shrinks faster than the displacement near x0x_0 and can grow without bound farther away. Next, III.5 derives rules that compute derivatives of sums, products and quotients without returning to limits each time.

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