%% MT240_NR_8_2_1_T RLC Circuit Template

% Objective: Gain a visual understanding of how the resistance in an RLC
% circuit can affect the circuit's response. 

% Commands: roots, real, imag 

% Background: So far you have explored specific solutions to the three RLC
% cases: underdamped, overdamped, and critically damped. In this program
% you will explore the general solution of a parallel RLC circuit that generates
% all three cases. Your book introduces the general solution in section 8-1, 
% and we will use it to derive the necessary equation.
% The goal is to create the general solution as shown below.
% X(t) = A1*exp(s1*t) + A2*exp(s2*t)

% Below I will explain how to derive the general solution
% The second order differential equation for a parallel RLC circuit is
% d^2i/dt + (1/RC)di/dt + I/LC = 0. 
% This equation is transformed into the characteristic equation.
% s^2 + (1/RC)*S + 1/LC = 0
% Notice how the equation is a second order polynomial that can be solved.
% By solving for the roots of the characteristic equation you obtain s1, and
% s2. 
% With s1 and s2 known, you can set up a system of equations to solve for
% A1 and A2.
% X_init = A1*exp(s1*0) + A2*exp(s2*0)
% dx/dt = A1*s1*exp(s1*0) + A2*s2*exp(s2*0)
% Now that A1, A2, s1, and s2 are found, you can generate the general
% solution.
% X(t) = A1*exp(s1*t) + A2*exp(s2*t)

% Exercise: You have a parallel RLC circuit as shown in the image below.
% The current source has a value of 3A, the capacitor 100uF, the inductor
% 4H, and a potentionmeter (R) assumes the values R = 70:200:1070. Assume
% that the switch has been closed for a long time before opening it at t =
% 0s.
% a) Calculate the general solution to the RLC circuit of the current going 
%    in the inductor for every resistor.
%    There should be 6 resistor values.
% b) Plot the current through the inductor as a function of time for all
%    values of R.
% c) What happens as resistance increases? And why?

%% Template

%Declare Variables
C = 100e-6; %value of the capacitor
L = 4; % value of the inductor
R = 70:200:1070; % Values that the potentiometer can assume
t = 0:.01:1; % time duration

%Initial conditions at t = 0+
% VcInit = ; %Voltage across the capacitor
% IcInit = ; % Current through the capacitor
% VrInit = ; % Voltage across the resistor
% IrInit = ; % Current through the resistor
% VlInit = ; % Voltage across the inductor
% IlInit = ; % Current through the inductor
% 
% %Final conditions at t = inf+
% VcFinal = ; %Voltage across the capacitor
% IcFinal = ; % Current through the capacitor
% VrFinal = ; %Voltage across the resistor
% IrFinal = ; % Current through the resistor
% VlFinal = ; %Voltage across the inductor
% IlFinal = ; % Current through the inductor

% Calculate the initial value derivative di/dt
% dIdT = ; % 

% Calculate coefficients of the characteristic equation
% polynomial 
% d^2i/dt + (1/RC)di/dt + V/LC = 0 
% => s^2 + (1/RC)*s + 1/LC = 0
% => a*s^2 + b*s + c = 0
% a =  % s^2 coefficient
% b =  % s coefficient
% c =  % last coefficient

% Calculate the roots of the characteristic equation for every resistor
% value. Every characteristic equation should have two roots.
% You can use a for-loop for this.
r = []; % an array that will contain the roots of the polynomial
for m = 1:length(R) % the for loop goes through all values of the resistor
r = [r, %INSERT CODE HERE ];
end


% Calculate A1 and A2 using system of equation
%              Matrix A           Matrix B   Matrix C
% |  exp(s1*0)   +  exp(s2*0)  | * | A1 | = | Ilinit |
% | s1*exp(s1*0) + s2*exp(s2*0)| * | A2 | = | dIdT |

% A*B=C
% => A^-1*A*B = A^-1*C
% => B = A^-1*C

% You can use a for-loop to find A1 and A2 for every resistor value
A1 = zeros(1,length(R));
A2 = zeros(1,length(R));
for m = 1:length(R) % the for loop goes through all values of the resistor
A = [%INSERT CODE...];
Ainv = inv(A);
C = [%INSERT CODE ];
B = Ainv*C;
A1(m) = B(1);
A2(m) = B(2);
end


% Now you have all of the necessary parts: A1, A2, s1, and s2. Now you just
% need to plug them into the general solution.

%Putting together the equation
I = []; % a matrix of voltages V(t)
figure(1);

for m = 1:length(R)

I = [I; %INSERT CODE HERE ];
%creates plots of them
subplot(2, 3, m);
plot(t,I(m,:));
%labels

title(['R = ', int2str(R(m))]);
xlabel('time (t)');
ylabel('current (A)');
axis([-.1 1 -3 3]);
end

