読者です 読者をやめる 読者になる 読者になる

SICPゼミ第31回

練習問題3.77
(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)))))

by tube

練習問題3.78
(define (solve-2nd a b y0 dy0 dt)
  (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)

下のコードで動かしてみた。cos になればOK

(display-stream-count (solve-2nd 0 -1 1 0 0.001) 10000)

結果
f:id:sicp-zemi:20161123174341p:plain

やったー!

by tube

練習問題3.79
(define (solve-2nd f y0 dy0 dt)
  (define y (integral (delay dy) y0 dt))
  (define dy (integral (delay ddy) dy0 dt))
  (define ddy (stream-map f y))
  y)


結果。sin になればOK

(display-stream-count (solve-2nd (lambda (y) (* y -1)) 0 1 0.001) 10000)


f:id:sicp-zemi:20161123180150p:plain

やったーー!
by tube

練習問題3.80
(define (solve-2-1st-ode f g s0 t0 dt)
  (define s (integral (delay ds) s0 dt))
  (define t (integral (delay dtt) t0 dt))
  (define ds (stream-map f s t))
  (define dtt (stream-map g s t))  
  (cons s t)
)

(define (RLC R L C dt)
  (define (f v i) (- (/ i C)))
  (define (g v i)  (- (/ v L) (/ (* R i) L)))
  (define (return vc0 il0)
    (solve-2-1st-ode f g vc0 il0 dt)
    )
  return)

by dolicas

(display-stream-count ((RLC 1 1 0.2 0.1) 10 0) 500)

f:id:sicp-zemi:20161123181842p:plain

(define (rlc-circuit R L C dt)
  (define (get-initial Vc0 iL0)
    (define Vc (integral (delay dVc) Vc0 dt))
    (define dVc (scale-stream iL (- (/ 1 C))))
    (define diL (add-streams (scale-stream Vc (/ 1 L)) (scale-stream iL (- (/ R L))) ))
    (define iL (integral (delay diL) iL0 dt))
    (cons Vc iL))
  get-initial)

なぜか動かない。