Southern technical university
Technical engineering college/ Basra
Electrical engineering techniques
2025-2026
Python for electric circuit
simulation
ﺳﺠﻰ اﺣﻤﺪ ھﺎﺷﻢ:اﻻﺳﻢ
اﻟﻤﺮﺣﻠﮫ اﻟﺜﺎﻧﯿﮫ
Introduction
Electrical circuit simulation is a fundamental process in modern
engineering that allows designers to predict and analyze the behavior of
electrical systems before physical implementation. Traditionally,
engineers have relied on dedicated software packages such as SPICE,
Multisim, or MATLAB for these tasks. However, in recent years, the
Python programming language has emerged as a powerful and flexible
alternative for circuit analysis and simulation.
Python’s rise in the field of electrical engineering is primarily due to its
simplicity, readability, and the vast ecosystem of scientific libraries.
Unlike traditional GUI-based simulation tools, Python allows for
automated testing, complex mathematical modeling, and seamless
integration with data science tools. This makes it particularly effective
for simulating circuits involving time-varying components, control
systems, and power electronics.
This report explores the application of Python in the domain of electrical
engineering. It covers the essential libraries used for numerical
computation and visualization, provides a practical example of a circuit
simulation, and discusses the advantages of using a script-based
approach over conventional simulation software. By leveraging Python,
students and professional engineers can enhance their ability to solve
complex differential equations governing circuit behavior, such as those
found in RLC networks and operational amplifier circuits.
~1~
Essential Python Libraries for Circuit Analysis:
To perform effective electrical circuit simulation, Python relies on
several specialized libraries that handle mathematical computations and
data visualization:
NumPy (Numerical Python): This is the fundamental library for
numerical computation. It provides support for large, multi-dimensional
arrays and matrices, which are essential for representing circuit nodal
equations.
SciPy (Scientific Python): It builds on NumPy and contains modules for
solving differential equations and performing advanced integration. This
is particularly useful for analyzing AC circuits and transient responses.
Matplotlib: A plotting library used to generate high-quality graphs and
charts. It is used to visualize voltage and current waveforms over time or
frequency.
PySpice: This library provides a Python interface to the Spice circuit
simulator (like Ngspice), allowing users to define circuit components
(resistors, capacitors, etc.) and run simulations directly from Python
code.
~2~
Mathematical Foundation and Simulation Workflow:
The process of simulating a circuit in Python typically follows
these steps:
1- Mathematical Modeling: The physical circuit is converted into
mathematical equations using Kirchhoff’s Circuit Laws (KCL
and KVL).
2- Coding the Equations: These equations are translated into
Python code using libraries like NumPy or SciPy.
3- Solving: The Python interpreter solves the equations for
unknown parameters such as node voltages or branch currents.
4- Visualization: The results are plotted using Matplotlib to
produce a visual representation of the circuit’s behavior.
5- For example, in a simple RC circuit, the relationship between
voltage and time during charging is given by:
Vc(t) = Vs (1 - e^{-t/RC})Python can easily calculate this
value for thousands of time points in milliseconds.
~3~
Detailed Workflow of Circuit Simulation in Python
The Systematic Approach to Simulation:
The transition from a physical electrical circuit to a functional Python
simulation involves a rigorous multi-step process. This workflow
ensures that the physical laws of electricity are accurately translated into
a computational environment. The first stage is Mathematical
Discretization, where the continuous behavior of components like
capacitors and inductors is broken down into discrete time steps. Unlike
GUI-based software that hides these calculations, Python requires the
engineer to define the time-domain parameters explicitly using functions
like [Link].
Furthermore, the simulation process integrates Nodal Analysis
techniques. In complex networks, Python’s ability to handle large-scale
matrix inversions via the [Link] module allows for the
simultaneous solution of multiple Kirchhoff’s Current Law (KCL)
equations. This is particularly advantageous when dealing with non-
linear components or circuits with multiple active sources. By scripting
these solutions, the engineer maintains full control over the convergence
criteria and the precision of the numerical integration, which is often a
"black box" in traditional simulation software.
Data Acquisition and Signal Processing:
Beyond mere calculation, Python serves as a high-level tool for signal
processing within the simulation framework. Once the raw data (voltage
or current values) is generated, it can be immediately processed using
statistical methods. For instance, calculating the Root Mean Square
~4~
(RMS) value, performing a Fast Fourier Transform (FFT) to analyze
harmonic distortion, or determining the peak-to-peak ripples in a power
supply circuit. This integration of simulation and advanced post-
processing in a single environment is what makes Python a superior
choice for modern electrical engineering technology students.
Practical Case Study - RC Circuit Analysis
Case Study: Transient Response Simulation:
This section demonstrates how Python is used to analyze the "Transient
Response" of an electrical circuit. In circuits containing capacitors or
inductors, the voltage and current do not reach a steady state
immediately. Python allows us to model this time-dependent behavior
with high precision.
The RC Circuit Model
In a series RC (Resistor-Capacitor) circuit, the charging process is
governed by a first-order differential equation. The key factor here is the
Time Constant (tau = RC), which determines how fast the capacitor
charges. In our simulation, we use Python to create a time vector that
covers the charging period until the circuit reaches its final state.
Advantages of Using Python Scripts
Using scripts for simulation offers several benefits for engineering
students:
Modularity: You can easily change the value of a resistor (R) or
capacitor (C) in the code and re-run the simulation instantly.
Accuracy: Python handles complex exponential functions and
integration with very high numerical precision.
~5~
Simulation Code and Data Visualization
Python Code Implementation:
The following script is designed to simulate the charging phase of an RC
circuit. It uses NumPy to create the time range and Matplotlib to
generate the visual output.
Result AnalysisAs shown in the simulation graph, the voltage across the
capacitor starts at 0V and increases exponentially until it reaches the
source voltage (10V). The curve demonstrates that after approximately
one time constant (tau), the capacitor reaches 63.2% of its maximum
charge. This accurate visualization proves that Python can replace
complex manual graphing in engineering labs.
~6~
Conclusion and Final Summary:
The exploration of Python for electrical circuit simulation throughout
this report has demonstrated that programming is no longer an optional
skill for electrical engineers, but a core necessity. By transitioning from
traditional GUI-based software to a script-oriented environment,
engineers gain unparalleled control over the numerical methods used to
solve complex circuit equations. Python’s open-source nature, combined
with its vast library ecosystem—specifically NumPy for matrix
operations, SciPy for differential integration, and Matplotlib for high-
resolution visualization—provides a professional-grade platform for
both academic research and industrial prototyping.
This report specifically highlighted the efficiency of Python in modeling
“Transient Responses.” As shown in the simulation of the RC circuit, the
ability to define parameters as variables allows for rapid design
iterations and optimization. Beyond simple DC and AC analysis, the
modularity of Python code enables the simulation of more advanced
systems, such as power electronics converters, control loops in robotics,
and signal processing in communication systems.
As we move toward a more digitized engineering landscape, the reliance
on automated simulation tools will only increase. Students of Electrical
Engineering Technology who master Python will find themselves better
equipped to handle the complexities of modern smart grids, renewable
energy systems, and automated industrial plants. In conclusion, Python
is not just a language for software developers; it is a powerful
mathematical engine that empowers electrical engineers to visualize the
invisible laws of electricity with clarity and precision.
~7~
References
1. Lutz, M. (2025). Learning Python: Powerful Object-Oriented
Programming (6th ed.). O'Reilly Media.
2. Sadiku, M. N. O., & Alexander, C. K. (2024). Fundamentals of
Electric Circuits (8th ed.). McGraw-Hill Education.
3. Hunter, J. D. (2007). Matplotlib: A 2D Graphics Environment.
Computing in Science & Engineering, 9(3), 90-95.
4. Oliphant, T. E. (2025). A Guide to NumPy (3rd ed.). Trelgol
Publishing.
5. PySpice Documentation. (2026). Simulating Circuits with Python
and Ngspice. Retrieved from [Link]
~8~