%  Figure 4.26      Feedback Control of Dynamic Systems, 8e
%                   Franklin, Powell, Emami 
%
% script to generate Figure 4.26(a) and 4.26(b)
%
% First, input the plant model and compute the reaction curve.
sysp = tf(1,[600 70 1]) 
set(sysp,'InputDelay', 5)

% Method based on the untimate gain
% in this case, kp = 7.65 for P and
% kp = 6.885 and TI = 35 for PI control
sysdP = tf(7.65,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.26(a): Closed-loop responses using Ultimate Period data')
text(120,0.8,'P')
hold on

%case II, PI control
%kp = 6.885, TI=35
sysdPI = 6.885*tf([35 1],[35 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(110,1.1,'PI')
grid on;

%
sysdP = tf(7.65/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.26(b): Closed-loop responses using Ultimate Period data')
text(100,0.65,'P')
hold on;

%case II, PI control
%kp = 6.885, TI=35
%kp/2

sysdPI = (6.885/2)*tf([35 1],[35 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.2,'PI')
grid on;
%
