Loading ...
Sorry, an error occurred while loading the content.

Ex. 1.29 Simpson's Rule for Integration

Expand Messages
  • Daniel Hawkins
    Hi, everyone. I m new here, but I thought I d contribute. I realize that we ve sort of passed over this exercise, but I wanted to post my solution in case it
    Message 1 of 1 , May 7, 2005
    • 0 Attachment
      Hi, everyone. I'm new here, but I thought I'd contribute. I realize
      that we've sort of passed over this exercise, but I wanted to post my
      solution in case it might help anyone. If anyone has suggestions or
      comments, feel free. Here's Ex. 1.29:

      (define (integrate f a b n)
      (define h (/ (- b a) n))
      (define (add-h x) (+ x h))
      (define (sum term x next y)
      (cond ((> x y) 0)
      ((or (= x a) (= x b))
      (+ (term x)
      (sum term (next x) next y)))
      ((and (divides? (- x a) (* 2 h)) (not (= x b)))
      (+ (* 2 (term x))
      (sum term (next x) next y)))
      (else
      (+ (* 4 (term x))
      (sum term (next x) next y)))))
      (* (/ h 3) (sum f a add-h b)))
      (define (divides? a b)
      (integer? (/ a b)))
    Your message has been successfully submitted and would be delivered to recipients shortly.