my attempt to do the exercises in sicp.

Tuesday, January 4, 2011

sicp exercise 3.80



;; Exercise 3.80.  A series RLC circuit consists of a resistor, a capacitor, and an inductor connected in series, as shown in figure 3.36. If R, L, and C are the resistance, inductance, and capacitance, then the relations between voltage (v) and current (i) for the three components are described by the equations



;;and the circuit connections dictate the relations



;;Combining these equations shows that the state of the circuit (summarized by vC, the voltage across the capacitor, and iL, the current in the inductor) is described by the pair of differential equations



;;The signal-flow diagram representing this system of differential equations is shown in figure 3.37.



;;Figure 3.36:  A series RLC circuit.



;;Figure 3.37:  A signal-flow diagram for the solution to a series RLC circuit.

;;Write a procedure RLC that takes as arguments the parameters R, L, and C of the circuit and the time increment dt. In a manner similar to that of the RC procedure of exercise 3.73, RLC should produce a procedure that takes the initial values of the state variables, vC0 and iL0, and produces a pair (using cons) of the streams of states vC and iL. Using RLC, generate the pair of streams that models the behavior of a series RLC circuit with R = 1 ohm, C = 0.2 farad, L = 1 henry, dt = 0.1 second, and initial values iL0 = 0 amps and vC0 = 10 volts.


(define (display-line str)
  (display str)
  (newline))

(define (display-stream str num)
  (define (internal index)
    (if (> index num) 'printed
        (begin
          (display-line (stream-ref str index))
          (internal (+ 1 index)))))
  (newline)
  (internal 0))

(define (display-stream-pair p num)
  (define (internal index)
    (if (> index num) 'printed
        (begin
          (display-line (list (stream-ref (car p) index) (stream-ref (cdr p) index)))
          (internal (+ 1 index)))))
  (newline)
  (internal 0))
 


(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)
  (define int
    (cons-stream initial-value
                 (let ((integrand (force delayed-integrand)))
                   (add-streams (scale-stream integrand dt)
                                int))))
  int)

(define (RLC R L C dt)
  (lambda(vc0 il0)
    (define vc (integral (delay dvc) vc0 dt))
    (define il (integral (delay dil) il0 dt))
    (define dvc (scale-stream il (/ -1 C)))
    (define dil (add-streams (scale-stream il (/ (* -1 R) L))
                             (scale-stream vc (/ 1 L))))
    (cons vc il)))

(define RLC123 (RLC 1 0.2 1 0.1))

(display-stream-pair (RLC123 10 0) 40)


;; Output
;Loading "sicp_prob_03.80.scm"...
;(10 0)
;(10 5.)
;(9.5 7.5)
;(8.75 8.5)
;(7.9 8.625)
;(7.0375000000000005 8.2625)
;(6.211250000000001 7.6499999999999995)
;(5.446250000000001 6.930624999999999)
;(4.753187500000001 6.1884375)
;(4.134343750000001 5.470812500000001)
;(3.587262500000001 4.802578125000001)
;(3.107004687500001 4.194920312500001)
;(2.687512656250001 3.6509625000000008)
;(2.322416406250001 3.1692375781250006)
;(2.005492648437501 2.7458269921875007)
;(1.7309099492187507 2.375659820312501)
;(1.4933439671875006 2.0532848847656258)
;(1.2880154787109381 1.7733144259765632)
;(1.1106840361132817 1.5306649523437508)
;(.9576175408789066 1.3206744942285162)
;(.825550091456055 1.1391460175537114)
;(.7116354897006838 .9823480545048832)
;(.6134006842501954 .8469917721027835)
;(.528701507039917 .7301962281764895)
;(.4556818842222681 .6294488676082033)
;(.39273699746144775 .5425653759152357)
;(.33848045986992414 .4676511866883417)
;(.29171534120109 .40306582327913293)
;(.2514087588731767 .34739058224011143)
;(.21666970064916558 .29939967055664407)
;(.18672973359350117 .2580346856029048)
;(.1609262650332107 .222382209598203)
;(.1386880440733904 .19165423731570685)
;(.11952262034181971 .16517114069454863)
;(.10300550627236485 .14234688051818417)
;(.08877081822054643 .1226761933952745)
;(.07650319888101897 .10572350580791047)
;(.06593084830022793 .09111335234446472)
;(.05681951306578145 .07852210032234633)
;(.04896730303354682 .06767080669406389)
;(4.2200222364140436e-2 5.8319054863805356e-2)
;... done


1 comment:

Anonymous said...

It seems your value for C was interchanged with one of the other parameters when calling RLC. It should be (RLC 1 1 0.2 0.1), which leads to the following stream of (vc,il)-pairs:

(10 . 0)
(10 . 1.)
(9.5 . 1.9)
(8.55 . 2.66)
(7.220000000000001 . 3.249)
(5.5955 . 3.6461)
...

Thanks for posting your answers!