% Nodal analysis - solving with Matlab
% Created by: Jon Erickson 18 Sep 2017

% This little demo will introduce you to some matlab basics.
% The end goal is to enable you to harness the power of matlab to solve
% some Circuits!  Away we go...

% Matlab is short for matrix laboratory.  It is really good at handling
% vectors and matrices ( as in [1/R][V] = [I])

% Define circuit parameters easily like this:
Vin = 9; %Volts <--always a good idea to specify units in comments
R1 = 1000; %Ohms
R2 = 2000;
R3 = 3000;
R4 = 4e3;  % <-- Notice the 'e3' means "x10 to the power of", just a little shorthand notation.
R5 = 5e3;

% You can create a COLUMN  vector (called 'a') like this. Note that the semicolon starts
% a new row.  Vectors and Matrices always begin and end with SQUARE
% brackets!

a = [0; -5; 10; -15; 20; 25]

% You can also use previously defined variables in a column vector like
% so; note this is our forcing vector [I] from the example on Monday.
% Vin was previously defined
I = [Vin; 0; 0]

% OK, let's create a matrix.  Commas separate columns, but are not strictly
% necessary. Strongly suggest using them to keep your code clean! Also, you
% can write out matrices in a single line, but strongly suggest breaking
% them into multiple lines for human readability. Again, note *SEMICOLON* vs
% *COMMA*.


b = [1, -2, 3;
    -5, 4, -6;
    7, -8, 9];

% Similarly, we can use previously defined constants R1 - R5 in our
% definition of the conductance matrix [G] = [1/R]

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

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

% First notice what happens when we *LEFT MULTIPLY* the inverse of G times G.
% We'll use the inv(...) function to do this.  You can learn more about
% this function or any matlab function by using the DOC or HELP at the
% command prompt like this: >> doc inv
M = inv(G)*G  %<-- Notice that function arguments are inside of *PARENTHESES*

% Hopefully M returns the IDENTITY MATRIX. This consists of 1's along the
% main diagonal and 0's otherwise
% To help shore up any lingering linear algebra quandaries, let's see what
% happens when you multiply any vector of matrix time the identity matrix

 c = M*G % <-- Hopefully this returns c = M*G = G
 d = M*I % <-- Verify this returns d = M*I =  I, the forcing vector

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

%  The variable V a 3x1 column vector, that is V = [Va; Vb; Vc];
%  Let's say you want to compute the voltage across R4: VR4 = Vb - Vc
%  You can reference elements within an array with integer index in
%  PARENTHESES.  Note that matlab uses 1-based indexing (not 0-based, as in
%  C++, etc)
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

VR4 = Vb - Vc

% Of course you could do this one step quicker like so:
Vr4 = V(2) - V(3) %<--Note matlab is CASE SENSITIVE, so now we have VR4 and Vr4 variables.

%In case you ever need to reference elements in a matrix, just do it with
%row and column indices like so:
g12 = G(1,2) %<--returns 1st row, 2nd column element

% This is the end of the demo.  Feel free to modify and adapt this SCRIPT
% to your heart's content to help you solve more Circuits! :)
a =

     0
    -5
    10
   -15
    20
    25


I =

     9
     0
     0


G =

    1.0000         0         0
    0.0010   -0.0021    0.0003
         0    0.0003   -0.0004


M =

    1.0000         0         0
         0    1.0000   -0.0000
         0   -0.0000    1.0000


c =

    1.0000         0         0
    0.0010   -0.0021    0.0003
   -0.0000    0.0003   -0.0005


d =

     9
     0
     0


V =

    9.0000
    4.6286
    2.5714


VR4 =

    2.0571


Vr4 =

    2.0571


g12 =

     0