clc
clear
%---------- CONTINUOUS TIME TRANSFER FUNCTION ----------%
numerator = 250;
denominator = [1 30 400];
Gs = tf(numerator, denominator);
%---------- BODE PLOT ----------%
% You will place a Data Cursor over the "starting point" of the plot,
% write down the Magnitude (dB) and then place another Data Cursor
% 10 dB after the first Magnitude. Write down the Frequency of the second
% Data Cursor.
%bode(Gs);
%--------- FC, FS, T ----------%
% You are now ready to obtain the sampling time based on the cutting
% frequency from the previous step
fc = 34; % You should change this field according to G(s) Bode Plot
fs = fc*7;
T = 2*pi/fs;
Ts = round(T,2); % The sampling time must be rounded up (2 decimal places)
%---------- DISCRETE TIME TRANSFER FUNCTION ----------%
Gz = c2d(Gs, Ts);
%---------- SISOTOOL G(Z) ----------%
% Please, refer to [1] for a complete guide on how this section works
% [1] https://dnplas.gitbooks.io/computarized-control-a-matlab-guide/content/chapter1/1-sisotool.html
%sisotool(Gz);
%---------- EXPANDING THE POLYNOMIAL ----------%
z1 = 0.62628;
z2 = 0.8686;
c = 2.157;
polynomial = c*[1 (z1+z2) z1*z2];
%---------- GETTING kd, kc, ki ----------%
kd = polynomial(3);
kc = polynomial(2)-(2*kd);
ki = polynomial(1) - kd - kc;