%Ziegler-Nichols step response method
% kja 070504

aminit;

dt=0.1;t=0:dt:7;
P=tf(1,[1 3 3 1]);
y=step(P,t);
yd=diff(y)/dt;
dydt=([0;yd]+[yd;0])/2;
[yy,kk]=max(dydt);
tmax=t(kk);
t0=t(kk)-y(kk)/dydt(kk);
a=t0*dydt(kk);
%ya=max(0,y(kk)+dydt(kk)*(t-t(kk)));
ya=-a+dydt(kk)*t;

figure(1); clf; subplot(221); hold on;

% Draw the axes
amaxis([-1 7 -0.3 1]); axis off;
arrow([0 -0.3], [0 1.0], AM_normal_arrowsize);
arrow([-1 0], [7 0], AM_normal_arrowsize);

% Draw the step response
plot(t, y, 'b-', 'LineWidth', AM_data_linewidth);

% Add the asymptote at the max slot
plot(t(kk), y(kk), 'ro', 'MarkerSize', AM_data_markersize);
plot(t0, 0, 'k+', 'MarkerSize', AM_data_markersize);
plot(t, ya, 'r-', 'LineWidth', AM_ref_linewidth)
plot(0, -a, 'k+', 'MarkerSize', AM_data_markersize);
amtext(t0,-0.1,'Td');
amtext(6.8,-0.1,'t');
amtext(-0.45, 1.0, 'y')
amtext(-0.45,-a+0.01,'a');

daspect([5 1 1]);

amprint('znstep.eps');
