Blob


1 (define (sum term a next b)
2 (if (> a b)
3 0
4 (+ (term a)
5 (sum term (next a) next b))))
7 ;; (define (simpsons-rule f a b n)
8 ;; (let ((h (/ (- b a) n)))
9 ;; (define (running-sum k)
10 ;; (let ((akh (+ a (* k h))))
11 ;; (if (= k n)
12 ;; (f akh)
13 ;; (+ (cond ((= k 0) (f akh))
14 ;; ((even? k) (* 2 (f akh)))
15 ;; ((odd? k) (* 4 (f akh))))
16 ;; (running-sum (+ k 1))))))
17 ;; (* (/ h 3)
18 ;; (running-sum 0))))
20 (define (simpsons-rule f a b n)
21 (let ((h (/ (- b a) n)))
22 (define (simpsons-term k)
23 (let ((akh (+ a (* k h))))
24 (* (f akh)
25 (cond ((or (= k 0) (= k n)) 1)
26 ((odd? k) 4)
27 ((even? k) 2)))))
28 (* (/ h 3)
29 (sum simpsons-term 0 1+ n))))
32 (define (test-case actual expected)
33 (load-option 'format)
34 (newline)
35 (format #t "Actual: ~A Expected: ~A" actual expected))
37 (define (cube x) (* x x x))
39 (test-case (simpsons-rule cube 0.0 1.0 5) 0.25)
40 (test-case (simpsons-rule cube 0.0 1.0 10) 0.25)
41 (test-case (simpsons-rule cube 0.0 1.0 100) 0.25)