% Design of PID controllers for oscillatory system
% kja 070329
aminit					% MATLAB initialization

% Parameter definitions normalized for computing
% renormalized in plot
z=0.01;                 %Relative damping 
sm=0.6;                 %Stability margin

%Compute integral gain to give desired stability margin
kii=2*z*(1-sm); 
w1=-3;w2=2;N=1e4;
w=logspace(w1,w2,N);
one=ones(size(w));
s=i*w;
Li=kii./(s.*(s.^2+2*z*s+1));
%Lpid=kipid./s;

%Nyquist plot
figure(1); clf; xsubplot(221);
ampolezero(tf([1], [1]), [-1.1 0.5 -1 0.6]); hold on;

plot(real(Li),imag(Li), 'b', 'LineWidth', AM_data_linewidth);
xlabel('real');ylabel('imag');hold on

plot(-1, 0, 'k+', 'MarkerSize', 6, 'LineWidth', 0.5);
amtext(-1.05, -0.16, '-1');
axis square

amprint('osc-inyq.eps');

%Bode plot
figure(2); clf; subplot(221);
[mh, ph] = ambode(tf([kii], [1 0]) * tf([1], [1 2*z 1]), w);

axes(mh); hold on;
plot([1e-3 100], [1 1], 'k-', 'LineWidth', AM_ref_linewidth);
set(gca,'Ytick',[0.01 1])
amaxis([1e-3 100 0.001 10]);
ylabel('gain');

axes(ph); hold on;
plot(w, -180*one, 'k-', 'LineWidth', AM_ref_linewidth);
set(gca,'Xtick',[0.01 1 100]);
set(gca,'Ytick',[-270 -180 -90]);
amaxis([1e-3 100 -280 -80]);
xlabel('w');
ylabel('phase');

amprint('osc-ibode.eps');
