% Frequency response of various compensators
% 4 November 2019

s = tf('s');

H = 1/(s*(s+1)^2);
bode(H)
% rlocus(H)

%%
% Proportional control only shifts the magnitude of the frequency response
% Derivative control adds a zero:

kp = 1;
kd = 1;
C = kp + s*kd;
bode(C);

% One way to think about derivative control: it's a high-pass filter

%%
% Integral control adds a pole:

kp = 1;
ki = 1;
C = kp + ki/s;
bode(C);

% This is a low-pass filter (with infinite gain at DC)

%%
% Lead compensation
C = (s + 1) / (s + 10);
% bode(C);
clf;
bode(H);
hold on;
bode(C*H)
grid on;

%% Lag compensation
C = (s + 10) / (s + 1);
% bode(C);
clf;
bode(H);
hold on;
bode(C*H)
grid on;

%% Phase margin and damping ratio
% wn = 1;
clf; hold on;
zeta = 0.5;
% for zeta = [0.1 0.5 0.9]
  for wn = [1, 10, 100]
%     G = 1 / (s^2 + 2*zeta*wn*s + wn^2);
    G = wn / (s*(s+2*zeta*wn));
    bode(G);
end

grid on;

%%
clf;
rlocus(