%rotating sinusoidal winding and air gap flux density distribution
close all ,clear clc
disp(date)
info= ...
        ['                   SINGLE-PHASE SINUSOIDALLY DISTRIBUTED WINDING                    '
         'A  sinusoidally distributed winding is excited with a sinusoidal current.           '
         'The resulting pulsating radial field distribution is shown plotted along a circular '  
         'path in the air gap (which is greatly enlarged for visual purposes).                '
         'The space vector representation of this sinusoidal distribution is shown as a vector' 
         'pointing to where the field is maximum positive (along the horizontal axis) and of  '
         'length proportional to the maximum field strength at that moment.                   '
         '                                                                                    '];                                       
disp(info)
ns=22;rs=1.55;z=0;d=0;e=0;
q=0:pi/80:2*pi;
aviwave=avifile('abcwdg.avi','compression','none');

 figure('units','normalized','Position',[.08 .05 .90 .82],'Name','Field distribution due to sinusoidal winding',...
        'NumberTitle','off');
 %
  theta0=(0:10:360)*pi/180;
    
for e=0:2:358
      beta=e*pi/180;
      z=z+1;
   subplot(2,2,1)
%Phase A
     alpha=0 ; gama=0;
     
theta=theta0-alpha;

p=cos(beta-alpha); 
w=abs(p);
u=cos(theta).*cos(theta);
v=cos(theta).*sin(theta);
x=cos(theta); y=sin(theta); set(gcf,'color','w')
fill(1.85*cos(q),1.85*sin(q),[.6 .6 .6],'LineWidth',1.0);hold on 
fill(1.25*cos(q),1.25*sin(q),[.95 .98 .95]);
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
plot(x,y,'y');
%h0=quiver(x,y,u,v,.6*p,'g');
if p>=0 ,h0=quiver('v6',x,y,u,v,.6*w,'r');
else h0=quiver('v6',x,y,-u,-v,.6*w,'r');
end
rotate(h0,[0 0 1],gama,[0 0 0])
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
g3=compass(1.5*p,0,'r');
rotate(g3,[0 0 1],gama,[0 0 0])
set(findobj(gca,'Type','line'),'LineWidth',2)
 thetas=0:2*pi/ns:2*pi; 
     fs=cos(thetas+.00001-alpha)*cos(beta-alpha);
    for w=1:ns
        dd=abs(fs(w));
         if fs(w)>0; cc='ko';pp=.6;
               else cc='kx';pp=1;
         end     
         
         m3=plot(0,rs,'ko','MarkerSize',dd*20,'MarkerFacecolor','r');
         rotate(m3,[0 0 1],d,[0 0 0])
         m4=plot(0,rs,cc,'MarkerSize',dd*20*pp,'MarkerFacecolor','k','linewidth',2);
         rotate(m4,[0 0 1],d,[0 0 0])
         d=d+360/ns;
     end
text(-.5,-2.1,'Phase A','FontSize',14,'FontWeight','bold','color','r');
axis ('equal')
set(gca,'Xlim',[-2.2 2.2],'Ylim',[-2.2 2.2],'visible','off')
hold off 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
subplot(2,2,3)
%Phase B
 alphab=2*pi/3; gamab=120;arb='g';
 
thetab=theta0-alphab;

pb=cos(beta-alphab); 
wb=abs(pb);
ub=cos(thetab).*cos(thetab);
vb=cos(thetab).*sin(thetab);
xb=cos(thetab); yb=sin(thetab); set(gcf,'color','w')
fill(1.85*cos(q),1.85*sin(q),[.6 .6 .6],'LineWidth',1.0);hold on 
fill(1.25*cos(q),1.25*sin(q),[.95 .98 .95]);
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
plot(xb,yb,'y');
%h0=quiver(xb,yb,ub,vb,.6*pb,'g');
if pb>=0 , h0=quiver('v6',xb,yb,ub,vb,.6*wb,'g');
 else h0=quiver('v6',xb,yb,-ub,-vb,.6*wb,'g');
end
rotate(h0,[0 0 1],gamab)
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
g3=compass(1.5*pb,0,'g');
rotate(g3,[0 0 1],gamab)
set(findobj(gca,'Type','line'),'LineWidth',2)
 thetasb=0:2*pi/ns:2*pi; 
     fsb=cos(thetasb+.00001-alphab)*cos(beta-alphab);
    for w=1:ns
        ddb=abs(fsb(w));
         if fsb(w)>0; cc='ko';pp=.6;
               else cc='kx';pp=1;
         end     
         
         m3=plot(0,rs,'ko','MarkerSize',ddb*20,'MarkerFacecolor','g');
         rotate(m3,[0 0 1],d,[0 0 0])
         m4=plot(0,rs,cc,'MarkerSize',ddb*20*pp,'MarkerFacecolor','k','linewidth',2);
         rotate(m4,[0 0 1],d,[0 0 0])
         d=d+360/ns;
     end 
text(-.5,-2.1,'Phase B','FontSize',14,'FontWeight','bold','color','g');
axis ('equal')
set(gca,'Xlim',[-2.2 2.2],'Ylim',[-2.2 2.2],'visible','off')
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,2,2)
%PhaseC
 alphac=-2*pi/3; gamac=-120;
 
thetac=theta0-alphac;

pc=cos(beta-alphac); 
w=abs(pc);
uc=cos(thetac).*cos(thetac);
vc=cos(thetac).*sin(thetac);
xc=cos(thetac); yc=sin(thetac); set(gcf,'color','w')
fill(1.85*cos(q),1.85*sin(q),[.6 .6 .6],'LineWidth',1.0);hold on 
fill(1.25*cos(q),1.25*sin(q),[.95 .98 .95]);
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
plot(xc,yc,'y');
%h0=quiver(xc,yc,uc,vc,.6*pc,'g');
if pc>=0 , h2=quiver('v6',xc,yc,uc,vc,.6*w,'b');
      else h2=quiver('v6',xc,yc,-uc,-vc,.6*w,'b');
end
rotate(h2,[0 0 1],-120,[0 0 0])
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
g3=compass(1.5*pc,0,'b');
rotate(g3,[0 0 1],gamac,[0 0 0])
set(findobj(gca,'Type','line'),'LineWidth',2)
 thetasc=0:2*pi/ns:2*pi; 
     fsc=cos(thetasc+.00001-alphac)*cos(beta-alphac);
    for w=1:ns
        ddc=abs(fsc(w));
         if fsc(w)>0; cc='ko';pp=.6;
               else cc='kx';pp=1;
         end     
         
         m3=plot(0,rs,'ko','MarkerSize',ddc*20,'MarkerFacecolor','b');
         rotate(m3,[0 0 1],d,[0 0 0])
         m4=plot(0,rs,cc,'MarkerSize',ddc*20*pp,'MarkerFacecolor','k','linewidth',2);
         rotate(m4,[0 0 1],d,[0 0 0])
         d=d+360/ns;
     end 
text(-.5,-2.1,'Phase C','FontSize',14,'FontWeight','bold','color','b');
axis ('equal')
set(gca,'Xlim',[-2.2 2.2],'Ylim',[-2.2 2.2],'visible','off')
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
subplot(2,2,4)
% Resultant
rsd=1.5;ds=0;

q=0:pi/80:2*pi;
thetad=(0:10:360)*pi/180;

ud=cos(thetad).*cos(thetad);
vd=cos(thetad).*sin(thetad);
xd=cos(thetad); yd=sin(thetad);
 set(gcf,'color','w')
fill(1.8*cos(q),1.8*sin(q),[.6 .6 .6],'LineWidth',1.0);hold on 
fill(1.25*cos(q),1.25*sin(q),[.95 .98 .95]);
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
plot(xd,yd,'y');
%h1=quiver(xd,yd,ud,vd,.6,'g');
h1=quiver('v6',xd,yd,ud,vd,.8,'m');
rotate(h1,[0 0 1],e)
%rotate(h0,[0 0 1],e,[0 0 0])

 thetasd=0:2*pi/ns:2*pi; 
    fSd=cos(thetasd+.0001); 
    for w=1:ns
        ddd=abs(fSd(w));
         if fSd(w)>0; cc='ko';pp=.6;
               else cc='kx';pp=1;
         end      
         m3=plot(0,rs,'ko','MarkerSize',17*ddd,'MarkerFacecolor','m');
         rotate(m3,[0 0 1],ds+e,[0 0 0])
         m4=plot(0,rs,cc,'MarkerSize',17*ddd*pp,'MarkerFacecolor','k','linewidth',2);
         rotate(m4,[0 0 1],ds+e,[0 0 0])
         ds=ds+360/ns;
    end 
fill(.75*cos(q),.75*sin(q),[.6 .6 .6]);
set(findobj(gca,'Type','line'),'LineWidth',1.5)
g3=plot(0,0,'xr',[0 2.2 1.9 2.2 1.9],[0 0 .1 0 -.1],'color','m','LineWidth',2);
rotate(g3,[0 0 1],e,[0 0 0])
%text(-1.6,1.97,'ROTATING SINUSOIDAL WINDING','FontSize',12,'FontWeight','bold','color','blue')
text(-.75,-2.1,'Resultant','FontSize',14,'FontWeight','bold','color','m');
axis ('square');
set(gca,'Xlim',[-2.2 2.2],'Ylim',[-2.2 2.2],'visible','off')
hold off
M(z)=getframe;
 g=getframe(gcf); 
 aviwave=addframe(aviwave,g);
    
end     
 aviwave=close(aviwave); 
    
uicontrol('Units','normalized', ...
   'Style','PushButton',...
	'BackgroundColor',[.2 .3 .8], ...
	'Callback','movie(M,.5)', ...
	'FontSize',10, ...
	'FontWeight','bold', ...
	'ForegroundColor',[1 1 1], ...
	'Position',[0.35 0.03 0.10 .05], ...
	'String','Repeat', ...
    'TooltipString','Repeat');
    uicontrol('Units','normalized', ...
   'Style','PushButton',...
	'BackgroundColor',[.2 .3 .8], ...
	'Callback','close,clear,close all', ...
	'FontSize',10, ...
	'FontWeight','bold', ...
	'ForegroundColor',[1 1 1], ...
	'Position',[0.6 0.03 0.09 .05], ...
	'String','Quit', ...
    'TooltipString','Quit');   