close all;
clear all;
clc;
k=3; % no of switches
f=50;
s=5; % no of switching in quater cycle
m=2*s+1; % no of levels of inverter
%no of variables to solve i.e k*s in this case is 15
%in this example ks is odd so harmonics removed upto 3ks-2
vrs=0.02; %delta change in neiche radius per iteration
M=300; %population size
groups=k*s; % no of groups
group_pop=M/groups;
itert_max=1000; % maximum number of iterations
v_max=0.02; % V max
Vdc=100; % dc voltage of inverter
%step1
swarm_pos=random('unif',0,180,1,M); % swarm particles initiated with random values between 0 to pi/2
%step2
swarm_vel=random('unif',-1*v_max,v_max,1,M);
%step 3
% harmonic components and switching angles calculation
dMI=10;%step size of modulation index
MIstart=1.1;%the initial modulation index
MI=MIstart*Vdc;
MIrange=10;%calculation of modulation index range
%values of switching angles
s1=6.90*pi/180;
s2=11.56*pi/180;
s3=13.89*pi/180;
s4=14.06*pi/180;
s5=16.48*pi/180;
s6=19.77*pi/180;
s7=28.40*pi/180;
s8=31.07*pi/180;
s9=33.03*pi/180;
s10=43.01*pi/180;
s11=44.97*pi/180;
s12=48.49*pi/180;
s13=61.10*pi/180;
s14=65.49*pi/180;
s15=68.08*pi/180;
S=[s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15];
for j=1:MIrange
t=[MI*pi/15 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';%harmonic matrix
for i=1:itert_max;
s1=S(:,1); s2=S(:,2); s3=S(:,3); s4=S(:,4); s5=S(:,5); s6=S(:,6); s7=S(:,7); s8=S(:,8);s9=S(:,9); s10=S(:,10); s11=S(:,11); s12=S(:,12); s13=S(:,13); s14=S(:,14); s15=S(:,15);
%switching angles
A=[cos(s1),-cos(s2),+cos(s3),-cos(s4),+cos(s5),-cos(s6),+cos(s7),-cos(s8),+cos(s9),-cos(s10),+cos(s11),-cos(s12),+cos(s13),-cos(s14),+cos(s15);
cos(5*s1),-cos(5*s2),+cos(5*s3),-cos(5*s4),+cos(5*s5),-cos(5*s6),+cos(5*s7),-cos(5*s8),+cos(5*s9),-cos(5*s10),+cos(5*s11),-cos(5*s12),+cos(5*s13),-cos(5*s14),+cos(5*s15);
cos(7*s1),-cos(7*s2),+cos(7*s3),-cos(7*s4),+cos(7*s5),-cos(7*s6),+cos(7*s7),-cos(7*s8),+cos(7*s9),-cos(7*s10),+cos(7*s11),-cos(7*s12),+cos(7*s13),-cos(7*s14),+cos(7*s15);
cos(11*s1),-cos(11*s2),+cos(11*s3),-cos(11*s4),+cos(11*s5),-cos(11*s6),+cos(11*s7),-cos(11*s8),+cos(11*s9),-cos(11*s10),+cos(11*s11),-cos(11*s12),+cos(11*s13),-cos(11*s14),+cos(11*s15);
cos(15*s1),-cos(15*s2),+cos(15*s3),-cos(15*s4),+cos(15*s5),-cos(15*s6),+cos(15*s7),-cos(15*s8),+cos(15*s9),-cos(15*s10),+cos(15*s11),-cos(15*s12),+cos(15*s13),-cos(15*s14),+cos(15*s15);
cos(17*s1),-cos(17*s2),+cos(17*s3),-cos(17*s4),+cos(17*s5),-cos(17*s6),+cos(17*s7),-cos(17*s8),+cos(17*s9),-cos(17*s10),+cos(17*s11),-cos(17*s12),+cos(17*s13),-cos(17*s14),+cos(17*s15);
cos(21*s1),-cos(21*s2),+cos(21*s3),-cos(21*s4),+cos(21*s5),-cos(21*s6),+cos(21*s7),-cos(21*s8),+cos(21*s9),-cos(21*s10),+cos(21*s11),-cos(21*s12),+cos(21*s13),-cos(21*s14),+cos(21*s15);
cos(25*s1),-cos(25*s2),+cos(25*s3),-cos(25*s4),+cos(25*s5),-cos(25*s6),+cos(25*s7),-cos(25*s8),+cos(25*s9),-cos(25*s10),+cos(25*s11),-cos(25*s12),+cos(25*s13),-cos(25*s14),+cos(25*s15);
cos(27*s1),-cos(27*s2),+cos(27*s3),-cos(27*s4),+cos(27*s5),-cos(27*s6),+cos(27*s7),-cos(27*s8),+cos(27*s9),-cos(27*s10),+cos(27*s11),-cos(27*s12),+cos(27*s13),-cos(27*s14),+cos(27*s15);
cos(31*s1),-cos(31*s2),+cos(31*s3),-cos(31*s4),+cos(31*s5),-cos(31*s6),+cos(31*s7),-cos(31*s8),+cos(31*s9),-cos(31*s10),+cos(31*s11),-cos(31*s12),+cos(31*s13),-cos(31*s14),+cos(31*s15);
cos(35*s1),-cos(35*s2),+cos(35*s3),-cos(35*s4),+cos(35*s5),-cos(35*s6),+cos(35*s7),-cos(35*s8),+cos(35*s9),-cos(35*s10),+cos(35*s11),-cos(35*s12),+cos(35*s13),-cos(35*s14),+cos(35*s15);
cos(37*s1),-cos(37*s2),+cos(37*s3),-cos(37*s4),+cos(37*s5),-cos(37*s6),+cos(37*s7),-cos(37*s8),+cos(37*s9),-cos(37*s10),+cos(37*s11),-cos(37*s12),+cos(37*s13),-cos(37*s14),+cos(37*s15);
cos(41*s1),-cos(41*s2),+cos(41*s3),-cos(41*s4),+cos(41*s5),-cos(41*s6),+cos(41*s7),-cos(41*s8),+cos(41*s9),-cos(41*s10),+cos(41*s11),-cos(41*s12),+cos(41*s13),-cos(41*s14),+cos(41*s15);
cos(45*s1),-cos(45*s2),+cos(45*s3),-cos(45*s4),+cos(45*s5),-cos(45*s6),+cos(45*s7),-cos(45*s8),+cos(45*s9),-cos(45*s10),+cos(45*s11),-cos(45*s12),+cos(45*s13),-cos(45*s14),+cos(45*s15);
cos(47*s1),-cos(47*s2),+cos(47*s3),-cos(47*s4),+cos(47*s5),-cos(47*s6),+cos(47*s7),-cos(47*s8),+cos(47*s9),-cos(47*s10),+cos(47*s11),-cos(47*s12),+cos(47*s13),-cos(47*s14),+cos(47*s15)];
Anew=[cos(s1)-cos(s2)+cos(s3)-cos(s4)+cos(s5)-cos(s6)+cos(s7)-cos(s8)+cos(s9)-cos(s10)+cos(s11)-cos(s12)+cos(s13)-cos(s14)+cos(s15);
cos(5*s1)-cos(5*s2)+cos(5*s3)-cos(5*s4)+cos(5*s5)-cos(5*s6)+cos(5*s7)-cos(5*s8)+cos(5*s9)-cos(5*s10)+cos(5*s11)-cos(5*s12)+cos(5*s13)-cos(5*s14)+cos(5*s15);
cos(7*s1)-cos(7*s2)+cos(7*s3)-cos(7*s4)+cos(7*s5)-cos(7*s6)+cos(7*s7)-cos(7*s8)+cos(7*s9)-cos(7*s10)+cos(7*s11)-cos(7*s12)+cos(7*s13)-cos(7*s14)+cos(7*s15);
cos(11*s1)-cos(11*s2)+cos(11*s3)-cos(11*s4)+cos(11*s5)-cos(11*s6)+cos(11*s7)-cos(11*s8)+cos(11*s9)-cos(11*s10)+cos(11*s11)-cos(11*s12)+cos(11*s13)-cos(11*s14)+cos(11*s15);
cos(15*s1)-cos(15*s2)+cos(15*s3)-cos(15*s4)+cos(15*s5)-cos(15*s6)+cos(15*s7)-cos(15*s8)+cos(15*s9)-cos(15*s10)+cos(15*s11)-cos(15*s12)+cos(15*s13)-cos(15*s14)+cos(15*s15);
cos(17*s1)-cos(17*s2)+cos(17*s3)-cos(17*s4)+cos(17*s5)-cos(17*s6)+cos(17*s7)-cos(17*s8)+cos(17*s9)-cos(17*s10)+cos(17*s11)-cos(17*s12)+cos(17*s13)-cos(17*s14)+cos(17*s15);
cos(21*s1)-cos(21*s2)+cos(21*s3)-cos(21*s4)+cos(21*s5)-cos(21*s6)+cos(21*s7)-cos(21*s8)+cos(21*s9)-cos(21*s10)+cos(21*s11)-cos(21*s12)+cos(21*s13)-cos(21*s14)+cos(21*s15);
cos(25*s1)-cos(25*s2)+cos(25*s3)-cos(25*s4)+cos(25*s5)-cos(25*s6)+cos(25*s7)-cos(25*s8)+cos(25*s9)-cos(25*s10)+cos(25*s11)-cos(25*s12)+cos(25*s13)-cos(25*s14)+cos(25*s15);
cos(27*s1)-cos(27*s2)+cos(27*s3)-cos(27*s4)+cos(27*s5)-cos(27*s6)+cos(27*s7)-cos(27*s8)+cos(27*s9)-cos(27*s10)+cos(27*s11)-cos(27*s12)+cos(27*s13)-cos(27*s14)+cos(27*s15);
cos(31*s1)-cos(31*s2)+cos(31*s3)-cos(31*s4)+cos(31*s5)-cos(31*s6)+cos(31*s7)-cos(31*s8)+cos(31*s9)-cos(31*s10)+cos(31*s11)-cos(31*s12)+cos(31*s13)-cos(31*s14)+cos(31*s15);
cos(35*s1)-cos(35*s2)+cos(35*s3)-cos(35*s4)+cos(35*s5)-cos(35*s6)+cos(35*s7)-cos(35*s8)+cos(35*s9)-cos(35*s10)+cos(35*s11)-cos(35*s12)+cos(35*s13)-cos(35*s14)+cos(35*s15);
cos(37*s1)-cos(37*s2)+cos(37*s3)-cos(37*s4)+cos(37*s5)-cos(37*s6)+cos(37*s7)-cos(37*s8)+cos(37*s9)-cos(37*s10)+cos(37*s11)-cos(37*s12)+cos(37*s13)-cos(37*s14)+cos(37*s15);
cos(41*s1)-cos(41*s2)+cos(41*s3)-cos(41*s4)+cos(41*s5)-cos(41*s6)+cos(41*s7)-cos(41*s8)+cos(41*s9)-cos(41*s10)+cos(41*s11)-cos(41*s12)+cos(41*s13)-cos(41*s14)+cos(41*s15);
cos(45*s1)-cos(45*s2)+cos(45*s3)-cos(45*s4)+cos(45*s5)-cos(45*s6)+cos(45*s7)-cos(45*s8)+cos(45*s9)-cos(45*s10)+cos(45*s11)-cos(45*s12)+cos(45*s13)-cos(45*s14)+cos(45*s15);
cos(47*s1)-cos(47*s2)+cos(47*s3)-cos(47*s4)+cos(47*s5)-cos(47*s6)+cos(47*s7)-cos(47*s8)+cos(47*s9)-cos(47*s10)+cos(47*s11)-cos(47*s12)+cos(47*s13)-cos(47*s14)+cos(47*s15)];
c=ceil(A);
a=inv(A);%inverse of A
b=t-Anew;
dA=b\a;%Error in calculation
S=S+dA;%calculate the new value
%haar wavelet
end
MI=MI-dMI;
mm(j)=MI/Vdc;%modulation index
x1(j)=abs(cos(s1));
x2(j)=abs(cos(s2));
x3(j)=abs(cos(s3));
x4(j)=abs(cos(s4));
x5(j)=abs(cos(s5));
x6(j)=abs(cos(s6));
x7(j)=abs(cos(s7));
x8(j)=abs(cos(s8));
x9(j)=abs(cos(s9));
x10(j)=abs(cos(s10));
x11(j)=abs(cos(s11));
x12(j)=abs(cos(s12));
x13(j)=abs(cos(s13));
x14(j)=abs(cos(s14));
x15(j)=abs(cos(s15));
end
%plotting of switching angles