Code covered by the BSD License

### Highlights fromCircuit Analysis Toolbox

from Circuit Analysis Toolbox by John McDermid
The circuit analysis toolbox allows you to perform an AC analysis of a circuit.

Solving the Circuit Equations

# Solving the Circuit Equations

The tableau object provides both properties and methods to work with circuits. Methods like BODEPLOT solve the equations and plot results without requiring any knowledge about the circuit equations. However, in some applications the user may wish to solve the equations themselves. This section describes how the equations are formulated, what properties of the tableau object are used in constructing the equations, how to solve the equations, and what properties are useful to index into the resulting vector.

## Defining the Circuit Equations

The tableau matrix is a system of equations contrived from two matrix relations. The first is the relationship between elemental values described by the simple equation ki*Ib-kv*Vb=s. Ib is the current through component and Vb is the voltage across the component. The second is A*IB=0 where A is the reduced incidence matrix and IB is a vector of branch currents. A*IB=0 is a matrix statement of Kirkoff's current law for all nodes.

```Values of ki, kv, and s are picked to match each component type.
Resistor
ki=R,  kv=1,  s=0,   making R=Vb/Ib
Inductor
ki=2*pi*f*L,  kv=1,  s=0,  making Vb/Ib=j*2*pi*f*L=j*XL
Capacitor
ki=1,  kv=j*2*pi*f*C,  s=0  making Vb/Ib=1/(j*2*pi*f*C)=j*XC
Independent Current Source
ki=1,  kv=0,  s=I   making Ib=I
Independent Voltage Source
ki=0,  kv=1,  s=V   making -Vb=V (note that the current in
the voltage source flows in the
opposite direction to the
voltage accross it and
hence the minus sign)
Current Dependent Current Source
ki=[1 -Beta],  kv=[0 0],  s=0, making [1 -Beta)*[Ib Ib_cntrl]'=0
or Ib=Beta*Ib_cntrl
Voltage Dependent Current Source
ki=1,  kv=gm,  s=0  making Ib-gm*Vb_cntrl
or Ib=gm*Vb_cntrl
Voltage Dependent Voltage Source
ki=[0 0],  kv=[1 Av],  s=0,  -[1 -Av]*[Vb VB_cntrl]'=0
or -Vb=Av*Vb_cntrl
(note that the current in
the voltage source flows in the
opposite direction to the
voltage accross it and
hence the minus sign)```
```Kv and Ki (constructed from kv and ki) are essentially diagonal
matricies except for the dependent sources.```
```The tableau matrix is constructed in the following way:
tableau=[Kv -Ki*A';A Z] where Z is a matrix of zeros and of proper
dimension to make tableau a square matrix```
`The S column vector is constructed from the individual s and 0`

## The Tableau Matrix

The tableau matrix is constructed by the method TABLEAU_MATRIX. The tableau object contains several properties that are used by TABLEAU_MATRIX in the construction of the matrix. These properties are Kv, Ki, A, and Z. The matricies Kv and Ki are constructed by the rules described above. Component types are placed in Kv and Ki in blocks such that resistors are in adjacent locations, capacitors are in adjacent locations, etc. The A matrix is the incidence matrix with the row containing the reference node removed, making it the reduced incidence matrix. The columns of the A matrix are grouped in the same block structure found in Kv and Ki. The Z matrix is a matrix of all zeros sized to make the tableau matrix square. The parameters Kv, Ki, A, and Z are stored as sparse vectors so the resulting tableau matrix is also sparse.

The TABLEAU_MATRIX method requires two parameters; a tableau object and the frequency of the independent sources. In the following command the tableau object is T and the tableau matrix is returned in Tmat. The frequency is 1 KHz.

```T=tableau(ckt);
Tmat=tableau_matrix(T,1000)
```
```Tmat =

1.0e+004 *

(6,1)     -0.0001
(2,2)      0.1000
(6,2)      0.0001
(7,2)     -0.0001
(3,3)      1.0000
(7,3)      0.0001
(8,3)     -0.0001
(4,4)      0.0001
(7,4)      0.0001
(5,5)      0.0001
(8,5)      0.0001
(1,6)      0.0001
(2,6)     -0.0001
(2,7)      0.0001
(3,7)     -0.0001
(4,7)           0 - 0.0000i
(3,8)      0.0001
(5,8)           0 - 0.0000i

```

Clearly the matrix in this example is quite sparse as can be seen when it is converted to a non-sparse (full) matrix.

```full(Tmat)
```
```ans =

1.0e+004 *

Columns 1 through 7

0                  0                  0                  0                  0             0.0001                  0
0             0.1000                  0                  0                  0            -0.0001             0.0001
0                  0             1.0000                  0                  0                  0            -0.0001
0                  0                  0             0.0001                  0                  0                  0 - 0.0000i
0                  0                  0                  0             0.0001                  0                  0
-0.0001             0.0001                  0                  0                  0                  0                  0
0            -0.0001             0.0001             0.0001                  0                  0                  0
0                  0            -0.0001                  0             0.0001                  0                  0

Column 8

0
0
0.0001
0
0 - 0.0000i
0
0
0

```

## The Source Vector

The source vector S contains the values of the the independent voltage and current sources and is stored in the tableau object. The S vector is a column vector constructed as a sparse vector. The example is shown below as a full (non-sparse) vector.

```full(T.S)
```
```ans =

10
0
0
0
0
0
0
0

```

## Formulating and Solving the Circuit Equation

Once the tableau matrix has been created, the formulation of circuit equations is quite simple. The equation to be solved is:

```Tmat*IV=S
where
Tmat is the tableau matrix
IV is a column vector of branch currents and node voltages
such that IV=[Ib;Vn]
S is the column vector of independent source values```

The equation can be easily solved using Matlab's left division operator, that is IV=Tmat\S.

```IV=Tmat\T.S
```
```IV =

(1,1)      0.0098 + 0.0016i
(2,1)      0.0098 + 0.0016i
(3,1)      0.0000 + 0.0000i
(4,1)      0.0097 + 0.0015i
(5,1)      0.0000 + 0.0000i
(6,1)     10.0000
(7,1)      0.2467 - 1.5507i
(8,1)      0.1486 - 1.5601i

```

## Using Tableau Object Properties to Locate Voltages and Currents

After the IV vector is obtained, there remains the problem of locating specific branch currents or node voltages. The tableau object has several properties to aid in this location. When working directly with the IV vector, the TmIndx property is the most useful. This property allows the vector location to be obtained from the branch or node name. For example,

```T.TmIndx('N1')
```
```ans =

6

```

returns the vector location of the voltage at node N1. The node voltage can be extracted directly from vector IV by indirect indexing. (The full() statement in this example simply removes the sparse indexing information from the result, making it more readable.)

```full(IV(T.TmIndx('N1')))
```
```ans =

10

```

Similarly, the current through R1 is obtained by:

```full(IV(T.TmIndx('R1')))
```
```ans =

0.0098 + 0.0016i

```

Sometimes it is more convient to break the IV vector into a vector of branch currents and a vector of node voltages. This is easily accomplished using the properties branchIndx and nodeVoltIndx. Using the IV matrix calculated above yields:

```Ib=IV(T.branchIndx)
```
```Ib =

(1,1)      0.0098 + 0.0016i
(2,1)      0.0098 + 0.0016i
(3,1)      0.0000 + 0.0000i
(4,1)      0.0097 + 0.0015i
(5,1)      0.0000 + 0.0000i

```

and

```Vn=IV(T.nodeVoltIndx)
```
```Vn =

(1,1)     10.0000
(2,1)      0.2467 - 1.5507i
(3,1)      0.1486 - 1.5601i

```

The containers desigIndx and nodeIndx can be used to retrieve information about a specific node or branch. For example,

```Ib(T.desigIndx('R1'))
Vn(T.VnNodeIndx('N1'))
```
```ans =

(1,1)      0.0098 + 0.0016i

ans =

(1,1)       10

```

Values for multiple branches or multiple nodes can be obtained from the container.map "value" function by placing the names in a cell in this way:

```values(T.desigIndx,{'R1','R2'})
```
```ans =

[2]    [3]

```

The result is returned in a cell array. If you want the result to be a vector (for indexing) it can be converted with a "cell2mat" statement.

```cell2mat(values(T.desigIndx,{'R1','R2'}))
```
```ans =

2     3

```

## Solving Equations Symbolically

Equations can be solved symbolically in much the same way as they are solved numerically. The first step is to create a symbolic tableau object.

```T=symTableau(ckt);
```

Having created the symbolic tableau object, the next step is to create a symbolic tableau matrix.

```Tmat=symTableau_matrix(T);
pretty(Tmat)
```
```
+-                                        -+
|   0,  0,  0, 0, 0,  1,    0,       0     |
|                                          |
|   0, R1,  0, 0, 0, -1,    1,       0     |
|                                          |
|   0,  0, R2, 0, 0,  0,    -1,      1     |
|                                          |
|   0,  0,  0, 1, 0,  0, -C1 w i,    0     |
|                                          |
|   0,  0,  0, 0, 1,  0,    0,    -C2 w i  |
|                                          |
|  -1,  1,  0, 0, 0,  0,    0,       0     |
|                                          |
|   0, -1,  1, 1, 0,  0,    0,       0     |
|                                          |
|   0,  0, -1, 0, 1,  0,    0,       0     |
+-                                        -+
```

When the symbol tableau matrix is available the matrix can be solved just like the numerical matrix was solved.

```IV=Tmat\T.S;
```

The same index is available to obtain the voltage at node N2

```pretty(IV(T.TmIndx('N2')))
```
```
V1 (C1 R1 w - i) (C2 R2 w - i)
- ------------------------------------------------------------------------
2
(1 + C1 R1 w i) (C2 R1 w i - C1 C2 R1 R2 w  + 1 + C2 R2 w i + C1 R1 w i)
```