SMALLTALK LAB
Trapezoidal integration: add up an area
Approximate an integral with straight-edged panels, keep exact Fraction arithmetic, and measure convergence with tested Squeak examples.
Before you begin
Use blocks, loops and ordinary arithmetic. We approximate the signed area under a function by joining sampled heights with straight lines. Begin with x squared on the interval zero to one.
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 - Find one trapezoid area
For a panel of width h and endpoint heights left and right, area = h * (left + right) / 2. The average height avoids choosing either endpoint as the whole panel height.
| h left right |
h := 1. left := 0. right := 1.
h * (left + right) / 2Expected: 1/2

Try it: For equal heights 3 and width 2, the result is the rectangle area 6.
02 - Evaluate a function block
Represent x squared with a block accepting x. value: supplies the sample location. Exact integer division in Squeak keeps one half and one quarter as Fractions.
| f |
f := [:x | x*x].
{f value: 0. f value: 1/2. f value: 1}Expected: {0. 1/4. 1.}

Try it: Try x = 1/4; the squared height is 1/16.
03 - Sum two panels explicitly
Split zero to one at one half. The middle sample contributes to both panels, whereas each outer sample contributes to just one. This explains the half weights at the two ends.
| h f |
h := 1/2. f := [:x | x*x].
(h * ((f value: 0)+(f value: 1/2))/2)
+ (h * ((f value: 1/2)+(f value: 1))/2)Expected: 3/8

Try it: Compare 3/8 with the exact integral 1/3: this convex curve is overestimated.
04 - Use the weighted-sum form
Compute n equally wide panels using endpoint half weights and full weights on interior samples. There are n-1 interior samples. Parentheses keep the sample position inside the value: message.
| f n h total |
f := [:x | x*x]. n := 4. h := 1/n.
total := ((f value: 0)+(f value: 1))/2.
1 to: n-1 do: [:i | total := total+(f value: i*h)].
h*totalExpected: 11/32

Try it: Check that n = 1 performs no interior iterations.
05 - Generalize the interval
Use h = (b-a)/n and sample a+i*h. The block rejects zero, negative and fractional panel counts. Reversing the interval intentionally reverses the sign of the answer.
| integrate |
integrate := [:f :a :b :n | | h total |
(n isInteger and: [n > 0])
ifFalse: [Error signal: 'Use a positive integer panel count'].
h := (b-a)/n.
total := ((f value: a)+(f value: b))/2.
1 to: n-1 do: [:i | total := total+(f value: a+(i*h))].
h*total].
integrate value: [:x | x*x] value: 0 value: 1 value: 8Expected: 43/128

Try it: Reverse a and b: the approximation becomes -43/128.
06 - Measure the error as panels double
Subtract the known integral 1/3. For this quadratic, halving the panel width divides the error by four. Exact Fractions make the relationship visible without decimal rounding noise.
| integrate |
integrate := [:f :a :b :n | | h total |
(n isInteger and: [n > 0])
ifFalse: [Error signal: 'Use a positive integer panel count'].
h := (b-a)/n.
total := ((f value: a)+(f value: b))/2.
1 to: n-1 do: [:i | total := total+(f value: a+(i*h))].
h*total].
#(1 2 4 8) collect: [:n |
(integrate value: [:x | x*x] value: 0 value: 1 value: n)-(1/3)]Expected: {1/6. 1/24. 1/96. 1/384.}

Try it: Divide each error by the next one: every ratio is 4.
07 - Test a straight line and reversed bounds
Trapezoids are exact for a linear function. The function 2*x+1 has integral 12 from 0 to 3. Check several panel counts and reverse the interval as a second invariant.
| integrate |
integrate := [:f :a :b :n | | h total |
(n isInteger and: [n > 0])
ifFalse: [Error signal: 'Use a positive integer panel count'].
h := (b-a)/n.
total := ((f value: a)+(f value: b))/2.
1 to: n-1 do: [:i | total := total+(f value: a+(i*h))].
h*total].
{(1 to: 12) allSatisfy: [:n |
(integrate value: [:x | 2*x+1] value: 0 value: 3 value: n)=12].
integrate value: [:x | 2*x+1] value: 3 value: 0 value: 3}Expected: {true. -12.}

Try it: Use equal bounds: h is zero and the integral is zero, provided the function is defined there.
08 - Test the invalid panel-count guard
The caller must choose a positive integer count. Catch the specific message in this test to verify the guard instead of mistaking an unrelated arithmetic error for success.
| integrate |
integrate := [:f :a :b :n | | h total |
(n isInteger and: [n > 0])
ifFalse: [Error signal: 'Use a positive integer panel count'].
h := (b-a)/n.
total := ((f value: a)+(f value: b))/2.
1 to: n-1 do: [:i | total := total+(f value: a+(i*h))].
h*total].
[integrate value: [:x | x*x] value: 0 value: 1 value: 0. false]
on: Error do: [:error | error messageText = 'Use a positive integer panel count']Expected: true

Try it: Test -2 and 1/2 as well; both are invalid panel counts.
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
The algorithm evaluates n+1 samples and takes O(n) time with constant auxiliary state. A function with side effects is a poor integrand: evaluate a deterministic numeric function at each point.
The familiar quadratic convergence requires adequate smoothness. More panels alone do not prove accuracy near discontinuities or singularities, and finite sampling can miss narrow features. This example is not an adaptive error estimator.
Exact rational input can produce exact rational arithmetic inside the approximation; it does not make the approximation equal the true integral. Floating-point inputs instead introduce roundoff, which eventually limits improvement.
Three practice challenges
Challenge 1
Predict the quadratic error for 16 panels.
Show solution
For this specific example it is 1/(6*16*16) = 1/1536. Do not assume this formula for a different function.
Challenge 2
Why do the two endpoints receive half weight?
Show solution
An interior sample belongs to two adjacent trapezoids; each endpoint belongs to only one.
Challenge 3
What happens to a constant function f(x)=5 on [2,7]?
Show solution
Every panel is an exact rectangle. The total is 5*(7-2)=25 for any positive panel count.
Continue learning
Fractions | Bisection | Newton Interpolation
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Windows.