1 day ago
my attempt to do the exercises in sicp.
Tuesday, January 4, 2011
sicp exercise 3.78
;; Exercise 3.78.
;;Figure 3.35: Signal-flow diagram for the solution to a second-order linear differential equation.
;;Consider the problem of designing a signal-processing system to study the homogeneous second-order linear differential equation
;;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 (display-line str)
(define (display-stream str num)
(define (internal index)
(if (> index num) 'printed
(display-line (stream-ref str index))
(internal (+ 1 index)))))
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream))
(define (add-streams s1 s2)
(stream-map + s1 s2))
(define (integral delayed-integrand initial-value dt)
(let ((integrand (force delayed-integrand)))
(add-streams (scale-stream integrand dt)
(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)))