<div dir="ltr"><div><div>I've asked the question on <a href="http://stackoverflow.com/questions/19706893/implementation-of-simpsons-rule-sicp-exercise-1-29">Stack Overflow</a> [1]. Óscar López gave me an</div><div>answer. But I still don't understand it. So I re-post it here:</div>
</div><div><br></div><div><div>Following is my code for <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html">SICP exercise 1.29 </a>[2]. The exercise asks us to</div><div>implement Simpson's Rule using higher order procedure `sum`. It's supposed to be</div>
<div>more accurate than the original `integral` procedure. But I don't know why it's</div><div>not the case in my code:</div></div><div><br></div><div><font face="courier new, monospace"> (define (simpson-integral f a b n)</font></div>
<div><font face="courier new, monospace"> (define h (/ (- b a) n))</font></div><div><font face="courier new, monospace"> (define (next x) (+ x (* 2 h)))</font></div><div><font face="courier new, monospace"> (* (/ h 3) (+ (f a)</font></div>
<div><font face="courier new, monospace"> (* 4 (sum f (+ a h) next (- b h)))</font></div><div><font face="courier new, monospace"> (* 2 (sum f (+ a (* 2 h)) next (- b (* 2 h))))</font></div>
<div><font face="courier new, monospace"> (f b))))</font></div><div><br></div><div>Some explanations of my code: As</div><div><br></div><div><font face="courier new, monospace"> h/3 * (y_{0} + 4*y_{1} + 2*y_{2} + 4*y_{3} + 2*y_{4} + ... + 2*y_{n-2} + 4*y_{n-1} + y_{n})</font></div>
<div><br></div><div>equals</div><div><br></div><div><font face="courier new, monospace"> h/3 * (y_{0}</font></div><div><font face="courier new, monospace"> + 4 * (y_{1} + y_{3} + ... + y_{n-1})</font></div><div>
<font face="courier new, monospace"> + 2 * (y_{2} + y_{4} + ... + y_{n-2})</font></div><div><font face="courier new, monospace"> + y_{n})</font></div><div><br></div><div>I just use `sum` to compute `y_{1} + y_{3} + ... + y_{n-1}` and `y_{2} +</div>
<div>y_{4} + ... + y_{n-2}`.</div><div><br></div><div>Complete code here:</div><div><br></div><div><font face="courier new, monospace"> #lang racket</font></div><div><font face="courier new, monospace"> (define (cube x) (* x x x))</font></div>
<div><font face="courier new, monospace"> (define (sum term a next b)</font></div><div><font face="courier new, monospace"> (if (> a b)</font></div><div><font face="courier new, monospace"> 0</font></div>
<div><font face="courier new, monospace"> (+ (term a)</font></div><div><font face="courier new, monospace"> (sum term (next a) next b))))</font></div><div><font face="courier new, monospace"> (define (integral f a b dx)</font></div>
<div><font face="courier new, monospace"> (define (add-dx x) (+ x dx))</font></div><div><font face="courier new, monospace"> (* (sum f (+ a (/ dx 2.0)) add-dx b)</font></div><div><font face="courier new, monospace"> dx))</font></div>
<div><font face="courier new, monospace"> (define (simpson-integral f a b n)</font></div><div><font face="courier new, monospace"> (define h (/ (- b a) n))</font></div><div><font face="courier new, monospace"> (define (next x) (+ x (* 2 h)))</font></div>
<div><font face="courier new, monospace"> (* (/ h 3) (+ (f a)</font></div><div><font face="courier new, monospace"> (* 4 (sum f (+ a h) next (- b h)))</font></div><div><font face="courier new, monospace"> (* 2 (sum f (+ a (* 2 h)) next (- b (* 2 h))))</font></div>
<div><font face="courier new, monospace"> (f b))))</font></div><div><br></div><div>Some tests(The exact value should be 0.25):</div><div><br></div><div><font face="courier new, monospace"> > (integral cube 0 1 0.01)</font></div>
<div><font face="courier new, monospace"> 0.24998750000000042</font></div><div><font face="courier new, monospace"> > (integral cube 0 1 0.001)</font></div><div><font face="courier new, monospace"> 0.249999875000001</font></div>
<div><font face="courier new, monospace"> </font></div><div><font face="courier new, monospace"> > (simpson-integral cube 0 1.0 100)</font></div><div><font face="courier new, monospace"> 0.23078806666666699</font></div>
<div><font face="courier new, monospace"> > (simpson-integral cube 0 1.0 1000)</font></div><div><font face="courier new, monospace"> 0.24800798800666748</font></div><div><font face="courier new, monospace"> > (simpson-integral cube 0 1.0 10000)</font></div>
<div><font face="courier new, monospace"> 0.2499999999999509</font></div><div><br></div><div>[1] <a href="http://stackoverflow.com/questions/19706893/implementation-of-simpsons-rule-sicp-exercise-1-29">http://stackoverflow.com/questions/19706893/implementation-of-simpsons-rule-sicp-exercise-1-29</a></div>
<div>[2] <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html">http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html</a></div><div><br></div><div>Thanks</div></div>