Wednesday, June 3, 2009

Ex-1.29

(define (integral f a b n)
(define (sum-iter partial-sum h count coefficient)
(cond ((= count n)
(+ partial-sum (f (+ a (* n h)))))
((= count 0)
(sum-iter (+ partial-sum (f a)) h (+ count 1) 4))
(else
(sum-iter
(+ partial-sum
(* coefficient (f (+ a (* count h)))))
h (+ count 1) (if (= coefficient 2) 4 2)))))
(* (/ (/ (- b a) n) 3)
(sum-iter 0 (/ (- b a) n) 0 1)))

(define (cube x) (* x x x))

1 ]=> (integral cube 0.0 1.0 100)
;Value: .25000000000000006
1 ]=> (integral cube 0.0 1.0 1000)
;Value: .25000000000000006

No comments:

Post a Comment