乐正

Actions speak louder than words.

Sicp-ex1-36

问题

请修改fixed-point,使它能打印出计算中产生的近似值的序列,用练习1.22展示的newlinedisplay基本过程。而后通过找出$x \mapsto \frac {\log (1000)} {\log (x)}$的不 动点的方式,确定$x^x = 1000$的一个根(请利用Scheme的基本过程log,它计算自然对 数的值)。请比较一下采用平均阻尼和不用平均阻尼时的计算步数。(注意:你不能用猜测 1去启动fixed-point,因为这将导致除以$\log (1) = 0$。)

解答

练习1.36 (ex1-36.scm) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(define tolerance 0.00001)

(define (displayed-fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))

  (define (fixed-point-log point count)
    (newline)
    (display "Find a fixed point: ")
    (display point)
    (newline)
    (display "Total steps: ")
    (display count))

  (define (try guess count)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          (fixed-point-log next count)
          (try next (1+ count)))))

  (try first-guess 1))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;;; 不使用平均阻尼
(displayed-fixed-point (lambda (x) (/ (log 1000)
                                      (log x)))
                       1.1)

Find a fixed point: 4.555538934848503
Total steps: 37
;Unspecified return value

;;; 使用平均阻尼
(displayed-fixed-point (lambda (x)
                         (/ (+ (/ (log 1000) (log x)) x) 2))
                       1.1)

Find a fixed point: 4.555536364911781
Total steps: 13
;Unspecified return value

可以看到使用了平均阻尼时的计算步数大大减少。

draft

« sicp-ex1-35 sicp-ex1-37 »

Comments