s LearnSqueak.com

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.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Pair nodes with values
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Replace the last y by 6; the last comparison becomes false.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
(2 - 1) / (1 - 0)

Expected: 1

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Compute a first divided difference
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Between the next two samples, (5-2)/(2-1) is 3.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
(3 - 1) / (2 - 0)

Expected: 1

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Take the difference of the slopes
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Use samples of a straight line. All second divided differences are zero.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Build the triangular table in one array
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Updating left to right would overwrite a value before its neighbor has used it.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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: 3

Expected: 10

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Evaluate by nested multiplication
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: At x=0, x=1 and x=2 the results must be 1, 2 and 5.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Keep rational coefficients exact
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Evaluate at x=1: the result is 5/4.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Verify the full node contract
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Change the test point to 4. The cubic value is 62.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Reject bad nodes and preserve the samples
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: The evaluation block is intentionally internal: pass it the matching nodes and coefficients produced by the constructor.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Newton interpolation: fit a polynomial through exact samples native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

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.