Newton-Raphson Square Roots
The Newton-Raphson iteration
The Newton-Raphson method computes square roots. Given , the iteration is:
In OCaml:
let next a x = (a /. x +. x) /. 2.0
(* val next : float -> float -> float = <fun> *)
Convergence on infinite sequences
iterates (next a) x₀ generates the infinite series of approximations. For with :
| Iteration | Approximation |
|---|---|
| 1.0 | |
| 1.5 | |
| 1.41667… | |
| 1.4142157… | |
| 1.414213562… |
The last value is accurate to 10 significant digits. Convergence is quadratic: the number of correct digits roughly doubles each iteration.
within: stopping criterion
let rec within eps = function
| Cons (x, xf) ->
match xf () with
| Cons (y, yf) ->
if abs_float (x -. y) <= eps then y
else within eps (Cons (y, yf))
(* val within : float -> float seq -> float = <fun> *)
within eps searches the lazy list for two consecutive approximations whose absolute difference is at most eps. When found, it returns the second (more accurate) value.
root: putting it together
let root a = within 1e-6 (iterates (next a) 1.0)
(* val root : float -> float = <fun> *)
root 2.0
(* : float = 1.41421356237309492 *)
This composes three small, interchangeable components:
next a- one iteration stepiterates (next a) 1.0- infinite series of approximationswithin 1e-6- convergence test
The initial approximation of 1.0 is deliberately poor to demonstrate the method’s robustness.
Richardson extrapolation
This compositional approach to numerical computation has received attention in research literature. A recurring example is Richardson extrapolation, which improves the accuracy of numerical integration by combining results at different step sizes. The principle is the same: build numerical methods from small, combinable parts operating on sequences.