Solution to SICP Exercise 2.13

Structure and Interpretation of Computer Programs

One solution to Exercise 2.13:

In exercise 2.12, we found a way to express a range as a tolerance.

(define (make-center-percent c p)
(make-center-width c (* c (/ p 100))))
#1

Where,

(define (make-center-width c w)
(make-interval (- c w) (+ c w)))
#2

Expanding #2 in #1 give us:

(define (make-center-percent c p)
(make-interval (- c (* c (/ p 100)))
(+ c (* c (/ p 100)))))
#3

Now, imagine a range, x with a center cx
and a percentage tolerance px.

(define x (make-center-percent cx px)) #4

Expanding #4 out, from #3, we get:

(define x
(make-interval (- cx (* cx (/ px 100)))
(+ cx (* cx (/ px 100)))))
#5

That’s a bit messy with all those divisions by 100. Let’s introduce
an absolute tolerance, tx, that is one hundreth the
size of the percentage tolerance.

(define px (* tx 100)) #6

Substituting #6 into #5, we get:

(define x
(make-interval (- cx (* cx tx))
(+ cx (* cx tx))))
#7

We’ll need another range if we are to do any multiplication. Let’s
likewise define y:

(define y
(make-interval (- cy (* cy ty))
(+ cy (* cy ty))))
#8

Another lesson we learned from exercise 2.12 is how to find the
width of a range:

(define (width i)
(/ (- (upper-bound i) (lower-bound i)) 2))
#9

A couple of relations that we are almost certain to find useful
(and I hope you’ll find obvious):

(equal u (upper-bound (make-interval l u))) #10
(equal l (lower-bound (make-interval l u))) #11

In exercise 2.11, we found that when all the numbers are positive,
we can define mul-interval as follows:

(define (mul-interval x y)
(make-interval (* (lower-bound x) (lower-bound y))
(* (upper-bound x) (upper-bound y))))
#12

We are interested in the percentage tolerance of the product of two
intervals in terms of their percentage tolerances. In other words,

(percent (mul-interval x y)) #13

You’ll recall the definition of percent from exercise
2.12:

(define (percent i)
(* (/ (width i) (center i)) 100))
#14

Expanding #13 with #14, we get:

(* (/ (width (mul-interval x y))
(center (mul-interval x y)))
100)
#15

To keep things from getting too messy, let’s just focus on the
width part for now:

(width (mul-interval x y)) #16

Substituting in #12

(width (make-interval
(* (lower-bound x)
(lower-bound y))
(* (upper-bound x)
(upper-bound y))))
#17

And the definitions of x and y from #7
and #8:

(width (make-interval
(* (lower-bound (make-interval (- cx (* cx tx))
(+ cx (* cx tx))))
(lower-bound (make-interval (- cy (* cy ty))
(+ cy (* cy ty)))))
(* (upper-bound (make-interval (- cx (* cx tx))
(+ cx (* cx tx))))
(upper-bound (make-interval (- cy (* cy ty))
(+ cy (* cy ty)))))))
#18

Boy, this is getting messy. We can rely on relations #10 and #11 to
tidy things up a bit.

(width (make-interval
(* (- cx (* cx tx))
(- cy (* cy ty)))
(* (+ cx (* cx tx))
(+ cy (* cy ty)))))
#19

Expanding out the products:

(width (make-interval
(- (+ (* cx cy) (* cx tx cy ty))
(+ (* cx cy ty) (* cy cx tx)))
(+ (* cx cy) (*cx tx cy ty)
(* cx cy ty) (* cy cx tx))))
#20

Substituting into #9

(/ (- (upper-bound
(make-interval
(- (+ (* cx cy) (* cx tx cy ty))
(+ (* cx cy ty) (* cy cx tx)))
(+ (* cx cy) (*cx tx cy ty)
(* cx cy ty) (* cy cx tx))))
(lower-bound
(make-interval
(- (+ (* cx cy) (* cx tx cy ty))
(+ (* cx cy ty) (* cy cx tx)))
(+ (* cx cy) (*cx tx cy ty)
(* cx cy ty) (* cy cx tx)))))
2)
#21

Another mess. But we can call on #10 and #11 again to clean things
up.

(/ (- (+ (* cx cy) (*cx tx cy ty)
(* cx cy ty) (* cy cx tx))
(- (+ (* cx cy) (* cx tx cy ty))
(+ (* cx cy ty) (* cy cx tx))))
2)
#22

Let’s rearrange the terms to see what, if anything, cancels out.

(/ (+ (- (* cx cy) (* cx cy))
(- (*cx tx cy ty) (*cx tx cy ty))
(* cx cy ty) (* cx cy ty)
(* cy cx tx) (* cy cx tx))
2)
#23

Now we’re getting somewhere:

(/ (+ (* 2 cx cy ty)
(* 2 cy cx tx))
2)
#24

Simplifying.

(+ (* cx cy ty) (* cy cx tx))) #25

And again.

(* cx cy (+ tx ty)) #26

So now we know what the width of a product is in terms
of the tolerances and centers. Now let’s take a look at the
center of a product.

(center (mul-interval x y)) #27

From exercise 2.12, we know the definiton of center

(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
#28

Expanding #27.

(/ (+ (lower-bound (mul-interval x y))
(upper-bound (mul-interval x y)))
2)
#29

Substituting #12.

(/ (+ (lower-bound (make-interval
(* (lower-bound x) (lower-bound y))
(* (upper-bound x) (upper-bound y))))
(upper-bound (make-interval
(* (lower-bound x) (lower-bound y))
(* (upper-bound x) (upper-bound y)))))
2)
#30

Using #10 and #11, again.

(/ (+ (* (lower-bound x) (lower-bound y))
(* (upper-bound x) (upper-bound y)))
2)
#31

Subsituting in #7 and #8:

(/ (+ (* (lower-bound (make-interval (- cx (* cx tx))
(+ cx (* cx tx))))
(lower-bound (make-interval (- cy (* cy ty))
(+ cy (* cy ty)))))
(* (upper-bound (make-interval (- cx (* cx tx))
(+ cx (* cx tx))))
(upper-bound (make-interval (- cy (* cy ty))
(+ cy (* cy ty))))))
2)
#32

Can’t get enough of #10 and #11:

(/ (+ (* (- cx (* cx tx))
(- cy (* cy ty)))
(* (+ cx (* cx tx))
(+ cy (* cy ty))))
2)
#33

Expanding it all out:

(/ (+ (- (* cx cy) (* cx cy ty))
(- (* cx tx cy ty) (* cy cx tx))
(* cx cy) (* cx cy ty)
(* cx tx cy ty) (* cy cx tx))
2)
#34

Several terms cancel out again, leaving:

(/ (+ (* 2 cx cy)
(* 2 cx tx cy ty))
2)
#35

Since we are assuming small tolerances, the second term
(* 2 cx tx cy ty) is effectively zero, leaving us
with:

(/ (* 2 cx cy)
2)
#36

Or,

(* cx cy) #37

Now we can return to #15, substituting #26 and #37 in:

(* (/ (* cx cy (+ tx ty)))
(* cx cy))
100)
#38

This simplifies quite nicely to:

(* (+ tx ty) 100) #39

Recall from #6 that we defined tx and ty
to eliminate the division by 100. If we switch back to percentage
notation, now, we get rid of that pesky 100 for good:

(+ px py) #40

There you have it. Assuming small tolerances, we can conclude that
the percentage tolerance of the product of two intervals is equal
to the sum of the tolerances of the multiplicands.

Advertisements