# How to do circuit analysis using Matlab?

I often hear of people using Matlab for circuit analysis, but I never actually figured out how it is done. I assume that there is something more to it than just setting up equations by hand and solving them in Matlab.

I'm looking for a good starting point.

-
Are you asking about using Matlab as a "SPICE replacement" to simulate discrete RLC components, or more along the lines of system simulation? –  W5VO Mar 23 '11 at 10:06
@W5VO Both, but with more focus on SPICE. I do know that system simulation can be done with Simulink and that it's not too difficult. –  AndrejaKo Mar 23 '11 at 10:16
Solving circuits is just solving nonlinear differential equations so it is possible in Matlab. OTOH I since SPICE is really nothing more than a nonlinear differential equation solver then I would use Matlab only if I had a really strange or special problem. –  jpc Mar 23 '11 at 12:02

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.

-
Our answers are maybe coming a little late but there is also SimElectronics that is more specific that Simpowersystem (but share with the problem of an expensive toolbox licence) –  Clement J. Aug 4 '11 at 15:27

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.

-
SciPy is free, unlike Matlab, and does a lot of the same stuff. scipy.org/NumPy_for_Matlab_Users I think it's pretty great, myself. –  endolith Mar 23 '11 at 13:25

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) = (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:

Vout/Vin = 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

-
Any chance of an example with a nice explanation of each step? –  AndrejaKo Nov 4 '11 at 0:52
of course - I will try and detail an example for you over the weekend! –  smashtastic Nov 4 '11 at 8:41

for simple RLC circuit with any topology(series and parallel) we can use "rlcdemo". It's good gui for analysis filters(LPF-HPF-BPF-BSF)

 rlcdemo
Analyzing the Response of an RLC Circuit
This demo shows how to use the Control System Toolbox(TM) functions
to analyze the time and frequency responses of common RLC circuits
as a function of their physical parameters.

-