%Time and frequency responses to reference inputs
%   kja 070704

aminit;					% initialize MATALB

%Simulation of the six step responses for the basic loop
%The simulation is based on 
%PID Control of a process with 3 equal lags
P = tf(1,[1 3 3 1]); 
%Controller parameters
k=0.6
ki=0.5;
T=0.5;
%---------------------------------------------------
%Prels
numc=[k ki];
denc=[1 0];
nump=1;
denp=[1 3 3 1];
dencl=conv(denp,denc)+conv([0 0 0 1],conv(nump,numc));
denpr=[T^3 3*T^2 3*T 1];
%---------------------------------------------------
%The six systems
sys11=tf(conv(nump,numc),dencl);
sys12=tf(conv(nump,denc),dencl);
sys21=tf(conv(denp,numc),dencl);
sys22=tf(conv(denp,denc),dencl);
sys11f=tf(1,denpr);
sys21f=tf(denp,denpr);

%
%Time responses
%
figure(1); clf;

t=0:0.01:30;
y11=step(sys11,t);
y12=step(sys12,t);
y21=step(sys21,t);
y22=step(sys22,t);
y11f=step(sys11f,t);
y21f=step(sys21f,t);

subplot(321)
pl=plot(t, y11, '-', t, y11f, '--', 'Linewidth', AM_data_linewidth);
amaxis([-0.3 25 0 1.5]);
ylabel('x');

lgh = legend('Error feedback', 'With feedforward', 'Location', 'southeast');
legend(lgh, 'boxoff');

subplot(323)
pl=plot(t, y21, '-', t, y21f, '--', 'Linewidth', AM_data_linewidth);
amaxis([-0.3 25 0 10])
ylabel('u');xlabel('t');

amprint('refstep.eps');

%
%Frequency responses
%
figure(2); clf;

w=logspace(-1,2,200);
f0=sqrt(2)/2;
m11f=squeeze(bode(sys11f,w));
m11=squeeze(bode(sys11,w));
[xxx,iii]=min(abs(m11f-f0));wbf=w(iii);
[xxx,iii]=min(abs(m11-f0));wb=w(iii);

subplot(321); 
loglog(w, ones(size(w)), 'k-', 'Linewidth', AM_ref_linewidth);
hold on;
loglog(w, m11, '-', w, m11f, '--', 'Linewidth', AM_bode_linewidth);
loglog(wbf, f0, 'ro', wb, f0, 'r+', 'MarkerSize', AM_bode_markersize);
amaxis([0.1 10 0.05 5]); 
set(gca, 'XTick', [0.1 1 10], 'XMinorGrid', 'off');
set(gca, 'YTick', [0.1 1 10], 'YMinorGrid', 'off'); 
ylabel('xr');

m21f=squeeze(bode(sys21f,w));
m21=squeeze(bode(sys21,w));

subplot(323)
loglog(w, m21, '-', w, m21f, '--', 'Linewidth', AM_bode_linewidth);
amaxis([0.1 10 0.1 10]); 
set(gca, 'XTick', [0.1 1 10], 'XMinorGrid', 'off');
set(gca, 'YTick', [0.1 1 10], 'YMinorGrid', 'off'); 
ylabel('ur');xlabel('w');

amprint('reffreqresp.eps');

