% extra credit movie for EE498C

f = 60;  % frequency = 60 Hz
T = 1/60;  % period
w = 2*pi*f;
Bm = 1;  % B amplitude

disp('Making movie...')
ct = 1;
for t=[0:T/75:T-T/75]
    Ba = Bm*sin(w*t);
    Bb = Bm*sin(w*t-2*pi/3)*exp(j*2*pi/3);
    % Swap 2 wires, reverse direction
    %Ba = Bm*sin(w*t)*exp(j*2*pi/3);
    %Bb = Bm*sin(w*t-2*pi/3);
    Bc = Bm*sin(w*t-4*pi/3)*exp(j*4*pi/3);
    Bnet = Ba + Bb + Bc;
    
    fig1 = figure(1);
    clf
    grid on
    set(fig1,'Name','Rotating B Field')
    set(fig1,'Position',[60 293 520 520])
    title('Magnetic fields of a three-phase stator')
    xlabel('B_x ')
    ylabel('B_y ')
    xlim([-1.8*Bm 1.8*Bm])
    ylim([-1.8*Bm 1.8*Bm])
    text(-1.6,1.6,['f = ',num2str(f),' Hz'])
    text(-1.6,1.4,['B_m = ',num2str(Bm),' T'])
    text(-1.6,1.2,['|B_n_e_t| = ',num2str(1.5*Bm),' T'])
    
    Bal = line([0 real(Ba)],[0 imag(Ba)]);
    Bbl = line([0 real(Bb)],[0 imag(Bb)]);
    Bcl = line([0 real(Bc)],[0 imag(Bc)]);
    Bnl = line([0 real(Bnet)],[0 imag(Bnet)]);
    set(Bal,'Color','r')
    set(Bbl,'Color','b')
    set(Bcl,'Color','g')
    set(Bnl,'Color','k')
    legend('B_a','B_b','B_c','B_n_e_t')
    
    M(ct) = getframe;
    ct = ct + 1;
end
clear ct

disp('Complete')

fig2 = figure(2);
clf
set(fig2,'Name','Coil Currents as Functions of Time')
set(fig2,'Position',[592 392 560 420])

hold on
grid on
plot([0:T/75:T],Bm*sin(w*[0:T/75:T]),'r')
plot([0:T/75:T],Bm*sin(w*[0:T/75:T]-2*pi/3),'b')
plot([0:T/75:T],Bm*sin(w*[0:T/75:T]-4*pi/3),'g')
plot([0:T/75:T],abs(Bm*sin(w*[0:T/75:T])+Bm*sin(w*[0:T/75:T]-2*pi/3)*exp(j*2*pi/3)...
    +Bm*sin(w*[0:T/75:T]-4*pi/3)*exp(j*4*pi/3)),'k')
xlim([0 T])
ylim([-1.1*Bm 1.7*Bm])
title('Magnetic fields of a three-phase stator')
xlabel('t')
ylabel('B ( t )')
legend('B_a','B_b','B_c','|B_n_e_t|')

disp('Press a key to play movie')
pause
fig1 = figure(1);
clf
grid on
set(fig1,'Name','EE498C Project')
set(fig1,'Position',[60 293 520 520])
title('Magnetic fields of a three-phase stator')
xlabel('B_x ')
ylabel('B_y ')
xlim([-1.8*Bm 1.8*Bm])
ylim([-1.8*Bm 1.8*Bm])
text(-1.6,1.6,['f = ',num2str(f),' Hz'])
text(-1.6,1.4,['B_m = ',num2str(Bm),' T'])
text(-1.6,1.2,['|B_n_e_t| = ',num2str(1.5*Bm),' T'])
disp('Playing movie')
movie(M)
disp('Done')
pause
movie(M)
pause
movie(M)
