5.4.24 · D1Scientific Computing (Python)

Foundations — Implementing numerical integration from scratch — trapezoidal, Simpson's

1,754 words8 min readBack to topic

Before you can read a single formula on the parent note, you must own every symbol in it. Below, each piece is built from zero: plain words first, then a picture, then why the topic needs it. Read top to bottom — each item uses only things defined above it.


1. The curve — a height machine

Picture. Slide left–right along the horizontal axis (that's choosing ); the curve tells you how tall the hill is right there.

Why the topic needs it. The whole game is measuring the area under this height curve. No height machine → nothing to measure.

Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's

2. , and the interval — where we start and stop

Picture. Two vertical fence-posts planted at and . We only care about the slice of hill between the fences.

Why the topic needs it. Area is meaningless without a region. is that region on the ground.


3. The integral sign — "the exact area"

Picture. Fill the region between the fences and under the curve with paint. is the amount of paint — the shaded area.

Why the topic needs it. This is the exact answer we are chasing. Every method on the parent page is an approximation of this one quantity.


4. Splitting the interval: , , and the nodes

We cannot handle infinitely many slivers, so we chop into a finite number of chunks.

Picture. A ruler laid from to , ticked at equal spacing . Each tick is a node ; the number under the tick is its index .

Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's

Why the topic needs it. is the width factor in every area formula; the are the only places we ever ask for a height.


5. Subscripts , , — naming the samples

Picture. At each ruler tick, draw a vertical stick up to the curve. Its length is . These sticks are the only numbers the methods ever plug in.

Why the topic needs it. Both rules are recipes built purely from the numbers — nothing else.


6. Sigma notation — "add these all up"

Picture. A conveyor belt: it feeds one at a time, each time dropping into a running total.

Why the topic needs it. A trapezoidal or Simpson answer is a weighted sum . Sigma is the compact way to write "add up all the sampled heights."


7. Weights — some heights count more

Picture. Colour every node by how many shapes lean on it: endpoints (one shape) one colour, shared interior nodes (two shapes) another.

Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's

Why the topic needs it. Mis-weighting endpoints is the parent note's "#1 silent bug." Understanding why weights differ makes that bug impossible.


8. Polynomial degree — how "bendy" a fit can be

Picture. Line = ruler edge. Parabola = a single smooth valley or hill. Cubic = a gentle S.

Why the topic needs it. Trapezoid fits a line (degree 1) to each slice; Simpson fits a parabola (degree 2) to each pair. "Exact for degree-1 / degree-3" on the parent note is a statement about which curves these shapes reproduce perfectly. See Polynomial interpolation and Newton-Cotes formulas.


9. Big-O notation , — how fast error shrinks

Picture. Two shrinking staircases: halving steps the error down one small stair, but sends the error plunging four stairs. Same effort, Simpson wins.

Why the topic needs it. It explains the parent's punchline: Simpson beats trapezoid dramatically for the same number of samples. The powers themselves come from Taylor series.


10. The symbol — "approximately equal"

Why the topic needs it. Honesty. Every method is an estimate; keeps that front and centre.


How these feed the topic

Function f x height machine

Integral = exact area

Limits a and b interval

Approximate with shapes

n pieces and step h

Nodes x i

Sampled heights f i

Sigma sum notation

Weighted sum sum wi fi

Weights wi shared vs edge

Polynomial degree line vs parabola

Trapezoidal rule

Simpsons rule

Big O error order

Once every box above feels obvious, the parent page — the main topic — reads as pure bookkeeping.


Equipment checklist

Test yourself: read the left side, say the answer aloud, then reveal.

What does mean in plain words?
The exact area under the curve between and .
What is and how is it computed?
The width of each subinterval; .
If is the number of subintervals, how many nodes (points) are there?
.
What does the node formula give you?
The position of the -th cut point along .
Read out loud.
The sum of the curve's heights at every interior node from to .
Why do interior nodes get a bigger weight than endpoints?
They are shared by two neighbouring shapes, so their height counts in both.
What shape (degree) does trapezoid fit per slice? Simpson per pair?
Trapezoid: a straight line (degree 1). Simpson: a parabola (degree 2).
If you halve , how much does an error shrink? An error?
To about a quarter; to about a sixteenth.
What does signal in ?
The result is an approximation, not exact, unless is a low-degree polynomial.
Why does the interior-node loop use range(1, n) not range(1, n+1)?
Indices to are the interior nodes; is the endpoint, handled separately.