# SICP Exercise 2.15

Eva (and W.Z.) is right. We need to distinguish between dependent and independent intervals. In the formula for resistance $R_1$ and $R_2$ are independent intervals (they correspond to resistance of physically different resistors), and in procedure par1 two occurrences of $R_1$ are dependent intervals. When we calculate the ratio $A/A$ we cannot suppose that those two intervals $A$ are independent, we cannot choose their values independently. If we randomly choose the value inside the interval $A$, we must assign this value to both occurrences of $A$ in the formula. For independent intervals the situation is different, we may choose one value inside $A$, some other value inside $B$, and their ratio will have meaning, namely this will be some value inside the interval which is the ratio of two intervals, $A/B$. Alyssa’s package treats all occurrences of the same interval in a complex formula as independent intervals and the answers are wrong. The calculation of $A/A$ by Alyssa’s package gives wrong result.

# SICP Exercise 2.14

Code

```#lang racket
; v1

(require "ex2.7.scm")
(require "ex2.10.scm")
(require "ex2.12.scm")

(define A (make-center-percent 100 0.01))
(define R (div-interval A A))

R
(width R)
(tolerance R)
```

Output

```'(0.9801980198019802 . 1.0202020202020203)
0.020002000200020076
0.019998000199980076
```

# SICP Exercise 2.12

Code

```#lang racket
; v1

(require "ex2.7.scm")

(provide make-center-width)
(provide center)
(provide width)
(provide make-center-percent)
(provide tolerance)

(define (make-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))

; tol: 0.1 means 10%
(define (make-center-percent c t)
(make-center-width c (* c t)))

(define (tolerance i)
(define c (center i))
(if (= c 0)
(error "Interval with center at zero has no tolerance")
(/ (width i) c)))
```

# SICP Exercise 2.11

Code

```#lang racket
; v1

(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)

(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 (new-mul-interval x y)
(define x1 (lower-bound x))
(define x2 (upper-bound x))
(define y1 (lower-bound y))
(define y2 (upper-bound y))
(cond
((and (<= 0 x1)  (<= x1 x2) (<= 0 y1)  (<= y1 y2)) (make-interval (* x1 y1) (* x2 y2)))
((and (<= x1 0)  (<= 0 x2)  (<= 0 y1)  (<= y1 y2)) (make-interval (* x1 y2) (* x2 y2)))
((and (<= x1 x2) (<= x2 0)  (<= 0 y1)  (<= y1 y2)) (make-interval (* x1 y2) (* x2 y1)))
((and (<= 0 x1)  (<= x1 x2) (<= y1 0)  (<= 0 y2))  (make-interval (* x2 y1) (* x2 y2)))
((and (<= x1 x2) (<= x2 0)  (<= y1 0)  (<= 0 y2))  (make-interval (* x1 y2) (* x1 y1)))
((and (<= 0 x1)  (<= x1 x2) (<= y1 y2) (<= y2 0))  (make-interval (* x2 y1) (* x1 y2)))
((and (<= x1 0)  (<= 0 x2)  (<= y1 y2) (<= y2 0))  (make-interval (* x2 y1) (* x1 y1)))
((and (<= x1 x2) (<= x2 0)  (<= y1 y2) (<= y2 0))  (make-interval (* x2 y2) (* x1 y1)))
;((and (<= x1 0)  (<= 0 x2)  (<= y1 0)  (<= 0 y2))  (mul-interval x y))
(else (mul-interval x y))))

(define (equal-intervals? r1 r2)
(and
(= (lower-bound r1)
(lower-bound r2))
(= (upper-bound r1)
(upper-bound r2))))

; tests

(define (same-mul? i1 i2)
(define r1 (new-mul-interval i1 i2))
(define r2 (mul-interval i1 i2))
(equal-intervals? r1 r2))

(define i1 (make-interval -10 -5))
(define i2 (make-interval -10 5))
(define i3 (make-interval 5 10))

(same-mul? i1 i1)
(same-mul? i2 i1)
(same-mul? i3 i1)
(same-mul? i1 i2)
(same-mul? i2 i2)
(same-mul? i3 i2)
(same-mul? i1 i3)
(same-mul? i2 i3)
(same-mul? i3 i3)
```

Output

```#t
#t
#t
#t
#t
#t
#t
#t
#t
```

# SICP Exercise 2.10

Code

```#lang racket
; v1

(provide div-interval)

(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)

(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 (div-interval x y)
(define y1 (lower-bound y))
(define y2 (upper-bound y))
(if (and (<= y1 0) (>= y2 0))
(error "division by interval with zero")
(mul-interval
x
(make-interval (/ 1.0 y2)
(/ 1.0 y1)))))

; tests

;(div-interval (make-interval 8 10) (make-interval 2 4))  ; '(2.0 . 5.0)
;(div-interval (make-interval 8 10) (make-interval -8 8)) ; division by interval with zero
```

# SICP Exercise 2.8

Code

```#lang racket
; v1

(require "ex2.7.scm")

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

# SICP Exercise 2.7

Code

```#lang racket
; v1

(provide make-interval)
(provide lower-bound)
(provide upper-bound)

(provide mul-interval)

(define make-interval cons)
(define lower-bound car)
(define upper-bound cdr)

(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))))
```