Skip to content

Integrals

Where derivatives measure rates of change, integrals measure accumulation: total distance from a speed, total CPU time from a utilization graph, total probability from a density. Differentiation and integration were developed as separate ideas, but one of the most important results in mathematics, the Fundamental Theorem of Calculus, shows that they are inverse operations. Read "Derivatives & Gradient Descent" first, because everything here leans on the derivative rules.


Antiderivatives

An antiderivative reverses differentiation: F(x)F(x) is an antiderivative of f(x)f(x) when F′(x)=f(x)F'(x) = f(x). Since ddx(x2)=2x\frac{d}{dx}(x^2) = 2x, an antiderivative of 2x2x is x2x^2. This is written with the integral sign ∫\int:

∫2x dx=x2+C\int 2x\,dx = x^2 + C

The dxdx says which variable you integrate with respect to. This form, without limits, is called an indefinite integral.

The Constant Of Integration

Why the +C+ C? The derivatives of x2x^2, x2+5x^2 + 5 and x2−100x^2 - 100 are all 2x2x, because constants disappear when you differentiate. Going backwards, you cannot know which constant was there, so you add an unknown constant of integration CC to represent all of them at once. Forgetting CC is technically wrong for indefinite integrals.

The Power Rule For Integration

Reverse the power rule for derivatives: instead of "multiply by the exponent, then subtract one", do "add one to the exponent, then divide by the new exponent".

∫xn dx=xn+1n+1+C,n≠−1\int x^n\,dx = \frac{x^{n+1}}{n+1} + C, \qquad n \neq -1

The exception n=−1n = -1 exists because it would mean dividing by zero (∫x−1 dx\int x^{-1}\,dx is ln⁡∣x∣+C\ln|x| + C, outside this course). You can always check an antiderivative by differentiating it back.

IntegralResultCheck by differentiating
∫x dx\int x\,dxx22+C\frac{x^2}{2} + C2x2=x\frac{2x}{2} = x
∫x2 dx\int x^2\,dxx33+C\frac{x^3}{3} + C3x23=x2\frac{3x^2}{3} = x^2
∫x3 dx\int x^3\,dxx44+C\frac{x^4}{4} + Cx3x^3
∫4x3 dx\int 4x^3\,dxx4+Cx^4 + C4x34x^3
∫5x4 dx\int 5x^4\,dxx5+Cx^5 + C5x45x^4
∫(3x2+1) dx\int (3x^2 + 1)\,dxx3+x+Cx^3 + x + C3x2+13x^2 + 1
∫ex dx\int e^x\,dxex+Ce^x + Cexe^x

Definite Integrals

A definite integral has limits: it computes the accumulated quantity between x=ax = a and x=bx = b, which is the area under the graph of ff between those two points (counting area below the x-axis as negative).

∫abf(x) dx\int_a^b f(x)\,dx

Here aa is the lower limit and bb the upper limit. A definite integral gives a number, not a function, so there is no +C+ C.

Evaluating With Brackets

To evaluate, find an antiderivative FF, then compute F(b)−F(a)F(b) - F(a). The bracket notation [F(x)]ab\big[F(x)\big]_a^b is shorthand for exactly that subtraction.

∫02x dx=[x22]02=222−022=2\int_0^2 x\,dx = \left[\frac{x^2}{2}\right]_0^2 = \frac{2^2}{2} - \frac{0^2}{2} = 2

That matches geometry: the region under f(x)=xf(x) = x from 00 to 22 is a triangle with base 22 and height 22, area 12⋅2⋅2=2\frac{1}{2} \cdot 2 \cdot 2 = 2.

Definite integrals as areas

With limits 11 and 33: ∫13x dx=92−12=4\int_1^3 x\,dx = \frac{9}{2} - \frac{1}{2} = 4. A quick sanity check for this one: the region is a trapezoid, and if you level the slanted top around its midpoint (2,2)(2, 2) you get a rectangle of width 22 and height 22, area 44.


The Fundamental Theorem Of Calculus

The Fundamental Theorem of Calculus (FTC) states that differentiation and integration are inverse operations. Concretely:

If F′(x)=f(x), then ∫abf(x) dx=F(b)−F(a)\text{If } F'(x) = f(x), \text{ then } \int_a^b f(x)\,dx = F(b) - F(a)

This is what makes the bracket method work. Without it, computing an area under a curved graph would require adding up infinitely many slices. With it, the hard problem "find an area" turns into the much easier problem "find an antiderivative".

Why It Makes Sense

Think of F(x)F(x) as the total area accumulated from some starting point up to xx. If you move xx a tiny bit further, the area grows by a thin strip of height f(x)f(x). So the rate at which the area grows is f(x)f(x), meaning F′(x)=f(x)F'(x) = f(x). The area function is an antiderivative, and the area between aa and bb is "total up to bb minus total up to aa". It is the same logic as an odometer: the distance driven between two moments is the reading at the end minus the reading at the start.

Examples

∫132x dx=[x2]13=9−1=8\int_1^3 2x\,dx = \big[x^2\big]_1^3 = 9 - 1 = 8

∫12x2 dx=[x33]12=83−13=73\int_1^2 x^2\,dx = \left[\frac{x^3}{3}\right]_1^2 = \frac{8}{3} - \frac{1}{3} = \frac{7}{3}

Verifying the FTC for f(x)=3x2f(x) = 3x^2 on [0,2][0, 2]: F(x)=x3F(x) = x^3 satisfies F′(x)=3x2F'(x) = 3x^2, and ∫023x2 dx=F(2)−F(0)=8\int_0^2 3x^2\,dx = F(2) - F(0) = 8. Going the other way, ∫x4 dx=x55+C\int x^4\,dx = \frac{x^5}{5} + C and ddx(x55+C)=x4\frac{d}{dx}\left(\frac{x^5}{5} + C\right) = x^4: you get back exactly where you started.


Riemann Sums

Underneath every integral is a Riemann sum: an approximation of the area by a finite number of rectangles. Computers calculate integrals numerically this way all the time, especially when no antiderivative formula exists.

Lower And Upper Sums

Split the interval into equal strips and draw a rectangle on each strip. For an increasing function like f(x)=xf(x) = x on [0,4][0, 4]:

  • The lower sum uses the function value at the left end of each strip, so every rectangle sits under the curve and the total underestimates the area.
  • The upper sum uses the value at the right end, so every rectangle pokes above the curve and the total overestimates the area.

Lower and upper Riemann sums

ApproximationArea
Lower sum, 4 rectangles1⋅(0+1+2+3)=61 \cdot (0 + 1 + 2 + 3) = 6
Upper sum, 4 rectangles1⋅(1+2+3+4)=101 \cdot (1 + 2 + 3 + 4) = 10
Lower sum, 8 rectangles0.5⋅(0+0.5+⋯+3.5)=70.5 \cdot (0 + 0.5 + \dots + 3.5) = 7
Upper sum, 8 rectangles0.5⋅(0.5+1+⋯+4)=90.5 \cdot (0.5 + 1 + \dots + 4) = 9
Exact integral ∫04x dx\int_0^4 x\,dx[x22]04=8\left[\frac{x^2}{2}\right]_0^4 = 8

As the rectangles get narrower, the lower and upper sums squeeze together. In the limit where the width goes to zero, both converge to the exact integral. That limit is the definition of the (Riemann) integral.

Riemann Sums In Code

A Riemann sum is a loop, which makes it the natural way for a program to integrate. More rectangles means a better answer.

python
def riemann(f, a, b, n, side="left"):
    width = (b - a) / n
    offset = 0 if side == "left" else width
    return sum(f(a + i * width + offset) * width for i in range(n))

f = lambda x: x
for n in (4, 8, 100, 10_000):
    print(n, riemann(f, 0, 4, n, "left"), riemann(f, 0, 4, n, "right"))
# both columns approach the exact value 8

Applications In Computer Science

Integrals are less visible than derivatives in day to day programming, but they sit underneath many standard tools.

Accumulated Resource Usage

If a server uses CPU at rate C(t)C(t), the total CPU consumed between times aa and bb is ∫abC(t) dt\int_a^b C(t)\,dt. In practice you only have samples, so you use a Riemann sum: each sample times the length of the interval it represents.

CPU usage as a Riemann sum

Samples of 35%35\%, 60%60\%, 45%45\% and 25%25\% every 15 minutes (900900 s) give

0.35⋅900+0.60⋅900+0.45⋅900+0.25⋅900=315+540+405+225=1485 CPU-seconds0.35 \cdot 900 + 0.60 \cdot 900 + 0.45 \cdot 900 + 0.25 \cdot 900 = 315 + 540 + 405 + 225 = 1485 \text{ CPU-seconds}

and the average utilisation over the hour is 14853600=41.25%\frac{1485}{3600} = 41.25\%. This is the "area divided by width" definition of an average value. Cloud providers bill exactly this way: at €0.04 per CPU-hour, samples of 20%20\%, 50%50\%, 80%80\%, 40%40\% give 17101710 CPU-seconds =0.475= 0.475 CPU-hours, costing 0.475⋅0.04=€0.0190.475 \cdot 0.04 = €0.019.

A related trap: a high Linux load average does not mean high CPU usage. Load average counts processes that are running or waiting, so processes blocked on disk I/O, network calls, file locks or database access raise the load while the CPU sits mostly idle.

Probability And Expected Value

For a continuous random variable with probability density function f(x)f(x), probabilities are areas under the density:

P(a≤X≤b)=∫abf(x) dxE[X]=∫−∞∞x f(x) dxP(a \leq X \leq b) = \int_a^b f(x)\,dx \qquad\qquad E[X] = \int_{-\infty}^{\infty} x\,f(x)\,dx

The expected value E[X]E[X] is the long-run average. Signal processing uses integrals the same way to compute the total energy and the average value of a signal, and many machine learning models are defined in terms of expectations even though they are trained with derivatives.


AUC In Machine Learning

A classifier (spam or not spam, sick or healthy, click or no click) usually outputs a score between 00 and 11, and you choose a threshold above which you predict "positive". Every threshold gives a different trade-off, and the ROC curve (Receiver Operating Characteristic) shows all of them at once.

The ROC Curve

The ROC curve plots, for every threshold:

  • the True Positive Rate (TPR, the share of real positives you catch) on the vertical axis,
  • the False Positive Rate (FPR, the share of real negatives you wrongly flag) on the horizontal axis.

A perfect classifier reaches the corner (0,1)(0, 1): all positives caught, no false alarms. A classifier that guesses randomly follows the diagonal y=xy = x.

Area Under The Curve

The AUC is the area under the ROC curve, a single number that summarises the whole curve:

AUC=∫01TPR(FPR)  d(FPR),0≤AUC≤1\text{AUC} = \int_0^1 \text{TPR}(\text{FPR})\;d(\text{FPR}), \qquad 0 \leq \text{AUC} \leq 1

ROC curve and AUC

AUCInterpretation
1.01.0Perfect classifier
0.90.9 to 1.01.0Excellent
0.80.8 to 0.90.9Good
0.70.7 to 0.80.8Fair
0.50.5Random guessing

Computing An AUC

For the ROC curve TPR(x)=1−(1−x)3\text{TPR}(x) = 1 - (1 - x)^3, expand the integrand first:

1−(1−3x+3x2−x3)=3x−3x2+x31 - (1 - 3x + 3x^2 - x^3) = 3x - 3x^2 + x^3

AUC=∫01(3x−3x2+x3) dx=[32x2−x3+x44]01=32−1+14=0.75\text{AUC} = \int_0^1 (3x - 3x^2 + x^3)\,dx = \left[\frac{3}{2}x^2 - x^3 + \frac{x^4}{4}\right]_0^1 = \frac{3}{2} - 1 + \frac{1}{4} = 0.75

So this classifier is "fair". Keep in mind that the ROC curve compares classifiers across all thresholds, but it does not tell you which threshold to use. That depends on the application: a cancer screening test and a spam filter weigh false negatives and false positives very differently.


Worked Exercises

These mirror the integral exercises of the course book.

Definite Integrals

∫01x dx=[x22]01=12\int_0^1 x\,dx = \left[\frac{x^2}{2}\right]_0^1 = \frac{1}{2}. ∫022x dx=[x2]02=4\int_0^2 2x\,dx = \big[x^2\big]_0^2 = 4. ∫13x2 dx=[x33]13=9−13=263\int_1^3 x^2\,dx = \left[\frac{x^3}{3}\right]_1^3 = 9 - \frac{1}{3} = \frac{26}{3}.

Requests Processed

A server handles R(t)=100tR(t) = 100t requests per hour (tt in hours). In the first two hours it handles ∫02100t dt=100[t22]02=200\int_0^2 100t\,dt = 100\left[\frac{t^2}{2}\right]_0^2 = 200 requests.

AUC Of Simple Curves

TPR(x)=x2\text{TPR}(x) = x^2 (with xx the FPR): ∫01x2 dx=13\int_0^1 x^2\,dx = \frac{1}{3}. That is below 0.50.5, so this classifier does worse than random guessing; flipping its predictions would give AUC 23\frac{2}{3}.

TPR(x)=2x−x2\text{TPR}(x) = 2x - x^2: ∫01(2x−x2) dx=[x2−x33]01=1−13=23\int_0^1 (2x - x^2)\,dx = \left[x^2 - \frac{x^3}{3}\right]_0^1 = 1 - \frac{1}{3} = \frac{2}{3}.


Quick Recap

  • An antiderivative FF satisfies F′=fF' = f. Indefinite integrals need +C+ C because constants vanish when differentiating.
  • Power rule: ∫xn dx=xn+1n+1+C\int x^n\,dx = \frac{x^{n+1}}{n+1} + C for n≠−1n \neq -1. Check by differentiating back.
  • A definite integral ∫abf(x) dx\int_a^b f(x)\,dx is the (signed) area under the curve, a number with no CC.
  • FTC: ∫abf(x) dx=F(b)−F(a)\int_a^b f(x)\,dx = F(b) - F(a). Integration and differentiation are inverses.
  • Riemann sums approximate the area with rectangles; lower sums underestimate, upper sums overestimate, and both converge to the integral as the width goes to 00.
  • CS uses: total resource usage from samples, average = area / width, probabilities and expected values for continuous variables.
  • AUC = area under the ROC curve (TPR vs FPR). 11 is perfect, 0.50.5 is random.