%  Figure 4.24      Feedback Control of Dynamic Systems, 8e
%                   Franklin, Powell, Emami
%% script to generate Figure 4.24(a), 4.24(b)

% case I, P control by reaction curve data.
% First, input the plant model and compute the reaction curve.

sysp = tf(1,[600 70 1])
set(sysp,'InputDelay', 5) 

% From the curve, kp = 6.92 for proportional control
sysdP = tf(6.92,1)

% To get the closed-loop response, 
% we must use a pade approximation to the delay
syspade = pade(sysp,3);
sysforward = syspade*sysdP;
sysback = tf(1,1)
syscl = feedback(sysforward,sysback)

figure(1)
clf

t=0:0.1:400;

[y,t] = step(syscl,t);

plot(t,y,'LineWidth',2);
title('Figure 4.24(a): Closed-loop response using Reaction Curve data')
text(120,0.8,'P')
hold on

%case II, PI control
%kp = 6.22, TI=43.3

sysdPI = 6.22*tf([43.3 1],[43.3 0]) 
sysforward = syspade*sysdPI;
syscl = feedback(sysforward,sysback)
[y,t] = step(syscl,t);

plot(t,y,'LineWidth',2);
xlabel('Time (sec)');
ylabel('$y$','Interpreter','LaTex');
text(120,1.1,'PI')
grid on;

%
% case I, P control by reaction curve data.
% From the curve, kp = 6.92 for proportional control
% kp/2

sysdP = tf(6.92/2,1)
% To get the closed-loop response, 
% we must use a pade approximation to the delay
syspade = pade(sysp,3);
sysforward = syspade*sysdP;
sysback = tf(1,1)
syscl = feedback(sysforward,sysback)

figure(2)
clf

[y,t] = step(syscl,t);

plot(t,y,'LineWidth',2);
xlabel('Time (sec)');
ylabel('$y$','Interpreter','LaTex');
title('Figure 4.24(b): Closed-loop response using Reaction Curve data')
text(100,0.65,'P')
hold on

%case II, PI control
%kp = 6.22, TI=43.3
%kp/2

sysdPI = (6.22/2)*tf([43.3 1],[43.3 0])
sysforward = syspade*sysdPI;
syscl = feedback(sysforward,sysback)
[y,t] = step(syscl,t);

plot(t,y,'LineWidth',2);
xlabel('Time (sec)');
ylabel('$y$','Interpreter','LaTex');
text(100,1.15,'PI')
grid on;
