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
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
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
%[num,den] = eqtflength(num,den); % Make lengths equal
%[z,p,k] = tf2zp(num,den) % Obtain zero-pole-gain form