s LearnSqueak.com

SMALLTALK LAB

Newton roots: improve a guess with a tangent

Build a bounded Newton iteration for square roots, watch the residual shrink, and understand when a fast numerical method can fail.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Know arithmetic, blocks and whileTrue:. This lesson solves x squared = a for a nonnegative real target. Unlike bisection, Newton uses a derivative and does not maintain a bracketing interval.

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 - Measure how wrong the guess is

For target two, f(x)=x*x-2 measures the signed residual. A residual of zero means the equation is satisfied; it is not the same quantity as distance from the true root.

Independent Workspace expression - select all, then Print it
| x |
x := 2.0.
x*x-2

Expected: 2.0

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Measure how wrong the guess is
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Try x=1.0: the residual changes sign.

Download this checkpoint

02 - Use the slope at the current guess

The derivative of x squared minus a is 2*x. Dividing residual by slope estimates how far to move along the tangent to reach zero. Parentheses make the intended arithmetic explicit.

Independent Workspace expression - select all, then Print it
| x residual slope |
x := 2.0. residual := x*x-2. slope := 2*x.
x-(residual/slope)

Expected: 1.5

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Use the slope at the current guess
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Check that the tangent correction is 0.5.

Download this checkpoint

03 - Simplify the update

For square roots, x-f(x)/f-prime(x) simplifies to (x+a/x)/2. Both forms agree here. Floating-point division is deliberate: exact Fractions would grow rapidly.

Independent Workspace expression - select all, then Print it
| x a |
x := 2.0. a := 2.
((x+(a/x))/2) = (x-((x*x-a)/(2*x)))

Expected: true

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

Try it: Use a=9 and x=4.0 to obtain 3.125.

Download this checkpoint

04 - Repeat a fixed number of improvements

Record the starting guess and three updates. This is a trace, not yet a stopping rule. The error falls quickly near this simple positive root.

Independent Workspace expression - select all, then Print it
| x values |
x := 2.0. values := OrderedCollection with: x.
3 timesRepeat: [x := (x+(2/x))/2. values add: x].
values collect: [:v | (v*1000000) rounded]

Expected: {2000000. 1500000. 1416667. 1414216.} asOrderedCollection

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Repeat a fixed number of improvements
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: The scaled integers make a stable readable trace; x still stores the full Float.

Download this checkpoint

05 - Stop by residual, with a hard limit

The block validates its starting conditions, tests residual before each update, and caps work at fifty iterations. A limit is essential for numerical algorithms: do not assume every input converges.

Independent Workspace expression - select all, then Print it
| root |
root := [:target :start | | x next iterations |
 target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
 start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
 x := start asFloat. iterations := 0.
 [(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
  next := (x+(target/x))/2.0.
  x := next. iterations := iterations+1].
 (x*x-target) abs > 0.00000001
  ifTrue: [Error signal: 'Iteration limit reached'].
 x].
((root value: 2 value: 2.0) squared-2) abs < 0.00000001

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Stop by residual, with a hard limit
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Try targets 3, 9 and 25 with a positive guess.

Download this checkpoint

06 - Compare against an independent square root

Use Squeak’s built-in sqrt as a check across positive targets. A residual threshold is our solver contract; a separate answer-distance check helps catch mistakes in the update.

Independent Workspace expression - select all, then Print it
| root |
root := [:target :start | | x next iterations |
 target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
 start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
 x := start asFloat. iterations := 0.
 [(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
  next := (x+(target/x))/2.0.
  x := next. iterations := iterations+1].
 (x*x-target) abs > 0.00000001
  ifTrue: [Error signal: 'Iteration limit reached'].
 x].
#(1 2 3 9 25 100) allSatisfy: [:a |
 ((root value: a value: 1.0)-a sqrt) abs < 0.000001]

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Compare against an independent square root
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Starting at one takes more iterations for a large target but still works for this test set.

Download this checkpoint

07 - Handle the zero target carefully

Zero is a repeated root: the derivative also vanishes at the exact solution. A positive guess avoids division by zero; each update halves it. Residual tolerance therefore permits a small positive answer.

Independent Workspace expression - select all, then Print it
| root answer |
root := [:target :start | | x next iterations |
 target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
 start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
 x := start asFloat. iterations := 0.
 [(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
  next := (x+(target/x))/2.0.
  x := next. iterations := iterations+1].
 (x*x-target) abs > 0.00000001
  ifTrue: [Error signal: 'Iteration limit reached'].
 x].
answer := root value: 0 value: 1.0.
{answer > 0. answer squared <= 0.00000001}

Expected: {true. true.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Handle the zero target carefully
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: A tiny squared residual does not imply the same tiny answer error near zero.

Download this checkpoint

08 - Reject a zero starting guess

An unguarded update would divide by zero. Test the precise validation message rather than accepting any exception as evidence of correct behavior.

Independent Workspace expression - select all, then Print it
| root |
root := [:target :start | | x next iterations |
 target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
 start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
 x := start asFloat. iterations := 0.
 [(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
  next := (x+(target/x))/2.0.
  x := next. iterations := iterations+1].
 (x*x-target) abs > 0.00000001
  ifTrue: [Error signal: 'Iteration limit reached'].
 x].
[root value: 2 value: 0. false]
 on: Error do: [:e | e messageText = 'Positive starting guess required']

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Reject a zero starting guess
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Negative targets are outside this real-valued solver’s contract.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Newton roots: improve a guess with a tangent 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

This is a square-root-specific Newton solver, not a universal root finder. General Newton steps can diverge, oscillate or meet zero derivatives. Bisection is slower but retains a guarantee when a valid continuous sign-changing bracket is maintained.

The residual tolerance is absolute and chosen for small teaching inputs. For widely varying scales use suitable relative/absolute tolerances and finite-value checks. Very large Float inputs may overflow; no robust industrial numerical-library claim is made.

Quadratic local convergence applies near simple roots under suitable smoothness assumptions. The zero target has a multiple root and behaves differently. The fifty-step cap bounds work but cannot guarantee success for every magnitude.

Three practice challenges

Challenge 1

Why must the starting guess be positive here?

Show solution

It prevents the first division by zero and keeps this square-root iteration on the positive branch.

Challenge 2

What happens for target zero?

Show solution

The update becomes x/2, so the guess approaches zero linearly rather than the typical simple-root quadratic behavior.

Challenge 3

How would you expose convergence information?

Show solution

Return the answer together with iteration count and residual, or signal a clear failure when the work limit is exhausted.

Continue learning

Bisection | 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.