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
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