-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise2-7_16.scm
191 lines (161 loc) · 5.69 KB
/
exercise2-7_16.scm
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#lang scheme
;Section 2.1.4 - Extended Exercise: Interval Arithmetic
;Exercises 2.7 to 2.16
;-----------Exercise 2.7-------------
;Create the interval abstraction
(define (make-interval a b) (cons a b))
(define (lower-bound x)
(min (car x) (cdr x)))
(define (upper-bound x)
(max (car x) (cdr x)))
;test code
(define x (make-interval 1 3))
(define y (make-interval 2 5))
(display x)
(newline)
(display y)
(newline)
(display (lower-bound x))
(newline)
(display (upper-bound x))
(newline)
;Alyssa P. Hacker's code
;for addition, multiplication and division of intervals.
(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))))
;----------Exercise 2.10---------------
;Modify Alyssa's code to check division by an interval that spans zero
(define (interval-spans-zero? x)
(if (and (>= (upper-bound x) 0) (<= (lower-bound x) 0))
(error "Division by an interval that spans zero!!")
#t))
(define (div-interval x y)
(interval-spans-zero? y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
;test code
(display (add-interval x y))
(newline)
(display (mul-interval x y))
(newline)
(display (div-interval x y))
(newline)
;----------Exercise 2.8--------------
;sub-interval using reasoning analogous to Alyssa's
;
;we subtract the upper bound and the lower bound of the second interval from the upper bound and the lower bound of the first interval respectively.
(define (sub-interval x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))))
;test
(display (sub-interval x y))
(newline)
;----------Exercise 2.9---------------
;width of the interval is half of the difference between its upper an dlower bounds.
(define (width-interval x)
(/ (- (upper-bound x) (lower-bound x)) 2))
(display (width-interval x))
(newline)
(display (width-interval y))
(newline)
;for some function of intervals
;width(f(interval1, interval2....)) is a function only of the widths of interval1, interval2...
;
;show this for subtraction and addition
; width [x, y] = y - x
; width [r, t] = t - r
;
; width ([x, y] + [r, t]) = width [x + r, y + t] = (y + t - x - r)/2 = (y - x + t - r)/2 = (width [x, y]) + (width [r, t])
; width ([x, y] - [r, t]) = width [x - t, y - r] = (y - r - x + t)/2 = (width [x, y]) + (width [r, t])
;
;and show examples to show this is not true for multiplication and division
(display (width-interval (add-interval x y)))
(newline)
(display (width-interval (sub-interval x y)))
(newline)
(display (width-interval (div-interval x y)))
(newline)
(display (width-interval (mul-interval x y)))
(newline)
;----------Exercise 2.10---------------
;Modify Alyssa's code to check division by an interval that spans zero
;code added earlier in the file.
;uncomment one by one to test
;(display (div-interval (make-interval 1 2) (make-interval -1 2)))
;(newline)
;(display (div-interval (make-interval 1 2) (make-interval 1 0)))
;(newline)
;----------Exercise 2.11---------------
(define (negative? num)
(if (< num 0)
#t
#f))
(define (positive? num)
(not (negative? num)))
(define (mul-interval-2 x y)
(let ((x1 (lower-bound x))
(x2 (upper-bound x))
(y1 (lower-bound y))
(y2 (upper-bound y)))
(cond ((or
(and (negative? x1) (negative? y1) (positive? x2) (positive? y2)))
(let ((p1 (* x1 y2))
(p2 (* x2 y1))
(p3 (* x2 y2)))
(make-interval (min p1 p2) p3)))
((or
(and (positive? x1) (positive? y1) (positive? x2) (positive? y2))
(and (negative? x1) (negative? y1) (negative? x2) (negative? y2))
(and (positive? x1) (negative? y1) (positive? x2) (negative? y2))
(and (negative? x1) (positive? y1) (negative? x2) (positive? y2)))
(make-interval (* x1 y1) (* x2 y2)))
((or
(and (negative? x1) (positive? y1) (positive? x2) (positive? y2))
(and (negative? x1) (negative? y1) (positive? x2) (negative? y2)))
(make-interval (* x1 y2) (* x2 y2)))
((or
(and (positive? x1) (negative? y1) (positive? x2) (positive? y2))
(and (negative? x1) (negative? y1) (negative? x2) (positive? y2)))
(make-interval (* x2 y1) (* x2 y2))))))
(display (mul-interval x y))
(newline)
(display (mul-interval-2 x y))
(newline)
;Alyssa's alternate constructor and selectors
;intervals represented as a center value and an additive 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))
;----------Exercise 2.12---------------
;constructor and selector percent for intervals represented as a center and a percentage tolerance
;selector for center remains the same as above
(define (make-center-percent c p)
(let ((w (* c (/ p 100))))
(make-center-width c w)))
(define (percent i)
(* (/ (width i) (center i)) 100))
;test
(display (make-center-width 10 2)) ;[8, 12]
(newline)
(display (center (make-center-width 10 2))) ;10
(newline)
(display (width (make-center-width 10 2))) ;2
(newline)
(display (make-center-percent 10 20)) ;[8, 12]
(newline)
(display (center (make-center-percent 10 20))) ;10
(newline)
(display (percent (make-center-percent 10 20))) ;20
(newline)