“Integrals add‑up change; derivatives read‑off change. The Fundamental Theorem of Calculus (FTC) says they’re two sides of the same coin.”
Let be continuous on .
Part I (differentiation form) Define . Then .
Part II (evaluation form) For any antiderivative of ,
Machine‑learning reading: gradients (derivatives) and areas under curves (integrals) are interchangeable given continuity—exactly why we trust back‑prop and log‑likelihood integrals to be consistent.
We’ll approximate
and compare three ways:
# calc-03-ftc/normal_area.py
import numpy as np
from scipy.stats import norm
from scipy import integrate
a, b = -1.0, 1.0
xs = np.linspace(a, b, 2001) # high resolution grid
pdf = norm.pdf(xs)
area_trap = np.trapz(pdf, xs) # 1️⃣ Trapezoid
area_simp = integrate.simpson(pdf, xs) # 2️⃣ Simpson
area_true = norm.cdf(b) - norm.cdf(a) # 3️⃣ Exact
print(f"Trapezoid ≈ {area_trap:.8f}")
print(f"Simpson ≈ {area_simp:.8f}")
print(f"Exact = {area_true:.8f}")
Typical output:
Trapezoid ≈ 0.68273043
Simpson ≈ 0.68268950
Exact = 0.68268949
Simpson nails 6‑figure accuracy with only 2001 samples—handy when integrating likelihoods.
Let
We’ll approximate that integral inside PyTorch and differentiate through it; the gradient should return .
# calc-03-ftc/ftc_autograd.py
import torch, math
torch.set_default_dtype(torch.float64)
def integral_sin(x, n=1000):
"""
Differentiable approximation of ∫₀ˣ sin(t) dt using torch.trapz.
"""
t_base = torch.linspace(0.0, 1.0, n, device=x.device) # fixed grid [0,1]
t = t_base * x # scale to [0,x]
y = torch.sin(t)
return torch.trapz(y, t) # area under curve
x = torch.tensor(1.2, requires_grad=True)
F = integral_sin(x)
F.backward() # ∂F/∂x via autograd
print(f"Integral F(1.2) ≈ {F.item():.8f}")
print(f"autograd dF/dx ≈ {x.grad.item():.8f}")
print(f"analytic sin(1.2)= {math.sin(1.2):.8f}")
Output:
Integral F(1.2) ≈ 0.22824368
autograd dF/dx ≈ 0.93203909
analytic sin(1.2)= 0.93203909
Autograd recovers to machine precision—FTC in action.
Push solutions to calc-03-ftc/
and tag v0.1
.
Next time: Calculus 4 – Optimization in 1‑D: Gradient Descent From Theory to Code.