# All of Cheever's MNA Pages Condensed Into One

Here I have condensed all of my MNA pages into one, for easy printing:

# Analysis of  Resistive Circuits

The following text is broken into several sections.  Most are simply explanatory.  You may skip directly to SCAM, a MATLAB® tool for deriving and solving circuit equations symbolically if you are not interested in the theory.

All documents condensed into one (for easy printing).

## Introduction

Solving a set of equations that represents a circuit is straightforward, if not always easy.  However, developing that set of equations is not so easy.  The two commonly taught methods for forming a set of equations are the node voltage (or nodal) method and the loop-current (or mesh) method.  I will briefly describe each of these, and mention their benefits and disadvantages.  I will end with a discussion of a third method, Modified Nodal Analysis, that has some unique benefits.  Among its benefits is the fact that it lends itself to algorithmic solution -- the ultimate goal of these pages is to describe how to use a MATLAB program for generating a set of equations representing the circuit that can be solved symbolically.  If you are only interested in using that program you may go directly to the page describing SyCiSi.

Circuits discussed herein are simple resistive circuits with independent voltage and current sources.  Dependent sources can be added in a straightforward way, but are not considered here.

## Node Voltage Method

To apply the node voltage method to a circuit with n nodes (with m voltage sources), perform the following steps (after Rizzoni).

1. Selective a reference node (usually ground).
2. Name the remaining n-1 nodes and label a current through each passive element and each current source.
3. Apply Kirchoff's current law to each node not connected to a voltage source.
4. Solve the system of n-1-m unknown voltages.

### Example 1

Consider the circuit shown below

Steps 1 and 2 have already been applied.  To apply step 3:

In this case there is only one unknown, vb.  Plugging in numbers and solving the circuit we get

The node-voltage method is generally straightforward to apply, but becomes a bit more difficult if one or more of the voltage sources is not grounded.

### Example 2

Consider the circuit shown below.

Clearly this circuit is the same as the one shown above, with V1 and R1 interchanged.  Now we write the equations:

The difficulty arises because the voltage source V1 is no longer identical to one of the node voltages.  Instead we have

Note that the last line is the same as that from the previous circuit, but to solve the circuit we had to first solve for va.  This procedure wasn't difficult, but required a little cleverness, and will be a bit different for each circuit layout.  Another way to handle this problem is to use the concept of a supernode, which complicates the rules for setting up the equations (DeCarlo/Lin).  However, the supernode concept handles the case of a non-grounded voltage source without any need for solving intermediate equations, as we did here.

The examples chosen here were simple but illustrated the basic techniques of nodal analysis.  It also illustrated one of the difficulties with the technique, setting up equations with a floating voltage source.  The technique of modified nodal analysis, introduced later, also has no difficulties when presented with floating voltage sources.

## Loop Current Method

The loop current (or mesh current) method is, not surprisingly, similar to the node voltage method.  The rules below follow those in Rizzoni.

To apply the loop current method to a circuit with n loops (and with m current sources), perform the following steps.

1. Define each loop current.  This is easiest with a consistent method, e.g. all unknown currents are clockwise, all know currents follow direction on current source.
2. Apply Kirchoff's voltage law to each loop not containing a current source.
3. Solve the system of n-m unknown voltages.

### Example 3

Consider the circuit from Example 1, with mesh currents defined.

We can apply KVL to both loops

Since there are two equations and two unknowns we can solve by substitution or by matrix methods.  To solve by matrix methods we rewrite the equations

or

Solving for the two unknown currents we get

While floating current sources tended to complicate the formulation of circuit equations when using the node voltage method, neither the presence of current sources or voltage sources complicates the loop current method.

The choice between the node voltage method and the loop current method is often made on the basis of the circuit at hand.  For the example chosen, there was only one independent node but two independent loops.  Therefore the node voltage method would be expected to be easier.  The situation shown below is the opposite, with two nodes, but only one loop; hence the loop current method is preferable.

For this circuit you would draw three loops, but two of them go through known current sources - so you would only need one equation.  Nodal analysis would require two equations, one each for the voltage on each side of R3.

The next document describes a modified nodal analysis (MNA) method that is amenable to computer solution.

# Modified Nodal Analysis

This document is comprised of a brief introduction to modified nodal analysis (MNA), 3 examples and some observations about the MNA method that will be useful for developing a computer algorithm.

## Introduction

Though the node voltage method and loop current method are the most widely taught, another powerful method is modified nodal analysis (MNA).  MNA often results in larger systems of equations than the other methods, but is easier to implement algorithmically on a computer which is a substantial advantage for automated solution.  To use modified nodal analysis you write one equation for each node not attached to a voltage source (as in standard nodal analysis), and you augment these equations with an equation for each voltage source.  To be more specific, the rules for standard nodal analysis are shown below:

### Node Voltage Method

To apply the node voltage method to a circuit with n nodes (with m voltage sources), perform the following steps (after Rizzoni).

1. Selective a reference node (usually ground).
2. Name the remaining n-1 nodes and label a current through each passive element and each current source.
3. Apply Kirchoff's current law to each node not connected to a voltage source.
4. Solve the system of n-1-m unknown voltages.

The difficulty with this method comes from having to consider the effect of voltage sources.  Either a separate equation is written for each source, or the supernode method must be used.

The rules for modified nodal analysis are given by:

## Modified Nodal Analysis

To apply the node voltage method to a circuit with n nodes (with m voltage sources), perform the following steps (after DeCarlo/Lin).

1. Selective a reference node (usually ground) and name the remaining n-1 nodes.  Also label currents through each current source.
2. Assign a name to the current through each voltage source.  We will use the convention that the current flows from the positive node to the negative node of the source.
3. Apply Kirchoff's current law to each node.  We will take currents out of the node to be positive.
4. Write an equation for the voltage each voltage source.
5. Solve the system of n-1 unknowns.

Note: I will only discuss independent current and voltage sources.  Dependent sources are a simple extension.  See Litovski or DeCarlo/Lin for reference.

As an example consider the circuit below (from the previous document)

### Example 1

Consider the circuit shown below (Step 1 has already been applied)

Apply step 2 (currents through the voltage sources with current from positive node to negative node):

Apply step 3 (with positive currents out of the node):

Apply step 4:

Apply step 5:

or

Now all that is left is to solve the 5x5 set of equations (recall that the nodal analysis method resulted in just 1 equation, though we did some substitutions along the way).  Solving the 5x5 equation is difficult by hand, but not so with a computer.

### Example 2

If you'll recall, the nodal analysis method  became a bit more difficult when one or more of the voltage sources was not connect to ground.  Let's repeat Example 2 of the previous page with MNA.  Here the circuit is repeated with steps 1 and 2 completed:

Steps 3 and 4

Step 5

The fact that V1 is not grounded presented no difficulty at all.

### Example 3

Let's consider one more example, this time with a current source (this example is from Litovski).  Steps 1 and 2 have been completed.

Now complete steps 3 and 4:

And finally bring all the know variables to the right hand side and complete step 5:

If you examine the matrix equations that resulted from the application of the MNA method, several patterns become apparent that we can use to develop an algorithm.  All of the circuits resulted in an equation of the form.

Let us examine example 2.  This circuit had 3 nodes and 2 voltage sources (n=3, m=2).  The resulting matrix is shown below.

Note that the pink highlighted portion of the A matrix is 3x3 (in general nxn), and includes only known quantities, specifically the values of the passive elements (the resistors).  In addition the highlighted portion of the A matrix is symmetric with positive values along the main diagonal, and only negative (or zero) values for the off-diagonal terms.  If an element is connected to ground, it only appears along the diagonal; a non-grounded (e.g. R2) appears both on and off the diagonal).  The rest of the terms in the A matrix (the non-highlighted portion) contains only ones, negative ones and zeros.  Note also that the matrix size is 5x5 (in general (m+n)x(m+n)).  For all of the circuits we will analyze (i.e., only passive elements and independent sources), these general observations about the A matrix will always hold.

Now consider the x matrix, the matrix of unknown quantities.  It is a 1x5 matrix (in general 1x(n+m)).  The topmost 3 (in general n) elements are simply the node voltages.  The bottom 2 (in general m) elements are the currents associated with the voltage sources.

This brings us to the z matrix that contains only known quantities.  It is also a 5x1 matrix (in general (n+m)x1).  The topmost 3 (in general n) elements are either zero, or the sum of independent current sources (see example 3 for an case in point).  The bottom 2 (in general m) elements are the independent voltage sources.

### To summarize:

MNA applied to a circuit with only passive elements (resistors) and independent current and voltage sources results in a matrix equation of the form:

For a circuit with n nodes and m independent voltage sources:

• The A matrix:
• is (n+m)x(n+m) in size, and consists only of known quantities.
• the nxn part of the matrix in the upper left:
• has only passive elements
• elements connected to ground appear only on the diagonal
• elements not connected to ground are both on the diagonal and off-diagonal terms.
• the rest of the A matrix (not included in the nxn upper left part) contains only 1, -1 and 0 (other values are possible if there are dependent current and voltage sources; I have not considered these cases.  Consult Litovski if interested.)
• The x matrix:
• is an (n+m)x1 vector that holds the unknown quantities (node voltages and the currents through the independent voltage sources).
• the top n elements are the n node voltages.
• the bottom m elements represent the currents through the m independent voltage sources in the circuit.
• The z matrix:
• is an (n+m)x1 vector that holds only known quantities
• the top n elements are either zero or the sum and difference of independent current sources in the circuit.
• the bottom m elements represent the m independent voltage sources in the circuit.

The circuit is solved by a simple matrix manipulation:

Though this may be difficult by hand, it is straightforward and so is easily done by computer.

In the next page we will use these observations to describe an algorithm for generating the matrices automatically.

# An Algorithm for Modified Nodal Analysis

This document describes an algorithmic method for generating MNA equations.  It consists of several parts:.

Many of the ideas and notation from this page are from Litovski, though the discussion here is quite simpler because only independent voltage and current sources are considered.

## Review

Recall from the previous document:

MNA applied to a circuit with only passive elements (resistors) and independent current and voltage sources results in a matrix equation of the form:

For a circuit with n nodes and m independent voltage sources:

• The A matrix:
• is (n+m)x(n+m) in size, and consists only of known quantities.
• the nxn part of the matrix in the upper left:
• has only passive elements
• elements connected to ground appear only on the diagonal
• elements not connected to ground are both on the diagonal and off-diagonal terms.
• the rest of the A matrix (not included in the nxn upper left part) contains only 1, -1 and 0 (other values are possible if there are dependent current and voltage sources; I have not considered these cases.  Consult Litovski if interested.)
• The x matrix:
• is an (n+m)x1 vector that holds the unknown quantities (node voltages and the currents through the independent voltage sources).
• the top n elements are the n node voltages.
• the bottom m elements represent the currents through the m independent voltage sources in the circuit.
• The z matrix:
• is an (n+m)x1 vector that holds only known quantities
• the top n elements are either zero or the sum and difference of independent current sources in the circuit.
• the bottom m elements represent the m independent voltage sources in the circuit.

The circuit is solved by a simple matrix manipulation:

Though this may be difficult by hand, it is straightforward and so is easily done by computer.

## Notational Convention

Obviously, the notation used does not change the solution.  However the convention described below will make it quite easy to develop the matrices necessary for solution of the circuit.

• Ground is labeled as node 0.
• The other nodes are labeled consecutively from 1 to n.
• We will refer to the voltage at node 1 as v_1, at node 2 as v_2 and so on.
• The naming of the independent voltage sources is quite loose, but the names must start with the letter "V" and must be unique from any node names.  For our purposes we will require that independent voltage sources have no underscore ("_") in their names.  So the names Va, Vsource, V1, Vxyz123 are all legitimate names, but V_3, V_A, Vsource_1 are not.
• The current through a voltage source will be labeled with "I_" followed by the name of the voltage source.  Therefore the current through Va is I_Va, the current through VSource is I_VSource, etc...
• The naming of the independent current sources is similar; the names must start with the letter "I" and must no underscore ("_") in their names.  So the names Ia, Isource, I1, Ixyz123 are all legitimate names, but I_3, I_A, Isource_1 are not.

These rules are somewhat restrictive (more so than they need to be) but they make development of the algorithm easier while still allowing quite a bit of freedom.

These rules are easily explained with an example (Example 3 from previous page):

This circuit is labeled according to the guidelines above.  Ground is node 0 and the other two nodes are labeled 1 and 2.  The voltage and current sources have labels with no underscores and the current through the voltage source Vs1 is labeled I_Vs1.  We will discuss how to use this diagram (with these labels) to generate the MNA equations below.

## Generating the MNA matrices

There are three matrices we need to generate, the A matrix, the x matrix and the z matrix.  Each of these will be created by combining several individual sub-matrices.  To motivate the rules for generating the matrices we will consider the two sample circuits below (example 2 and example 3 from previous web page, with Notational Convention as above).  I simply state the resulting MNA equations, but in the following text we will show how to generate each one.

 Case 1 Circuit Diagram

 Case 2 Circuit Diagram

### The A matrix

The A matrix will be developed as the combination of 4 smaller matrices, G, B, C, and D.

The A matrix is (m+n)x(m+n) (n is the number of nodes, and m is the number of independent voltage sources) and:

• the G matrix is nxn and is determined by the interconnections between the passive circuit elements (resistors)
• the B matrix is nxm and is determined by the connection of the voltage sources.
• the C matrix is mxn and is determined by the connection of the voltage sources.  (B and C are closely related, particularly when only independent sources are considered).
• the D matrix is mxm and is zero if only independent sources are considered.

#### Rules for making the G matrix

The G matrix is an nxn matrix formed in two steps

1. Each element in the diagonal matrix is equal to the sum of the conductance (one over the resistance) of each element connected to the corresponding node.  So the first diagonal element is the sum of conductances connected to node 1, the second diagonal element is the sum of conductances connected to node 2, and so on.
2. The off diagonal elements are the negative conductance of the element connected to the pair of corresponding node.  Therefore a resistor between nodes 1 and 2 goes into the G matrix at location (1,2) and locations (2,1).

Demonstrations

 Case 1 Circuit Diagram MNA Equations

 Case 2 Circuit Diagram MNA Equations

If an element is grounded, it will only have contribute to one entry in the G matrix -- at the appropriate location on the diagonal.  If it is ungrounded it will contribute to four entries in the matrix -- two diagonal entries (corresponding to the two nodes) and two off-diagonal entries.

#### Rules for making the B matrix

The B matrix is an nxm matrix with only 0, 1 and -1 elements.  Each location in the matrix corresponds to a particular voltage source (first dimension) or a node (second dimension).  If the positive terminal of the ith voltage source is connected to node k, then the element (i,k) in the B matrix is a 1.  If the negative terminal of the ith voltage source is connected to node k, then the element (i,k) in the B matrix is a -1.  Otherwise, elements of the B matrix are zero.

Demonstrations

 Case 1 Circuit Diagram MNA Equations

 Case 2 Circuit Diagram MNA Equations

If a voltage source is ungrounded, it will have two elements in the B matrix (a 1 and a -1 in the same column).  If it is grounded it will only have one element in the matrix.

#### Rules for making the C matrix

The C matrix is an mxn matrix with only 0, 1 and -1 elements.  Each location in the matrix corresponds to a particular node (first dimension) or voltage source (second dimension).  If the positive terminal of the ith voltage source is connected to node k, then the element (k,i) in the C matrix is a 1.  If the negative terminal of the ith voltage source is connected to node k, then the element (k,i) in the C matrix is a -1.  Otherwise, elements of the C matrix are zero.

In other words, the C matrix is the transpose of the B matrix.  (This is not the case when dependent sources are present.)

Case 1:

Case 2:

#### Rules for making the D matrix

The D matrix is an mxm matrix that is composed entirely of zeros.  (It can be non-zero if dependent sources are considered.)

Case 1:

Case 2:

### The x matrix

The x matrix holds our unknown quantities and will be developed as the combination of 2 smaller matrices v and j.  It is considerably easier to define than the A matrix.

The x matrix is (m+n)x1 (n is the number of nodes, and m is the number of independent voltage sources) and:

• the v matrix is nx1 and hold the unknown voltages
• the j matrix is mx1 and holds the unknown currents through the voltage sources

#### Rules for making the v matrix

The v matrix is an nx1 matrix formed of the node voltages.  Each element in v corresponds to the voltage at the equivalent node in the circuit (there is no entry for ground -- node 0).

For example if a circuit has three nodes, the v matrix is

For a circuit with n nodes we get

and so on.

#### Rules for making the j matrix

The j matrix is an mx1 matrix, with one entry for the current through each voltage source.  So if there are two voltage sources V1 and V2, the j matrix will be:

Demonstrations for the x matrix.

 Case 1 Circuit Diagram MNA Equations

 Case 2 Circuit Diagram MNA Equations

### The z matrix

The z matrix holds our independent voltage and current sources and will be developed as the combination of 2 smaller matrices i and e.  It is quite easy to formulate.

The z matrix is (m+n)x1 (n is the number of nodes, and m is the number of independent voltage sources) and:

• the i matrix is nx1 and contains the sum of the currents through the passive elements into the corresponding node (either zero, or the sum of independent current sources).
• the e matrix is mx1 and holds the values of the independent voltage sources.

#### Rules for making the i matrix

The i matrix is an nx1 matrix with each element of the matrix corresponding to a particular node.  The value of each element of i is determined by the sum of current sources into the corresponding node.  If there are no current sources connected to the node, the value is zero.

#### Rules for making the e matrix

The e matrix is an mx1 matrix with each element of the matrix equal in value to the corresponding independent voltage source.

Demonstrations for the z matrix.

 Case 1 Circuit Diagram MNA Equations

 Case 2 Circuit Diagram MNA Equations

## Putting it Together

We can now write out the full matrix solutions for both cases that we have been developing.

 Case 1 Circuit Diagram MNA Equations

 Case 2 Circuit Diagram MNA Equations

If you are still unsure about the application of the algorithm, some more examples are available.

So far we have only dealt with resistors and independent voltage and current sources.  The next document will show that adding capacitors and inductors is trivial, and adding op-amps only adds minimally to the difficulty.

# Some Examples of Modified Nodal Analysis

This document describes an algorithmic method for generating MNA equations.  It consists of several parts:.

Many of the ideas and notation from this page are from Litovski, though the discussion here is quite simpler because only independent voltage and current sources are considered.

## Review

Recall from the previous document:

MNA applied to a circuit with only passive elements (resistors) and independent current and voltage sources results in a matrix equation of the form:

We will take n to be the number of nodes (not including ground) and m to be the number of independent voltage sources.

### Notation

• Ground is labeled as node 0.
• The other nodes are labeled consecutively from 1 to n.
• We will refer to the voltage at node 1 as v_1, at node 2 as v_2 and so on.
• The naming of the independent voltage sources is quite loose, but the names must start with the letter "V" and must be unique from any node names.  For our purposes we will require that independent voltage sources have no underscore ("_") in their names.  So the names Va, Vsource, V1, Vxyz123 are all legitimate names, but V_3, V_A, Vsource_1 are not.
• The current through a voltage source will be labeled with "I_" followed by the name of the voltage source.  Therefore the current through Va is I_Va, the current through VSource is I_VSource, etc...
• The naming of the independent current sources is similar; the names must start with the letter "I" and must no underscore ("_") in their names.  So the names Ia, Isource, I1, Ixyz123 are all legitimate names, but I_3, I_A, Isource_1 are not.

Review the rules for forming MNA matrices (if needed).

## Example 1

The example given is from Smith, Figure 2.8.  First the MNA equations will be derived from the circuit.  They will then be derived according to the algorithm in the previous document -- hopefully the results will agree.  The nodes and sources have been labeled as required, and the current through the voltage source is defined.

To apply the MNA technique we will need 5 equations (one for each of 4 nodes, and 1 for the independent voltage source).  Note that Smith only required 2 equations -- MNA often requires more equations than other techniques, but is amenable to computer solution.  By inspection we get:

Using the algorithm, we get:

Putting these together yields:

Careful comparison of this result with the original result verifies that the two solutions are identical.

## Example 2

The example given is from Smith, Problem 2.11.  First the MNA equations will be derived from the circuit.  They will then be derived according to the algorithm in the previous document -- hopefully the results will agree.  The nodes and sources have been labeled as required, and the current through the voltage source is defined.

To apply the MNA technique we will need 3 equations (one for each of 2 nodes, and 1 for the independent voltage source).  By inspection we get:

Using the algorithm, we get:

If we apply these results to the MNA equation, we get

Careful comparison of this result with the original result verifies that the two solutions are identical.

## Example 3

The last example is a bit more involved, it has two voltage sources and one current source.  The current source and one of the voltage sources are not grounded.

First we must label the nodes and define currents through the voltage sources

To apply the MNA technique we will need 5 equations (one for each of 3 nodes, and 2 for the independent voltage sources).  By inspection we get:

Using the algorithm we get:

If we apply these results to the MNA equation, we get

Careful comparison of this result with the original result verifies that the two solutions are identical.

 ← Comments or Questions? Erik Cheever Engineering Department Swarthmore College

# Modified Nodal Analysis with Inductors, Capacitors and Op-Amps

This document describes an algorithmic method for generating modified nodal analysis (MNA) equations when the circuit has inductors, capacitors and/or operational amplifiers (op-amps).  It consists of several parts:.

## MNA with Reactive Elements

Applying modified nodal analysis to circuits with inductors and capacitors presents no special difficulty if one uses the complex impedance of these elements.

Let us apply MNA to the following circuit (which already has nodes labeled, and the current through the voltage source defined and labeled):

MNA will generate 4 equations, one for each of the three nodes, and one for Vin.  By inspection we get:

Using the MNA algorithm we get:

Since the matrices are all defined, we can finish the solution:

Careful inspection of this result verifies that it is identical to the original result. Don't worry about solving this equation; a later page will introduce SCAM - A MATLAB tool for deriving and solving circuit equations symbolically.

## MNA with Op Amps

Applying modified nodal analysis to circuits with ideal operational amplifiers (op- amps) is a bit more difficult.  Each op-amp increases the count of voltage sources by 1 (because the output of an op amp is treated as a voltage source), but also complicates the creation of the MNA matrices.  In particular, the B and C matrices are no longer transposes of each other.  To see how the ideal operational amplifier is handled, consider the circuit below:

Note that we have labelled all of the nodes, and defined a current through each voltage source.  The current through the voltage source Vs is I_Vs, and the voltage into the op-amp is labeled I_OA. We will make the standard assumption for an ideal op-amp. Namely that there is no current into the device at either input to the op-amp, and the voltage difference between the inputs is zero (note caveats below).

This circuit will require 6 equations -- one each for the 4 nodes and one each for the 2 labeled currents.  We can now write the circuit equations by inspection.

or in matrix form:

The only difference between this equation and the others that we have developed is that the equation for the op-amp is not in terms of the voltage at its output, but rather it specifies that the two input terminals are at the same potential.

### Changes to formation of the MNA matrices.

The A matrix:

Recall that the A matrix is formed of four smaller matrices, G, B, C, and D.

• The rule for the G matrix is unchanged.

• The rule for the B matrix is unchanged -- the op-amp is treated as another voltage source.

• The rule for the C matrix does change.  The C matrix is an nxm matrix with only 0, 1 and -1 elements.  Each location in the matrix corresponds to a particular node (first dimension) or voltage source (second dimension).  For each indendent voltage source, if the positive terminal of the ith voltage source is connected to node k, then the element (k,i) in the C matrix is a 1; if the negative terminal of the ith voltage source is connected to node k, then the element (k,i) in the C matrix is a -1.  For each op-amp let the positive input  terminal be at node k and negative terminal at node j;  the corresponding (ith) row of the C matrix has a 1 at location corresponding to the positive terminal (k,i), and a -1 at the location corresponding to the negative terminal (j,i).  Otherwise, elements of the C matrix are zero.

• The rule for the D matrix is unchanged.

The x matrix is unchanged.

The z matrix:

Recall that the z matrix is formed of two smaller matrices, i and e.

• The rule for the i matrix is unchanges.

• The rule for the e matrix does change.  The e matrix is an 1xm matrix with each element of the matrix corresponding to a voltage source.  If the element in the e matrix corresponds to an independent source it is set equal to the value of that voltage source.  If the element corresponds to an op-amp, then its value is set to zero.

All of the MNA rules for use with circuits with op-amps are here.

Caveats: The two ideal op-amp assumptions (no input current, no potential difference at inputs) only hold for circuits with negative feedback.  Because of this, the MNA method as applied here can give erroneous results if negative feedback is not present.  This problem actually persists in many commercial circuit simulators -- you can hook up a circuit with the input terminals reversed (so that it would not operate properly in practice due to lack of negative feedback) and the simulation behaves as if there is no problem.  Note also that the output of the op-amp is not limited by any power supply -- this circuit will happily generate hundreds of volts.

Also: the op-amp rule can be simplified by taking advantage of the redundancy in node voltages since the two input nodes are at the same voltage.  Taking advantage of this fact reduces the number of equations by 1, though it makes the generation of the equations a bit more difficult (The diagonal of the G matrix can have negative values, the off-diagonal element elements can be positive, the C matrix can have values besides 1, -1 and 0 and the D matrix is non-zero).  See Litovski for details.

Let's apply the algorithm to the circuit at hand:

Using the rules for the algorithm we get:

This result, upon inspection, is identical to our previous result.

### Another Example

Let's look at another simple example just to reinforce the concepts.

This circuit will require just 3 equations (2 nodes, 1 voltage sources).  By inspection we get:

Using our MNA algorithm we get:

We can now use these matrices to solve the circuit:

which agrees with our previous result.

The next document describes SCAM, a MATLAB program that performs all of these manipulations to set up the matrices, and then solves the circuit.

# SCAMSymbolic Circuit Analysis in MatLab

This document describes a  A MATLAB® tool for deriving and solving circuit equations symbolically.  It is split up into several segments.

Before using the program you must first download it and save it on your computer where MATLAB can access it.  Download the file now.  You can also get the MATLAB file along with the example netlists in a zip file.  Note: you must have the symbolic toolbox to run this code.

## Defining circuits for SCAM: Defining the netlist

The SCAM program cannot simply read a schematic diagram so we need to develop a method for representing a circuit textually.  This can be done using a device called a netlist that defines the interconnection between circuit elements.  If you have used SPICE (Simulation Program with Integrated Circuit Emphasis) this is a familiar concept.  A good review is given by Jan Van Der Spiegel at the University of Pennsylvania.  The process is easily demonstrated by example.

Let us use SCAM to define the circuit below:

We start by defining the nodes.  The only restriction here is that the nodes must be labeled such that ground is node 0, and the other nodes are named consecutively starting at 1.  The choice of which number to assign to which node is entirely arbitrary.

SCAM requires a text file with one line for each component in the circuit.  This circuit has 4 components (3 resistors and 1voltage source), and will require 6 lines to define it.  Each type of component has its own format for its corresponding lines in a file.  These are shown below.  The labels N1, N2, etc... correspond to the nodes in the circuit.

 Component Type Symbol SCAM Description Resistor R1 N1 N2 1000 R1 is between nodes N1 and N2, and has a value of 1000 Ohms The value of the component must be written out (no abbreviations like kOhm) as a number. The name of the component is Rx, where x can be any combination of letters and numbers.  R1, Rabc, Ra1 are all valid names. Capacitor C1 N1 N2 1E-6 Similar comments to the resistor Inductor L1 N1 N2 1E-3 Similar comments to the resistor Voltage Source V1 N1 N2 12 Similar comments to the resistor Node N1 is connected to the positive node, N2 to the negative node. The current through the source is one of the unknowns, it is defined as shown below: Current Source I1 N1 N2 1 Similar comments to the resistor Current flows out of node N1 and into node N2. Op Amp O1 N1 N2 N3 Similar comments to the resistor but with three nodes as shown.

Let's define the current through the voltage source to give a final circuit diagram:

The netlist file is:

 ```V1 1 0 12 R1 1 2 1000 R2 2 0 2000 R3 2 0 2000```

## Example 1:  Voltages

Let's use the circuit we have been examining

We will create a text file containing the netlist:

 ```V1 1 0 12 R1 1 2 1000 R2 2 0 2000 R3 2 0 2000```

and save it in the directory seen by MATLAB.  I edited such a file (using the MATLAB editor) and saved it in my SCAM directory as Example1.cir.  If you are running MATLAB, you should see the file if you use the dir command:

```>> dir
. .. scam.asv scam.m Example1.cir ```

To run the program, assign the filename of the circuit to be analyzed to the variable fname, and then call the program.  The output from the MATLAB window is shown below:

```>> fname='Example1.cir';
>> scam

Done! Elapsed time = 0.651 seconds.

Netlist
V1 1 0 12
R1 1 2 1000
R2 2 0 2000
R3 2 0 2000

Solved variables:
[ v_1]
[ v_2]
[ I_V1]```

The netlist is displayed, followed by the unknown variables whose solution is determined.  Let's look at the value of v_2 (the voltage at node 2):

>> v_2
v_2 = 1/(R2*R3+R1*R3+R1*R2)*R2*R3*V1

or I_V1 (the current through the voltage source)

>> I_V1
I_V1 = (-R3-R2)/(R2*R3+R1*R3+R1*R2)*V1

In addition to the unknowns, several other variables are created in the workspace (this is why the SCAM program is a script instead of a function).  The important variables created, in addition to the unknowns, are a value corresponding to each of the elements.  We can examine the value of any element, for example V1 or R2

>> V1
V1 = 12

>> R1
R1 = 1000

We can use these values to get numeric values for the unknowns:

>> eval(v_2)
ans = 6

>> eval(I_V1)
ans = -0.0060

which shows that the voltage at node 2 is 6 volts, and the current through V1 is 6 mA.

## Example 2:  Currents

What happens if we are interested in the current through R2 instead of just node voltages, from node 2 to node 0.  We know that the current through R2 is just the voltage drop across R2, divided by the value of the resistance.  We can do the solution either symbolically or numerically.

>> v_2/R2
ans = 1/2000/(R2*R3+R1*R3+R1*R2)*R2*R3*V1

>> eval(ans)
ans = 0.0030

The current through R1, from node 1 to node 2,  is just the voltage across R1 divided by its value.

>> (v_1-v_2)/R1
ans = 1/1000*V1-1/1000/(R2*R3+R1*R3+R1*R2)*R2*R3*V1

>> eval(ans)
ans = 0.0060

Other quantities can be similarly determined.  For example the ratio of v_2 to V1:

>> v_2/V1
ans = 1/12/(R2*R3+R1*R3+R1*R2)*R2*R3*V1

>> eval(ans)
ans = 0.5000

## Example 3:  Generating MNA Equations

The SCAM program also defines the A, X and Z matrices from the MNA method. (Note that the SCAM program uses uppercase variables for X and Z).

>> A
A = [ 1/R1, -1/R1, 1]
[ -1/R1, 1/R1+1/R2+1/R3, 0]
[ 1, 0, 0]

>> X
X = [ v_1]
[ v_2]
[ I_V1]

>> Z
Z = [ 0]
[ 0]
[ V1]

We can use these variables to recreate the circuit equations.  To get the left side of the equations we just multiply A*X:

```>> A*X
ans = [         1/R1*v_1-1/R1*v_2+I_V1]
[ -1/R1*v_1+(1/R1+1/R2+1/R3)*v_2]
[                            v_1]
```

or, in a slightly easier to read form:

```>> pretty(A*X)
[        v_1   v_2               ]
[        --- - --- + I_V1        ]
[        R1    R1                ]
[                                ]
[  v_1   / 1      1      1  \    ]
[- --- + |---- + ---- + ----| v_2]
[  R1    \ R1     R2     R3 /    ]
[                                ]
[              v_1               ]

```

The left side of the equation is given by Z:

```Z =
[  0]
[  0]
[ V1]```

Using the information above, we can get any of the MNA equations.  To get the equations for node 2, simply take the 2nd row of the right and left sides of the equations:

## Example 4:  A More Complex Circuit

We can also apply the program to more complex circuits, such as the following (Example 3 from the MNA Examples page with values given to each component) (with nodes already labeled, and the currents through the voltage sources also labeled for clarity):

The netlist for this circuit is given by

 ```Vg 1 0 4 Vx 3 2 6 R1 1 2 1 R2 2 0 4 R3 3 0 2 It 1 2 1```

I entered this into a file and named it Example4.cir.  To analyze the circuit we proceed as before, by setting the fname variable, and starting the program:

```>> fname='Example4.cir';
>> scam

Done! Elapsed time = 2.544 seconds.

Netlist
Vg 1 0 4
Vx 3 2 6
R1 1 2 1
R2 2 0 4
R3 3 0 2
It 1 2 1

Solved variables:
[  v_1]
[  v_2]
[  v_3]
[ I_Vg]
[ I_Vx]```

We can solve for the voltage at node 2 either symbolically or numerically:

```>> v_2
v_2 = R2*(It*R1*R3+Vg*R3-Vx*R1)/(R2*R3+R1*R3+R1*R2)

>> eval(v_2)
ans = 1.1429```

We can find the current through R1 (symbolically or numerically):

```>> (v_1-v_2)/R1
ans = Vg-R2*(It*R1*R3+Vg*R3-Vx*R1)/(R2*R3+R1*R3+R1*R2)

>> eval(ans)
ans = 2.8571```

We can find the MNA equation for node 2:

```>> q=A*X
q = [         1/R1*v_1-1/R1*v_2+I_Vg]
[ -1/R1*v_1+(1/R1+1/R2)*v_2-I_Vx]
[                  1/R3*v_3+I_Vx]
[                            v_1]
[                       -v_2+v_3]

>> pretty(q(2))
v_1   / 1      1  \
- --- + |---- + ----| v_2 - I_Vx
R1    \ R1     R2 /
>> Z(2)
ans = It ```

or, in a cleaner format

## Example 5:  Op Amps w/ resistors

The next example shows an op-amp with 2 resistors in the standard inverting configuration along with its netlist (Example5.cir).

 Circuit Netlist ```Vin 3 0 Symbolic R1 1 3 Symbolic R2 2 1 Symbolic OAmp 0 1 2```

Since we don't have values for the components in the circuit, they are declared to be "Symbolic".  We can now solve this circuit to determine the gain between Vin and node 2.

```>> fname='Example5.cir';
>> scam

Done! Elapsed time = 1.633 seconds.

Netlist
Vin 3 0 Symbolic
R1 1 3 Symbolic
R2 2 1 Symbolic
OAmp 0 1 2

Solved variables:
[    v_1]
[    v_2]
[    v_3]
[  I_Vin]
[ I_OAmp]

>> v_2/Vin
ans = -1/R1*R2```

Some notes on this circuit:

• If a value is not given for an element, the value can be declared symbolic (as above).  However this is not necessary, the user must simply ensure that the value is not a number -- so instead of "Symbolic", the value of R1 could have been "zyx" or even left blank.
• Because of the way we have used MNA to handle op-amps, the circuit above would give the exact same results even if the input terminals were switched.  In practice this would not work.  It is the responsibility of the user to make sure negative feedback exists.

## Example 6:  Converting the symbolic transfer function to a MATLAB object.

Often we would like to take our results and use them in MATLAB for other calculations.  this is especially true when working with transfer functions.  The example below shows how this can be accomplished. This circuit is from the MNA with Capacitors and Inductors  page with values given to each component, with nodes already labeled, and the currents through the voltage sources also labeled for clarity.  The netlist is called Example6.cir.

 Circuit Netlist ```Vin 3 0 Symbolic R2 3 2 1000 R1 1 0 1000 C1 1 0 1E-6 C2 2 1 10E-6 L1 1 0 0.001```

Now let's solve and get the transfer function symbolically

```>> fname='Example6.cir';
>> scam

Done! Elapsed time = 1.583 seconds.

Netlist
Vin 3 0 Symbolic
R2 3 2 1000
R1 1 0 1000
C1 1 0 1E-6
C2 2 1 10E-6
L1 1 0 0.001

Solved variables:
[   v_1]
[   v_2]
[   v_3]
[ I_Vin]

>> v_2/Vin              %Find transfer function between input and node 2
ans =
(s*L1+s^2*C1*R1*L1+s^2*C2*R1*L1+R1)/(s*L1+s^2*L1*C2*R2+s^2*C1*R1*L1+s^3*C1*R1*L1*C2*R2+s^2*C2*R1*L1+R1+R1*s*C2*R2)

>> collect(ans)          %Collect similar terms
ans =
((C2*R1*L1+C1*R1*L1)*s^2+s*L1+R1)/(s^3*C1*R1*L1*C2*R2+(C2*R1*L1+L1*C2*R2+C1*R1*L1)*s^2+(L1+R1*C2*R2)*s+R1)

>> pretty(ans)            %Pretty print it

2                /   3
((C2 R1 L1 + C1 R1 L1) s  + s L1 + R1)  /  (s  C1 R1 L1 C2 R2
/

2
+ (C2 R1 L1 + L1 C2 R2 + C1 R1 L1) s  + (L1 + R1 C2 R2) s + R1)```

We can also get a numerical transfer function

```>> eval(v_2/Vin)
ans = (1/1000*s+11/1000000*s^2+1000)/(10001/1000*s+21/1000000*s^2+1/100000000*s^3+1000)```

While this answer is correct, it is not in a very convenient form, and we can't do any actual simulation with it.  However we can easily convert the expression to a MATLAB transfer function object.  First we separate out the numerator and denominator, and then convert them to MATLAB polynomials.

>> [n,d]=numden(eval(v_2/Vin))
n = 100000*s+1100*s^2+100000000000
d = 1000100000*s+2100*s^2+s^3+100000000000

>> mySys=tf(sym2poly(n),sym2poly(d))
Transfer function:
1100 s^2 + 100000 s + 1e011
--------------------------------
s^3 + 2100 s^2 + 1e009 s + 1e011

We are now free to perform any of the MATLAB function relating to polynomials.  Shown below are a step response and a Bode plot.

 Step Response Bode Plot >> step(mySys) >> bode(mySys)

## Example 7:  Finding the current in a wire

Now consider the case of finding the current through a wire.  In particular, consider the previous circuit:

We would like to find the current shown.  How do we do this.  One way would be to find each current into and out of node 1 and solve for the unknown current.  Another way is to introduce a voltage source of zero volts (i.e., a short circuit), and its concomitant node.  The netlist is Example7.cir.

 Circuit Netlist ```Vin 3 0 Symbolic R2 3 2 1000 R1 1 0 1000 C1 4 0 1E-6 C2 2 4 10E-6 L1 1 0 0.001 Vsc 4 1 0```

This voltage source has no effect on the circuit, but forces the computation of the current I_Vsc.

```>> fname='Example7.cir';
>> scam

Done! Elapsed time = 3.014 seconds.

Netlist
Vin 3 0 Symbolic
R2 3 2 1000
R1 1 0 1000
C1 4 0 1E-6
C2 2 4 10E-6
L1 1 0 0.001
Vsc 4 1 0

Solved variables:
[   v_1]
[   v_2]
[   v_3]
[   v_4]
[ I_Vin]
[ I_Vsc]

>> eval(I_Vsc)
ans = -1/100000*(-1/1000*s-1000)*s*Vin/(10001/1000*s+21/1000000*s^2+1/100000000*s^3+1000)

>> simplify(ans)
ans = (s+1000000)*s*Vin/(1000100000*s+2100*s^2+s^3+100000000000)

>> pretty(ans)

(s + 1000000) s Vin
------------------------------------------
2    3
1000100000 s + 2100 s  + s  + 100000000000```

This is the end of my description of SCAM.  We have come a long way from some simple circuit theory, to a description of Modified Nodal Analysis (along with an algorithm for applying MNA - including reactive elements and op-amps), to this page describing a MATLAB tool that performs MNA on a circuit, given a netlist.

# Rules for Forming MNA Matrices without OpAmps

MNA applied to a circuit with only passive elements (resistors) and independent current and voltage sources results in a matrix equation of the form:

We will take n to be the number of nodes (not including ground) and m to be the number of independent voltage sources.

### Notation

• Ground is labeled as node 0.
• The other nodes are labeled consecutively from 1 to n.
• We will refer to the voltage at node 1 as v_1, at node 2 as v_2 and so on.
• The naming of the independent voltage sources is quite loose, but the names must start with the letter "V" and must be unique from any node names.  For our purposes we will require that independent voltage sources have no underscore ("_") in their names.  So the names Va, Vsource, V1, Vxyz123 are all legitimate names, but V_3, V_A, Vsource_1 are not.
• The current through a voltage source will be labeled with "I_" followed by the name of the voltage source.  Therefore the current through Va is I_Va, the current through VSource is I_VSource, etc...
• The naming of the independent current sources is similar; the names must start with the letter "I" and must no underscore ("_") in their names.  So the names Ia, Isource, I1, Ixyz123 are all legitimate names, but I_3, I_A, Isource_1 are not.

### The A matrix

The A matrix is (m+n)x(m+n) and will be developed as the combination of 4 smaller matrices, G, B, C, and D.

• the G matrix is nxn and is determined by the interconnections between the passive circuit elements (resistors)
• the B matrix is nxm and is determined by the connection of the voltage sources.
• the C matrix is mxn and is determined by the connection of the voltage sources.  (B and C are closely related, particularly when only independent sources are considered).
• the D matrix is mxm and is zero if only independent sources are considered.

#### Rules for making the G matrix

The G matrix is an nxn matrix formed in two steps

1. Each element in the diagonal matrix is equal to the sum of the conductance (one over the resistance) of each element connected to the corresponding node.  So the first diagonal element is the sum of conductances connected to node 1, the second diagonal element is the sum of conductances connected to node 2, and so on.
2. The off diagonal elements are the negative conductance of the element connected to the pair of corresponding node.  Therefore a resistor between nodes 1 and 2 goes into the G matrix at location (1,2) and locations (2,1).

#### Rules for making the B matrix

The B matrix is an nxm matrix with only 0, 1 and -1 elements.  Each location in the matrix corresponds to a particular voltage source (first dimension) or a node (second dimension).  If the positive terminal of the ith voltage source is connected to node k, then the element (k,i) in the B matrix is a 1.  If the negative terminal of the ith voltage source is connected to node k, then the element (k,i) in the B matrix is a -1.  Otherwise, elements of the B matrix are zero.

#### Rules for making the C matrix

The C matrix is an mxn matrix with only 0, 1 and -1 elements.  Each location in the matrix corresponds to a particular node (first dimension) or voltage source (second dimension).  For each indendent voltage source, if the positive terminal of the ith voltage source is connected to node k, then the element (i,k) in the C matrix is a 1; if the negative terminal of the ith voltage source is connected to node k, then the element (i,k) in the C matrix is a -1.  For each op-amp let the positive input terminal be at node k and negative terminal at node j;  the corresponding (ith) row of the C matrix has a 1 at location corresponding to the positive terminal (i,k), and a -1 at the location corresponding to the negative terminal (i,j).  Otherwise, elements of the C matrix are zero.

#### Rules for making the D matrix

The D matrix is an mxm matrix that is composed entirely of zeros.  (It can be non-zero if dependent sources are considered.)

### The x matrix

The x matrix is (m+n)x1 and holds our unknown quantities.  It will be developed as the combination of 2 smaller matrices v and j.

• the v matrix is 1xn and hold the unknown voltages
• the j matrix is 1xm and holds the unknown currents through the voltage sources

#### Rules for making the v matrix

The v matrix is an 1xn matrix formed of the node voltages.  Each element in v corresponds to the voltage at the equivalent node in the circuit (there is no entry for ground -- node 0).

#### Rules for making the j matrix

The j matrix is an 1xm matrix, with one entry for the current through each voltage source.  So if there are two voltage sources V1 and V2, the j matrix will be:

### The z matrix

The z matrix is (m+n)x1 z matrix and holds our independent voltage and current sources.  It will be developed as the combination of 2 smaller matrices i and e.  It is quite easy to formulate.

• the i matrix is 1xn and contains the sum of the currents through the passive elements into the corresponding node (either zero, or the sum of independent current sources).
• the e matrix is 1xm and holds the values of the independent voltage sources.

#### Rules for making the i matrix

The i matrix is an 1xn matrix with each element of the matrix corresponding to a particular node.  The value of each element of i is determined by the sum of current sources into the corresponding node.  If there are no current sources connected to the node, the value is zero.

#### Rules for making the e matrix

The e matrix is an 1xm matrix with each element of the matrix corresponding to a voltage source.  If the element in the e matrix corresponds to an independent source it is set equal to the value of that voltage source.  If the element corresponds to an op-amp, then its value is set to zero.

 ← Comments or Questions? Erik Cheever Engineering Department Swarthmore College

# References

DeCarlo, RA, Lin PM, Linear Circuit Analysis: Time Domain, Phasor and Laplace Transform Approaches, Oxford University Press, 2001.  Node Voltage, Loop Current, and Modified Nodal Analysis.

Litovski, V, Zwolinski, M, VLSI Circuit Simulation and Optimization, Kluwer Academic Publishers, 1997.  Modified Nodal Analysis and Computer Simulation.  Also check out first chapter of book.

Rizzoni, G, Principles and Applications of Electrical Engineering, McGraw Hill, 2003.

Smith, RJ, Dorf, RC, Circuits, Devices and Systems, John Wiley & Sons, 1992.  Node Voltage and  Mesh Current.