From: Ramakrishnan Muthukrishnan Date: Mon, 10 May 2010 17:18:38 +0000 (+0530) Subject: section 1.3.3 X-Git-Url: https://git.rkrishnan.org/simplejson/components/%22news.html//%22%3C?a=commitdiff_plain;h=7dc93f494694d882d0ee832e25cdc7246767640e;p=sicp.git section 1.3.3 --- diff --git a/src/sicp/ch1_3.clj b/src/sicp/ch1_3.clj index 0cf7b28..ebf7a20 100644 --- a/src/sicp/ch1_3.clj +++ b/src/sicp/ch1_3.clj @@ -1,5 +1,4 @@ - -(ns sicp.ch1-3 +(ns sicp.ch1_3 (:use [sicp utils] [clojure.contrib test-is])) @@ -40,4 +39,57 @@ (integral cube 0 1 0.001) ;;=>0.249999875000001 (integral cube 0 1 0.005) -;;=>0.24999687500000028 \ No newline at end of file +;;=>0.24999687500000028 + +;; section 1.3.3 +(defn close-enough? [x y tolerance] + (< (abs (- x y)) tolerance)) + +(defn search [f neg-point pos-point] + (let [mid-point (average neg-point pos-point)] + (if (close-enough? neg-point pos-point 0.001) + mid-point + (let [test-value (f mid-point)] + (cond (pos? test-value) (search f neg-point mid-point) + (neg? test-value) (search f mid-point pos-point) + :else mid-point))))) + +(defn half-interval-method [f a b] + (let [a-val (f a) + b-val (f b)] + (cond (and (neg? a-val) (pos? b-val)) (search f a b) + (and (pos? a-val) (neg? b-val)) (search f b a) + :else (str "values are not of opposite sign")))) + +(comment + (half-interval-method #(Math/sin %) 2.0 4.0) + ;;=> 3.14111328125 + (half-interval-method (fn [x] (- (* x x x) (* 2 x) 3)) 1.0 2.0) + ;;=> 1.89306640625 +) + +;;; fixed point of a function +(defn fixed-point [f guess] + (let [next (f guess)] + (if (close-enough? next guess 0.00001) + next + (fixed-point f next)))) + +(comment +(fixed-point #(Math/cos %) 1.0) +;;;=> 0.7390822985224024 +(fixed-point #(+ (Math/cos %) (Math/sin %)) 1.0) +;;;=> 1.2587315962971173 +) + +;; sqrt as fixed point of y/x +(defn mysqrt [x] + (fixed-point (fn [y] (average y (/ x y))) + 1.0)) + +(comment +(mysqrt 10) +;;;=> 3.162277660168379 +(mysqrt 4) +;;;=> 2.000000000000002 +) \ No newline at end of file diff --git a/src/sicp/utils.clj b/src/sicp/utils.clj index 0ee9355..3ab538c 100644 --- a/src/sicp/utils.clj +++ b/src/sicp/utils.clj @@ -35,6 +35,9 @@ a (gcd b (rem a b)))) +(defn average [a b] + (/ (+ a b) 2.0)) + (defmacro microbench " Evaluates the expression n number of times, returning the average time spent in computation, removing highest and lowest values.