Exercise 1.36. Modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in exercise 1.22. Then find a solution to xx = 1000 by finding a fixed point of x log(1000)/log(x). (Use Scheme's primitive log procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. (Note that you cannot start fixed-point with a guess of 1, as this would cause division by log(1) = 0.)

 
(define tolerance 0.00001)
 
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (display v1)
    (newline)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))
 
 
(define (sqrt x)
  (fixed-point (lambda (y) (average y (/ x y)))
               1.0))
 
 
(define (average x y)
  (/ (+ x y) 2))
 
(define (gr x) (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0))
 
(define (bar x) (fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) 2.0))
 
exercise_1.36.txt · Last modified: 2009/03/02 01:32 by mariorz
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki