% Demonstration of steady-state power.
%
% ELEC 226, Rich Kozick

% Magnitude of voltage and current

Vm = 2;
Im = 3;

% Phase of voltage and current, in degrees

ThetaVd = 30;
ThetaId = 0;

ThetaV = ThetaVd/180*pi;  % Convert to radians
ThetaI = ThetaId/180*pi;

% Generate voltage and current waveforms, with frequency 1 Hz

T = 5;       % Time interval, in seconds
dt = 0.01;   % Spacing between time samples
t = 0:dt:T;  % Sampling times

v = Vm*cos(2*pi*t + ThetaV);
i = Im*cos(2*pi*t + ThetaI);
p = v .* i;     % Instantaneous power
P = Vm * Im / 2 * cos(ThetaV - ThetaI) * ones(size(v));
Q = Vm * Im / 2 * sin(ThetaV - ThetaI) * ones(size(v));
aS = sqrt(P.^2 + Q.^2);
fprintf('P = %.2f (W), Q = %.2f (VAR), |S| = %.2f (VA)\n', P(1), Q(1), aS(1));

figure(1)
plot(t, v, 'r-', t, i, 'y-', t, p, 'k:', t, P, 'b-', t, Q, 'g--', t, aS, 'm-.')
legend('v(t)', 'i(t)', 'p(t) = v(t) * i(t)', 'P', 'Q', '|S|')
xlabel('TIME (sec)')
ylabel('V / A / W')
title('Voltage, Current, and Instanteous Power')
