Preface

The origin of this book stems from a Python circuit analysis coding project that I started in 2015. Over the years I’ve put my code on GitHub along with some test circuits, documentation and JupyterLab notebooks. See Source Code for a link to the repository. I think I have enough content that I’m able to turn the effort into an online book.

One of the first topics taught to electrical engineering students is basic electrical circuit analysis. This can be thought of as EE 101, and deals with circuit models not physical circuits. The voltage to current relationship for resistors is described by Ohm’s law, \(v=iR\). The voltage to current relationships for capacitors and inductors can be described by the following differential equations: \[v(t) = \frac{1}{C} \int_{t_0}^t i \mathrm d t+v(t_0)\] \[v=L \frac {\mathrm d i}{\mathrm d t}\] Since differential equations are used to model capacitors and inductors, EE 101 is usually taught after courses in calculus and differential equations. If an electrical network has several branches or loops, then there is a family of equations that describe the circuit, and these are a system of differential equations. Most of the problems presented in textbooks are relatively simple since developing the differential equations and solving them in the time domain with pencil and paper is laborious. After subjecting their students to the rigors of time domain analysis, professors introduce phasors and frequency domain analysis using Laplace transformed circuit elements. Then the problem turns into an algebra problem and students wonder why so much classroom effort was placed on obtaining solutions by solving differential equations.

The circuit analysis technique presented in this book analyzes electrical circuits in the frequency domain. The inductors and capacitors in the circuit are replaced by their transformed values. Inductors are replaced by their steady state Laplace value, \(sL\), where L is the value of the inductor with units of henrys (named after Joseph Henery) and \(s\) is the Laplace variable equal to \(j \omega\) where \(j\) is the imaginary number and \(\omega\) is the radian frequency. Capacitors in the circuit are replaced by their steady state Laplace value, \(\frac {1}{sC}\) where C is the value of the capacitor with units in farads (named after Micheal Faraday).

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).

This book is the result of my continued interest in circuit analysis and synthesis.