1

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?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • Have a look at https://stackoverflow.com/questions/3477283/what-is-the-maximum-float-in-python: by adding a print for the value of `x`, I get 1.861351852240379e+293 and `x**2` is >> than `sys.float_info`, that is, `1.7976931348623157e+308`. Neither `decimal.Decimal` helps you, as it crashes at `x = 4.061798841378423761845858231E+600616` – fcracker79 Dec 30 '19 at 07:31
  • `def f(x): return math.sqrt(392/(x+1))` – gboffi Dec 30 '19 at 07:49
  • Given the original formula (@gboffi what is that function?), your nex and your guess will always be positive. In order to solve the specific problem , you have to solve `x-(392//x+x**2)( <= tolerance`. Given the value of `tolerance`, you accept all the values below 7 (https://www.wolframalpha.com/input/?i=plot+x%5E3+%2B+x%5E2+-392+-+0.00001), but, since that value must be below the tolerance in its abs value, you also have https://www.wolframalpha.com/input/?i=plot+-x%5E3+-+x%5E2+%2B392+-+0.00001. So, your solution is x=7. – fcracker79 Dec 30 '19 at 08:23
  • @gboffi sorry can't see the point: it gets 19.30529743549647 as a solution, but does not solve the original problem. – fcracker79 Dec 30 '19 at 08:34
  • thanks @gboffi. I am missing some math behind. I see it is converging to the solution but I do not know why. – fcracker79 Dec 30 '19 at 09:18
  • @Algebra the expression `392 / (x + x**2)` either becomes zero after a couple of iterations (when doing integer division) or never converges (when doing floating-point division), so I suspect there's something wrong with your fixed point solution. What's the number of this exercise in SICP? you should always provide this info for SICP-related questions. – Óscar López Dec 30 '19 at 19:45
  • ty, I add the reference to question and thus cannot appreciate it more for your patience on tutoring and thus delighted to continue chapter 2 in scheme. @ÓscarLópez – AbstProcDo Dec 31 '19 at 01:57

1 Answers1

1

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386