I tried to find the root of a²+a³=392 with fixed_point introduced by SICP 1.1.3 functions as general method
#+BEGIN_SRC scheme :session sicp
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2))
tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
#+END_SRC
I rewrite it in python as:
#+begin_src ipython :session sicp :results output :tangle pySrc/sicp_fixedpoint.py
import math
tolerance = 0.00001
import math
def fixed_point(f, guess):
while True:
nex = f(guess)
if abs(guess-nex) < tolerance:
return nex
else:
guess = nex
def f(x):
return 392 / (x + x**2)
fixed_point(f, 1)
#+end_src
It report error:
OverflowError: (34, 'Numerical result out of range')
What's the problem?
I think this problem is not well suited for solving via a fixed-point procedure. If you print the (abs (- next guess))
for each iteration, you'll see that the value is increasing between iterations, it'll never converge to the specified tolerance - hence the Numerical result out of range
error.