]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex1_29.clj
namespace confusions resolved.
[sicp.git] / src / sicp / ex1_29.clj
1 (ns sicp.ex1_29
2   (:use [sicp utils]
3         [clojure.contrib trace test-is]))
4
5 ;; simpson's rule of integration
6 (defn sum [term a next b]
7   (if (> a b)
8     0
9     (+ (term a)
10        (sum term (next a) next b))))
11
12 (defn make-fac [k n]
13   (cond (= k 0) 1
14         (= k n) 1
15         (even? k) 2
16         :else 4))
17
18 (defn simpson-sum [term a next-fn b n k]
19   (let [fac (make-fac k n)]
20     (if (> k n)
21       0
22       (+ (* fac (term a))
23          (simpson-sum term (next-fn a) next-fn b n (+ k 1))))))
24
25 (defn simpson-rule [f a b n]
26   (let [h (/ (- b a) n)]
27     (* (simpson-sum f a #(+ h %) b n 0)
28        (/ h 3.0))))