blob: a514f9042011654a736502f4e7053498dc8338cc (
plain) (
blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
(define (stream-withdraw balance amount-stream)
(cons-stream balance
(stream-withdraw (- balance
(head amount-stream))
(tail amount-stream))))
;; a withdrawal stream
(define (wd-stream)(cons-stream (prompt)(wd-stream)))
(define (prompt)(print "new withdrawal -->")(read))
(define mywd (wd-stream))
;;; insert a few values by (tail(tail (tail mywd)))
;;; (stream-withdraw 100 mywd)
(define (for-each-stream proc stream)
(if (empty-stream? stream)
'done
(sequence (proc (head stream))
(for-each-stream proc (tail stream)))))
;; print a (finite) stream.
(define (print-stream s)
(for-each-stream print s))
(define ps print-stream)
(define (for-each-stream-count proc stream n)
(if (or (= n 0) (empty-stream? stream))
'done
(sequence (proc (head stream))
(for-each-stream-count proc (tail stream) (+ n -1)))))
(define (psn s n)(for-each-stream-count print s n))
;;;;;;;;;;;;;;;;
;;; TAYLOR SERIES
;;; (we could use abstraction better here...)
;;; a term in a series looks like (a b) , a list
;;; signifying b*x^a
;; here is the number 1 = 1*x^0
(define unit-term (list 0 1)) ;; could be (make-term 0 1)
(define (integrate-series series)
(map integrate-term series))
;;; to integrate b*x^a with respect to x, produce
;;; (b/(a+1)*x^(a+1)
(define (integrate-term tt)
(let ((new-order (1+ (car tt)))) ;; could use (order tt) instead of car
(list new-order (/ (cadr tt) new-order)))) ;; make-term; coeff;
(define exp-series (cons-stream unit-term (integrate-series exp-series)))
;; (- integrate sin) to get cos (also, cos(0) = 1)
;; cos x is 1 -x^2/2 + x^4/4! + ..
;; 1 -0.5 x^2 + 0.04166 x^4+ ...
(define cos-series (cons-stream unit-term
(neg-st (integrate-series sin-series))))
;; we need to compute the negative of a "taylor series stream"
(define (neg-st s)(if (empty-stream? s) s
(cons-stream (list (car (head s))(- (cadr (head s))))
(neg-st (tail s)))))
;; integrate cos to get sin
(define sin-series (integrate-series cos-series))
;;; sin x = x -x^3/3!+ x^5/5! + ...
;;; x - 0.166 x^3 + 0.0083*x^5 + ...
;;(psn sin-series 5)
|