1.29|

$ cd ~

SICP Exercise 1.29

The power of sigma notation is that it allows mathematicians to deal with the concept of summation itself rather than only with particular sums-for example, to formulate a general result about sums that are independant of the particular series being summed.

Building a procedure for the Simpson’s Rule:

\[\int^a_b f(x) dx = \frac{h}{3} (y_0 + 4y_1 + 2y_2 + 4y_3 + 2y_4 + \dots + 2y_{n-2} + 4y_{n-1} + y_n) \\\]

where,

\[h = \frac{b-a}{n}\]

And here’s the procedure:

(define (sum term a next b) 
   (if (> a b) 
       0 
       (+ (term a) 
          (sum term (next a) next b)))) 

(define (simpson f a b n)
  (define h (/ (- b a) n))
  (define (yk k) (f (+ a (* k h))))
  (define (s-term k)
    (* (cond ((or (= k 0) (= k n)) 1)
             ((odd? k) 4)
             (else 2))
       (yk k)))
  (* (/ h 3) (sum s-term 0 inc n)))

(define (cube x) (* x x x)) 
  
(define (inc n) (+ n 1))