%% EEET 3016 Power Electronics (2024)
%% Lecture 6: Modeling and controller design
% Dr. Yee Wei Law \langleyeewei.law@unisa.edu.au\rangle
%% Example 4.2
% The averaged model is a nonlinear state equation:
%         \pmatrix{\langle i_L' \rangle \cr \langle v_C' \rangle}
% =
% \pmatrix{
%   -\frac{r}{L} & -\frac{1}{L}(1-\alpha) \cr
%   \frac{1}{C}(1-\alpha) & -\frac{1}{RC}
% }\pmatrix{\langle i_L \rangle \cr \langle v_C \rangle}
% +
% \pmatrix{
%   \frac{\alpha}{L}E \cr
%   0
% }.
% Define function f_1 as the right-hand side of the first line:

syms i_L v_C alpha % Variables
syms r R L C i_Le v_Ce alpha_e E % Constants
f1 = -r/L*i_L - 1/L*(1-alpha)*v_C + alpha/L*E

% Define function f_2 as the right-hand side of the second line:

f2 = 1/C*(1-alpha)*i_L - 1/(R*C)*v_C

% Determine the equilibrium states \langle i_{L}\rangle_e, \langle v_{C}\rangle_e:

[i_Le, v_Ce] = solve(...
    [subs(f1, alpha, alpha_e)==0; ...
     subs(f2, alpha, alpha_e)==0], [i_L, v_C])

% The state Jacobian \frac{\partial \vec{f}}{\partial \vec{x}} is then:

A = subs(jacobian([f1, f2], [i_L, v_C]), [i_L, v_C, alpha], [i_Le, v_Ce, alpha_e])

% The input Jacobian \frac{\partial \vec{f}}{\partial \vec{u}} is then:

B = simplify(subs(jacobian([f1, f2], alpha), [i_L, v_C, alpha], [i_Le, v_Ce, alpha_e]), 10)