# fastred/sicp

1 parent a8d2d7d commit f856360bb24bdd18af619ec49377a8441b9c427b committed Jan 20, 2013
Showing with 86 additions and 0 deletions.
1. +28 −0 2_13.scm
2. +55 −0 2_14.scm
3. +3 −0 2_15.scm
28 2_13.scm
 @@ -0,0 +1,28 @@ +(define p1 0.01) +(define p2 0.01) +(define c1 2) +(define c2 3) +(define a (make-center-percent c1 p1)) +(define b (make-center-percent c2 p2)) + +Evaluation of (mul-interval a b) will produce these values of p1-p4: + +p1 = (c1 - (c1 * p1)) * (c2 - (c2 * p2)) = (c1 - c1*p1) * (c2 - c2*p2) + = (c1*c2 - c1*c2*p2 - c2*c1*p1 + c1*c2*p1*p2) +p2 = (c1 - (c1 * p1)) * (c2 + (c2 * p2)) = (c1 - c1*p1) * (c2 + c2*p2) + = (c1*c2 + c1*c2*p2 - c2*c1*p1 - c1*c2*p1*p2) +p3 = (c1 + (c1 * p1)) * (c2 - (c2 * p2)) = (c1 + c1*p1) * (c2 - c2*p2) + = (c1*c2 - c1*c2*p2 + c2*c1*p1 - c1*c2*p1*p2) +p4 = (c1 + (c1 * p1)) * (c2 + (c2 * p2)) = (c1 + c1*p1) * (c2 + c2*p2) + = (c1*c2 + c1*c2*p2 + c2*c1*p1 + c1*c2*p1*p2) + +Assuming intervals are positive the resulting interval will span points p1 and p4. + +Now we can calculate properties of resulting interval: +width of result = (p4 - p1)/2 = (2*c1*c2*p2 + 2*c1*c2*p1)/2 = c1*c2*(p1 + p2) +center of result = (p1 + p4)/2 = (2*c1*c2 + 2*c1*c2*p1*p2)/2 = c1*c2 + c1*c2*p1*p2 = c1*c2*(1 + p1*p2) +percentage error of result = (width of result) / (center of result) = +c1*c2*(p1 + p2)/(c1*c2*(1 + p1*p2)) = (p1 + p2)/(1 + p1*p2) + +p1*p2 will be really small, so we can approximate resulting percentage tolerance to: p1 + p2 +
55 2_14.scm
 @@ -0,0 +1,55 @@ +(load "2_12.scm") +(define (add-interval x y) + (make-interval (+ (lower-bound x) (lower-bound y)) + (+ (upper-bound x) (upper-bound y)))) + +(define (div-interval x y) + (mul-interval x + (make-interval (/ 1.0 (upper-bound y)) + (/ 1.0 (lower-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 p1 0.001) +(define p2 0.001) +(define c1 3) +(define c2 6) +(define a (make-center-percent c1 p1)) +(define b (make-center-percent c2 p2)) + +(define (par1 r1 r2) + (div-interval (mul-interval r1 r2) + (add-interval r1 r2))) +(define (par2 r1 r2) + (let ((one (make-interval 1 1))) + (div-interval one + (add-interval (div-interval one r1) + (div-interval one r2))))) + +(define res (par1 a a)) +(center res) +;1.5000000005999998 +(percent res) +;2.999999999219334e-3 +(define res (par2 a a)) +(center res) +;1.5 +(percent res) +;1.0000000000065512e-3 +(define res (par1 a b)) +(center res) +;2.0000000008 +(percent res) +;2.9999999991971293e-3 +(define res (par2 a b)) +(center res) +;2. +(percent res) +;1.0000000000065512e-3 +
3 2_15.scm
 @@ -0,0 +1,3 @@ +Eva Lu Ator is right because every additional algebraic expression using variable +with uncertainty causes cumulative uncertainty to grow. +