% close all;

BodeOptions = bodeoptions;
BodeOptions.FreqUnits = 'Hz';
BodeOptions.Xlim = [1 1e6];
BodeOptions.Ylim = {[-60,50];[-180,180]};
BodeOptions.YLimMode = {'manual';'manual'}; %{maglimits mode;phaselimits mode}
BodeOptions.Grid = 'on';
BodeOptions.PhaseWrapping = 'on';
BodeOptions.PhaseMatching = 'on';
BodeOptions.PhaseMatchingFreq = .001;
BodeOptions.PhaseMatchingValue = 0;

L = 100e-6;
C = 4*270e-6;
Vbat = 26;
Vbus = 50;

Pmin = -0;
Pmax = 250;

Prange = [-50 -10 10 50 100 150 250 ];
% for Vbus = [30 50 75]
for Vbus = 50
for Pbus = Prange;

    Ibus = Pbus/Vbus;
    Rs = 1;
    Vm = 1;
    H = 1;

    s = tf('s');

    D = 1-Vbat/Vbus;
    Il = Ibus/(1-D);

    Gid0 = Il/(1-D);
    wzi = 1/C*Ibus/Vbus;
    wo =(1-D)/sqrt(L*C);
    Gid = Gid0*(1+s/wzi)/(1+s^2/wo^2);
    
%     bode(Gid,BodeOptions)
    
    Kpi = .1;
    wzi = 5000;
    wpi = 1e5;
    Gci =  Kpi*(1+wzi/s);
    
%     bode(Gci)


    Ti = Gci/Vm*Gid*Rs;

    Gi = Ti/(1+Ti);
    
%     bode(Gi, BodeOptions);

%     bode(Ti, BodeOptions);

    Gvio = (1-D)*Vbus/Ibus;
    wzvi = (1-D)^2/L*Vbus/Ibus;
    wpvi = 1/C*Ibus/Vbus;
    Gvi = Gvio*(1-s/wzvi)/(1+s/wpvi);

    Kpv = 1;
    wzv = 100;

    wpv = 1000*2*pi;
    Gvs = Kpv*(1+wzv/s)/(1+s/wpv);

    Tv = (H*Gvs*Gi/Rs*Gvi);

    


%     bode(Tv, BodeOptions);

  
    
    Gv = (Tv/(1+Tv));
%     bode(Gv,BodeOptions)

    stepplot(minreal(Gv));
    hold on;

	
end
end
return;
