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.
- Rate of change: how much the output changes per unit change in the input; for it is 4 per person. Read more: Chapter 2: Change and growth
- fundamental theorem of calculus: if , then . Read more: 7.4 Definite Integrals: Trapezoid and Simpson
- training step: one zero-grad, loss, finiteness check, backward, clip, and optimizer update on one batch. Read more: 6.6 Training loop and the autograd bigram in AI Engineering
- Newton’s method: the iteration , jumping to the zero of the tangent line. Read more: 7.2 Newton’s Method
- RMSNorm: divides a vector by its root mean square plus eps (inside the root) and multiplies by a learned gain; no mean subtraction, no bias. Read more: 13.1 Pre-LN and RMSNorm in AI Engineering
- activation function: a nonlinear function applied elementwise between linear layers. Read more: 7.3 Activation Functions and Their Derivatives
- definite integral: the limit of Riemann sums, the signed area between and the axis. Read more: 7.4 Definite Integrals: Trapezoid and Simpson
- related rates: rates of change tied together by differentiating a constraint in time. Read more: 7.5 Calculus 1 Problem Set
- L’Hôpital’s rule: for or forms, when the latter exists. Read more: 7.5 Calculus 1 Problem Set
- rounding error: the error from floating-point rounding, about in a difference quotient. Read more: 7.1 Derivatives and Finite Differences
- chain rule: . Read more: 7.5 Calculus 1 Problem Set
- relative accuracy: the number of correct significant digits, which subtraction of nearly equal numbers destroys. Read more: 7.3 Activation Functions and Their Derivatives
- central differences: the numerical derivative , used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all in AI Engineering
- Richardson extrapolation: combining differences at with weights to cancel successive even powers of . Read more: 7.1 Derivatives and Finite Differences
- quadratic convergence: , so correct digits double each step. Read more: 7.2 Newton’s Method
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives
- softplus: , a smooth relu whose derivative is . Read more: 7.3 Activation Functions and Their Derivatives
- GELU: , the input weighted by the standard normal CDF; its tanh approximation is a different function. Read more: 7.3 Activation Functions and Their Derivatives
- SiLU (swish): , the gate inside SwiGLU. Read more: 7.3 Activation Functions and Their Derivatives
- ROC curve: the staircase of (FPR, TPR) pairs traced as the decision threshold falls through every distinct score. Read more: 13.8 Logistic regression (IRLS), ROC-AUC, calibration (ECE)
- indeterminate form: an expression such as or that signals a limit must be rewritten. Read more: 7.5 Calculus 1 Problem Set
- critical point: a point where , a candidate for an interior optimum. Read more: 7.5 Calculus 1 Problem Set
- Greedy algorithm: an algorithm that commits at each step to the choice that looks best now and never reconsiders it; it is correct only when the problem has the greedy-choice property. Read more: Chapter 10: Greedy algorithms in Programming and Algorithms
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.
Before you begin
Section titled “Before you begin”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.
In this chapter
Section titled “In this chapter”- 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.
- Newton’s Method. Root finding by tangent lines, quadratic convergence and its failure modes, and the division-free iteration for .
- 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.
- 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.
- Calculus 1 problem set. Fifty-seven problems with worked answers on limits, derivatives, related rates, optimization, integrals, and L’Hôpital’s rule.
A map of Calculus 1
Section titled “A map of Calculus 1”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.
Limits and continuity
Section titled “Limits and continuity”The limit means that for every there exists such that implies . A function is continuous at if , and the one-sided limits and restrict to one side. Two theorems do much of the work. The squeeze theorem says that if near and , then : a function trapped between two functions that converge to the same value must converge there too. The intermediate value theorem says that if is continuous on and lies between and , then for some in : a continuous function cannot jump over a value.
For example, cannot be evaluated by substitution, but for you have . As , , so by the squeeze theorem the limit is 1. (OpenStax sections 2.1 to 2.5.)
Derivatives
Section titled “Derivatives”The derivative is the instantaneous rate of change; is differentiable at if exists, and differentiability implies continuity. The rules are the power rule , the product rule , the quotient rule , and the chain rule . 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 , treating as a function of , and solves for .
For example, on the circle , differentiating gives , so . At the slope of the tangent is . (OpenStax sections 3.1 to 3.9.)
Applications of derivatives
Section titled “Applications of derivatives”A critical point is a where or is undefined. A local maximum or minimum there is identified by a sign change of (the first derivative test) or by the sign of (the second derivative test). An inflection point is where changes sign, so the concavity changes. The mean value theorem says that if is continuous on and differentiable on , then for some in : a car that averages 60 mph must at some instant be going exactly 60 mph. L’Hôpital’s rule says that if is a or form, then , 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 , the length is and the area is . Then at , and confirms a maximum of m². (OpenStax sections 4.1 to 4.7.)
Integration
Section titled “Integration”A Riemann sum over a partition approximates the area under a curve, and the definite integral is its limit as the partition is refined. is an antiderivative of if . The fundamental theorem has two parts. Part 1: if , then , so differentiation undoes integration. Part 2: for any antiderivative , so total accumulation is the antiderivative evaluated at the endpoints. Substitution is the chain rule in reverse: with .
For example, to evaluate , let , so and ; the bounds become and . The integral is . (OpenStax sections 5.1 to 5.7.)
Applications of integration
Section titled “Applications of integration”The area between curves with is . Rotating a region about the axis gives a volume by the disk method, , and rotating about the axis gives one by the shell method, .
For example, rotating for about the axis gives, by the disk method, . (OpenStax sections 6.1 to 6.4.)
Choosing a technique
Section titled “Choosing a technique”The following table matches each standard technique to the situation that calls for it:
| Technique | When to use | Key idea |
|---|---|---|
| Squeeze theorem | limit of an oscillating or bounded function | bound between and with the same limit |
| L’Hôpital’s rule | or indeterminate forms | differentiate numerator and denominator |
| Power rule | polynomial derivatives | |
| Chain rule | composite functions | multiply derivatives through the composition |
| Substitution | integrals of compositions | reverse the chain rule |
| Disk or shell method | volume of revolution | choose by the axis of rotation versus the variable |
| Optimization | maximum or minimum of a quantity | set ; check endpoints and critical points |
Where Calculus 1 shows up in computing
Section titled “Where Calculus 1 shows up in computing”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.
Key terms
Section titled “Key terms”- Continuous function / continuity: is continuous at when , so the graph has no jump or hole there; many theorems of calculus, such as the intermediate value theorem, require it.
- One-sided limit: or , the limit as approaches from the right or the left only; the two-sided limit exists exactly when both one-sided limits exist and agree.
- Squeeze theorem: if near and , then ; it settles limits such as that substitution cannot.
- Intermediate value theorem: if is continuous on and lies between and , then for some in ; a continuous function cannot jump over a value, which is why bisection finds roots.
- Product rule / quotient rule: and , the rules for differentiating a product or a ratio of two functions.
- Power rule: , the derivative of a power, and with linearity the derivative of any polynomial.
- Implicit differentiation: differentiating both sides of an equation in and , treating as a function of , then solving for ; it gives slopes of curves such as that are not written as .
- First derivative test: a critical point is a local maximum if changes from positive to negative at and a local minimum if it changes from negative to positive.
- Second derivative test: at a critical point with , means a local maximum and a local minimum; when the test is inconclusive.
- Inflection point: a point where changes sign, so the graph switches between curving up (concave up) and curving down (concave down).
- Mean value theorem: if is continuous on and differentiable on , then for some in ; somewhere the instantaneous rate equals the average rate.
- Antiderivative: a function with ; by the fundamental theorem of calculus, for any antiderivative of .
- Integration by substitution / u-substitution: the chain rule in reverse, with ; it turns an integral of a composition into a simpler one, with the bounds changed to and .
- Disk method / shell method: formulas for the volume of a solid of revolution; the disk method slices perpendicular to the axis of rotation, and the shell method wraps cylinders around the axis.
Further reading
Section titled “Further reading”- 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.