Implement till 3.78
parent
5dddcd897c
commit
42a52d14c4
|
@ -77,10 +77,62 @@
|
|||
(assert (take 11 (make-zero-crossings-smoothed sense-data 0))
|
||||
'(0 0 0 0 0 -1 0 0 0 0 1))
|
||||
|
||||
(display "\nex-3.77\n")
|
||||
(display "\nex-3.77 - lazy-integral\n")
|
||||
|
||||
(define (integral delayed-integrand initial-value dt)
|
||||
(define int
|
||||
(cons-stream initial-value
|
||||
(let ((integrand (force delayed-integrand)))
|
||||
(add-streams (scale-stream integrand dt)
|
||||
int))))
|
||||
int)
|
||||
|
||||
(define (solve f y0 dt)
|
||||
(define y (integral (delay dy) y0 dt))
|
||||
(define dy (stream-map f y))
|
||||
y)
|
||||
|
||||
(assert (stream-ref (solve (lambda (y) y) 1 0.001) 1000) 2.716923932235896)
|
||||
|
||||
(define (integral delayed-integrand initial-value dt)
|
||||
(cons-stream
|
||||
initial-value
|
||||
(let ((integrand (force delayed-integrand)))
|
||||
(if (stream-null? integrand)
|
||||
the-empty-stream
|
||||
(integral (delay (stream-cdr integrand))
|
||||
(+ (* dt (stream-car integrand)) initial-value)
|
||||
dt)))))
|
||||
|
||||
(assert (stream-ref (solve (lambda (y) y) 1 0.001) 1000) 2.716923932235896)
|
||||
|
||||
(display "\nex-3.78 - solve-2nd\n")
|
||||
|
||||
; The output stream, modeling y, is generated by a network that contains a loop.
|
||||
; This is because the value of d2y/dt2 depends upon the values of y and dy/dt and
|
||||
; both of these are determined by integrating d2y/dt2. The diagram we would like
|
||||
; to encode is shown in figure 3.35. Write a procedure solve-2nd that takes as
|
||||
; arguments the constants a, b, and dt and the initial values y0 and dy0 for y
|
||||
; and dy/dt and generates the stream of successive values of y.
|
||||
|
||||
(define (solve-2nd a b dt y0 dy0)
|
||||
(define y (integral (delay dy) y0 dt))
|
||||
(define dy (integral (delay ddy) dy0 dt))
|
||||
(define ddy (add-streams (scale-stream dy a)
|
||||
(scale-stream y b)))
|
||||
y)
|
||||
|
||||
(assert (stream-ref (solve-2nd 1 0 0.0001 1 1) 10000)
|
||||
2.7181459268252266) ; e
|
||||
(assert (stream-ref (solve-2nd 0 -1 0.0001 1 0) 10472)
|
||||
0.5000240628699462) ; cos pi/3 = 0.5
|
||||
(assert (stream-ref (solve-2nd 0 -1 0.0001 0 1) 5236)
|
||||
0.5000141490501059) ; sin pi/6 = 0.5
|
||||
|
||||
(display "\nex-3.79 - solve-2nd-general\n")
|
||||
|
||||
(define (solve-2nd-general a b) '())
|
||||
|
||||
;(display "\nex-3.78\n")
|
||||
;(display "\nex-3.79\n")
|
||||
;(display "\nex-3.80\n")
|
||||
;(display "\nex-3.81\n")
|
||||
;(display "\nex-3.82\n")
|
||||
|
|
Loading…
Reference in New Issue