-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.1.4-interval-arithmetic.rkt
139 lines (119 loc) · 3.6 KB
/
2.1.4-interval-arithmetic.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#lang sicp
; exercise 2.7
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(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 (div-interval x y)
(mul-interval
x
(make-interval
(/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
; exercise 3.8
(define a (make-interval 1 3))
(define b (make-interval 2 4))
(define c (add-interval a b)) ; '(3 . 7)
; (3 . 7) = (2 . 4) + a, thus a = (3 . 7) - (2 . 4).
; Then (3 . 7) - (2 . 4) = (1 . 3) = ((3-2) . (7-4))
(define (sub-interval a b)
(make-interval (- (lower-bound a) (lower-bound b))
(- (upper-bound a) (upper-bound b))))
(sub-interval c b) ; '(1 . 3)
; exercise 3.9
(define (width a)
(/ (- (upper-bound a) (lower-bound a))
2))
; Let a, b intervals, and x = a + b.
; Then width(x) = .5 (upper(x) - lower(x))
; = .5 upper(x) - .5 lower(x)
; = .5 (upper(a) + upper(b)) - .5 (lower(a) + lower(b))
; = .5 upper(a) - .5 lower(a) + .5 upper(b) - .5 lower(b)
; = .5 (upper(a) - lower(a)) + .5 (upper(b) - lower(b))
; = width(a) + width(b), and similarly for the difference of intervals.
(define a (make-interval 5 10))
(define b (make-interval 1 2))
(width a) ; 5/2
(width b) ; 1/2
(width (add-interval a b)) ; 3
(width (mul-interval a b)) ; 15/2
(width (div-interval a b)) ; 3.75 ???
; exercise 2.10
(define (div-interval-checked x y)
(if (= 0 (width y))
(error "division by null interval")
(div-interval x y)))
(define c (make-interval 1 1))
(width c) ; 0
(div-interval-checked a c) ; division by null interval [,bt for context]
; exercise 2.11
; cases:
; 1. (-, -); (-, -)
; 2. (-, -); (-, +)
; 3. (-, -); (+, +)
; 4. (-, +); (-, -)
; 5. (-, +); (-, +)
; 6. (-, +); (+, +)
; 7. (+, +); (-, -) same as case 3
; 8. (+, +); (-, +) same as case 6
; 9. (+, +); (+, +) same as case 1
;
; We can assume that the lower bound of an interval is less than the upper bound.
; Thus in cases with mixed signs, it's possible to determine the minimum and maximum
; products directly, only spending the two multiplications for the bounds
; of the resulting interval.
;(define (neg x) (< x 0))
;(define (pos x) (not (neg x)))
;
;(define (mul-interval-cryptic a b)
; (let ((la (lower-bound a))
; (ua (upper-bound a))
; (lb (lower-bound b))
; (ub (upper-bound b)))
; (cond
; ; cases 1 and 9
; ((or (and (neg la) (neg ua) (neg lb) (neg ub))
; (and (pos la) (pos ua) (pos lb) (pos ub))) ...)
; ; case 2
; ((and (neg la) (neg ua) (neg lb) (pos ub))
; (make-interval (* (min la lb) ub) (* (min la lb) ua))
; ; case 3
; ((and (neg la) (neg ua) (pos lb) (pos ub))
; (make-interval (* ua ub) (max (* lb ub) (* la ua)))) ; use abs?
; ; case 4
; ((and (neg la) (pos ua) (neg lb) (neg ub))
; (make-interval (* (min la ub) ua) (* (min la lb) ub)))
; ; case ... to hell with this exercise
; exercise 2.12
(define (make-center-width c w)
(make-interval (- c w) (+ c w)))
(define (center i)
(/ (+ (lower-bound i)
(upper-bound i))
2))
; p = w / c
; pc = w
(define (make-center-percent c p)
(make-center-width c (* c p)))
(define (percent i)
(/ (width i) (center i)))
(make-center-percent 1 0.3) ; '(0.7 . 1.3)
(define i (make-center-percent 10 0.3)) ; '(7.0 . 13.0)
(percent i) ; 0.3
; exercise 2.13 .. 2.16
; We'll get back to this.