simpson法の実装。(- split 2)が汚い。もっといい方法があるんだろうけど。
(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (simpson f a b split) (define h (/ (- b (* 1.0 a)) split)) (define (y k) (f (+ a (* k h)))) (define (next i) (+ i 2)) (define (term n) (+ (y n) (* 4 (y (+ n 1))) (y (+ n 2)))) (* (/ h 3.0) (sum term 0 next (- split 2))))
x^3を定積分してみる。
gosh> (define (cube x) (* x x x)) cube gosh> (simpson cube 0 1 100) gosh> 0.2500000000000001 gosh> (simpson cube 0 1 1000) gosh> 0.25000000000000017