SICP-2nd-Edition-Exercise-1.36
From Boozled
Contents |
Exercise 1.31 From SICP
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
. (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.)
Attempt
The damped method requires less steps.
Scheme
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance))
(define (try guess)
(display guess)
(newline)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (phi x) (+ 1 (/ 1 x)))
(define (xsquared x) (/ (log 1000) (log x)))
(define (hxsquared x) (/ (+ x (/ (log 1000) (log x))) 2))
(define (xy x) (/ x y))
(define (sqroot x)
(fixed-point (lambda (y) (/ x y))
1.0))
(display (fixed-point xsquared 1.1))
(newline)
(newline)
(display (fixed-point hxsquared 1.1))
Parent Course
6.001_Structure_and_Interpretation_of_Computer_Programs

