% Generates Fig 4.18
% Computer Controlled Systems (3rd ed)
% Author: B. Wittenmark
% Last edit: 1997-07-02
% Copyright (c) 1996 by K. J. Åström, B. Wittenmark and
% Department of Automatic Control, Lund Institute of
% Technology, Lund, Sweden

close all; clear all;

newplot;clf;
set(gcf,'PaperUnits','centimeters','PaperPosition',[2 2 13 9.5])
set(gcf,'Units','centimeters','position',get(gcf,'PaperPosition'))

lw=2;

J1=10/9;
J2=10;
k=1;
d=0.1;
kI=1;
om0=sqrt(k*(J1+J2)/(J1*J2));
al=J1/(J1+J2);
be1=d/(J1*om0);
be2=d/(J2*om0);
ga=kI/(J1*om0);
de=1/(J1*om0);

A=om0*[0 1 -1;al-1 -be1 be1;al be2 -be2];
B=[0;ga;0];
C=[0 0 om0];
D=0;

wrad=logspace(-2,2,300);
[mag1,ph1,wrad]=bode(A,B,C,D,1,wrad);

subplot(2,1,1)
loglog(wrad,mag1,'r-','Linew',lw)
axis([0.01 50 0.0001 10])
%title('Fig 4.14');
set(gca,'Fontsize',9,'Fontname','NewCenturySchlbk');
set(gca,'ytick',[0.001 1]);
set(gca,'xtick',[0.1 10]);
ylabel('Gain','Fontname','NewCenturySchlbk','Fontsize',9)
title('Bode plot of the flexible-robot-arm example')
hold on

plot([0.01 50],[1 1],'k:','Linew',lw)
subplot(2,1,2)
semilogx(wrad,ph1,'r-','Linew',lw)
axis([0.01 100 -300 100]);
hold on
plot([0.01 50],[-180 -180],'k:','Linew',lw)
set(gca,'Fontsize',9,'Fontname','NewCenturySchlbk');
set(gca,'xtick',[0.1 10]);
set(gca,'ytick',[-180 0]);
xlabel('Frequency, rad/s','Fontname','NewCenturySchlbk','Fontsize',9)
ylabel('Phase','Fontname','NewCenturySchlbk','Fontsize',9)

drawnow
ylblalgn;
