% clc_modopt_p10.m    	Optimise modulation for 10 powers     	15-6-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 decile;pin;pout;thi;m1;m2;i1;ic2;vsm1;vsm2.

pmax = 4000;                                        % max operating p
vdc1 = 400;   vdc2 = 400;                           % supply range

tr = 1; f = 5e4; 
xd = 33.2; k1 = 1; 
% xd = 34.8; k1 = 0.6; 
modopt = zeros(9,10);              % 10 cols, 1 for each p decile,
% 10 rows are decile, pin, pout, thi, m1, m2, I1, IC2, vsm1, vsm2.
modopt(1,:) = 1e9;                                  % pin default
for a = 1:10                                        %
    a                                               % progress
	pdes = pmax*a/10;                            	% 
    for b = 1:18                                  	%
%     for b = 1:1                                  	%
        phi = -0.48 - 0.02*b;                    	% range 0.5..0.8
        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 0 or 1:
            m2 = 0.5; m2ss = 0.5;                   %
            while m2ss > 1e-5
                [pin,pout,i1,ic2,vsm1,vsm2] = clc_d_2igbt ...
                    (vdc1,vdc2,phi,m1,m2,xd,k1,tr,f);
                if (pout >= pdes)
                    if (pout <= 1.01*pdes)      % pdes < pout < 1.01 pdes
                        pin = pin*pdes/pout;        % scale to pdes for 
                        if pin < modopt(1,a)        %   better pin value
                            i1 = i1*pdes/pout;
                            ic2 = ic2*pdes/pout;
                            pout = pdes;
                            cv = [pin;pout;phi;m1;m2;i1;ic2;vsm1;vsm2];
                            modopt(1:9,a) = cv;     % store new data
                        end
                        break;
                    else    
                        m2 = m2 - m2ss;
                        if m2 < 0
                            break;
                        end
                    end
                else
                    if (pout > 0.99*pdes)       % 0.99 pdes < pout < pdes
                        pin = pin*pdes/pout;        % scale to pdes for 
                        if pin < modopt(1,a)        %   better pin value
                            i1 = i1*pdes/pout;
                            ic2 = ic2*pdes/pout;
                            pout = pdes;
                            cv = [pin;pout;phi;m1;m2;i1;ic2;vsm1;vsm2];
                            modopt(1:9,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

% Test:   opt_fcn_2igbt(360,400,0.7,0.8,0.75,34,0.6,1.1,5e4) =>
% [pin,pout,i1,ic2,vsm1,vsm2] = 2.7059e+03, 2.5731e+03, 10.8115
% 10.0880, 4.3632e+03, 2.6446e+03. After scaling by 0.9327,
% pin, pout, i1 and ic2 equal 2523.8, 2400, 10.0841 and 9.4093.



