Advertisement
timothy235

sicp-2-1-4-interval-arithmetic

Feb 19th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 6.20 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (add-interval x y)
  4.   (make-interval (+ (lower-bound x) (lower-bound y))
  5.                  (+ (upper-bound x) (upper-bound y))))
  6.  
  7. (define (mul-interval x y)
  8.   (define p1 (* (lower-bound x) (lower-bound y)))
  9.   (define p2 (* (lower-bound x) (upper-bound y)))
  10.   (define p3 (* (upper-bound x) (lower-bound y)))
  11.   (define p4 (* (upper-bound x) (upper-bound y)))
  12.   (make-interval (min p1 p2 p3 p4)
  13.                  (max p1 p2 p3 p4)))
  14.  
  15. (define (div-interval x y)
  16.   (mul-interval x
  17.                 (make-interval (/ 1.0 (upper-bound y))
  18.                                (/ 1.0 (lower-bound y)))))
  19.  
  20. ;;;;;;;;;
  21. ;; 2.7 ;;
  22. ;;;;;;;;;
  23.  
  24. (define (make-interval a b)
  25.   (if (<= a b)
  26.     (cons a b)
  27.     (error "lower bound must not be greater than upper bound" a b)))
  28.  
  29. (define (lower-bound i)
  30.   (car i))
  31.  
  32. (define (upper-bound i)
  33.   (cdr i))
  34.  
  35. ;;;;;;;;;
  36. ;; 2.8 ;;
  37. ;;;;;;;;;
  38.  
  39. (define (sub-interval x y)
  40.   (make-interval (- (lower-bound x) (upper-bound y))
  41.                  (- (upper-bound x) (lower-bound y))))
  42.  
  43. ;;;;;;;;;
  44. ;; 2.9 ;;
  45. ;;;;;;;;;
  46.  
  47. (define (width i)
  48.   (/ (- (upper-bound i) (lower-bound i)) 2.0))
  49.  
  50. (define a (make-interval 0.2 0.4))
  51. (define b (make-interval 1.1 1.5))
  52.  
  53. (width a)
  54. ;; 0.1
  55. (width b)
  56. ;; 0.19999999999999996
  57. (width (add-interval a b))
  58. ;; 0.29999999999999993
  59. (width (sub-interval a b))
  60. ;; 0.3
  61. (width (mul-interval a b))
  62. ;; 0.19000000000000003
  63. (width (div-interval a b))
  64. ;; 0.11515151515151516
  65.  
  66. ;; In general, the width of the sum or difference of intervals is the sum of the
  67. ;; widths.  However the widths of the product or quotient of intervals depends on
  68. ;; the upper and lower bounds of the intervals and not just on their widths.
  69.  
  70. (define b2 (make-interval 0.1 0.5)) ; same width as b
  71. (width (mul-interval a b2))
  72. ;; 0.09
  73. (width (div-interval a b2))
  74. ;; 1.8
  75.  
  76. ;;;;;;;;;;
  77. ;; 2.10 ;;
  78. ;;;;;;;;;;
  79.  
  80. (define (contains-zero? i)
  81.   (<= (* (upper-bound i)
  82.          (lower-bound i))
  83.       0))
  84.  
  85. (define (new-div-interval x y)
  86.   (if (contains-zero? y)
  87.     (error "divisor interval cannot contain zero"
  88.            (lower-bound y)
  89.            (upper-bound y))
  90.     (mul-interval x
  91.                   (make-interval (/ 1.0 (upper-bound y))
  92.                                  (/ 1.0 (lower-bound y))))))
  93.  
  94. ;;;;;;;;;;
  95. ;; 2.11 ;;
  96. ;;;;;;;;;;
  97.  
  98. ;; The nine cases are:
  99. ;; x positive and y positive,
  100. ;; x positive and y negative,
  101. ;; x negative and y positive,
  102. ;; x negative and y negative,
  103. ;; x contains zero and y positive,
  104. ;; x contains zero and y negative,
  105. ;; y contains zero and x positive,
  106. ;; y contains zero and x negative,
  107. ;; x and y both contain zero; only this last case needs four multiplications.
  108.  
  109. (define (new-mul-interval x y)
  110.   (define xl (lower-bound x))
  111.   (define xu (upper-bound x))
  112.   (define yl (lower-bound y))
  113.   (define yu (upper-bound y))
  114.   (cond [(and (>= xl 0) (>= yl 0)) (make-interval (* xl yl) (* xu yu))]
  115.         [(and (>= xl 0) (<= yu 0)) (make-interval (* xu yl) (* xl yu))]
  116.         [(and (<= xu 0) (>= yl 0)) (make-interval (* xl yu) (* xu yl))]
  117.         [(and (<= xu 0) (<= yu 0)) (make-interval (* xu yu) (* xl yl))]
  118.         [(>= yl 0) (make-interval (* xl yu) (* xu yu))]
  119.         [(<= yu 0) (make-interval (* xu yl) (* xl yl))]
  120.         [(>= xl 0) (make-interval (* xu yl) (* xu yu))]
  121.         [(<= xu 0) (make-interval (* xl yu) (* xl yl))]
  122.         [else
  123.           (define p1 (* xl yl))
  124.           (define p2 (* xl yu))
  125.           (define p3 (* xu yl))
  126.           (define p4 (* xu yu))
  127.           (make-interval (min p1 p2 p3 p4)
  128.                          (max p1 p2 p3 p4))]))
  129.  
  130. (define x1 (make-interval 1 2))
  131. (define x2 (make-interval -2 -1))
  132. (define x3 (make-interval -1 2))
  133.  
  134. (define y1 (make-interval 3 5))
  135. (define y2 (make-interval -5 -3))
  136. (define y3 (make-interval -3 5))
  137.  
  138. (define (equal-interval? x y)
  139.   (and (= (lower-bound x) (lower-bound y))
  140.        (= (upper-bound x) (upper-bound y))))
  141.  
  142. (define xs (list x1 x1 x1 x2 x2 x2 x3 x3 x3))
  143. (define ys (list y1 y2 y3 y1 y2 y3 y1 y2 y3))
  144.  
  145. (for/and ([x xs]
  146.           [y ys])
  147.          (equal-interval? (mul-interval x y)
  148.                           (new-mul-interval x y)))
  149. ;; #t
  150.  
  151. ;;;;;;;;;;
  152. ;; 2.12 ;;
  153. ;;;;;;;;;;
  154.  
  155. (define (make-center-width c w)
  156.   (make-interval (- c w) (+ c w)))
  157.  
  158. (define (center i)
  159.   (/ (+ (lower-bound i) (upper-bound i)) 2.0))
  160.  
  161. (define (make-center-percent c p)
  162.   (make-center-width c (* c p)))
  163.  
  164. (define (percent i)
  165.   (/ (width i) (center i)))
  166.  
  167. ;;;;;;;;;;
  168. ;; 2.13 ;;
  169. ;;;;;;;;;;
  170.  
  171. ;; x = interval with center c1 and percent p1
  172. ;; y = interval with center c2 and percent p2
  173.  
  174. ;; x has lower-bound c1 - c1 * p1 and upper-bound c1 + c1 * p1
  175. ;; y has lower-bound c2 - c2 * p2 and upper-bound c2 + c2 * p2
  176.  
  177. ;; assuming all numbers are positive, the product
  178. ;; has lower-bound c1 * (1 - p1) * c2 * (1 - p2) ~= c1 * c2 * (1 - p1 - p2)
  179. ;; and upper-bound c1 * (1 + p1) * c2 * (1 + p2) ~= c1 * c2 * (1 + p1 + p2)
  180. ;; where we drop the insignificant second-order terms involving p1 * p2
  181.  
  182. ;; so the product has center c1 * c2 and width c1 * c2 * (p1 + p2)
  183.  
  184. ;; so the tolerance of the product is p1 + p2
  185.  
  186. ;;;;;;;;;;
  187. ;; 2.14 ;;
  188. ;;;;;;;;;;
  189.  
  190. (define (par1 r1 r2)
  191.   (div-interval (mul-interval r1 r2)
  192.                 (add-interval r1 r2)))
  193.  
  194. (define (par2 r1 r2)
  195.   (define one (make-interval 1 1))
  196.   (div-interval one
  197.                 (add-interval (div-interval one r1)
  198.                               (div-interval one r2))))
  199.  
  200. (define r1 (make-center-percent 4 0.1))
  201. (define r2 (make-center-percent 10 0.2))
  202.  
  203. (define circ1 (par1 r1 r2))
  204. (center circ1)
  205. ;; 3.1539108494533226
  206. (percent circ1)
  207. ;; 0.4432000000000001
  208.  
  209. (define circ2 (par2 r1 r2))
  210. (center circ2)
  211. ;; 2.8511354079058036
  212. (percent circ2)
  213. ;; 0.12920353982300886
  214.  
  215. ;;;;;;;;;;
  216. ;; 2.15 ;;
  217. ;;;;;;;;;;
  218.  
  219. ;; I think so.  It stands to reason that more uncertain inputs should increase the
  220. ;; uncertainty of the output.
  221.  
  222. ;;;;;;;;;;
  223. ;; 2.16 ;;
  224. ;;;;;;;;;;
  225.  
  226. ;; Simplifying an algebraic expression before doing the interval arithmetic will
  227. ;; usually decrease the uncertainty in the final answer, like in 2.15.
  228.  
  229. ;; One way around this issue would be to choose a normal form for each equivalence
  230. ;; class of algebraic expressions and always use that form to compute the answer.
  231. ;; I'm not sure if that's possible though.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement