% Pspice lab 5
% using degrees as independent variable.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Square-wave operation.
% Purely inductive load with NO back-emf.

% To save figure as vector image,
% go to "Print Preview" and set correct orientation (eg. landscape,
% fill page),
% then "Save As..." and pick pdf, svg etc...

f = 50; % Hz
omega = 2*pi*f; % rad/s
periods = 2;
t = linspace(0, periods/f, 1000); % generate time var for two periods at frequency f.
theta = r2d(omega*t); % calculate phase angle at time t.
fi = 120; % phase angle between phases.
L = 10e-3;

Vd = 300; %V

VAN = Vd*(square(d2r(theta)) + 1)/2;
VBN = Vd*(square(d2r(theta) + d2r(fi)) + 1)/2;
VCN = Vd*(square(d2r(theta) + 2*d2r(fi)) + 1)/2;
VAn = 2/3 * VAN - 1/3 * (VBN + VCN);

VAn1 = 2/pi * Vd * sin(d2r(theta));


m = 4; n = 1;
subplot(m, n, 1)
plot(theta, VAN) %plot with x axis normalized to n*pi
xticks(0:60:720);
xlabel('\theta');, ylabel('VAN'); grid on;

subplot(m, n, 2)
plot(theta, VBN) %plot with x axis normalized to n*pi
xticks(0:60:720);
xlabel('\theta'); ylabel('VBN'); grid on;

subplot(m, n, 3)
plot(theta, VCN) %plot with x axis normalized to n*pi
xticks(0:60:720);
xlabel('\theta'); ylabel('VCN'); grid on;

subplot(m, n, 4)
iA = (1/(r2d(omega).*L)) .* cumtrapz(theta,(VAn)); % compute phase current.
plot(theta, VAn, theta, VAn1, '--', theta, iA - iA(125), 'g')
xticks(0:60:720);
ylabel('VAn'); xlabel('\theta'); grid on;
legend('show'); legend('V_{An}', 'V_{An_{1}}', 'i_o');

suptitle('Three-phase inverter, purely inductive load')

