% Problem 8.20
disp('Problem 8.20 - Ramp response of the system')
n=[0 0 0 0 15];
d=conv([1 0],conv([1 20],[1 4 8]));
disp('Original system')
K=20;
printsys(K*n,d,'s')

% Velocity constant
Kv=K*n(length(n))/d(length(d)-1); 
disp('Steady-state unit ramp error:')
ess_ramp=1/Kv

% Phase-lag compensator
nc=[1 0.1]; dc=[1 0.01];
disp(' '); disp('Phase-lag compensator')
printsys(nc,dc,'s')

% Closed-loop uncompensated system
[n_cl,d_cl]=cloop(K*n,d,-1);

% Closed-loop compensated system
n_c=conv(n,nc); d_c=conv(d,dc);

% Velocity constant of compensated system
Kvc=K*n_c(length(n_c))/d_c(length(d_c)-1);
disp('steady-state unit ramp error of compensated system:')
ess_rampc=1/Kvc
% Closed-loop system
[nc_cl,dc_cl]=cloop(K*n_c,d_c,-1);

% Step response of the compensated system
t=0:0.1:10;
u=t;
[y,x]=lsim(n_cl,d_cl,u,t);
[yc,xc]=lsim(nc_cl,dc_cl,u,t);

plot(t,y,'--',t,yc,'-',t,u,':')
xlabel('time [s]')
ylabel('y,yc')
title('Compensated system')

