% lcl_modopt_p10.m  	Optimise modulation for 10 powers     	25-4-15

% Iterate thi, m1 and m2 to obtain the lowest pin for the desired pout,
%   pdes for each decile of the maximum required power. The values
%   recorded in the table have been linearly corrected for pout 
%   equalling the exact pdes, after a solution is found for pdes 
%   within 1% of the target value. If pout isn't obtained, pin = 1e9
%   is recorded as a flag to ignore the other values.
% Each column has pin;pout;thi;m1;m2;i1;ib2;ic1;vsl1;vsl2;vsm1;vsm2.

pmax = 4000;                                        % max operating p
vdc1 = 400;   vdc2 = 400;                           % supply range
tr = 1; xd = 32; f = 5e4; k4 = 1e3; 
modopt = zeros(12,10);
modopt(1,:) = 1e9;                                  % pin default
fwd = 1;                                            % fwd = -1 for reverse
for a = 1:10                                        %
    a                                               % progress
	pdes = pmax*a/10;                            	% 
    for b = 1:16                                  	%
        phi = (0.18 + 0.02*b)*fwd;                 	% range 0.2..0.5
        for c = 1:50                                %
            m1 = c/50;                              % 
% Starting step size of m2 is 0.5, rather than usual 0.25, to prevent 
%   indefinate convergence of m2 on 1:
            m2 = 0.5; m2ss = 0.5;                   %
            while m2ss > 0.003
%                 m1 = m2;
                [p1,p2,i1,i2,ib2,ic1,vsl1,vsm1,vsm2] = lcl_d_2igbt ...
                    (vdc1,vdc2,phi,m1,m2,xd,km,trr,f);
                if fwd == 1                              % forward
                    pin = p1; pout = p2;
                else                                    % reverse
                    pin = -p2; pout = -p1;
                end
                if (pout >= pdes)
                    if (pout <= 1.02*pdes)      % pdes < pout < 1.02 pdes
                        pin = pin*pdes/pout;        % scale to pdes for 
                        if pin < modopt(1,a)        %   better pin value
                            i1 = i1*pdes/pout;
                            i2 = i2*pdes/pout;
                            ib2 = ib2*pdes/pout;
                            ic1 = ic1*pdes/pout;
                            pout = pdes;
                            cv = [pin;pout;phi;m1;m2;i1;i2;ib2;ic1; ...
                                vsl1;vsm1;vsm2];
                            modopt(1:12,a) = cv;     % store new data
                        end
                        break;
                    else    
                        m2 = m2 - m2ss;
                        if m2 < 0
                            break;
                        end
                    end
                else
                    if (pout > 0.98*pdes)       % 0.98 pdes < pout < pdes
                        pin = pin*pdes/pout;        % scale to pdes for 
                        if pin < modopt(1,a)        %   better pin value
                            i1 = i1*pdes/pout;
                            i2 = i2*pdes/pout;
                            ib2 = ib2*pdes/pout;
                            ic1 = ic1*pdes/pout;
                            pout = pdes;
                            cv = [pin;pout;phi;m1;m2;i1;i2;ib2;ic1; ...
                                vsl1;vsm1;vsm2];
                            modopt(1:12,a) = cv;     % store new data
                        end
                     	break;
                    else                            
                        m2 = m2 + m2ss;
                        if m2 > 1
                            break;
                        end
                    end
                end                
                m2ss = m2ss/2;
            end                                     % while 1                        
        end
    end
end




