How to do circuit analysis using Matlab?

I use MATLAB quite a bit for circuit analysis. Sometimes I prefer it to spice, other times I prefer spice, depends on my mood and requirements.

These are the following steps:

  • 1: take the Laplace transform of the circuit
  • 2: obtain the transfer function
  • 3: plot/analyse using MATLAB functions. bode, impulse, freqresp and so on.

The trickiest part I find is to take the Laplace transform and derive your transfer function equation.

There are many examples and text books on taking a Laplace on the Internet. Briefly the aim here is to get the equation in the form of

$$H(s) = \dfrac{as^2 + bs + c } {ds^2 + es + f} $$

where \$a\$ to \$c\$ is the numerator and \$d\$ to \$f\$ the denominator in the example presented below.

To do this convert all you passive elements into complex impedances. Thats is

  • C = 1/sC
  • R = R
  • L = sL

Next derive an equation for your circuit in the form of Vout/Vin.

For a simple low pass filter in the form of:

Vin -------R-------------- Vout
               |
               C
               |
------------------------------

this would yield:

\$ \dfrac{V_{out}}{V_{in}} = \dfrac{sC}{R + sC}\$

Write the above equation in the form of num and den for MATLAB:

num = [C 0];
den = [C R];

Then follow on using any matlab function you like to analyse the transfer function (bode), pole zero diagram and so on.

Below is an example of filter I was recently playing with and trying to tune the values:

R1 = 20e3;
C1 = 235e-9;
R2 = 2e3;
C2 = 22e-9;
num = [2*R2*C1 0];
den = [C1*R1*C2*R2*2 (2*C1*R1 + C2*2*R2) 2];
g = tf(num,den);
P = bodeoptions; % Set phase visiblity to off and frequency units to Hz in options
P.FreqUnits = 'Hz'; % Create plot with the options specified by P
bode(g,P);
%[num,den] = eqtflength(num,den);      % Make lengths equal
%[z,p,k] = tf2zp(num,den)          % Obtain zero-pole-gain form

I some time use scipy (a numerical toolset for python) to do circuit analysis. And yes, that typically involves solving the circuit equations by hand first. This is mostly helpful when doing tolerance analysis and sensitivity analysis on the circuit.

There is a book on the subject "Tolerance Analysis of Electronic Circuits Using MATLAB" that provides some examples of how to carry out the typical analysis on some common circuits. It's not really a replacement for something like SPICE, but is useful when trying to design for good production yield over all component tolerances, or to account for component drift over time and temperature.


You can use the Matlab Simulink Simpowersystem toolbox to make circuit analysis. It includes RLC components, switches, electrical machines, etc. You can create your own component and modify any parameters of the library components. As you can combine your circuits with any Simulink blocs, any Simulink solver or any Matlab function, this tool is very powerful. There is no need to solve the circuit equations first because you work in the Simulink environment. It is originally oriented for power systems but I think you can use it for any electronics circuit.

Tags:

Matlab