Mathematical Foundations

III.3 · Calculus

What does a slope at a single point mean?

Two points make a secant. Slide the second point toward the first and watch the secant slopes settle — or refuse to settle at a corner, a jump or a vertical tangent.

Shrink a secant toward a tangent

Secant and tangent of f(x) = (1/6)x³ − x

Graph of f(x) = (1/6)x³ − x. P = (1, −5/6), Q at x + h = 2. The secant through P and Q has slope 1/6. The dashed tangent has slope −0.5.

Point
1
Step (must be nonzero for a secant)
1
Value at x
−5/6−0.833333…
Value at x + h
−2/3−0.666667…
Secant slope
1/60.166667…
Tangent slope (derived separately)
−0.5
Left slope limit
−0.5
Right slope limit
−0.5
Secant − tangent gap
2/30.666667…

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

The secant slope 1/6 approaches the tangent slope f′(1) = −0.5 as h → 0 from either side.

m(h)=f(x+h)−f(x)h=f(2)−f(1)1m(h)=\frac{f(x+h)-f(x)}{h}=\frac{f(2)-f(1)}{1}
m(1)=−23−(−56)1=16m(1)=\frac{-\frac{2}{3}-(-\frac{5}{6})}{1}=\frac{1}{6}
m(h)=16h2+0.5h−0.5(h≠0)m(h)=\tfrac{1}{6}h^{2}+0.5h-0.5\quad(h\ne0)
lim⁡h→0m(h)=−0.5\lim_{h\to0}m(h)=-0.5
f′(1)=lim⁡h→0f(1+h)−f(1)h=−0.5f'(1)=\lim_{h\to0}\frac{f(1+h)-f(1)}{h}=-0.5
Secant slopes m(h) = [f(x+h) − f(x)]/h for shrinking nonzero steps, from both sides of x
khm(h)m(−h)
011/6−5/6
10.5−5/24−17/24
20.25−35/96−59/96
30.125−167/384−215/384
40.0625−719/1536−815/1536
50.03125−2975/6144−3167/6144
60.015625−12095/24576−12479/24576

Secant 1: h = 1

Two distinct points, x and x + h, give the slope 1/6. No division by zero occurs.

m(1)=f(1+1)−f(1)1=16m(1)=\frac{f(1+1)-f(1)}{1}=\frac{1}{6}

from −2.5 to 2.5, up to 2 decimal places

from −2 to 2, up to 2 decimal places

Drag P along the graph, drag Q toward P, or type x and h. The table shrinks h from both sides; the dashed line is the separately derived tangent.

Start with the smooth cubic and drag QQ toward PP. The orange secant pivots and its slope settles near the dashed tangent. Then choose Corner |x|: the secant slope is 11 from the right and −1-1 from the left, however small hh becomes. Finally type h=0h=0: the stage refuses to draw a secant, because two equal points determine no line.

Intuition: zooming in on a curve

A secant is the straight line through two points of a graph, P=(x,f(x))P=(x,f(x)) and Q=(x+h,f(x+h))Q=(x+h,f(x+h)). Its slope is an ordinary average rate of change: rise over run. When hh is small, QQ is close to PP and the secant hugs the graph near PP. For a smooth function the secant slopes approach one number. That number is the slope of the graph at PP.

The important word is approach. The secant formula is never evaluated with h=0h=0; the tangent slope is what the secant slopes tend to as hh shrinks through nonzero values.

Definition

The difference quotient of ff at xx with step h≠0h\ne0 is

m(h)=f(x+h)−f(x)h.m(h)=\frac{f(x+h)-f(x)}{h}.

The function ff is differentiable at xx when f(x)f(x) exists and the two-sided limit

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}{h}

exists as a finite real number. The tangent line at PP is then y=f(x)+f′(x)(t−x)y=f(x)+f'(x)(t-x). The one-sided limits h→0−h\to0^- and h→0+h\to0^+ are the left and right slopes; the derivative exists exactly when both exist, are finite and agree.

Mechanics: what the explorer computes

Each secant slope is an actual quotient of two function values with a nonzero step. For the cubic f(x)=x3/6−xf(x)=x^3/6-x and decimal inputs, the values are exact rationals, so the inspector shows ==. For sin⁡x\sin x they are binary64 approximations, marked ≈\approx. The tangent slope row is not read from the table: it comes from the function’s analytic description (for a polynomial, the exact derivative; for a corner, the two exact one-sided slopes). The table is evidence that agrees with it, not a proof.

Moving PP changes xx and keeps hh; moving QQ changes hh. Arrow keys move the focused point by 0.250.25, and the numeric fields accept two decimal places.

Derivation: why the secant slope approaches the derivative

For a polynomial the quotient simplifies before any limit is taken. With f(x)=x2f(x)=x^2,

(x+h)2−x2h=2xh+h2h=2x+h(h≠0).\begin{aligned}\frac{(x+h)^2-x^2}{h}&=\frac{2xh+h^2}{h}\\ &=2x+h\qquad(h\ne0).\end{aligned}

The cancellation of hh is legitimate only because h≠0h\ne0. The simplified expression 2x+h2x+h is continuous in hh, so its limit as h→0h\to0 is 2x2x. In general, for a polynomial pp, Taylor expansion gives

m(h)=p′(x)+12p′′(x) h+⋯ ,m(h)=p'(x)+\tfrac12p''(x)\,h+\cdots,

a polynomial in hh (the omitted terms carry h2h^2, h3h^3, …) whose constant term is p′(x)p'(x). The explorer prints this exact polynomial for the cubic: its constant term is the tangent slope and the remaining terms measure how far a finite secant is from the tangent.

Worked example

Take f(x)=x3/6−xf(x)=x^3/6-x at x=1x=1. Then f(1)=−5/6f(1)=-5/6, and for every h≠0h\ne0 the exact difference quotient is

m(h)=−12+12h+16h2m(h)=-\tfrac12+\tfrac12h+\tfrac16h^2

With h=1h=1 the secant slope is 1/61/6; with h=1/2h=1/2 it is −5/24-5/24; with h=1/4h=1/4 it is −35/96-35/96. These approach f′(1)=12/2−1=−1/2f'(1)=1^2/2-1=-1/2. The replay below steps through the halving sequence and ends with the separately derived limit.

Replay the worked derivation

Secant 1: h = 1

Two distinct points, x and x + h, give the slope 1/6. No division by zero occurs.

m(1)=f(1+1)−f(1)1=16m(1)=\frac{f(1+1)-f(1)}{1}=\frac{1}{6}
Replay the h → 0 sequence: each step is a genuine secant with a nonzero step.

When no derivative exists

  • Corner. For ∣x∣|x| at 00, m(h)=∣h∣/hm(h)=|h|/h equals 11 for h>0h>0 and −1-1 for h<0h<0. Both one-sided slopes exist, but they differ, so f′(0)f'(0) does not exist.
  • Vertical tangent. For x3\sqrt[3]{x} at 00, m(h)=h−2/3→+∞m(h)=h^{-2/3}\to+\infty. The graph is smooth to the eye, but the slope is not a real number.
  • Endpoint. x\sqrt x is defined only for x≥0x\ge0. At 00 only h>0h>0 is possible, and the right quotient 1/h1/\sqrt h is unbounded.
  • Jump. A function that is not continuous at xx cannot be differentiable there: from the broken side, f(x+h)−f(x)f(x+h)-f(x) does not shrink while hh does.

Every one of these is reported by the stage as an undefined derivative with its reason; none is replaced by a nearby number.

Executable reference code

The pedagogical reference uses exact Fraction arithmetic for the difference quotient of a polynomial and shows the one-sided slopes of ∣x∣|x|. The practical version tabulates secant slopes with NumPy and compares them with the analytic derivative.

code/python/calculus_secant.py
"""Secant slopes as exact difference quotients (III.3). h must be nonzero; the derivative is a limit, never m(0)."""
from fractions import Fraction as F


def poly_value(coefficients, x):
    """coefficients[k] multiplies x**k (constant term first)."""
    return sum(c * x**k for k, c in enumerate(coefficients))


def secant_slope(f, x, h):
    if h == 0:
        raise ZeroDivisionError("h = 0: two equal points determine no secant")
    return (f(x + h) - f(x)) / h


cubic = [F(0), F(-1), F(0), F(1, 6)]           # x**3/6 - x
f = lambda x: poly_value(cubic, x)

# Exact slopes for h = 1, 1/2, 1/4, ...: they approach f'(1) = 1/2 - 1 = -1/2.
slopes = [secant_slope(f, F(1), F(1, 2**k)) for k in range(6)]
assert slopes[:3] == [F(1, 6), F(-5, 24), F(-35, 96)]
assert all(abs(b + F(1, 2)) < abs(a + F(1, 2)) for a, b in zip(slopes, slopes[1:]))

# |x| at 0: the one-sided slopes are exactly -1 and 1, so no derivative exists.
absolute = lambda x: abs(x)
right = {secant_slope(absolute, F(0), F(1, 2**k)) for k in range(8)}
left = {secant_slope(absolute, F(0), -F(1, 2**k)) for k in range(8)}
assert right == {1} and left == {-1}

try:
    secant_slope(f, F(1), F(0))
except ZeroDivisionError:
    pass
else:
    raise AssertionError("h = 0 must not produce a slope")
code/python/calculus_secant_numpy.py
"""Tabulate secant slopes with NumPy and compare with the analytic derivative (III.3)."""
import numpy as np

f = lambda x: x**3 / 6 - x
fprime = lambda x: x**2 / 2 - 1          # derived analytically, not from the table

x = 1.0
h = 2.0 ** -np.arange(0, 12)              # nonzero steps only
slopes = (f(x + h) - f(x)) / h
gaps = np.abs(slopes - fprime(x))

# For this cubic m(h) = f'(x) + h/2 + h**2/6, so the gap shrinks roughly in proportion to h.
assert np.all(np.diff(gaps) < 0)
assert gaps[-1] < 1e-3
# Both sides agree for a smooth function.
left = (f(x - h) - f(x)) / (-h)
assert np.allclose(left[-4:], fprime(x), atol=3e-3)   # gap ≈ h/2 ≤ 2⁻⁹

Explore

Make hh negative and compare with positive hh of the same size. Move PP to a point where the cubic’s tangent is horizontal (x=2x=\sqrt2 is not on the input grid; try x=1.5x=1.5 and x=1.25x=1.25 and watch the sign of the tangent slope). On Jump, compare the left and right columns of the table. On sin x, notice that every value is marked ≈\approx.

Common mistakes

Substituting h=0h=0 into the quotient produces 0/00/0, which has no value. Cancelling hh is valid only while h≠0h\ne0. A derivative needs agreement from both sides; averaging two different one-sided slopes does not create one. A graph without a visible break can still fail to be differentiable (a vertical tangent), and a numerical table that looks like it settles is not a proof that a limit exists.

Numerical considerations

In exact rational arithmetic, smaller hh always brings the secant closer to the tangent of a polynomial. In binary64 it does not: f(x+h)−f(x)f(x+h)-f(x) subtracts two nearly equal numbers, and the rounding error is divided by a tiny hh. Chapter III.14 measures this trade-off between truncation and rounding error. Here the halving table stops at h/64h/64, far above that regime.

Engineering and ML connections

Every gradient used to train a model is a collection of derivatives. Automatic differentiation computes them from the operations of a program (III.10) rather than from secants. Finite-difference quotients remain the standard independent check of a gradient implementation, provided the step size and its error are understood.

Exercises

calculation

Compute a secant slope

For f(x) = x², find the secant slope between x = 1 and x = 1 + h with h = 0.5.

calculation

Let the step shrink

For f(x) = x², the secant slope at x = 1 is m(h) = 2 + h for h ≠ 0. What value do the slopes approach as h → 0?

prediction

What happens at h = 0?

Why can you not simply put h = 0 into [f(x+h) − f(x)]/h?

Choose one answer

prediction

A corner

For f(x) = |x| at x = 0, the right secant slopes are 1 and the left ones are −1. What is f′(0)?

Choose one answer

prediction

A vertical tangent

For f(x) = ∛x at 0, the secant slope is h^(−2/3). What does this say?

Choose one answer

Summary and checkpoints

A secant slope is a quotient with h≠0h\ne0. The derivative is the limit of these slopes as h→0h\to0 from both sides, when that limit exists and is finite. Corners, vertical tangents, endpoints and jumps are places where it does not. Next, III.4 reads the derivative as a rate of change and uses the tangent line as a local linear model.

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