5.4.8 · D2Scientific Computing (Python)

Visual walkthrough — SciPy — overview of submodules

1,634 words7 min readBack to topic

Step 1 — What does "area under a curve" even mean?

WHAT. We draw a curve and shade the region trapped between the curve, the horizontal axis, and two vertical walls at (left) and (right). That shaded amount of surface is what the symbol names.

WHY this question. Before we approximate anything, we must agree on what the target number is. "Integral" is not magic notation — it is a shorthand for a quantity of area. If we know the exact area we can grade every approximation later.

PICTURE. Below, on . The plum region is the area we want.

Figure — SciPy — overview of submodules

For the exact answer happens to be — hold onto that; it is our answer key.


Step 2 — One slice, one rectangle: the crudest guess

WHAT. Chop into equal-width pieces. Each piece has width Here is "how wide one slice is": total distance shared among slices. Over one slice we pretend the curve is flat at its left height , giving a rectangle of area .

WHY a rectangle first. A rectangle is the simplest shape whose area we already know (width height). Starting crude lets us see the error we will later fix.

PICTURE. Orange rectangles under . Notice they all fall below the curve — they systematically undershoot. That visible gap is the error we must attack.

Figure — SciPy — overview of submodules

Step 3 — Fix the tilt: use a trapezoid, not a rectangle

WHAT. A rectangle uses ONE height. A trapezoid uses BOTH edge heights, and , joined by a straight slanted top. Its area is width times the average of the two heights:

WHY the average of two heights. The rectangle ignored the slope; the trapezoid respects it. By following the straight line between the two curve points, the top hugs the real curve far more closely, so the leftover sliver of error shrinks dramatically.

Why THIS tool and not something fancier yet? A trapezoid is the first shape that captures rate of change (slope) without needing calculus. It is the natural one-notch-up from a rectangle — we earn each improvement.

PICTURE. The same slice, rectangle (orange, dashed) vs. trapezoid (teal). The trapezoid's slanted top removes most of the gap.

Figure — SciPy — overview of submodules

Step 4 — Add up all the strips (and see the telescoping)

WHAT. Sum over every strip from to : Every interior point is the right edge of one strip and the left edge of the next, so its height gets counted twice. The two endpoints are counted once. Pull the out and you get the classic form.

WHY simplify. Counting each interior height twice and the ends once is exactly the pattern . Seeing it lets us compute the whole sum in one clean pass over a NumPy array.

PICTURE. A strip of trapezoids side by side; the shared vertical edges (counted twice) are marked in plum, the two lonely endpoints (counted once) in orange.

Figure — SciPy — overview of submodules

Step 5 — Watch the error die as grows

WHAT. Compute the trapezoid estimate of for a few values of and compare to the true . As we double , the error drops by roughly a factor of .

WHY it matters. This is the whole payoff: refinement works. The gap between our staircase of trapezoids and the smooth curve is not stubborn — it vanishes predictably. That "predictable vanishing" is precisely what an adaptive method like quad exploits.

PICTURE. Error (log scale) vs. number of strips . A straight downhill line on log axes means the error follows a power law — here .

Figure — SciPy — overview of submodules

Step 6 — Where trapezoid struggles, and why quad wins

WHAT. The trapezoid rule spreads its work evenly — same everywhere. But real curves are calm in some places and wildly wiggly in others. Wasting strips on the calm parts and starving the wiggly parts is inefficient. quad uses adaptive quadrature: it estimates the local error and adds more sample points only where the curve misbehaves.

WHY a smarter tool. Two reasons quad beats plain trapezoid:

  1. Higher order — instead of straight-line tops it fits little polynomials (Gauss–Kronrod), so each panel is far more accurate.
  2. Adaptive — it refines where the error estimate is large, ignoring the boring regions.

PICTURE. A peaky function: evenly-spaced trapezoid nodes (teal, uniform) vs. quad's adaptive nodes (orange, clustered under the spike).

Figure — SciPy — overview of submodules

The one-picture summary

Figure — SciPy — overview of submodules

This single figure compresses the whole walkthrough: the true plum area, the crude orange rectangles that undershoot, the teal trapezoids that hug the curve, and the note that quad refines adaptively to reach the exact value .

Recall Feynman retelling — say it like a story

We wanted the amount of paint filling the space under a curve. Step one, we agreed that amount is the integral. Step two, we lazily approximated it with flat-topped boxes — they all came up short because the curve slopes up. Step three, we tilted each box's top into a slanted trapezoid, and most of the missing sliver vanished. Step four, we lined all the trapezoids up and noticed the shared inner walls get counted twice while the two outer walls count once — that's the half-one-one-half pattern. Step five, we made the strips thinner and watched the error crash by a factor of four each time we doubled the count. Step six, we admitted uniform strips are wasteful on peaky curves, and that's exactly the job quad does better: it fits little curves instead of straight lines and sprinkles extra points only where the function is wild. So when you type integrate.quad(f, a, b), picture this whole parade of shapes marching toward the true area — and remember it hands you the answer and an honesty rating (the error bound).

Recall Quick self-test

What geometric shape upgrades a rectangle by respecting slope? ::: A trapezoid (uses both edge heights). Why are interior heights weighted 1 but endpoints weighted 1/2? ::: Each interior point is shared by two strips (counted twice); the two ends belong to one strip only. As doubles for the trapezoid rule, roughly how does the error change? ::: It drops by about a factor of 4 (error ). Two ways quad beats plain trapezoid? ::: Higher-order panels (polynomial fits) and adaptivity (extra samples only where the curve is wiggly). What two numbers does quad return? ::: The area estimate and an absolute error bound.