65 lines
1.6 KiB
Scheme
65 lines
1.6 KiB
Scheme
(define (add-interval x y)
|
|
(make-interval (+ (lower-bound x) (lower-bound y))
|
|
(+ (upper-bound x) (upper-bound y))))
|
|
|
|
(define (mul-interval 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 (mul-interval x y)
|
|
;;(cond ((and (> (lower-bound x) 0)
|
|
;;(> (lower-bound y) 0))
|
|
;;(make-interval
|
|
;;(* (lower-bound x))))))
|
|
|
|
(define (div-interval x y)
|
|
(if (not (= (upper-bound y) (lower-bound y)))
|
|
(mul-interval x
|
|
(make-interval (/ 1.0 (upper-bound y))
|
|
(/ 1.0 (lower-bound y))))
|
|
(begin
|
|
(error "Error: second interval spans 0")
|
|
(newline))))
|
|
|
|
(define (make-interval a b) (cons a b))
|
|
|
|
(define (lower-bound x) (car x))
|
|
(define (upper-bound x) (cdr x))
|
|
|
|
(define (sub-interval x y)
|
|
(make-interval (- (lower-bound x) (upper-bound y))
|
|
(- (upper-bound x) (lower-bound y))))
|
|
|
|
;; 2.12
|
|
(define (make-centre-percent c p)
|
|
(make-interval (- c (/ (* c p) 100))
|
|
(+ c (/ (* c p) 100))))
|
|
|
|
(define (center i)
|
|
(/ (+ (lower-bound i) (upper-bound i)) 2))
|
|
|
|
(define (width i)
|
|
(/ (- (upper-bound i) (lower-bound i)) 2))
|
|
|
|
(define (percent i)
|
|
(* (/ (width i) (center i)) 100))
|
|
|
|
|
|
;; 2.14
|
|
;; parallel resistors
|
|
|
|
(define (par1 r1 r2)
|
|
(div-interval (mul-interval r1 r2)
|
|
(add-interval r1 r2)))
|
|
|
|
(define (par2 r1 r2)
|
|
(let ((one (make-interval 1 1)))
|
|
(div-interval one
|
|
(add-interval (div-interval one r1)
|
|
(div-interval one r2)))))
|
|
|