%E77 Lab2 Task 1
%David Luong, Adem Kader, Mark Piper
%Code adapted from work done by Erik Cheever

% Set Circuit Values
R1=1000;
R2=4000;
R3=1000;
 
DT=1E-5;        %Time increment
t=0:DT:0.02;    %Time vector
Vin=4*sin(2*pi()*1000*t);  %1kHz sine wave for Vin.
 
% Allocate memory for arrays.
V1=zeros(size(t)); V2=zeros(size(t)); V3=zeros(size(t));
 
%Initial conditions
V1(1)=0; V2(1)=0; V3(1)=0;
 
A=[ 0       -1/R2       1/R2+1/R3;
    -1/R1   1/R1+1/R2   -1/R2;
    1       0           0 ];
    
invA=inv(A);
 
%solve and store results
for i=2:length(t),
    b=[0 0 Vin(i)]';
    x=invA*b;
    V1(i)=x(1);  V2(i)=x(2);  V3(i)=x(3);
end

%plot results
plot(t*1000,V1,'r',t*1000,V2,'g',t*1000,V3,'b');
axis([0 4 -4 4]);
grid on
title('Task 1, MatLab');
xlabel('Time (mS)');
ylabel('Voltage (V)');