% File clc_zvs_rng.m                                           28-2-15

% Find the minimum value of each of 4 leg currents at the switching points,
%   as a function of modulation, for a range of dcr values and +- 10%
%   Monte Carlo variations of k2,k3,k4. dcr = tr*Vdc2/Vdc1.
% Based on equal bridge modns m*pi, B2 leading B1 by 0.5*pi.
% The reactances of XL1, XC1, XL2 and XC2 at fs are k1, k2, k3 and k4.

k1 = 0.6;                          	% relative L1 factor
imin = 33*ones(4,20);               % 4 legs, arbitrarily extreme limits
for mod = 1:20                      % 20 Modulations
    m = 0.05*mod;                 	%   0.05 .. 1        
    for d = 1:2
        dcr = 0.7 + 0.7*(d-1);                      %   0.7, 1.4
        norm = 1/sqrt(dcr);                         % normalise i values
        for a = 1:3
        	k2 = (1 + 0.1*(a-2))*(1 + k1);       	% 0.9,1,1.1 *
        	for b = 1:3
            	k3 = 1 + 0.1*(b-2);               	% 0.9,1,1.1
                for c = 1:3
                    k4 = 1 + 0.1*(c-2);           	% 0.9,1,1.1                   
%----------------------------------------------------------------------        
rv = norm*clc_iv12(1,1,-0.5,m,m,1,k1,k2,k3,k4,1,(2 - m)/2);
if rv(1) < imin(1,mod)                  % +i1 value at Q2 turn-on
 	imin(1,mod) = rv(1);                %   => ZVS of leg1 
end
rv = -norm*clc_iv12(1,1,-0.5,m,m,1,k1,k2,k3,k4,1,(2 + m)/2);
if rv(1) < imin(2,mod)                  % -i1 value at Q4 turn-on
	imin(2,mod) = rv(1);                %   => ZVS of leg2
end
rv = -norm*clc_iv12(1,1,-0.5,m,m,1,k1,k2,k3,k4,1,(1 - m)/2);
if rv(2) < imin(3,mod)                  % -i2 value at Q6 turn-on
    imin(3,mod) = rv(2);                %   => ZVS of leg3
end
rv = norm*clc_iv12(1,1,-0.5,m,m,1,k1,k2,k3,k4,1,(1 + m)/2);
if rv(2) < imin(4,mod)                  % +i2 value at Q8 turn-on
    imin(4,mod) = rv(2);                %   => ZVS of leg4    
end        
%----------------------------------------------------------------------        
                end
            end
        end    
    end        
end

save('D:\Power_Elect\Results\CLC\CLC_Imin_0.6.mat','imin')

m = 0.05:0.05:1;                                    % for display purposes
%   Display current minimum values at the switching points:
hold off
figure(1);
h = plot (m,imin(3,:),'-b*',m,imin(2,:),'--b',...
    m,imin(4,:),'-rx',m,imin(1,:),'-r');
% title('Minimum Leg Currents at Sw Points','Fontsize',12)
set(gca,'xtick',0:0.2:1)
set(gca,'xticklabel',0:0.2:1)
% set(gca,'xticklabel',[0;0.2;0.4;0.6;0.8;1])
grid on
xlabel('Relative Modulation','fontsize',16)
ylabel('Normalised Current','fontsize',16)
legend(h,'Leg3','Leg2','Leg4','Leg1','location','southeast')
set(gca,'fontsize',16)
set (h,'LineWidth',2.5)






    
