SMALLTALK LAB
Simpson integration: weighted samples and honest error checks
Build composite Simpson quadrature in Smalltalk, test its cubic exactness, refine a quartic with rational arithmetic, and discover how a sample grid can miss an entire feature.
Before you begin
Use Squeak 6.1 and a Workspace. Each checkpoint is independent: select its entire expression and choose Print it. Know blocks, Arrays and numeric messages. Unlike the trapezoidal lesson, this rule uses parabolic interpolation over pairs of equally spaced subintervals and weights 1, 4, 2, ..., 4, 1. Native Squeak Fractions keep our polynomial fixtures exact.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
01 - Place equally spaced sample points
Integrate x squared from 0 to 1. Four subintervals need five points, including both endpoints. Integer division with / produces exact Fractions in Smalltalk, so no Float roundoff is introduced in this example.
| a b n h points |
a := 0. b := 1. n := 4. h := (b - a) / n.
points := (0 to: n) collect: [:i | a + (i * h)].
{h. points asArray}Expected: {1/4. {0. 1/4. 1/2. 3/4. 1}}

Try it: Increase n to 8. There must be nine points, not eight; h becomes 1/8.
02 - Give the middle of each pair extra weight
Simpson combines pairs of adjacent subintervals. Endpoint weights are 1; internal odd indexes get 4 and internal even indexes get 2. The point index starts at zero, although the returned Array is indexed from one.
| n weights |
n := 4.
weights := (0 to: n) collect: [:i |
(i = 0 or: [i = n]) ifTrue: [1]
ifFalse: [i odd ifTrue: [4] ifFalse: [2]]].
weights asArrayExpected: #(1 4 2 4 1)

Try it: For n = 6 the weights should be #(1 4 2 4 2 4 1). Do not alternate 4 and 2 at the endpoints.
03 - Turn weighted heights into signed area
Evaluate the five squared heights and multiply each by its weight. The weighted sum is 4. Multiply by h/3, not h or h/2: the exact area is 1/3. This is a quadrature rule, not the sum of the displayed bar areas.
| points weights sum h |
points := {0. 1/4. 1/2. 3/4. 1}.
weights := #(1 4 2 4 1). h := 1/4. sum := 0.
1 to: points size do: [:i |
sum := sum + ((weights at: i) * (points at: i) squared)].
{sum. (h / 3) * sum}Expected: {4. 1/3}

Try it: Replace the squared heights with constant height 1. The weighted sum is 12 and the area is 1.
04 - Build a reusable composite rule
Pass the function as a one-argument block. Validate n before dividing or iterating: each Simpson panel spans two equal subintervals, so n must be positive and even. The solver visits n+1 nodes without storing them.
| simpson |
simpson := [:function :a :b :n | | h sum x weight |
(n isInteger and: [n > 0 and: [n even]])
ifFalse: [Error signal: 'Use a positive even subinterval count'].
h := (b - a) / n. sum := 0.
0 to: n do: [:i |
x := a + (i * h).
weight := (i = 0 or: [i = n])
ifTrue: [1] ifFalse: [i odd ifTrue: [4] ifFalse: [2]].
sum := sum + (weight * (function value: x))].
(h / 3) * sum].
simpson value: [:x | x squared] value: 0 value: 1 value: 4Expected: 1/3

Try it: Change the upper limit to 2 with n = 4. The integral of x squared should be 8/3.
05 - Check exactness through degree three
On an equally spaced grid, Simpson integrates constants, linear, quadratic and cubic polynomials exactly in exact arithmetic. Test all four rather than assuming one correct answer proves the implementation. Float inputs may introduce small rounding errors.
| simpson |
simpson := [:function :a :b :n | | h sum x weight |
(n isInteger and: [n > 0 and: [n even]])
ifFalse: [Error signal: 'Use a positive even subinterval count'].
h := (b - a) / n. sum := 0.
0 to: n do: [:i |
x := a + (i * h).
weight := (i = 0 or: [i = n])
ifTrue: [1] ifFalse: [i odd ifTrue: [4] ifFalse: [2]].
sum := sum + (weight * (function value: x))].
(h / 3) * sum].
{simpson value: [:x | 1] value: 0 value: 1 value: 2.
simpson value: [:x | x] value: 0 value: 1 value: 2.
simpson value: [:x | x squared] value: 0 value: 1 value: 2.
simpson value: [:x | x * x * x] value: 0 value: 1 value: 2}Expected: {1. 1/2. 1/3. 1/4}

Try it: Try the cubic on -1 to 1. Its positive and negative signed areas cancel to zero.
06 - Measure an error that refinement can reduce
For x to the fourth on 0 to 1, the exact integral is 1/5. Simpson is not exact for this quartic. Compare n = 2, 4 and 8 using exact arithmetic; doubling n divides the error by 16 for this fixture.
| simpson answers errors |
simpson := [:function :a :b :n | | h sum x weight |
(n isInteger and: [n > 0 and: [n even]])
ifFalse: [Error signal: 'Use a positive even subinterval count'].
h := (b - a) / n. sum := 0.
0 to: n do: [:i |
x := a + (i * h).
weight := (i = 0 or: [i = n])
ifTrue: [1] ifFalse: [i odd ifTrue: [4] ifFalse: [2]].
sum := sum + (weight * (function value: x))].
(h / 3) * sum].
answers := #(2 4 8) collect: [:n |
simpson value: [:x | x squared squared] value: 0 value: 1 value: n].
errors := answers collect: [:answer | answer - (1/5)].
{answers. errors. errors first / errors second}Expected: {{5/24. 77/384. 1229/6144}. {1/120. 1/1920. 1/30720}. 16}

Try it: Predict the error at n = 16: 1/491520. A refinement ratio is evidence for this smooth fixture, not a guarantee for every function.
07 - Respect orientation and reject invalid counts
Swapping bounds reverses the signed integral. Equal bounds yield zero for a defined function. Invalid subinterval counts are caught deliberately so this whole expression still returns a value; checking the expected error text avoids accepting an unrelated failure.
| simpson reversed zero rejected |
simpson := [:function :a :b :n | | h sum x weight |
(n isInteger and: [n > 0 and: [n even]])
ifFalse: [Error signal: 'Use a positive even subinterval count'].
h := (b - a) / n. sum := 0.
0 to: n do: [:i |
x := a + (i * h).
weight := (i = 0 or: [i = n])
ifTrue: [1] ifFalse: [i odd ifTrue: [4] ifFalse: [2]].
sum := sum + (weight * (function value: x))].
(h / 3) * sum].
reversed := simpson value: [:x | x squared] value: 1 value: 0 value: 4.
zero := simpson value: [:x | x squared] value: 2 value: 2 value: 4.
rejected := #(0 -2 3 2.5) collect: [:n |
[simpson value: [:x | x] value: 0 value: 1 value: n. false]
on: Error do: [:e | e messageText = 'Use a positive even subinterval count']].
{reversed. zero. rejected}Expected: {-1/3. 0. #(true true true true)}

Try it: Equal bounds do not skip function evaluation in this implementation. A function undefined at that point can still raise an error.
08 - Expose a misleadingly reassuring grid
An integrator only sees its sample values. This polynomial vanishes at every node of the n = 2 grid, so that grid reports zero even though the function is positive between nodes. A finer grid reveals nonzero area. This is why agreement on a few nodes cannot certify an arbitrary integrand.
| simpson hidden coarse fine |
simpson := [:function :a :b :n | | h sum x weight |
(n isInteger and: [n > 0 and: [n even]])
ifFalse: [Error signal: 'Use a positive even subinterval count'].
h := (b - a) / n. sum := 0.
0 to: n do: [:i |
x := a + (i * h).
weight := (i = 0 or: [i = n])
ifTrue: [1] ifFalse: [i odd ifTrue: [4] ifFalse: [2]].
sum := sum + (weight * (function value: x))].
(h / 3) * sum].
hidden := [:x | (x * (x - (1/2)) * (x - 1)) squared].
coarse := simpson value: hidden value: 0 value: 1 value: 2.
fine := simpson value: hidden value: 0 value: 1 value: 4.
{coarse. fine. fine > coarse}Expected: {0. 3/2048. true}

Try it: The exact integral of this polynomial is 1/840. A finer result can expose a missed feature without being exact or furnishing a certified error bound.
Watch the computed states

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.
Reason about correctness and limits
For sufficiently smooth functions on a fixed interval, composite Simpson error is O(h to the fourth), with a bound involving a bound on the fourth derivative. That assumption matters: discontinuities, singularities and unresolved narrow features can defeat the usual refinement pattern. No certified error estimator is implemented here.
A positive even n counts subintervals, not sample points. There are n+1 function evaluations and n/2 parabolic panels. The running-sum implementation uses O(n) evaluations and constant auxiliary storage; exact Fraction numerator and denominator sizes can still grow.
Use equally spaced nodes with this formula. Irregular measured data needs a different integration method or an explicitly justified resampling step. Do not apply the same alternating weights to arbitrary x coordinates.
The result is a signed integral, not necessarily geometric area. Reversed bounds negate it; negative function values contribute negatively. The solver assumes numeric endpoints and a defined numeric function at every sampled node. Its n check is not a general untrusted-input parser.
Three practice challenges
Challenge 1: integrate a cubic
Use n = 2 to integrate x cubed from 0 to 2. Predict the answer before running.
Show solution
The exact answer is 4. Simpson is exact for this cubic in exact arithmetic; n = 4 and n = 8 give the same result.
Challenge 2: compare effort and improvement
For x to the fourth on 0 to 1, how many function evaluations does n = 16 need, and how does its error compare with n = 8?
Show solution
It evaluates 17 points. The error is 1/491520, one sixteenth of 1/30720. Doubling n roughly doubles evaluation work while this smooth example gains a factor of 16 in accuracy.
Challenge 3: zero samples are not zero area
Why does the hidden polynomial in checkpoint 8 produce zero on the coarse grid, and what should you conclude from the finer answer?
Show solution
The coarse nodes are 0, 1/2 and 1, all roots. The finer grid adds 1/4 and 3/4, where the squared polynomial is positive. Its 3/2048 estimate disproves the coarse conclusion but is not the exact 1/840 integral. Try more even n and compare with that known reference.
Continue learning
Trapezoidal Integration | Newton Interpolation | Fractions
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.