read

[CODE]
(define (integral f a b n)
  (define (h) (/ (- b a) n))
  (define (y k) (f (+ a (* k (h)))))
  (define (integral-function i)
    (* (/ (h) 3) (* (y i) (cond ((or (= i 0) (= i n)) 1)
                              ((odd? i) 4)
                              ((even? i) 2)))))
  (define (integral-iter sum i)
    (if (> i n) sum
        (integral-iter (+ sum (integral-function i)) (+ i 1))))
  (integral-iter 0 0))

(define (cube x) (* x x x))

; Example in textbook
(define (integral2 f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2.0)) add-dx b)
     dx))

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))
[/CODE]
수행 결과
> (integral cube 0 1 100)
1/4
> (integral cube 0 1 1000)
1/4
> (integral2 cube 0 1 0.01)
0.24998750000000042
> (integral2 cube 0 1 0.001)
0.249999875000001

Simpson rule 을 사용한 방법이 cube 함수에 대해서는 훨씬 참에 가까운 값을 리턴해준다.

Blog Logo

Ki Sung Bae


Published

Image

Gsong's Blog

Developer + Entrepreneur = Entreveloper

Back to Overview