Skip to content

Chapter 7: Calculus 1

Calculus is the mathematics of change and accumulation. Differentiation measures an instantaneous rate of change; integration measures a total accumulated amount. The fundamental theorem of calculus shows that the two are inverse operations, one of the most powerful ideas in all of mathematics. For a programmer working on machine learning, both halves are everyday tools: every training step follows a derivative downhill, and every probability, area under a curve, and averaged metric is an integral.

Concepts from other sections (23)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This chapter teaches Calculus 1 the way a numerical programmer meets it. It opens with the derivative as a limit and asks what happens when a computer, which cannot take a limit, evaluates the difference quotient at one step size; the answer gives the finite differences that later check every gradient in the book. It then uses the derivative to find roots with Newton’s method, including the division-free reciprocal square root that RMSNorm kernels rely on. Next it derives and implements the activation functions of neural networks together with their derivatives, written so that no input can overflow. It closes with the definite integral and the trapezoid and Simpson rules that compute ROC areas. A problem set at the end works through the pen-and-paper side of the subject: limits, derivative rules, related rates, optimization, integrals, and L’Hôpital’s rule.

Learning objectives

  • Explain the derivative as a limit and predict the truncation and rounding error of a finite-difference approximation.
  • Differentiate any elementary formula with the product, quotient, and chain rules, and use derivatives to optimize and to find roots.
  • Evaluate activation functions and their derivatives without overflow or loss of relative accuracy.
  • Compute definite integrals exactly with the fundamental theorem and numerically with the trapezoid and Simpson rules, knowing each rule’s error order.
  • Recognize where these ideas reappear in gradient checking, optimization, and evaluation metrics.

This chapter assumes precalculus: functions, exponents and logarithms, trigonometry, and polynomials, as covered in the Precalculus chapter, in particular Exponents, logs, and units of information and Polynomials, Horner, and stable roots. The code sections use Python and numpy. No earlier calculus is required.

  1. Derivatives and finite differences. The derivative as a limit, the forward and central differences, the step size that balances truncation against rounding, and Richardson extrapolation.
  2. Newton’s Method. Root finding by tangent lines, quadratic convergence and its failure modes, and the division-free iteration for 1/x1/\sqrt{x}.
  3. Activation Functions and Their Derivatives. Sigmoid, tanh, ReLU, softplus, both GELUs, and SiLU, with derivatives derived from the rules of calculus and evaluated without overflow.
  4. Definite integrals: trapezoid and Simpson. The definite integral, the trapezoid and Simpson rules with their error orders, Simpson as Richardson extrapolation, and the signed-width rule for ROC curves.
  5. Calculus 1 problem set. Fifty-seven problems with worked answers on limits, derivatives, related rates, optimization, integrals, and L’Hôpital’s rule.

The sections above build what the rest of the book needs. Here, for reference, is the classical Calculus 1 core they rest on, with one worked example per topic. The section numbers refer to OpenStax, Calculus Volume 1.

The limit lim⁡x→af(x)=L\lim_{x \to a} f(x) = L means that for every ε>0\varepsilon > 0 there exists δ>0\delta > 0 such that 0<∣x−a∣<δ0 < |x - a| < \delta implies ∣f(x)−L∣<ε|f(x) - L| < \varepsilon. A function is continuous at aa if lim⁡x→af(x)=f(a)\lim_{x \to a} f(x) = f(a), and the one-sided limits lim⁡x→a+f(x)\lim_{x \to a^+} f(x) and lim⁡x→a−f(x)\lim_{x \to a^-} f(x) restrict xx to one side. Two theorems do much of the work. The squeeze theorem says that if g(x)≤f(x)≤h(x)g(x) \le f(x) \le h(x) near aa and lim⁡g=lim⁡h=L\lim g = \lim h = L, then lim⁡f=L\lim f = L: a function trapped between two functions that converge to the same value must converge there too. The intermediate value theorem says that if ff is continuous on [a,b][a, b] and NN lies between f(a)f(a) and f(b)f(b), then f(c)=Nf(c) = N for some cc in (a,b)(a, b): a continuous function cannot jump over a value.

For example, lim⁡x→0sin⁡xx\lim_{x \to 0} \frac{\sin x}{x} cannot be evaluated by substitution, but for 0<x<π/20 < x < \pi/2 you have cos⁡x≤sin⁡xx≤1\cos x \le \frac{\sin x}{x} \le 1. As x→0x \to 0, cos⁡x→1\cos x \to 1, so by the squeeze theorem the limit is 1. (OpenStax sections 2.1 to 2.5.)

The derivative f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} is the instantaneous rate of change; ff is differentiable at aa if f′(a)f'(a) exists, and differentiability implies continuity. The rules are the power rule ddxxn=nxn−1\frac{d}{dx} x^n = n x^{n-1}, the product rule (fg)′=f′g+fg′(fg)' = f'g + fg', the quotient rule (f/g)′=(f′g−fg′)/g2(f/g)' = (f'g - fg')/g^2, and the chain rule ddxf(g(x))=f′(g(x)) g′(x)\frac{d}{dx} f(g(x)) = f'(g(x))\, g'(x). Rates of change multiply through composition: if gear A turns gear B, which turns gear C, the total rate is the product of the individual rates. Implicit differentiation differentiates both sides of an equation in xx, treating yy as a function of xx, and solves for dy/dxdy/dx.

For example, on the circle x2+y2=25x^2 + y^2 = 25, differentiating gives 2x+2y dydx=02x + 2y\,\frac{dy}{dx} = 0, so dydx=−x/y\frac{dy}{dx} = -x/y. At (3,4)(3, 4) the slope of the tangent is −3/4-3/4. (OpenStax sections 3.1 to 3.9.)

A critical point is a cc where f′(c)=0f'(c) = 0 or f′(c)f'(c) is undefined. A local maximum or minimum there is identified by a sign change of f′f' (the first derivative test) or by the sign of f′′(c)f''(c) (the second derivative test). An inflection point is where f′′f'' changes sign, so the concavity changes. The mean value theorem says that if ff is continuous on [a,b][a, b] and differentiable on (a,b)(a, b), then f′(c)=f(b)−f(a)b−af'(c) = \frac{f(b) - f(a)}{b - a} for some cc in (a,b)(a, b): a car that averages 60 mph must at some instant be going exactly 60 mph. L’Hôpital’s rule says that if lim⁡f/g\lim f/g is a 0/00/0 or ∞/∞\infty/\infty form, then lim⁡f/g=lim⁡f′/g′\lim f/g = \lim f'/g', provided the latter exists: when numerator and denominator both vanish, their rates of vanishing determine the limit.

For example, a farmer has 200 m of fencing to enclose a rectangle against a river, with no fence on the river side. With width xx, the length is 200−2x200 - 2x and the area is A(x)=x(200−2x)=200x−2x2A(x) = x(200 - 2x) = 200x - 2x^2. Then A′(x)=200−4x=0A'(x) = 200 - 4x = 0 at x=50x = 50, and A′′(50)=−4<0A''(50) = -4 < 0 confirms a maximum of 50⋅100=500050 \cdot 100 = 5000 m². (OpenStax sections 4.1 to 4.7.)

A Riemann sum ∑if(xi∗) Δx\sum_i f(x_i^*)\,\Delta x over a partition approximates the area under a curve, and the definite integral ∫abf(x) dx\int_a^b f(x)\,dx is its limit as the partition is refined. FF is an antiderivative of ff if F′=fF' = f. The fundamental theorem has two parts. Part 1: if F(x)=∫axf(t) dtF(x) = \int_a^x f(t)\,dt, then F′(x)=f(x)F'(x) = f(x), so differentiation undoes integration. Part 2: ∫abf(x) dx=F(b)−F(a)\int_a^b f(x)\,dx = F(b) - F(a) for any antiderivative FF, so total accumulation is the antiderivative evaluated at the endpoints. Substitution is the chain rule in reverse: ∫f(g(x)) g′(x) dx=∫f(u) du\int f(g(x))\,g'(x)\,dx = \int f(u)\,du with u=g(x)u = g(x).

For example, to evaluate ∫02x ex2 dx\int_0^2 x\,e^{x^2}\,dx, let u=x2u = x^2, so du=2x dxdu = 2x\,dx and x dx=du/2x\,dx = du/2; the bounds become u(0)=0u(0) = 0 and u(2)=4u(2) = 4. The integral is 12∫04eu du=12(e4−1)\frac12 \int_0^4 e^u\,du = \frac12(e^4 - 1). (OpenStax sections 5.1 to 5.7.)

The area between curves with f(x)≥g(x)f(x) \ge g(x) is ∫ab[f(x)−g(x)] dx\int_a^b [f(x) - g(x)]\,dx. Rotating a region about the xx axis gives a volume by the disk method, V=π∫ab[R(x)]2 dxV = \pi \int_a^b [R(x)]^2\,dx, and rotating about the yy axis gives one by the shell method, V=2π∫abx f(x) dxV = 2\pi \int_a^b x\,f(x)\,dx.

For example, rotating y=xy = \sqrt{x} for 0≤x≤40 \le x \le 4 about the xx axis gives, by the disk method, V=π∫04(x)2 dx=π∫04x dx=π[x2/2]04=8πV = \pi \int_0^4 (\sqrt{x})^2\,dx = \pi \int_0^4 x\,dx = \pi \left[x^2/2\right]_0^4 = 8\pi. (OpenStax sections 6.1 to 6.4.)

The following table matches each standard technique to the situation that calls for it:

TechniqueWhen to useKey idea
Squeeze theoremlimit of an oscillating or bounded functionbound ff between gg and hh with the same limit
L’Hôpital’s rule0/00/0 or ∞/∞\infty/\infty indeterminate formsdifferentiate numerator and denominator
Power rulepolynomial derivativesddxxn=nxn−1\frac{d}{dx} x^n = n x^{n-1}
Chain rulecomposite functionsmultiply derivatives through the composition
Substitutionintegrals of compositionsreverse the chain rule
Disk or shell methodvolume of revolutionchoose by the axis of rotation versus the variable
Optimizationmaximum or minimum of a quantityset f′=0f' = 0; check endpoints and critical points

Limits underlie asymptotic analysis (Big-O and Big-Theta) in Algorithms. Derivatives drive gradient descent in machine learning, developed in Gradient descent and line search. Optimization connects to greedy algorithms and linear-programming relaxations. Integration defines probability densities, the subject of Probability and statistics. Rates of change reappear in the amortized analysis of data structures. Interviews and quantitative roles test the same ideas: optimization problems, the mathematics behind PageRank, derivative pricing and rate-of-change models, and the derivative intuition gradient descent requires.

  • Continuous function / continuity: ff is continuous at aa when lim⁡x→af(x)=f(a)\lim_{x \to a} f(x) = f(a), so the graph has no jump or hole there; many theorems of calculus, such as the intermediate value theorem, require it.
  • One-sided limit: lim⁡x→a+f(x)\lim_{x \to a^+} f(x) or lim⁡x→a−f(x)\lim_{x \to a^-} f(x), the limit as xx approaches aa from the right or the left only; the two-sided limit exists exactly when both one-sided limits exist and agree.
  • Squeeze theorem: if g(x)≤f(x)≤h(x)g(x) \le f(x) \le h(x) near aa and lim⁡g=lim⁡h=L\lim g = \lim h = L, then lim⁡f=L\lim f = L; it settles limits such as lim⁡x→0sin⁡x/x=1\lim_{x \to 0} \sin x / x = 1 that substitution cannot.
  • Intermediate value theorem: if ff is continuous on [a,b][a, b] and NN lies between f(a)f(a) and f(b)f(b), then f(c)=Nf(c) = N for some cc in (a,b)(a, b); a continuous function cannot jump over a value, which is why bisection finds roots.
  • Product rule / quotient rule: (fg)′=f′g+fg′(fg)' = f'g + fg' and (f/g)′=(f′g−fg′)/g2(f/g)' = (f'g - fg')/g^2, the rules for differentiating a product or a ratio of two functions.
  • Power rule: ddxxn=nxn−1\frac{d}{dx} x^n = n x^{n-1}, the derivative of a power, and with linearity the derivative of any polynomial.
  • Implicit differentiation: differentiating both sides of an equation in xx and yy, treating yy as a function of xx, then solving for dy/dxdy/dx; it gives slopes of curves such as x2+y2=25x^2 + y^2 = 25 that are not written as y=f(x)y = f(x).
  • First derivative test: a critical point cc is a local maximum if f′f' changes from positive to negative at cc and a local minimum if it changes from negative to positive.
  • Second derivative test: at a critical point cc with f′(c)=0f'(c) = 0, f′′(c)<0f''(c) < 0 means a local maximum and f′′(c)>0f''(c) > 0 a local minimum; when f′′(c)=0f''(c) = 0 the test is inconclusive.
  • Inflection point: a point where f′′f'' changes sign, so the graph switches between curving up (concave up) and curving down (concave down).
  • Mean value theorem: if ff is continuous on [a,b][a, b] and differentiable on (a,b)(a, b), then f′(c)=(f(b)−f(a))/(b−a)f'(c) = (f(b) - f(a))/(b - a) for some cc in (a,b)(a, b); somewhere the instantaneous rate equals the average rate.
  • Antiderivative: a function FF with F′=fF' = f; by the fundamental theorem of calculus, ∫abf(x) dx=F(b)−F(a)\int_a^b f(x)\,dx = F(b) - F(a) for any antiderivative FF of ff.
  • Integration by substitution / u-substitution: the chain rule in reverse, ∫f(g(x)) g′(x) dx=∫f(u) du\int f(g(x))\,g'(x)\,dx = \int f(u)\,du with u=g(x)u = g(x); it turns an integral of a composition into a simpler one, with the bounds changed to g(a)g(a) and g(b)g(b).
  • Disk method / shell method: formulas for the volume of a solid of revolution; the disk method V=π∫abR(x)2 dxV = \pi \int_a^b R(x)^2\,dx slices perpendicular to the axis of rotation, and the shell method V=2π∫abx f(x) dxV = 2\pi \int_a^b x\,f(x)\,dx wraps cylinders around the yy axis.
  • OpenStax, Calculus Volume 1 (free, CC BY-NC-SA 4.0), chapters 2 to 6. Useful exercises: section 2.2 #65 to 72, 2.3 #95 to 106, 2.4 #131 to 140, and the challenge set 2.5 #191 to 196; section 3.2 #55 to 68, 3.3 #100 to 110, 3.4 #140 to 155, 3.6 #210 to 222, and the challenge set 3.8 #308 to 315; section 4.3 #101 to 110, 4.5 #198 to 210, 4.7 #300 to 310, and the challenge set 4.7 #316 to 320; section 5.2 #67 to 76, 5.3 #148 to 160, 5.5 #256 to 268, and the challenge set 5.7 #340 to 348; section 6.1 #1 to 12, 6.2 #55 to 70, 6.3 #105 to 115, and the challenge set 6.4 #140 to 148.
  • Paul’s Online Math Notes: additional worked examples on tricky topics.
  • 3Blue1Brown, Essence of Calculus: geometric intuition, best watched before or after reading.