2 (:use [clojure.contrib test-is]
5 (defn iterative-improve [good-enough-fn? improve-guess-fn]
7 (let [new-guess (improve-guess-fn guess)]
8 (if (good-enough-fn? guess new-guess)
13 (let [initial-guess 1.0]
15 (fn [old new] (< (Math/abs (- old new)) 0.00001))
16 (fn [guess] (average guess (/ x guess))))
19 (defn fixed-point [f initial-guess]
21 (fn [old new] (< (Math/abs (- old new)) 0.00001))
25 (is #(< (Math/abs (- (sqrt 4) (Math/sqrt 4))) 0.00001)))
28 (is #(< (Math/abs (- (sqrt 10) (Math/sqrt 10))) 0.00001)))
30 (deftest test-fixed-point-cos
31 (is (fn [_] (< (Math/abs (- (fixed-point #(Math/cos %) 1.0)
35 (deftest test-fixed-point-cos-plus-sin
36 (is (fn [_] (< (Math/abs (- (fixed-point #(+ (Math/cos %) (Math/sin %)) 1.0)
43 user> (fixed-point #(Math/cos %) 1.0)
44 ;;=> 0.7390822985224024
45 user> (fixed-point #(+ (Math/cos %) (Math/sin %)) 1.0)
46 ;;=> 1.2587315962971173