1 # [Newton's method](https://en.wikipedia.org/wiki/Newton%27s_method)
2 Let's use the Newton-Raphson method for finding the root of an equation to write a function that can compute the square root of a number.
4 Cf. ["Why Functional Programming Matters" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf)
8 from notebook_preamble import J, V, define
11 ## A Generator for Approximations
13 To make a generator that generates successive approximations let’s start by assuming an initial approximation and then derive the function that computes the next approximation:
19 ### A Function to Compute the Next Approximation
21 This is the equation for computing the next approximate value of the square root:
23 $a_{i+1} = \frac{(a_i+\frac{n}{a_i})}{2}$
31 The function we want has the argument `n` in it:
35 ### Make it into a Generator
37 Our generator would be created by:
39 a [dup F] make_generator
41 With n as part of the function F, but n is the input to the sqrt function we’re writing. If we let 1 be the initial approximation:
49 The generator can be written as:
51 23 1 swap [over / + 2 /] cons [dup] swoncat make_generator
52 1 23 [over / + 2 /] cons [dup] swoncat make_generator
53 1 [23 over / + 2 /] [dup] swoncat make_generator
54 1 [dup 23 over / + 2 /] make_generator
58 define('gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
66 [1 [dup 23 over / + 2 /] codireco]
69 Let's drive the generator a few time (with the `x` combinator) and square the approximation to see how well it works...
73 J('23 gsra 6 [x popd] times first sqr')
79 ## Finding Consecutive Approximations within a Tolerance
81 From ["Why Functional Programming Matters" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf):
84 > The remainder of a square root finder is a function _within_, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.
86 (And note that by “list” he means a lazily-evaluated list.)
88 Using the _output_ `[a G]` of the above generator for square root approximations, and further assuming that the first term a has been generated already and epsilon ε is handy on the stack...
91 ---------------------- a b - abs ε <=
96 ---------------------- a b - abs ε >
103 a [b G] ε [first - abs] dip <=
104 a [b G] first - abs ε <=
112 define('_within_P [first - abs] dip <=')
117 a [b G] ε roll< popop first
118 [b G] ε a popop first
124 define('_within_B roll< popop first')
129 a [b G] ε R0 [within] R1
132 2. Use `x` combinator to generate next term from `G`.
133 3. Run `within` with `i` (it is a "tail-recursive" function.)
135 Pretty straightforward:
137 a [b G] ε R0 [within] R1
138 a [b G] ε [popd x] dip [within] i
139 a [b G] popd x ε [within] i
148 define('_within_R [popd x] dip')
153 The recursive function we have defined so far needs a slight preamble: `x` to prime the generator and the epsilon value to use:
160 define('within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec')
161 define('sqrt gsra within')
198 from math import sqrt