41 lines
743 B
Scheme
41 lines
743 B
Scheme
|
;; 1.29
|
||
|
(define(cube x)
|
||
|
(* x x x))
|
||
|
|
||
|
(define(sum-integers a b)
|
||
|
(if ( > a b)
|
||
|
0
|
||
|
(+ a (sum-integers (+ a 1) b))))
|
||
|
|
||
|
(define(sum-cubes a b)
|
||
|
(if (> a b)
|
||
|
0
|
||
|
(+ (cube a) (sum-cubes(+ a 1) b))))
|
||
|
|
||
|
|
||
|
(define (sum term a next b)
|
||
|
(if (> a b)
|
||
|
0
|
||
|
(+ (term a)
|
||
|
(sum term (next a) next b))))
|
||
|
|
||
|
(define (integral f a b dx)
|
||
|
(define (add-dx x) (+ x dx))
|
||
|
(* (sum f (+ a (/ dx 2.0)) add-dx b)
|
||
|
dx))
|
||
|
|
||
|
(define (simpson f a b n)
|
||
|
(define h (/ (- b a) n))
|
||
|
(define (add-hh a) (+ a h h))
|
||
|
(* (+ (- (f a))
|
||
|
(* 2 (sum f a add-hh b))
|
||
|
(* 4 (sum f (+ a h) add-hh b))
|
||
|
(- (f b)))
|
||
|
(/ h 3.0)))
|
||
|
|
||
|
|
||
|
(display (simpson cube 1 2 100))
|
||
|
(display "\n")
|
||
|
(display (simpson cube 1 2 1000))
|
||
|
(display "\n")
|