Exercise 1.29. Simpson’s Rule is a more accurate method of numerical integration than the method illustrated above. Using Simpson’s Rule, the integral of a function f between a and b is approximated as
where h = (b – a)/n, for some even integer n, and yk = f(a + kh). (Increasing n increases the accuracy of the approximation.) Define a procedure that takes as arguments f, a, b, and n and returns the value of the integral, computed using Simpson’s Rule. Use your procedure to integrate cube between 0 and 1 (with n = 100 and n = 1000), and compare the results to those of the integral procedure shown above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (even? n) | |
(= (remainder n 2) 0)) | |
(define (inc n) (+ n 1)) | |
(define (cube x) (* (* x x) x)) | |
(define | |
(sum-iter-helper | |
runningsum | |
termfunction | |
termvalue | |
nextfunction | |
upperbound) | |
(if | |
(> termvalue upperbound) | |
runningsum | |
(sum-iter-helper | |
(+ runningsum (termfunction termvalue)) | |
termfunction | |
(nextfunction termvalue) | |
nextfunction | |
upperbound))) | |
(define (sum term a next b) | |
(sum-iter-helper | |
0 | |
term | |
a | |
next | |
b)) | |
(define (simpson-rule f a b n) | |
(define (find-yk k) | |
(if (even? k) | |
(* | |
2 | |
(f (+ a (* k (/ | |
(- b a) | |
n))))) | |
(* | |
4 | |
(f (+ a (* k (/ | |
(- b a) | |
n))))))) | |
(* | |
1.0 | |
(* | |
(/ | |
(/ | |
(- b a) | |
n) | |
3) | |
(sum | |
find-yk | |
0 | |
inc | |
n))) | |