%Sensitivity functions
%Closed loop system slower than process
% kja060714
aminit;

figure(1); clf;

w=logspace(-3,1);
a=1;p1=0.05;p2=0.2;z=abs(p1*p2/(p1+p2-a));
%a=1;p1=0.2;p2=0.9;z=abs(p1*p2/(p1+p2-a));
s=i*w;
S=s.*(s+a)./(s+p1)./(s+p2);
T=((p1+p2-a)*s+p1*p2)./(s+p1)./(s+p2);
gS=abs(S);
gT=abs(T);
%Plot sensitivity function and asymptotes
s1=min(a*w/p1/p2,a/p2);
s2=max(1,1./w);
aS=min(s1,s2);

subplot(321)
pl=loglog(w,gS,'-', w, aS, '--'); hold on;
set(gca,'Xtick',[0.001 0.01 0.1 1 10]);
set(gca,'Ytick',[0.1 1 10]);
set(pl,'Linewidth', AM_bode_linewidth)
amaxis([1e-3 10 0.1 30])
loglog([0.001 10], [1 1], 'k-', 'LineWidth', AM_ref_linewidth)

% Replace units with pole zero locations
set(gca, 'XTickLabel', [' ']);
loglog([p1, p1], [0.1 8], 'k:'); amtext(p1, 0.058,'p1');
loglog([p2, p2], [0.1 8], 'k:'); amtext(p2, 0.058,'p2');
loglog([ a,  a], [0.1 8], 'k:'); amtext(a, 0.058,'a');
xlabel('w');
ylabel('S');

lgh = legend('Exact', 'Approx', 'Orientation', 'horizontal');
legend(lgh, 'boxoff');

amprint('sensitivities-1.eps');

figure(2); clf;

%-------------------------------------------------------------------------
%Closed loop system faster than process
w=logspace(-1,3);
a=1;p1=5;p2=10;z=p1*p2/abs(p1+p2-a);
s=i*w;
S=s.*(s+a)./(s+p1)./(s+p2);
gS=abs(S);
s1=max(a*w/p1/p2,w.^2/p1/p2);
s2=min(1,w/p2);
aS=min(s1,s2);
T=((p1+p2-a)*s+p1*p2)./(s+p1)./(s+p2);
gS=abs(S);
gT=abs(T);

%Plot the sensitivity function and asymptotoes
subplot(322)
pl=loglog(w,gS,'-',w,aS,'--'); hold on;
set(gca,'Xtick',[0.1 1 10 100]);
set(gca,'Ytick',[0.01 1]);
set(pl,'Linewidth', AM_bode_linewidth)
amaxis([1e-1 100 0.001 5])
loglog([0.01 100], [1 1], 'k-', 'LineWidth', AM_ref_linewidth)

% Replace units with pole zero locations
set(gca, 'XTickLabel', [' ']);
loglog([p1, p1], [0.0001 8], 'k:'); amtext(p1, 0.00042,'p1');
loglog([p2, p2], [0.0001 8], 'k:'); amtext(p2, 0.00042,'p2');
loglog([ a,  a], [0.0001 8], 'k:'); amtext(a, 0.00042,'a');

lgh = legend('Exact', 'Approx', 'Orientation', 'horizontal', ...
  'Location', 'southeast');
legend(lgh, 'boxoff');

xlabel('w');
ylabel('S');

amprint('sensitivities-2.eps');
