%
% Figure 17.9
%
yo = 200;
%
uo = 2;
%
tau1 = 20;
tau2 = 150;
dt = 1;
gain = 25;
%
kp = 0.40;
ki = 0.005;
esum = 0;
%
y = yo;
yd = yo + 5;
yhat1 = 0;
yhat2 = 0;
u = uo;
yy = [];
uu = [];
%
for i = 1:200,
   e = yd - y;
   esum = esum + e * dt;
   u = uo + kp * e + ki * esum;
   uu = [ uu; u ];
   uhat = u - uo;
   yhat1 = yhat1 * ( tau1 - dt ) / tau1 + ...
            uhat * dt / tau1;
   yhat2 = yhat2 * ( tau2 - dt ) / tau2 + ...
            yhat1 * gain * dt / tau2;
   y = yhat2 + yo;
   yy = [ yy; y ];
end;
%
plot( [ yy ] );
set( gca, 'fontsize', 16 );
axis( [ 0 200 199 207.5 ] );
xlabel( 'Time in minutes' );
ylabel( 'Sensor signal' );
%axis( [ 0 200 199 207.5 ] )
hold on;
%
kd = 2.5;
esum = 0;
laste = 0;
%
y = yo;
yd = yo + 5;
yhat1 = 0;
yhat2 = 0;
u = uo;
yy = [];
uu = [];
%
for i = 1:200,
   e = yd - y;
   esum = esum + e * dt;
   dedt = ( e - laste ) / dt;
   laste = e;
   u = uo + kp * e + ki * esum + kd * dedt;
   uu = [ uu; u ];
   uhat = u - uo;
   yhat1 = yhat1 * ( tau1 - dt ) / tau1 + ...
            uhat * dt / tau1;
   yhat2 = yhat2 * ( tau2 - dt ) / tau2 + ...
            yhat1 * gain * dt / tau2;
   y = yhat2 + yo;
   yy = [ yy; y ];
end;
%
plot( [ yy ] );
hold off;
%
h = line( [ 0 200 ], [ 205 205 ] );
set( h, 'linestyle', '--', 'color', 'k' );
arrow( [ 138 206 ], [ 115 205.2 ] );
h = text( 140, 206, 'PID control' );
set( h, 'fontsize', 16 );
arrow( [ 138 203.5 ], [ 115 204.65 ] );
h = text( 140, 203.5, 'PI control' );
set( h, 'fontsize', 16 );
%
print -f1 -deps2 fig17-9.eps
%

