]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 2.14
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Mon, 14 Jun 2010 03:20:02 +0000 (08:50 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Mon, 14 Jun 2010 03:20:02 +0000 (08:50 +0530)
src/sicp/ch2_1_extended.clj
src/sicp/ex2_14.clj [new file with mode: 0644]

index 399b66246a0ef72112ed38b2fc1b7811e6a0ae3e..dca737b4e737e47fc858b95f4c507ac769f13298 100644 (file)
 
 (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 (file)
index 0000000..e0aaf62
--- /dev/null
@@ -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