From: Ramakrishnan Muthukrishnan Date: Mon, 14 Jun 2010 03:20:02 +0000 (+0530) Subject: solution to 2.14 X-Git-Url: https://git.rkrishnan.org/components/%22news.html/...?a=commitdiff_plain;h=7dcde3358da6c67365387ad2a4280368277df79b;p=sicp.git solution to 2.14 --- diff --git a/src/sicp/ch2_1_extended.clj b/src/sicp/ch2_1_extended.clj index 399b662..dca737b 100644 --- a/src/sicp/ch2_1_extended.clj +++ b/src/sicp/ch2_1_extended.clj @@ -32,3 +32,14 @@ (defn width [i] (/ (- (upper-bound i) (lower-bound i)) 2)) + +;; parallel-resistor formula +(defn par1 [r1 r2] + (div-interval (mul-interval r1 r2) + (add-interval r1 r2))) + +(defn par2 [r1 r2] + (let [one (make-interval 1 1)] + (div-interval one + (add-interval (div-interval one r1) + (div-interval one r2))))) \ No newline at end of file diff --git a/src/sicp/ex2_14.clj b/src/sicp/ex2_14.clj new file mode 100644 index 0000000..e0aaf62 --- /dev/null +++ b/src/sicp/ex2_14.clj @@ -0,0 +1,30 @@ +(ns sicp.ex2_14 + (:use [sicp ch2_1_extended ex2_12 ex2_7])) + +(def r1 (make-center-percent 100 5)) +(def r2 (make-center-percent 500 5)) + +(par1 r1 r2) +;;=> (71.62698412698413 96.71052631578948) +(par2 r1 r2) +;;=> (79.16666666666667 87.49999999999999) + +(def r4 (make-center-percent 500 1)) +(def r3 (make-center-percent 1000 1)) + +(div-interval r3 r4) +;;=> (1.9603960396039604 2.04040404040404) + +(div-interval r4 r4) +;;=> (0.9801980198019802 1.02020202020202) + +(div-interval r4 r3) +;;=> (0.4900990099009901 0.51010101010101) + +(percentage (div-interval r4 r3)) +;;=> 1.999800019998008 + +(percentage (div-interval r3 r3)) +;;=> 1.999800019998008 +(center (div-interval r3 r3)) +;;=> 1.000200020002 \ No newline at end of file