%% Transfer function from control to output

%% Settings
graphics_toolkit gnuplot
%graphics_toolkit fltk

%% Constants
Io = 0.5;

%% Parameters
n = 9;
Vg = 24;
R = 800;
D_on = Io*R / (Vg*n + Io*R);
D_off = 1 - D_on;

L = 75.7e-6;
C = 4.7e-6;

%% Bode Parameters
G0 = n*Vg / (D_off^2 * R);
omega_z = (D_off * R) / (n^2 * L);
omega_p = (D_off) / (n * sqrt(L*C));
Q = (D_off * R / n) * sqrt(C / L);

%% Transfer Function
omega = linspace(10, 1e6, 10000);
s = i * omega;
G_do = G0 * (1 - s / omega_z) ./ (1 + s / (Q*omega_p) + s.^2 / (omega_p^2));

%% Plot

% Magnitude
figure(1, 'visible', 'off');
%figure(1);
plot_mag = semilogx(omega, 20*log10(abs(G_do))), xlabel('\omega'), ylabel('20*log_{10}(|G_{do}|)');
print('Control_to_Output_Magnitude.pdf');

% Phase
figure(2, 'visible', 'off');
%figure(2);
plot_phase = semilogx(omega, 180 / pi * arg(G_do)), xlabel('\omega'), ylabel('\theta');
print('Control_to_Output_Phase.pdf');
