% DCS u31, p25
% with CT plant
% DSC u11, p10

clear all; close all;

A = [ 0 1; 0 0 ];
B = [ 0; 1 ];
C = [ 1 0 ];
D = 0;

sysC = ss( A, B, C, D )

h = 1;

F = [ 1 1; 0 1 ]
H = [ 0.5; 1 ]

sysD = ss( F, H, C, D, h )

tC = 0:(h/10):11*h;
tD = 0:h:11*h;


figure(1)

subplot(211)
step( sysD, tD, 'b')
title('DT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);

subplot(212)
step( sysC, tC, 'r' )
title('CT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);


Wc = [ H F*H ]
rank(Wc)
poly(F)
eig(F)


P = conv( [ 1 -0.5 ], [ 1 -0.2 ] )
roots(P)

K = place( F, H, roots(P) )

figure(2)

subplot(311)
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b')
title('DT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 3])

subplot(312)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r')
title('CT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 3])

subplot(313)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r' )
hold on;
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b' )
title('DT & CT Step Responses')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 3])


% 2nd gains
%P = conv( [ 1 0 ], [ 1 -0.0001 ] )
%P = conv( [ 1 0 ], [ 1 -0.01 ] )
P = conv( [ 1 0 ], [ 1 -0.3 ] )
%P = conv( [ 1 0 ], [ 1 0 ] )
roots(P)

K = place( F, H, roots(P) )

figure(3)

subplot(311)
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b')
title('DT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.8])

subplot(312)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r')
title('CT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.8])

subplot(313)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r' )
hold on;
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b' )
title('DT & CT Step Responses')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.8])


% 2nd gains
%P = conv( [ 1 0 ], [ 1 -0.0001 ] )
%P = conv( [ 1 0 ], [ 1 -0.01 ] )
P = conv( [ 1 0 ], [ 1 -0.1 ] )
%P = conv( [ 1 0 ], [ 1 0 ] )
roots(P)

K = place( F, H, roots(P) )

figure(4)

subplot(311)
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b')
title('DT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.3])

subplot(312)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r')
title('CT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.3])

subplot(313)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r' )
hold on;
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b' )
title('DT & CT Step Responses')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.3])


% 2nd gains
%P = conv( [ 1 0 ], [ 1 -0.0001 ] )
%P = conv( [ 1 0 ], [ 1 -0.01 ] )
P = conv( [ 1 0 ], [ 1 -0.001 ] )
%P = conv( [ 1 0 ], [ 1 0 ] )
roots(P)

K = place( F, H, roots(P) )

figure(5)

subplot(311)
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b')
title('DT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.3])

subplot(312)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r')
title('CT Step Response')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.3])

subplot(313)
sysC_K = ss( A-B*K, B, C, D );
step( sysC_K, tC, 'r' )
hold on;
sysD_K = ss( F-H*K, H, C, D, h );
step( sysD_K, tD, 'b' )
title('DT & CT Step Responses')
set(findall(gcf,'type','line'),'linewidth',3);
axis( [ 0, 10, -0.1, 1.3])


