sicp 2.14

Exercise 2.14.  Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals Aand B, and use them in computing the expressions A/A and A/B. You will get the most insight by using intervals whose width is a small percentage of the center value. Examine the results of the computation in center-percent form (see exercise 2.12).

 

 

(define (make-center-percent center percent)
  (make-center-width center (* center percent)))

(define (center z)
  (* 0.5 (+ (lower-bound z) (upper-bound z))))

(define (percent z)
  (/ (* 0.5 (- (upper-bound z) (lower-bound z))) (center z)))

(define (make-center-width c w)
  (make-interval (- c w) (+ c w)))

(define (make-interval a b) (cons a b))

(define (upper-bound z)
  (cdr z))
 
(define (lower-bound z)
  (car z))

(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

(define (sub-interval x y)
  (make-interval (- (lower-bound x) (upper-bound y))
                 (- (upper-bound x) (lower-bound y))))

(define (mul-interval x y)
  (cond ((and (>= (lower-bound x) 0) (>= (lower-bound y) 0))
         (make-interval (* (lower-bound x) (lower-bound y))
                        (* (upper-bound x) (upper-bound y))))
        ((and (< (lower-bound x) 0) (>= (upper-bound x) 0) (>= (lower-bound y) 0))
         (make-interval (* (lower-bound x) (upper-bound y))
                        (* (upper-bound x) (upper-bound y))))
        ((and (< (upper-bound x) 0) (>= (lower-bound y) 0))
         (make-interval (* (lower-bound x) (upper-bound y))
                        (* (upper-bound x) (lower-bound y))))
        ((and (>= (lower-bound x) 0) (< (lower-bound y) 0) (>= (upper-bound y) 0))
         (make-interval (* (upper-bound x) (lower-bound y))
                        (* (upper-bound x) (upper-bound y))))
        ((and (< (lower-bound x) 0) (>= (upper-bound x) 0)
              (< (lower-bound y) 0) (>= (upper-bound y) 0))
         (make-interval (min (* (lower-bound x) (upper-bound y))
                             (* (upper-bound x) (lower-bound y)))
                        (max (* (lower-bound x) (lower-bound y))
                             (* (upper-bound x) (upper-bound y)))))
        ((and (< (upper-bound x) 0) (< (lower-bound y) 0) (>= (upper-bound y) 0))
         (make-interval (* (lower-bound x) (upper-bound y))
                        (* (lower-bound x) (lower-bound y))))
        ((and (>= (lower-bound x) 0) (< (upper-bound y) 0))
         (make-interval (* (upper-bound x) (lower-bound y))
                        (* (lower-bound x) (upper-bound y))))
        ((and (< (lower-bound x) 0) (>= (upper-bound x) 0) (< (upper-bound y) 0))
         (make-interval (* (upper-bound x) (lower-bound y))
                        (* (lower-bound x) (lower-bound y))))
        ((and (< (upper-bound x) 0) (< (upper-bound y) 0))
         (make-interval (* (upper-bound x) (upper-bound y))
                        (* (lower-bound x) (lower-bound y))))))

(define (div-interval x y)
  (mul-interval x 
                (make-interval (/ 1.0 (upper-bound y))
                               (/ 1.0 (lower-bound y)))))

(define a (make-center-percent 3.14 0.01))
(define b (make-center-percent 9.8 0.01))
(center (div-interval a a))
(percent (div-interval a a))
(center (div-interval a b))
(percent (div-interval a b))

 

1.0002000200020003

0.019998000199980076

0.3204722513067634

0.019998000199980114

 

用两个不同的公式计算并联电阻值,从数值计算角度来说,造成的误差是不同的,所以结果不同。从区间运算角度来说,A/A不等于(1,1),所以这两个公式不等价,结果当然不同

 

你可能感兴趣的:(SICP)