Preface

This book describes a Python coding project that I started in 2015. Over the years I’ve put the code on Github along with some test circuits and documentation, linked here. I think I have enough content that I’m able to turn the effort into an online book.

The nodal analysis code started as a translation from some C code to generate a nodal admittance matrix that I had written in 1988. I wrote the C code for two reasons. Free versions of SPICE for the PC didn’t exist at the time and I wanted to use some of the code from, Numerical Recipes in C, Press (1992). The original C code worked well and calculated numerical solutions. I then started writing some C code to generate the matrices with symbolic values and then intended to use LISP to symbolically solve the equations. I didn’t get too far with this effort. The LISP code would generate huge symbolic strings with no simplification. The output was a big pile of trash that was not in the least bit useful or decipherable. Currently, there are many free circuit simulators available.

In 2014, I started to use Python for my coding projects and engineering calculations. There are some nice Python libraries for numeric and symbolic calculations (such as NumPy and SymPy), so I decided to try writing a Python script to generate node equations based on my old C code I had written many years before. Part way into this project I discovered that there is a new nodal analysis technique being taught today in engineering school called modified nodal analysis (MNA). My motivation for reviving this coding project is my continued interest in circuit analysis and synthesis.

The MNA procedure provides an algorithmic method for generating systems of independent equations for linear circuit analysis. Some of my younger colleagues at work were taught this method, but I never heard of it. These days, I never really analyze a circuit by hand, unless it’s so simple that you can almost do it by inspection. Most problems that an electrical engineer encounters on the job are complex enough that they use computers to analyze the circuits. LTSpice is the version of SPICE that I use, since it’s free and does a good job converging when analyzing switching circuits.

My code started initially by following the code from Cheever (2022), which used MATLAB to generate modified nodal equations. I somewhat followed his MATLAB code for resistors, capacitors, op amps and independent sources. The naming of the matrices follows his convention. The preprocessor and parser code was converted from my old C code. The use of Pandas for a data frame is new and SymPy is used to do the math; the use of element stamps is from Palusinski (2005).

Inductors are being addressed in the D matrix, which is different from the way Erik Cheever’s code works. Erik’s code puts inductors into the G matrix as 1/s/L. My code puts the inductor contribution into the D matrix. Coupled inductors also affect the D matrix, so it makes sense to allow the inductors to be in the D matrix rather than the G matrix.

Survey of other symbolic circuit analysis code

The Python code presented in this notebook is somewhat unique since Python is open source, free and runs on a variety of platforms, the code presented in this IPython notebook is portable. As described in the About this book, this code is made available under a public domain license and archived in a github repository.

There are other symbolic circuit analysis codes available and some of these are described here. Some of these codes are based on commercial software such as MATLAB, TINA and Maple.

SLiCAP is a symbolic linear analysis tool. SLiCAP is now a Python program, but originally it was written in MATLAB.

TINA is an acronym of Toolkit for Interactive Network Analysis. The TINA design suite is a circuit simulator and PCB design software package for analyzing, designing, and real time testing of analog, digital, HDL, MCU, and mixed electronic circuits and their PCB layouts. TINA has some symbolic analysis capability.

Maple is a mathematical package and there is an application note available describing its use in symbolic circuit analysis. The application note presents a method for evaluating, solving and designing a common, but not so simple pulse-mode high-gain transimpedance amplifier or TIA circuit.

Symbolic Circuit Analysis is a web page devoted to symbolic circuit analysis.

SAPWIN is a windows program package for symbolic and numerical simulation of analog circuits.

Lcapy is an experimental Python package for teaching linear circuit analysis. It uses SymPy for symbolic mathematics. Hayes (2022) provides an overview of Lcapy as well as a survey of symbolic circuit analysis packages.

Chapter contents

The first four chapters describe my implementation of symbolic circuit analysis with Python. The next part of the book consists of 15 circuits that are used to validate the Python code. These are Jupyter notebooks that have been rendered into chapters. The last part of the book contains circuit analysis example problems; followed by references, code listing and a change log. Section links are provided below.

  • 1  Introduction introduces the topic of MNA and describes how Python can easily solve mathematical expressions that would be very hard and laborious to do with pencil and paper.
    • Jupyterlab, variable names, large exponents
    • Practical network size for symbolic solutions
    • Linear and nonlinear circuits
    • State variables
    • Pros and cons of MNA
  • 2  Theory describes the theory behind MNA.
  • 3  SMNA Example walks through a circuit analysis problem from netlist generation to AC analysis and generation of a frequency response plot of the circuit.
  • 4  SMNA function provides a description of the Python function that implements the symbolic MNA procedure.

Validation tests: There are 15 circuits that were used to validate the Python code. These circuits range from simple circuits that you would find in textbooks, to complex circuits that would be very difficult to analyze if not for computers. There are also a couple of test circuits designed to generate sub-matrices of all zeros during the formulation of the network equations.

Example problems: The last part of the book is a collection of interesting circuits (at least to me) that showcase the power and ease with which the Python code can solve circuit analysis problems.

References: This section provides information about works cited in this book.

Appendix A — SymMNA.py is the Python source listing of the MNA function called in the verification tests and example problems sections.

Appendix B — Change Log is a list of the changes to the MNA code and this book.