%% EEET 3016 Power Electronics (2022)
%% Lecture 5: DC-DC step-up conversion
% Dr. Yee Wei Law \langleyeewei.law@unisa.edu.au\rangle
% Table of Contents
% EEET 3016 Power Electronics (2022)
% Lecture 5: DC-DC step-up conversion
%     Boost converter example
%     Buck-boost example
%
%% Boost converter example
% Symbolic analysis:

clear all
syms V_I V_O D R f_s L(Vi) k
D = 1-V_I/V_O;
IL = V_O^2/(V_I*R);
diL = k*IL;
L = V_I*D/(f_s*diL)
dLdV_I = simplify(diff(L, V_I), 10)
V_Isol = solve(dLdV_I==0, V_I)
d2LdV_I2 = simplify(diff(dLdV_I, V_I), 10)
subs(d2LdV_I2, V_I, V_Isol(2))
syms dVC dVO L C r r_C(V_I) % Redefine L
dVC = D*V_O/(f_s*R*C);
dVO = r*V_O;
ILmax = collect(V_I*(1/(R*(1-D)^2) + D/(2*f_s*L)), V_I)
r_C = collect((dVO - dVC)/ILmax, V_I)

%
% Exploratory plots:

clear all
Vi=2.7:0.1:6; Vo=8*ones(size(Vi)); R=Vo./1.*ones(size(Vi)); fs=200e3*ones(size(Vi));
D = 1-Vi./Vo;
IL = (Vo.^2./Vi)./R;
diL = 0.4*IL;
L = Vi.*D./(fs.*diL)
plot(Vi, L); xlabel('V_I (V)'); ylabel('L (H)')
dVc = 0.02;
C = D./(fs.*R.*dVc);
plot(Vi, C)
L = 15e-6; C = 22e-6;
dVC = Vo.*D./(R.*fs.*C);
dVO = 0.02*Vo;
ILmax = Vi.*(1./(R.*(1-D).^2) + D./(2.*fs.*L))
rC = (dVO - dVC)./ILmax;
plot(Vi, rC); xlabel('V_I (V)'); ylabel('r_C (\Omega)')

%

clear all
Vi=[2.7 4.2 2*8/3 6]; % First two values used in textbook. Third value maximizes L.
Vo=8*ones(size(Vi)); R=Vo./1.*ones(size(Vi)); fs=200e3;
D = 1-Vi./Vo
IL = (Vo.^2./Vi)./R
diL = 0.4*IL

% Inductor:

L = Vi.*D./(fs*diL)
L = 15e-6; % Pick

% Capactor:

C = D./(fs.*R*0.02)
C = 22e-6; % Pick

% ESR:

dVc = D.*Vo./(fs.*R*C)
dVo = 0.02*Vo
ILmax = Vi.*(1./(R.*(1-D).^2) + D./(2*fs*L))
ESR = (dVo-dVc)./ILmax % Textbook has dVo./ILmax

%
%% Buck-boost example

Vi=24; Vo=8; R=5; L=20e-6; fs=100e3;
D = Vo/(Vi+Vo)
ILmin = Vi*D*(1/(R*(1-D)^2) - 1/(2*fs*L))
C = D/(fs*R*0.02)