SMALLTALK LAB
Newton interpolation: fit a polynomial through exact samples
Build divided differences and evaluate Newton-form polynomials in Squeak, retaining exact fractions and checking that every original sample is reproduced.
Before you begin
You need ordered Arrays, blocks and arithmetic. Unlike root finding, interpolation starts with measured or chosen sample pairs and constructs a polynomial through them. Distinct x values are required. This lesson uses exact small integers and Fractions, not a general-purpose numerical fitting package.
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 - Pair nodes with values
These samples come from x squared plus one. They let us check the implementation against an independent known formula rather than only against itself.
| xs ys |
xs := #(0 1 2). ys := #(1 2 5).
(1 to: xs size) collect: [:i |
(ys at: i) = ((xs at: i) squared + 1)]Expected: #(true true true)

Try it: Replace the last y by 6; the last comparison becomes false.
02 - Compute a first divided difference
The slope between two points is change in y divided by change in x. For the first two samples it is one.
(2 - 1) / (1 - 0)Expected: 1

Try it: Between the next two samples, (5-2)/(2-1) is 3.
03 - Take the difference of the slopes
A second divided difference compares the neighboring slopes over the full span of their three x nodes. The coefficients here are 1, 1 and 1.
(3 - 1) / (2 - 0)Expected: 1

Try it: Use samples of a straight line. All second divided differences are zero.
04 - Build the triangular table in one array
Start with y values. At each order update right to left, so the left neighbor still belongs to the previous order. The surviving array entries are the Newton coefficients.
| coefficients |
coefficients := [:nodes :samples |
| work n |
n := nodes size.
(n = 0 or: [n ~= samples size or: [nodes asSet size ~= n]])
ifTrue: [Error signal: 'Need matching values at distinct nodes'].
work := samples copy.
1 to: n - 1 do: [:order |
n to: order + 1 by: -1 do: [:i |
work at: i put: (((work at: i) - (work at: i - 1)) /
((nodes at: i) - (nodes at: i - order))) ]].
work].
coefficients value: #(0 1 2) value: #(1 2 5)Expected: #(1 1 1)

Try it: Updating left to right would overwrite a value before its neighbor has used it.
05 - Evaluate by nested multiplication
The Newton form is 1 + (x-0)*(1 + (x-1)*1). Work backward through the coefficients. This is similar to Horner evaluation, but each factor uses its own sample node.
| evaluate |
evaluate := [:knots :terms :atX |
| answer |
answer := terms last.
terms size - 1 to: 1 by: -1 do: [:i |
answer := (terms at: i) + ((atX - (knots at: i)) * answer)].
answer].
evaluate value: #(0 1 2) value: #(1 1 1) value: 3Expected: 10

Try it: At x=0, x=1 and x=2 the results must be 1, 2 and 5.
06 - Keep rational coefficients exact
Nodes need not be consecutive or sorted. The first divided difference here is 1/2. Exact arithmetic lets this small example avoid rounding entirely.
| coefficients evaluate xs c |
coefficients := [:nodes :samples |
| work n |
n := nodes size.
(n = 0 or: [n ~= samples size or: [nodes asSet size ~= n]])
ifTrue: [Error signal: 'Need matching values at distinct nodes'].
work := samples copy.
1 to: n - 1 do: [:order |
n to: order + 1 by: -1 do: [:i |
work at: i put: (((work at: i) - (work at: i - 1)) /
((nodes at: i) - (nodes at: i - order))) ]].
work].
evaluate := [:knots :terms :atX |
| answer |
answer := terms last.
terms size - 1 to: 1 by: -1 do: [:i |
answer := (terms at: i) + ((atX - (knots at: i)) * answer)].
answer].
xs := #(0 2 4).
c := coefficients value: xs value: #(1 2 5).
{c. evaluate value: xs value: c value: 3}Expected: {{1. 1/2. 1/4}. 13/4}

Try it: Evaluate at x=1: the result is 5/4.
07 - Verify the full node contract
Use a cubic with nonuniform nodes. Verify every supplied sample and a new point against the independent cubic formula. A single sample should produce a constant polynomial.
| coefficients evaluate xs ys c |
coefficients := [:nodes :samples |
| work n |
n := nodes size.
(n = 0 or: [n ~= samples size or: [nodes asSet size ~= n]])
ifTrue: [Error signal: 'Need matching values at distinct nodes'].
work := samples copy.
1 to: n - 1 do: [:order |
n to: order + 1 by: -1 do: [:i |
work at: i put: (((work at: i) - (work at: i - 1)) /
((nodes at: i) - (nodes at: i - order))) ]].
work].
evaluate := [:knots :terms :atX |
| answer |
answer := terms last.
terms size - 1 to: 1 by: -1 do: [:i |
answer := (terms at: i) + ((atX - (knots at: i)) * answer)].
answer].
xs := #(-2 0 1 3). ys := xs collect: [:x | (x*x*x) - x + 2].
c := coefficients value: xs value: ys.
{(1 to: xs size) allSatisfy: [:i |
(evaluate value: xs value: c value: (xs at: i)) = (ys at: i)].
(evaluate value: xs value: c value: 2) = 8.
evaluate value: #(5) value: #(7) value: 99}Expected: #(true true 7)

Try it: Change the test point to 4. The cubic value is 62.
08 - Reject bad nodes and preserve the samples
Repeated x values would divide by zero. Mismatched arrays and an empty input have no meaning here. Copying the y array ensures coefficient construction leaves the caller data intact.
| coefficients ys checks |
coefficients := [:nodes :samples |
| work n |
n := nodes size.
(n = 0 or: [n ~= samples size or: [nodes asSet size ~= n]])
ifTrue: [Error signal: 'Need matching values at distinct nodes'].
work := samples copy.
1 to: n - 1 do: [:order |
n to: order + 1 by: -1 do: [:i |
work at: i put: (((work at: i) - (work at: i - 1)) /
((nodes at: i) - (nodes at: i - order))) ]].
work].
ys := #(1 2 5) copy.
coefficients value: #(0 1 2) value: ys.
checks := {ys = #(1 2 5).
([coefficients value: #(1 1) value: #(2 3). false]
on: Error do: [:e | true]).
([coefficients value: #() value: #(). false]
on: Error do: [:e | true]).
([coefficients value: #(1 2) value: #(3). false]
on: Error do: [:e | true])}.
checks allSatisfy: [:each | each]Expected: true

Try it: The evaluation block is intentionally internal: pass it the matching nodes and coefficients produced by the constructor.
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
Construction takes O(n squared) arithmetic operations and O(n) extra storage. Each evaluation uses O(n) operations and constant extra storage. Exact Fraction numerators can grow; these are operation counts, not constant-time big-integer costs.
Interpolation passes through every sample; regression instead balances errors across samples. Do not interpret a perfect interpolation fit as evidence that an underlying process is polynomial.
High-degree interpolation and extrapolation can behave badly, especially with rounded measurements. This learning implementation has no numerical-conditioning safeguards. Prefer a suitable numerical method for scientific production work.
Three practice challenges
Straight line
Build coefficients for x=#(0 2 4), y=#(3 7 11).
Show solution
They are #(3 2 0). Evaluating at 5 gives 13.
Change order
Reverse both node and value arrays. Does the polynomial change?
Show solution
The coefficients generally change, but the interpolating polynomial and evaluated results stay the same in exact arithmetic.
One sample
Why does the backward loop do nothing for one coefficient?
Show solution
The loop starts at zero and stops at one with a negative step, so it has no iterations. The sole y value is returned as a constant.
Continue learning
Horner Polynomials | Fractions | Bisection
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Windows.