SICP ex2-6 2-15 Alyssa

ex2-6 2-12 一連串問題過多,不再贅述,前面幾個難度都不是很大
(define (make-interval a b)
	(cons a b))
(define (upper-bound x)
	(cdr x))
(define (lower-bound x)
	(car x))
(define (intadd x y)
	(make-interval (+ (lower-bound x) (lower-bound y))
			(+ (upper-bound x) (upper-bound y))))

;;all negative          it is not necessary (make-interval (* u u) (* l l))
;;one upper positive   	it is not necessary (make-interval (* u l) (* l l))
;;two uppers positive   it is necessary
;;one lower positive    it is not necessary (make-interval (* u (min l l)) (* u u))
;;two lower positive    it is not necessary (make-interval (* l l) (* u u))
;;one upper equals zero it is not necessary (make-interval (* (max u u) l) (* l l))
;;two upper equal zero  it is not necessary (make-interval 0 (* l l))
;;one lower equal zero  it is not necessary (make-interval (* (min l l) u) (* u u))
;;two lower equal zero  it is not necessary (make-interval 0 (* u u))
;;It seems this idea become much more complex.
;;so I use the frist one to take the 
;;if you have a better idea please let me know
(define (intmul x y)
	(let (
		(p1 (* (lower-bound x) (lower-bound y)))
		(p2 (* (lower-bound x) (upper-bound y)))
		(p3 (* (upper-bound x) (lower-bound y)))
		(p4 (* (upper-bound x) (upper-bound y))))
		(make-interval 
				(min p1 p2 p3 p4)
				(max p1 p2 p3 p4))))
(define (intdiv x y)
	(if (or (= (upper-bound y) 0) (= (lower-bound y) 0) ) (error "it could not be zero!")
	(intmul x (make-interval  (/ 1 (upper-bound y)) (/ 1 (lower-bound y))))))
(define (intsub x y)
	(make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y))))
(define (GetWidth x)
	(- (upper-bound x) (lower-bound x)))
(define (Right? combiner function x y)
	(= (GetWidth (function x y)) (combiner (GetWidth x) (GetWidth y))))
(define (center-width c w)
	(make-interval (- c w) (+ c w)))
(define (center i)
	(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
	(/ (- (upper-bound i) (lower-bound i)) 2))
(define (make-center-percent c pw)
	(make-interval (- c (* c pw)) (+ c (* c pw))))
(define (percent x)
	(let ((c (center x)))
		(/ (- (upper-bound x) c) c)))
(define (mulpercent x y)
	(/ (- (* (+ 1 x)(+ 1 y)) (* (- 1 x) (- 1 y))) (+ (* (+ 1 x)(+ 1 y)) (* (- 1 x) (- 1 y))))) 
(define (par1 r1 r2)
	(intdiv (intmul r1 r2)
		(intadd r1 r2)))
(define (par2 r1 r2)
	(let ((one (make-interval 1 1)))
		(intdiv one
			(intadd (intdiv one r1)
				(intdiv one r2)))))
ex2-13引出問題 1/(1/r1+1/r2) 同r1r2/(r1+r2)結果不同
能力有限,參考了一些解答
問題在於a/a應爲1 但是由於a是某個範圍內的一個值
因此,我們可以理解爲a b均屬於A(A爲容錯區間)
這樣當且僅當a=b時纔等於1 我們就可以理解a/a!=1的理由了
之後我們看到2-14中的解釋,由於一式只使用了一次r1 一次r2 因此,結果更準確,她的分析應該是正確的
也就是說我們接下來要實現的就是能夠使除法 r1/r1=1 減法 r1-r1=0
能力有限,無法給出正確答案,以後如果想到會補充

發佈了35 篇原創文章 · 獲贊 1 · 訪問量 8306
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章