Skip to content
AI-grafen
DAI developerMathematics· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Integrals — the basics

Be able to compute simple integrals and interpret them as area and accumulation.

Practise in Mattegrafen ↗ · Grafiska och digitala metoder för integrPractise in Mattegrafen ↗ · Matematik 3cPractise in Mattegrafen ↗ · Primitiv funktion och bestämd integral

Prerequisites

Intuition

The derivative answers «how fast is it changing?». The integral answers «how much did it come to in total?».

If you drive at 80 km/h for 2 hours you have driven 160 km — speed integrated over time gives distance. Graphically it is the area under the speed curve.

The basic rule: ∫xⁿ dx = xⁿ⁺¹/(n+1) + C (for n ≠ −1).

The definite integral: ∫ₐᵇ f(x) dx = F(b) − F(a), where F is an antiderivative.

An example: ∫₀² x² dx = [x³/3]₀² = 8/3 − 0 = 8/3 ≈ 2.67.

Formal

The fundamental theorem of calculus: if F′(x)=f(x)F'(x) = f(x) then ∫abf(x) dx=F(b)−F(a)\int_a^b f(x)\,dx = F(b) - F(a). Differentiation and integration are each other's opposites.

The integral is defined as the limit of a Riemann sum: split the interval into nn strips of width Δx\Delta x, sum f(xi)Δxf(x_i)\Delta x, let n→∞n\to\infty.

Why it matters in AI:

  • A probability density is integrated to give probabilities: P(a<X<b)=∫abp(x) dxP(a < X < b) = \int_a^b p(x)\,dx, and ∫−∞∞p(x) dx=1\int_{-\infty}^{\infty} p(x)\,dx = 1.
  • The expectation: E[X]=∫x p(x) dx\mathbb E[X] = \int x\,p(x)\,dx — the basis of nearly every loss function, which are expectations over the data distribution.
  • In practice most integrals in ML are approximated with averages over samples (Monte Carlo), since they can rarely be computed exactly.

Code

import numpy as np
from scipy import integrate

f = lambda x: x**2
print(integrate.quad(f, 0, 2)[0])          # 2.6667  (= 8/3)

# A Riemann sum by hand — the same answer as n grows
for n in (10, 100, 10_000):
    x = np.linspace(0, 2, n, endpoint=False)
    print(n, round(float((f(x) * (2 / n)).sum()), 4))
# 10     2.3080
# 100    2.6268
# 10000  2.6663

The Monte Carlo variant: draw random x, take the mean of f(x), multiply by the length of the interval. That works even in a hundred dimensions, where strips become impossible.

Mastery means

  • Computes simple definite integrals
  • Interprets the integral as area and accumulation

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences