s LearnSqueak.com

SMALLTALK LAB

Runge–Kutta in Squeak: Four Slopes per Step

Approximate growth when its rate depends on the current value. Compare Euler with RK4 and verify one step using exact fractions.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.

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.

Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - Read the slope

Our differential equation says the current slope is equal to the current value. Time is an argument so other equations can use it.

Independent Workspace expression - select all, then Print it
| rate |
rate := [:t :y | y].
{rate value: 0 value: 1. rate value: 0 value: 2}

Expected: #(1 2)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Read the slope
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Double the current value and see that the slope doubles.

Download this checkpoint

02 - Take one Euler step

Euler extrapolates the slope at the beginning across the entire half-step. For growth, that misses the fact that the slope increases between endpoints.

Independent Workspace expression - select all, then Print it
| y h slope |
y := 1. h := 1/2. slope := y.
y + (h * slope)

Expected: 3/2

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Take one Euler step
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try another half-step from this result; Euler gives 9/4 at time one.

Download this checkpoint

03 - Probe a midpoint

Move halfway in time, predict a new state using the initial slope, and ask the rate function about that state. Here f(t,y) = y, so the midpoint state also equals its slope.

Independent Workspace expression - select all, then Print it
| y h k1 k2 |
y := 1. h := 1/2.
k1 := y.
k2 := y + ((h / 2) * k1).
{k1. k2}

Expected: {1. 5/4}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Probe a midpoint
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The half-step is 1/4, not h itself.

Download this checkpoint

04 - Combine four slopes

RK4 predicts two midpoint states and one end state. The two midpoint slopes each receive double weight; the total weight is six. Each prediction is made from the original y.

Independent Workspace expression - select all, then Print it
| step f |
f := [:t :y | y].
step := [:t :y :h | | half k1 k2 k3 k4 |
  half := h / 2.
  k1 := f value: t value: y.
  k2 := f value: t + half value: y + (half * k1).
  k3 := f value: t + half value: y + (half * k2).
  k4 := f value: t + h value: y + (h * k3).
  y + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4))].
step value: 0 value: 1 value: 1/2

Expected: 211/128

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Combine four slopes
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The exact fraction is the computed approximation, not the exact true growth.

Download this checkpoint

05 - Compare step sizes at the same time

Two half-steps use the first answer as the next starting value. Both results estimate e at time one. The second, about 2.71735, is closer to e ≈ 2.71828 than 65/24 ≈ 2.70833.

Independent Workspace expression - select all, then Print it
| step f |
f := [:t :y | y].
step := [:t :y :h | | half k1 k2 k3 k4 |
  half := h / 2.
  k1 := f value: t value: y.
  k2 := f value: t + half value: y + (half * k1).
  k3 := f value: t + half value: y + (half * k2).
  k4 := f value: t + h value: y + (h * k3).
  y + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4))].
{step value: 0 value: 1 value: 1.
 step value: 1/2 value: (step value: 0 value: 1 value: 1/2) value: 1/2}

Expected: {65/24. 44521/16384}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Compare step sizes at the same time
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Compare both to 1.0 exp in a separate Workspace expression.

Download this checkpoint

06 - Verify a zero-length step

When h is zero, the weighted slope contribution vanishes. A zero-length step leaves the state unchanged, a useful sanity check independent of the solution formula.

Independent Workspace expression - select all, then Print it
| step f |
f := [:t :y | y].
step := [:t :y :h | | half k1 k2 k3 k4 |
  half := h / 2.
  k1 := f value: t value: y.
  k2 := f value: t + half value: y + (half * k1).
  k3 := f value: t + half value: y + (half * k2).
  k4 := f value: t + h value: y + (h * k3).
  y + ((h / 6) * (k1 + (2 * k2) + (2 * k3) + k4))].
step value: 5 value: 7 value: 0

Expected: 7

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Verify a zero-length step
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: If you turn this into a time loop, reject a zero step size so time can advance.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Runge–Kutta in Squeak: Four Slopes per Step native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The complete static checkpoints appear above.

Reason about correctness and limits

Fixed-step RK4 uses four evaluations of the rate per step. A smaller step can improve accuracy on this smooth example but costs more evaluations.

Exact fractions are helpful in this small lesson but can grow expensive in a long run. RK4 is not a guarantee of stability for stiff equations.

Three practice challenges

Constant slope

What should a half-step from y=3 give if the rate is always 2?

Show solution

4. Each k is 2, so their weighted mean is 2.

End time

In the second of two steps of length 1/2, which time is given to k4?

Show solution

1, because the second step starts at time 1/2 and advances by another 1/2.

Check the first step

Which expected value changes if f is replaced by a zero slope?

Show solution

Euler and RK4 would both keep the starting state; the RK4 result at time 1/2 would become 1.

Continue learning

Newton Roots | Trapezoidal Integration | Simpson Integration

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.