% Nodal analysis - single stage transistor amp
% Created by: Jon Erickson 20 Sep 2017

% This solves the single stage amplifier example from class.


% Define circuit parameters using those given in PS2, prob 3
Vin = 2; %Volts <--always a good idea to specify units in comments
Rin = 50;
R1 = 2800; %Ohms
R2 = 35e3;
R3 = 2e3;
R4 = 250;
R5 = 2500;
B = 200; %beta value

%Define forcing vector [I]
I = [Vin; 0; 0; 0];


% Define Conductance matrix [G] (4x4 in this case, since we have 4 nodes to solve for).

G = [1, 0, 0, 0;
    1/Rin, -(1/Rin + 1/R1 + 1/R2 + 1/R3), 1/R3, 0;
    0, (B+1)/R3, -((B+1)/R3 + 1/R4), 0;
    0, B/R3, -B/R3, 1/R5 ]

% OK, now let's see how to solve matrix equations.  We want the nodal
% voltages vis:  [G^-1]*([G][V] = [I])


% Let's compute the nodal voltages:
 V = inv(G)*I

%  The variable V a 4x1 column vector, that is V = [Va; Vb; Vc; Vd];
Va = V(1); % <--returns the 1st element of V, ie what we prev defined to be Va
Vb = V(2); % <-- returns the 2nd element of column vector V, and so on
Vc = V(3); % <-- the 3rd element of V
Vd = V(4);

Vout = Vd; %output voltage

Av = Vout/Vin %voltage gain

i3 = (Vb-Vc)/R3
i4 = Vc/R4

currentGain = i4/i3


%Note: The voltage gain is negative, which means the output signal is
%INVERTED relative to the input.  A positive swing on a sine wave at the
%input appears at the output as a negative swing, and vice-versa
G =

    1.0000         0         0         0
    0.0200   -0.0209    0.0005         0
         0    0.1005   -0.1045         0
         0    0.1000   -0.1000    0.0004


V =

    2.0000
    1.9603
    1.8853
  -18.7590


Av =

   -9.3795


i3 =

   3.7518e-05


i4 =

    0.0075


currentGain =

  201.0000