                MASSACHUSETTS INSTITUTE OF TECHNOLOGY

      Department of Electrical Engineering and Computer Science

       6.001 Structure and Interpretation of Computer Programs

                         Fall Semester 1984

                            Problem Set 4

Issued: Tuesday, October 2, 1984

Due: on Wednesday, October 17, 1984
             for recitations meeting at 9:00, 10:00, and 11:00

     on Friday, October 19, 1984
             for recitations meeting at 12:00, 1:00, and 2:00


Note this is a 2-week problem set.


Reading assignment: Sections 2.2.3 through 2.3
		    PS4-RES.SCM, PS4-IMP.SCM (attached listings)



                                Part I

     Louis Reasoner, having failed in problem set 2 to make his
fortune in the videogames business, has embarked on a new venture.  He
is implementing an on-line electrical circuit analyzer that he will
rent out to harrassed 6.002 students.  Louis' partner in this scheme
is his roommate, Lem E. Tweakit, a 6-1 major with a flair for
hardware.  Lem suggests that they begin with a program that computes
resistances of simple ``linear resistive networks.''  These are
constructed from primitive elements called RESISTORS.  A resistor is
characterized by a number R called the RESISTANCE of the resistor.  It
can be depicted as follows:

                          o---/\/\/\/\----o
                                  R

For computational purposes, it is also convenient to talk about the
CONDUCTANCE, of a resistor, which is defined to be the reciprocal of
the resistance. (Conductance is traditionally denoted by the letter
G.)

                          o---/\/\/\/\----o
                                G=1/R

A resistor is the simplest example of a kind of element called a
TWO-TERMINAL NETWORK, i.e., a network that has exactly two terminals to
which other objects can be connected:

                               --------
                               |      |
                          o----|  N   |----o
                               |      |
                               --------



Networks can be constructed from other networks by attaching their
terminals.  A network has a resistance (and conductance) that is
determined by the resistances of its parts, and the ways in which they
are interconnected.

Louis' and Lem's initial system will provide two basic methods of
combination for constucting two-terminal networks from simpler
two-terminal networks.  The first method is called SERIES combination:

                       --------       --------
                       |      |       |      |
               o-------|  A   |-------|  B   |--------o
                       |      |       |      |
                       --------       --------

                             R = R  +  R
                                  A     B

The combined resistance of two networks connected in series is the sum
of the resistances.

The second method is PARALLEL combination: 

                    --------
                    |      |
               -----|  A   |-----                   
               |    |      |    |                   
               |    --------    | 
        o------|                |-----o         G = G  +  G
               |                |                    A     B
               |    --------    |                    
               |    |      |    |                     
               -----|  B   |-----     
                    |      |         
                    --------              
                                            
The combined conductance of two networks joined in parallel is the sum
of the conductances of the pieces.

After a few hours of terminal work, Louis and Lem have quite an
elegant program which handles such series and parallel combinations.
The program is provided in the first appendix to this handout,
labelled PS4-RES.SCM.

You should read through this code now to get some idea of how it
works.  There are constructors MAKE-RESISTOR, MAKE-SERIES, and
MAKE-PARALLEL, and an operation called RESISTANCE that gets its answer
by finding the resistances of the parts of a network and combining
them appropriately.

Louis and Lem test their program by computing the resistance of the
following network:

               R=10         R=50
    o---------\/\/\/-------\/\/\/--------o
         |                           |
         |           R=20            |
         |    -----\/\/\/-----       |
         -----|              |--------
              -----\/\/\/-----
                     R=30

They type:

(define r1 (make-resistor 10))
(define r2 (make-resistor 50))
(define r3 (make-resistor 20))
(define r4 (make-resistor 30))

(define N
  (make-parallel (make-series r1 r2)
		 (make-parallel r3 r4)))

-->(resistance N)
10.000

Indeed, the answer is correct (as Lem verifies).



PROBLEM 1

    Consider the data structure representing the network, N.

a. Draw the box-and-pointer structure that represents the object N.

b. How many times was RESISTANCE called in computing the resistance of
N?  What were the arguments to RESISTANCE for each call?

PROBLEM 2

    Given a 2-terminal network B, we can form a new two-terminal
network by attaching an ``L-section,'' which is itself constructed
from two two-terminal networks S and P as follows:

            -----
            |    |
    o-------| S  |-------------------
            |    |       |          |
            ------     ------     ------
                       |    |     |    |
                       | P  |     | B  |
                       |    |     |    |
                       ------     ------
                         |          |
    o--------------------------------

This operation is called ``L-extension'' of a base B by a ``series
part'' S and a ``parallel part'' P.

Write a procedure L-EXTEND, which takes three two-terminal networks
BASE, SERIES-PART, and PARALLEL-PART and combines them using
MAKE-SERIES and MAKE-PARALLEL to produce the extended network as shown
above.

Use your L-EXTEND procedure to make a new network that extends the
network, N, from Problem 1 (as the base) by a 10 ohm resistor (the
series part) and another 10 ohm resistor (the parallel part).

Verify that the resulting network has a resistance of 15 ohms.



PROBLEM 3

    By repeatedly L-extending a base by a given series and parallel
part, we obtain a circuit called a ``ladder.''  The following diagram
shows a 4-stage ladder.


   o-----S---------S-------S-------S---------
              |        |       |       |    |
              P        P       P       P    B
              |        |       |       |    |
   o----------------------------------------- 


Louis decides that he can easily implement a ladder by making use of
his L-EXTEND from problem 2, together with the REPEATED procedure
below:

(define (repeated f n)
  (lambda (x)
    (if (= n 0)
        x
        ((repeated f (-1+ n)) (f x)))))


He defines the following procedure, that constructs a ladder with a
given number of stages:

(define (ladder-extension stages base series-part parallel-part)
  ((repeated <EXP-1> stages) <EXP-2>))

Complete the procedure by filling in the appropriate expressions
<EXP-1> and <EXP-2>.

A classical network-theory problem used to plague 6.002 students is to
compute the resistance of an infinitely long ladder of 1 ohm
resistors:  


   o-----1--------      ---1-------1---------
              |    ...         |       |    |
              1                1       1    1
              |                |       |    |
   o----------------------------------------- 

Use your LADDER-EXTENSION procedure to generate longer-and-longer
ladders of this kind.  What value of resistance does it appear to
converge to?  Where have you seen this number before?



PROBLEM 4

    Louis and Lem demonstrate their system to Ben Bitdiddle.  As an
example, they try to compute the resistance of a long ladder.  To
their surprise, they find that although the program gets correct
answers, it seems to run more slowly than before.

Taking a careful look at the code, they find that Louis has changed
the definition of RESISTANCE-PARALLEL so that it now reads:

(define (resistance-parallel ckt)
  (/ (* (resistance (left-branch ckt))
	(resistance (right-branch ckt)))
     (+ (resistance (left-branch ckt))
	(resistance (right-branch ckt)))))

Explain why this change makes the program run so slowly.  As an
example, consider a ladder where each of the series, parallel, and
base pieces is a simple resistor.  If the ladder has N stages:

a.  How many resistors are contained in the ladder ?

b.  In computing the resistance of the ladder, how many times will the
    procedure RESISTANCE-RESISTOR be run, if the system uses the
    original version of the RESISTANCE-PARALLEL procedure ?

c.  After Louis installs the new version of RESISTANCE-PARALLEL, how
    many times will RESISTANCE-RESISTOR be run in computing the resistance
    of the N-stage ladder?  (You should be able to obtain an exact answer
    in terms of N.  However, partial credit will be given for good partial
    answers.  Show your work.)

d.  How has Louis' change affected the running time of the system?
    (E.g., slowed it down by a constant factor?  slowed it down
    quadratically? slowed it down exponentially?)



                                Part 2

Louis goes off to try to rent out the system in the 6.002 lab in
building 38.  His first potential customer is Anna Logg, a sophomore
who is busily doing her 6.002 problem set.  ``Foo,'' says Anna, ``this
is useless unless it can handle capacitors and inductors as well as
resistors.''

Louis goes back home and asks Lem for a short course on circuit
theory.  Lem says that indeed, interesting circuits have elements
called capacitors and inductors, and solving such circuits involves
(ugh, bletch!) differential equations.  However, Lem is too tired to
give a course in circuit theory (Louis has to take 6.002 next semester
anyway).  But luckily, Lem explains, it is unnecessary to understand
this in order to make a useful system.  In particular, explains Lem,
capacitors and inductors can be thought of as ``resistors'', whose
``resistance'' (actually called impedance) is a complex number that
varies with a parameter s, called the ``complex frequency.''

Lem says that for a capacitor of capacitance C (measured in farads)
the ``resistance'' is 1/sC.  For an inductor of inductance L (measured
in henrys) the ``resistance'' is sL:

        C                             1/sC
      ||                              
 -----||-----    ------->      o---/\/\/\/\---o
      ||


          L                           sL

 ---      ---    ------->      o---/\/\/\/\---o

For example, a so-called ``parallel resonant circuit'' can be thought
of as a network of 3 ``resistors'' and analyzed in the same way as
before.  Only this time, the ``resistance'' of the network will be a
function of s.

-----------------                 -----------------
    |     |     |                     |     |     |
    \           |                     \     \     \
    /          ---                    /     /     /
    \R         ---    ------->        \R    \sL   \1/sC
    /           |C                    /     /     /
    |     |L    |                     |     |     |
-----------------                 -----------------


Louis meditates on this for a few days and turns for help to the great
wizard Alyssa P. Hacker.  Alyssa suggests a clever scheme that allows
Louis to handle these things without greatly changing his program.
The idea is that the ``resistance'' of a circuit (or a circuit
element) will now be a procedure that takes some s as argument, rather
than a simple number.  For instance, the resistance of a (real)
resistor is a procedure that computes a constant function:

(define (make-resistor resistance)
  (attach-type 'resistor
	       (lambda (s) resistance)))

For a capacitor, the ``resistance'' is the procedure that takes an s
and returns 1/sC:

(define (make-capacitor capacitance)
  (attach-type 'resistor
	       (lambda (s)
		 (/ 1 (* capacitance s)))))

and so on.  Then, for example, the ``resistance'' of a series
combination is a procedure that, given an argument s, will compute the
resistances of the branches for that s, and return their sum:

(define (resistance-series ckt)
  (lambda (s)
    (+ ((resistance (left-branch ckt)) s)
       ((resistance (right-branch ckt)) s))))

Actually, this is not quite right because, in order to use this idea
to get information about circuits, one needs to use inputs s that are
complex numbers.  (Ask Lem if you want to know why, or take 6.002.)

So Louis has to make a further modification: All arithmetic must be
changed so that it works with complex numbers.  He builds a
representation for complex numbers and complex number operations and
modifies the code acordingly.  The result of his labors (which works,
amazingly!) is in the file PS4-IMP.SCM.  This is attached as the
second appendix to this problem set.

PROBLEM 5

    Load up Louis' new program.  Build a parallel resonant circuit (as
shown above) using a 2 ohm resistor, a 1 farad capacitor, and a 1
henry inductor -- call it N2. 

a. What is the impedance (``resistance'') of N2 at s=2+j0?, at s=0+j2?
(Note that Louis' code contains a constructor that makes complex
numbers of the form A+jB, where j is the squre root of -1.)

Lem has suggested that it is very useful to look at a plot of the
magnitude of the impedance of a circuit as a function of imaginary
values of the complex frequency s.  He has constructed a program that
plots functions on the chipmunk screen.  (You will find the plotter
in PS4-GRAPH.SCM, also attached as an appendix).  To get the idea of
how it works you should load it and type (line-plot square -2 2 .1).

b. Make a plot of the magnitude of the impedance of N2 from s=0+j.001
to s=0+j2, stepping s by j*.05 for each point.  Where is the peak?
You should get a curve like the one shown below.





























PROBLEM 6

    Now we are about to have some fun!  There are only a finite number
of circuits that one can construct with n parts using series and
parallel combinations.  In fact, one can easily enumerate all the
circuits which have n parts by a recursive process which adds one part
in each possible way to each circuit made of n-1 parts.

a. Write a procedure, ENUMERATE, which takes a list of parts to be
used, and which produces a list of all circuits (constructed by
series and parallel combination) which use those parts.  Remember,
each part may be used only once in each circuit.

b. How many circuits may be formed by such an enumeration -- as a
function of n?  Assuming the parts are distinct, can there be
duplications in the enumeration?

c. Use your program to enumerate all of the circuits that can be made
from one 1 farad capacitor, one 1 henry inductor, and one 2 ohm
resistor.  Pretty-print the results.

d. Using the plotter, find out which of the circuits you made has a
peak in the impedance between s=0 and s=0+j2.



PROBLEM 7 (optional -- very hard)

    A circuit whose magnitude plot has a single sharp peak or sharp
dip in a region is called a resonant circuit.  Write a program that
automatically tests circuits for resonant behavior.  If you like, and
have a lot of time, search out the series-parallel circuits with four
components to find those with resonant behavior.  Take the four
components to be a 10 ohm resistor, a 2 farad capacitor, a 1 farad
capacitor, and a 2 henry inductor.  Search the region from s=0 to
s=0+j2, as before.
