(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (inc x) (+ x 1)) (define (simpsons-rule f a b n) (define h (/ (- b a) n)) (define (term k) (* (f (+ a (* k h))) (cond ((= k 0) 1) ((= k n) 1) ((even? k) 2) (else 4)))) (/ (* h (sum term 0 inc n)) 3)) (define (cube x) (* x x x)) ; Simpson's rule is much more accurate than the naive integration method ; 1 ]=> (simpsons-rule cube 0.0 1.0 100) ; ; ;Value: .24999999999999992 ; ; 1 ]=> (simpsons-rule cube 0.0 1.0 1000) ; ; ;Value: .2500000000000003 ; ; 1 ]=> (simpsons-rule cube 0.0 1.0 10) ; ; ;Value: .25000000000000006