sicp 1.29

simpson积分:

 

(define (simpson f a b n)
  (let ((h (/ (- b a) n)))
    (/ (* h
       (+ (f a)
          (f b)
          (* 4 (sum f (+ a h) (lambda (x) (+ x (* 2 h))) (- b h)))
          (* 2 (sum f (+ a h h) (lambda (x) (+ x (* 2 h))) (- b (* 2 h))))))
       3.0)))

(define (integral f a b dx)
  (* (sum f (+ a (/ dx 2.0)) (lambda (x) (+ x dx)) b) dx))

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

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

 

> (simpson cube 0 1 2)
0.25

 

simpson积分对于三次函数,取n=2即可以得出精确值,不明白书中为何要求计算n=100,1000的值

你可能感兴趣的:(F#)