%% MT240_10_4_1_T Complex Power Template
% 
%  Objective: Gain a visual understanding of complex power
% 
%  Functions to learn: refline (this could be useful) to plot avg P and
%  reactive P. text (this could be useful) to label your average and
%  reactive power.
% 
%  Exercise: You have seven black boxes(representing unknown circuits), 
%  each with terminals coming out of them. You are curious to identify 
%  which boxes are more inductive, capacitive, or neither. You remember
%  from your circuits class that if you send a sinusoidal signal into a circuit
%  and measure the phase between the voltage and current of the output, you
%  can determine the type of circuit (inductive, capacitivie, or neither).
%
%  You conntect each black box to a sinusoidal signal and measure the
%  voltage and current through each box. You measured the voltages to be
%  the following values:

%           w = 100*pi; % frequency in rads/s
%           T = 2*pi/w; % period of the output signal
%           t = (t = 0:T/100:2*T - T/100);
%           thetaV = [-pi/2, -pi/4, -pi/6, 0, pi/6, pi/4, pi/2]; %phase
%           % shift between the voltage and the current with the current
%           % phase as the refrence phase and a value of 0. Each thetaV 
%           % corresponds to a different black box.
%           I = 1.25*cos(w*t);

%  To gain a visual understanding you decide to calculate and plot the 
%  average power, reactive power, and instantaneous power for the 
%  different voltage phase shifts.


%  a) Calculate the voltage for every value of thetaV using a for loop.
%               % V = 5*cos(w*t + thetaV);

             %the for-loop calculates the voltage for every thetaV
%             for n = 1:length(thetaV)
%             V(n,:) = 5*cos(w*t + INSERT CODE HERE);
%             end

%  b) Calculate the Vrms and Irms using a for-loop to approximate an integral 
%     Vrms = sqrt((1/T)*integral(V(t), from 0 to T)dt) bounds of integration are
%     from 0 to T. Verify your calculation using the equaiton Vrms =
%     Vm/sqrt(2).

%         %rms values
%         Vrms = zeros(1, length(thetaV));
%         Irms =0;
% 
%         % the rms_sum variables hold the value of the integral
%         Vrms_sum = zeros(1, length(thetaV));
%         Irms_sum = 0;
% 
%         % used to calculate the Vrms and Irms
%         % Vrms = sqrt(integral(V, 0, T)/T)
%         % Vrms = sqrt(SUM(V^2*dt,0,T)/T
%         % Vrms_sum = SUM(V^2*dt,0,T)
%         % Vrms = sqrt(Vrms_Sum/T)

%         % T represents the period; however, we need to know how many elements of V
%         % and I represent 1 period. We know that the time array used is 2 periods
%         % long and so the number of elements in V and I represent 2 periods. Thus
%         % the number of elements that represent 1 period in I is 
%         num_of_elem = length(I)/2;

%         % the outer for loop performs the integral.
%         for m = 1:num_of_elem 
%             % the inner for loop iterates through the different values of thetaV
%             for n = 1: length(thetaV);
%              Vrms_sum(n) = Vrms(n) + INSERT CODE HERE;
%             end
%             Irms_sum = Irms_sum + INSERT CODE HERE;
%         end
% 
%         Vrms = sqrt(INSERT CODE HERE); 
%         Irms = sqrt(INSERT CODE HERE);



%  c) Calculate average, reactive, and instantaneous power for every thetaV.

%         % average power 
%         Pavg = zeros(1,length(thetaV));
%         % reactive power 
%         Q = zeros(1,length(thetaV));
%         %instantaneous power
%         Pinst = zeros(length(thetaV),length(V));
% 
% 
% 
%         % caculates Pavg, Q, and Pinst. Plots Pavg, Q, Pinst, V, and I.
%         for n = 1:length(thetaV);
%             Pavg(n) = INSERT CODE HERE;
%             Q(n) =  INSERT CODE HERE;
%             Pinst(n,:) =  V(n,:).*I;
% 
%             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%             %            The following code plots your values
%             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%             % Plot
%             if n < 5
%                 figure(1);
%                 subplot(2,2,n);
%             else
%                 figure(2)
%                 subplot(2,2,n-4)
%             end
%             plot(t, Pinst(n,:), t, V(n,:), '.r', t, I,'*g');
%             legend('Instantaneous Power', 'Voltage', 'Current');
%             refline([0, Pavg(n)]);
%             text(0, Pavg(n)+.25, 'Average Power');
%             refline([0, Q(n)]);
%             text(0, Q(n)-.25, 'Reactant Power');
%             xlabel('time (s)');
%             title(['thetaV = ' , int2str(thetaV(n)/pi*180), ' in degrees']);
% 
% 
%         end
%
%  d) Questions: 
%   1) Why are all of the calculated Vrms values the same even though the
%        phase shift was different?
%    2) When the reactant power is zero what is the relationship between the
%        phases of voltage and current? What does this tell you about the
%        circuit.
%    3) When the reactant power is below zero does the voltage lead or lag the current.
%        Is the circuit more inductive or capacitive?
%   4) When the current and voltage are in phase, why is the instantaneous
%        power never below 0? When it is below zero what is happening to the
%        power?




%% Template

% parameters
w = 100*pi; % frequency
T = 2*pi/w; %Period
time_step = T/50;
t = 0: time_step: 2*T - time_step; %time duration
thetaV = [-pi/2, -pi/4, -pi/6, 0, pi/6, pi/4, pi/2]; % different values of theta
V = zeros(length(thetaV),length(t)); %a matrix of voltage 
thetaI = 0; % hold theta constant

%the for-loop calculates the voltage for every thetaV
for n = 1:length(thetaV)
V(n,:) = 5*cos(w*t + %INSERT CODE HERE);
end


%Current function
I = 1.25*cos(w*t + thetaI);

%rms values
Vrms = zeros(1, length(thetaV));
Irms =0;

% the rms_sum variables hold the value of the integral
Vrms_sum = zeros(1, length(thetaV));
Irms_sum = 0;

% used to calculate the Vrms and Irms
% Vrms = sqrt(integral(V, 0, T)/T)
% Vrms = sqrt(SUM(V^2*dt,0,T)/T
% Vrms_sum = SUM(V^2*dt,0,T)
% Vrms = sqrt(Vrms_Sum/T)
% T represents the period; however, we need to know how many elements of V
% and I represent 1 period. We know that the time array used is 2 periods
% long and so the number of elements in V and I represent 2 periods. Thus
% the number of elements that represent 1 period in I is 

num_of_elem = length(I)/2;
% the outer for loop performs the integral.
for m = 1:num_of_elem 
    % the inner for loop iterates through the different values of thetaV
    for n = 1: length(thetaV);
     Vrms_sum(n) = Vrms(n) + %INSERT CODE HERE;
    end
    Irms_sum = Irms_sum + %INSERT CODE HERE;
end

Vrms = sqrt(%INSERT CODE HERE); 
Irms = sqrt(%INSERT CODE HERE); 

% average power 
Pavg = zeros(1,length(thetaV));
% reactive power 
Q = zeros(1,length(thetaV));
%instantaneous power
Pinst = zeros(length(thetaV),length(V));



% caculates Pavg, Q, and Pinst. Plots Pavg, Q, Pinst, V, and I.
for n = 1:length(thetaV);
    Pavg(n) = %INSERT CODE HERE;
    Q(n) = %INSERT CODE HERE;
    Pinst(n,:) =  V(n,:).*I;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %            The following code plots your values
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    % Plot
    if n < 5
        figure(1);
        subplot(2,2,n);
    else
        figure(2)
        subplot(2,2,n-4)
    end
    plot(t, Pinst(n,:), t, V(n,:), '.r', t, I,'*g');
    legend('Instantaneous Power', 'Voltage', 'Current');
    refline([0, Pavg(n)]);
    text(0, Pavg(n)+.25, 'Average Power');
    refline([0, Q(n)]);
    text(0, Q(n)-.25, 'Reactant Power');
    xlabel('time (s)');
    title(['thetaV = ' , int2str(thetaV(n)/pi*180), ' in degrees']);
    
    
end