TExcelTAD
Home › Pre-Calculus, Geometry & Trigonometry › A First Look at the Integral

A First Look at the Integral

The derivative answers "how fast is it changing?". The integral answers the opposite question: "how much has accumulated?". Total distance from a changing speed, total energy from a changing power, area under a curve: all are integrals. This lesson builds the integral as the limit of sums of thin rectangles (Riemann sums), connects it to the series from earlier in the course, and shows why a game loop that adds v * dt every frame is computing an integral.

Area under a curve

If a car drives at a constant 20 m/s20\ \text{m/s} for 5 s5\ \text{s}, it covers 20⋅5=100 m20 \cdot 5 = 100\ \text{m}: the area of a rectangle under the speed graph. When the speed changes, the distance is still the area under the speed curve, but the region is no longer a rectangle.

The trick is to cut the region into nn thin vertical strips of width Δx\Delta x, approximate each strip by a rectangle, and add:

area≈f(x1)Δx+f(x2)Δx+⋯+f(xn)Δx=∑i=1nf(xi)ΔxΔx=b−an\text{area} \approx f(x_1)\Delta x + f(x_2)\Delta x + \cdots + f(x_n)\Delta x = \sum_{i=1}^{n} f(x_i)\Delta x \qquad \Delta x = \frac{b - a}{n}

This is a Riemann sum. The heights can be taken at the left end, right end or midpoint of each strip. As n→∞n \to \infty and the strips get thinner, the sum approaches a limit, the definite integral:

∫abf(x) dx=lim⁡n→∞∑f(xi)Δx\int_a^b f(x)\,dx = \lim_{n \to \infty} \sum f(x_i)\Delta x

The ∫\int sign is a stretched S for "sum", and dxdx is the infinitely thin version of Δx\Delta x.

Riemann sums in code

Let's find the area under f(x)=x2f(x) = x^2 from 0 to 2:

python · try it yourself
def riemann(f, a, b, n, where="left"):
    dx = (b - a) / n
    offset = {"left": 0.0, "right": 1.0, "mid": 0.5}[where]
    return sum(f(a + (i + offset) * dx) for i in range(n)) * dx

f = lambda x: x * x
print("     n        left       right    midpoint")
for n in [4, 10, 100, 1000, 10000]:
    print(f"{n:>6} {riemann(f, 0, 2, n):11.6f} {riemann(f, 0, 2, n, 'right'):11.6f} {riemann(f, 0, 2, n, 'mid'):11.6f}")
print("exact 8/3 =", 8 / 3)
     n        left       right    midpoint
     4    1.750000    3.750000    2.625000
    10    2.280000    3.080000    2.660000
   100    2.626800    2.706800    2.666600
  1000    2.662668    2.670668    2.666666
 10000    2.666267    2.667067    2.666667
exact 8/3 = 2.6666666666666665

Left sums are too small and right sums too big here (x2x^2 is increasing), and both close in on 8/3≈2.66678/3 \approx 2.6667. The midpoint rule gets there much faster.

The exact answer from a series formula

For x2x^2 on [0,2][0, 2] with right endpoints, xi=2i/nx_i = 2i/n and Δx=2/n\Delta x = 2/n, so the sum is

∑i=1n(2in)2⋅2n=8n3⋅∑i2=8n3⋅n(n+1)(2n+1)6=86⋅(1+1n)(2+1n)  →  86⋅2=83\begin{aligned} \sum_{i=1}^{n} \left(\frac{2i}{n}\right)^2 \cdot \frac{2}{n} &= \frac{8}{n^3} \cdot \sum i^2 = \frac{8}{n^3} \cdot \frac{n(n + 1)(2n + 1)}{6} \\ &= \frac{8}{6} \cdot \left(1 + \frac{1}{n}\right)\left(2 + \frac{1}{n}\right) \;\to\; \frac{8}{6} \cdot 2 = \frac{8}{3} \end{aligned}

That uses the sum-of-squares formula from Series and a limit at infinity. In calculus a much quicker route opens up: the Fundamental Theorem of Calculus says integration undoes differentiation. Because the derivative of x3/3x^3/3 is x2x^2, the area is 23/3−03/3=8/32^3/3 - 0^3/3 = 8/3.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

f = lambda x: x**2
a, b, n = 0, 2, 8
dx = (b - a) / n
xs = np.linspace(a, b, 300)
mids = a + (np.arange(n) + 0.5) * dx

plt.figure(figsize=(6, 4))
plt.bar(mids, f(mids), width=dx, alpha=0.4, edgecolor="k", label=f"{n} midpoint rectangles")
plt.plot(xs, f(xs), "k", lw=2, label="f(x) = x²")
plt.legend(); plt.title("Riemann sum for ∫₀² x² dx")
print("midpoint sum with 8 rectangles:", float(np.sum(f(mids)) * dx))
midpoint sum with 8 rectangles: 2.65625

Signed area and accumulation

When ff is negative, its rectangles count as negative, so the integral is a signed area: ∫02πsin⁡x dx=0\int_0^{2\pi} \sin x\,dx = 0, because the part below the axis cancels the part above. For a velocity, that is the difference between displacement (signed, can cancel) and total distance travelled (the integral of ∣v∣\lvert v \rvert).

Integrand Integral means
speed distance travelled
velocity change in position (displacement)
power (watts) energy used (joules)
flow rate (litres/s) volume
probability density probability

In software: every frame is a rectangle

A game loop that updates position += velocity * dt each frame adds one thin rectangle, velocity⋅dt\text{velocity} \cdot dt, per frame. Over many frames it computes a left Riemann sum of the velocity: the integral of velocity, which is the change in position. As dt→0dt \to 0 the sum approaches the exact integral, which is why smaller time steps give more accurate physics (see Introduction to Limits and Game Physics Integration).

The same accumulation appears in data code: a running total of bytes per second gives total bytes, a running total of a probability density gives a cumulative distribution, and numpy.trapezoid or scipy.integrate.quad compute integrals numerically when no formula is available (Numerical Integration).

To go further: Riemann Sums, The Definite Integral, and in depth Areas and Distances and The Fundamental Theorem of Calculus.

Exercises

Exercise 1: A rectangle sum by hand

Estimate ∫04x dx\int_0^4 x\,dx with 4 right-endpoint rectangles. What is the exact value?

Answer

Estimate 10; exact 8.

Solution
Δx=1, right endpoints 1,2,3,4:(1+2+3+4)⋅1=10Exact: the region is a triangle with base 4 and height 4: 12⋅4⋅4=8\begin{aligned} &\Delta x = 1,\ \text{right endpoints } 1, 2, 3, 4{:}\quad (1 + 2 + 3 + 4) \cdot 1 = 10 \\ &\text{Exact: the region is a triangle with base 4 and height 4: } \tfrac{1}{2} \cdot 4 \cdot 4 = 8 \end{aligned}

Exercise 2: Distance from a speed table

A runner's speed in m/s, read every 10 s, is 3, 4, 4, 5, 6, 5 (at t=0,10,…,50t = 0, 10, \ldots, 50). Estimate the distance covered in the first 50 s using left endpoints.

Answer

220 m

Solution
Left endpoints use the speeds at t=0,10,20,30,40:(3+4+4+5+6)⋅10=22⋅10=220 m\begin{aligned} &\text{Left endpoints use the speeds at } t = 0, 10, 20, 30, 40{:} \\ &(3 + 4 + 4 + 5 + 6) \cdot 10 = 22 \cdot 10 = 220\ \text{m} \end{aligned}

Exercise 3: Code the midpoint rule

Use a midpoint Riemann sum with n=1000n = 1000 to estimate ∫0πsin⁡x dx\int_0^\pi \sin x\,dx. Guess the exact value from your result.

Answer

About 2.0000008; the exact value is 2.

Solution
python · try it yourself
import math

n = 1000
dx = math.pi / n
total = sum(math.sin((i + 0.5) * dx) for i in range(n)) * dx
print(f"{total:.7f}")
2.0000008

Exercise 4: Signed area

What is ∫−11x3 dx\int_{-1}^1 x^3\,dx, and why? What is the total (unsigned) area between x3x^3 and the xx-axis on that interval?

Answer

The integral is 0; the unsigned area is 12\frac{1}{2}.

Solution
x3 is odd: the area below the axis on [−1,0] cancels the area above on [0,1].Area above on [0,1]: x44 from 0 to 1=14, so total unsigned area=2⋅14=12.\begin{aligned} &x^3 \text{ is odd: the area below the axis on } [-1, 0] \text{ cancels the area above on } [0, 1]. \\ &\text{Area above on } [0, 1]{:}\ \tfrac{x^4}{4} \text{ from 0 to 1} = \tfrac{1}{4}, \text{ so total unsigned area} = 2 \cdot \tfrac{1}{4} = \tfrac{1}{2}. \end{aligned}

Test yourself

What does the definite integral of a velocity function over a time interval give?

  1. The acceleration
  2. The average velocity
  3. The displacement (change in position)
  4. The maximum speed

ff is increasing on [a,b][a, b]. How does a left Riemann sum compare with the exact integral?

  1. It is too small
  2. It is too big
  3. It is exact
  4. It depends on nn only

A game updates position += velocity * dt every frame. Mathematically it is computing…

  1. a derivative
  2. a Riemann sum approximating an integral
  3. a limit at infinity
  4. a geometric series