16
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Are you asking about using Matlab as a "SPICE replacement" to simulate discrete RLC components, or more along the lines of system simulation? \$\endgroup\$
    – W5VO
    Commented Mar 23, 2011 at 10:06
  • \$\begingroup\$ @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. \$\endgroup\$
    – AndrejaKo
    Commented Mar 23, 2011 at 10:16
  • 5
    \$\begingroup\$ 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. \$\endgroup\$
    – jpc
    Commented Mar 23, 2011 at 12:02

7 Answers 7

7
\$\begingroup\$

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.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ 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) \$\endgroup\$
    – Clement J.
    Commented Aug 4, 2011 at 15:27
  • \$\begingroup\$ The thing is I found Simulink even with all the toolboxes added rather cumbersome. Just look at what it takes to get a RLC circuit simulated. Far more complicated than in SPICE. Only worthwhile IMO in very specific cases where you have controller-related behavioral code blocks that can't be done in SPICE in any reasonable way. \$\endgroup\$ Commented Dec 16, 2015 at 14:08
  • \$\begingroup\$ Also, to just get a symbolic transfer function qsapecng is free and you just draw the circuit and click one button and it's done. \$\endgroup\$ Commented Dec 16, 2015 at 14:15
14
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ Any chance of an example with a nice explanation of each step? \$\endgroup\$
    – AndrejaKo
    Commented Nov 4, 2011 at 0:52
  • \$\begingroup\$ of course - I will try and detail an example for you over the weekend! \$\endgroup\$ Commented Nov 4, 2011 at 8:41
  • \$\begingroup\$ This is perfecly correct, but totally pointless. Do not judge me, i love Laplace and Z transforms... :).... Just try to use this approach for simulating a transistor and op-amp amplifiers... :) \$\endgroup\$
    – Brethlosze
    Commented Apr 22, 2016 at 20:58
  • \$\begingroup\$ I think you made a mistake when writing the transfer function for the low pass filter. I think you meant Vout/Vin = (1/sC)/(R + (1/sC)) \$\endgroup\$ Commented Sep 18, 2016 at 6:19
6
\$\begingroup\$

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.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – endolith
    Commented Mar 23, 2011 at 13:25
  • \$\begingroup\$ Some SPICEs have Monte Carlo simulation and sensitivity analysis added too. SIMetrix in particular has the former for free even in the non-expiring demo/intro version, but the latter function is not free. Both Intersil (iSim:PE) and Analog Devices (ADIsimPE) now ship SIMetrix as their company SPICE, but it's basically the intro version with their extra chip libraries. \$\endgroup\$ Commented Dec 16, 2015 at 14:24
  • \$\begingroup\$ Also that textbook cover misspelled MATLAB as MATHLAB... pretty amusing and doesn't inspire a lot of confidence. \$\endgroup\$ Commented Dec 16, 2015 at 14:32
1
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

You can use a program created in Matlab called SCAM (symbolic circuit analysis in Matlab), and is here: https://www.swarthmore.edu/NatSci/echeeve1/Ref/mna/MNA6.html

\$\endgroup\$
2
  • \$\begingroup\$ Why? just use SimScape+SimElectronics as it can use SPICE models ... \$\endgroup\$
    – user16222
    Commented Jul 30, 2016 at 20:56
  • \$\begingroup\$ SimElectronics does not give you analytical equations which are sometimes very useful \$\endgroup\$
    – JLo
    Commented Jun 13, 2017 at 8:25
0
\$\begingroup\$

Besides SCAM in Matlab, there is also a slick online symbolic circuit analysis tool at CircuitNAV, which uses netlist files (from LTspice, Micro-Cap, TINA-TI, PSpice, etc) as the input and generates the algebraic solution for each circuit parameter.

CircuitNAV also provides a demo and a tutorial.

\$\endgroup\$
-1
\$\begingroup\$

The pdf book of Spice and Matlab examples, http://www.elsevierdirect.com/companions/9780750659321/exercises/SpiceAndMatlab.pdf , a companion book to Introduction to Linear Circuit Analysis and Modelling by Moura & Darwazeh, looks really good for my purposes.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. \$\endgroup\$
    – user17592
    Commented May 2, 2013 at 20:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.