%% EEET 3016 Power Electronics (2022)
%% Lecture 4: DC-DC step-down conversion
% Dr. Yee Wei Law \langleyeewei.law@unisa.edu.au\rangle
% Table of Contents
% EEET 3016 Power Electronics (2022)
% Lecture 4: DC-DC step-down conversion
%     Capacitor selection
%     Buck converter example
%     DCM analysis
%     DCM example
%

clear all;
syms deltaQ diL T C L q ii Io Imax Vo D fs
% di_L = Vo*(1-D)/(fs*L)
dQ = 1/2*T/2*diL/2
Eload = simplify(1/C*int(q, q, 0, dQ/2) + L*int(ii, ii, Imax-diL/2, Imax), 10)
Eload = simplify(1/C*int(q, q, 0, C*Vo) + L*int(ii, ii, 0, Imax), 10)

%
%% Capacitor selection

clear all;
fs = 3e6; Vi = 3.3; Vo = 1.1; L = 0.47e-6; Io = 3; Vos = 50e-3;
D = Vo/Vi;
diL = Vo*(1-D)/(fs*L)
C_LT = L*(Io + diL/2)^2/((Vo+Vos)^2-Vo^2)

%
%% Buck converter example

Vo = 18; Vi = 48; 
R = 10; Vripple = 0.5e-2; fs = 40e3;

% Calculate L.

D = Vo/Vi;
L = R*(1-D)/(2*fs)

% Add 25% margin to L:

L = L*1.25

% Pick L = 100~\muH:

L = 100e-6;

% Now calculate C.

C = (1-D)/(8*L*fs^2*Vripple)

% No change to calculated C.
% RMS value of inductor current:

I_L = Vo/R;
diL = Vo*(1-D)/(fs*L);
I_Lrms = sqrt(I_L^2 + diL^2/12)

% RMS value of capacitor current:

I_Crms = diL/sqrt(12)
Vo*100.5/100

%
%% DCM analysis

syms L R T Vi Vo D D1 t

assume(L, 'positive'); assume(R, 'positive'); assume(T, 'positive');
assume(D, 'positive'); assume(D1, 'positive');
assume(Vi, 'positive'); assume(Vo, 'positive');

iL1 = simplify(subs((Vi-Vo)*t/L, Vi, Vo*(D+D1)/D), 10)
iL2 = collect(subs(-Vo*t/L + Vi*D*T/L, Vi, Vo*(D+D1)/D), t)
IL = collect(1/T*(int(iL1, t, 0, D*T) + int(iL2, t, D*T, (D+D1)*T)), D1)
IL = collect(IL, D1)
simplify(solve(IL == Vo/R, D1), 10)

%
%% DCM example

Vi=24; L=200e-6; R=20; C=1e-3; fs=10e3; D=0.4;
D1=(-D+sqrt(D^2+8*L*fs/R))/2
Vo=Vi*D/(D+D1)
ILmin=Vo*(1/R-(1-D)/(2*L*fs))