1 day ago
my attempt to do the exercises in sicp.
Monday, June 30, 2008
sicp exercise 1.36
; 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 x^x = 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)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(display (rationalize guess 0.00001))(newline)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (average x y) (/ (+ x y) 2))
(define (func x) (/ (log 1000) (log x)))
(define (func-avg-damp x) (average x (/ (log 1000) (log x))))
;(fixed-point func 2)(newline)
(fixed-point func-avg-damp 2)(newline)
; Answer:
; 4.55554089709763 in 34 steps without average damping
; 4.55555555555556 in 9 steps with average damping
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment