CG@CPPBLOG

/*=========================================*/
随笔 - 76, 文章 - 39, 评论 - 137, 引用 - 0
数据加载中……

我的SICP习题答案(1.35~1.39)

1.35

若 φ=0 , 则 φ^2=φ+1 不成立 , 故 φ≠0
φ^2 = φ+1 ==>
φ = (φ+1)/φ = 1 + (1/φ)

(fixed-point (lambda(x) (+ 1 (/ 1 x))) 1.0)

1.36

(define tolerance 0.00001)

(define (fixed-point f first-guess)
  (define (close-enough? x y)
    (< (abs (- x y)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (display next)
      (newline)
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))

平均阻尼法和不用平均阻尼分别如下,它们步数分别为 9 和 34 。

(fixed-point (lambda(x) (/ (+ x (/ (log 1000) (log x))) 2)) 2.0)
(fixed-point (lambda(x) (/ (log 
1000) (log x))) 2.0)

1.37

(define (cont-frac-r n d k)
  (define (redu i)
    (if (
= i k)
        (/ (n i) (d i))
        (/ (n i) (+ (d i) (redu n d (+ i 
1))))))
  (redu 
1))

(define (cont-frac n d k)
  (define (iter i result)
    (if (
= i 0
        result
        (iter (- i 
1) (/ (n i) (+ (d i) result)))))
  (iter k 
0))

(define (get-phai k)
  (/ 
1 (cont-frac (lambda(i) 1.0) (lambda(i) 1.0) k)))

(define (get-k)
  (define (iter i)
    (if (< (abs (- (get-phai i) 
1.6180)) 0.00005)
        i
        (iter (+ i 
1))))
  (iter 
1))

k = 11 时,精度满足 4 位 十进制数。

1.38

(define (euler-d i)
  (cond ((
= i 22.0)
        ((and (> i 
2) (= 0 (remainder (- i 23)))
         (* (/ (+ i 
13.02.0))
        (else 
1.0)))

(define (get-e k)
  (+ 
2 (cont-frac (lambda(i) 1.0) euler-d k)))

1.39

(define (tan-cf x k)
  (define (tan-n i)
    (if (
= 1 i)
        x
        (- (* x x))))
  (cont-frac tan-n (lambda(i) (- (* i 
2.01.0)) k))

posted on 2008-04-16 00:22 cuigang 阅读(797) 评论(1)  编辑 收藏 引用 所属分类: Lisp/Scheme我的SICP答案

评论

# re: 我的SICP习题答案(1.35~1.39)[未登录]  回复  更多评论   

1.37 答案经验证有误:
第五行应改为: (/ (n i) (+ (d i) (redu (+ i 1))))))
2011-07-11 17:37 | wang

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理