-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch1.3.1.scm
57 lines (37 loc) · 1.03 KB
/
ch1.3.1.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(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 (even? n)
(= (remainder n 2) 0))
(define (simpson f a b n)
(define (simpit f a b n k acc)
(let ((h (/ (- b a) n)))
(cond ((= k n) (+ acc (f (+ a (* k h)))))
((even? k) (simpit f a b n (+ k 1) (+ acc (* 4 (f (+ a (* k h)))))))
((= k 0) (simpit f a b n (+ k 1) (+acc (f (+ a (* k h))))))
(else (simpit f a b n (+ k 1) (+ acc (* 2 (f (+ a (* k h))))))))))
(* (/ (/ (- b a) n) 3) (simpit f a b n 0 0)))
(define (simpsum f a b n)
(define (h)
(/ (- b a) n))
(define (y k)
(f (+ a (* k (h)))))
(define (term x)
(cond ((= x n) (y x))
((= x 0) (y x))
((even? x) (* 4 (y x)))
(else (* 2 (y x)))))
(* (/ (h) 3) (sum term 0 inc n)))
(define (inc x)
(+ x 1))
(define (cube x)
(* x x x))
(integral cube 0 1 0.01)
(simpson cube 0 1 1000)
(simpsum cube 0 1 1000)