close all;
clear all;

s = tf('s');

%% System
sysa = tf([1], conv([1.5 1], [0.4 1]));

% PI regulator
r0a = 1;
Ti = 1.2;
%reg = tf([Ti*r0 r0], [Ti 0]);
rega = r0a * (1 + 1/(Ti * s));

% Otevrena smycka (system a regulator)
ola = sysa * rega;

% Najde amplitudovou a fazovou bezpecnost otevrene smycky
[Gma,Pma,Wga,Wpa] = margin(ola);

%% System s dopravnim zpozdenim
delay = 1.5;
sysb = tf([1], conv([1.5 1], [0.4 1]));
sysb = sysb * exp(-s * delay);  % Pridani dopravniho zpozdeni
%sysb.outputdelay = delay;  % Jednodussi zpusob :-)

% PI regulator
r0b = 0.218393;
Ti = 1.2;
%reg = tf([Ti*r0 r0], [Ti 0]);
regb = r0b * (1 + 1/(Ti * s));

% Otevrena smycka (system a regulator)
olb = sysb * regb;

% Najde amplitudovou a fazovou bezpecnost otevrene smycky
[Gmb,Pmb,Wgb,Wpb] = margin(olb);