-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.29.scm
59 lines (41 loc) · 1.04 KB
/
1.29.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
55
56
57
58
59
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (inc n)
(+ n 1))
(define (cube a)
(* a a a))
(define (sum-cubes a b)
(sum cube a inc b))
(sum-cubes 1 10) ; 3025
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
(sum-integers 1 10) ; 55
(define (pi-sum a b)
(define (pi-next x)
(+ x 4))
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(sum pi-term a pi-next b))
(* 8 (pi-sum 1 1000)) ; 3.139592655589783
(define (integral f a b dx)
(define (add-dx x)
(+ x dx))
(* dx
(sum f (+ a (/ dx 2)) add-dx b)))
(integral cube 0 1 0.01) ; 0.24998750000000042
(integral cube 0 1 0.001) ; 0.249999875000001
(define (simpson f a b n)
(define h (/ (- b a) n))
(define (next x)
(+ x h h))
(* (/ h 3)
(+ (- (f a))
(- (f b))
(* 2 (sum f a next b))
(* 4 (sum f (+ a h) next b)))))
(simpson cube 0 1 100) ; 1/4
(simpson cube 0 1 1000) ; 1/4