%   Script BODEINTERPR.M zur Erzeugung von Bildern
%
%   Interpretation des Bodediagramms
%
% J. Lunze
% 14.11.2014
% für 11. Auflage: 18.11.2014
% für 12. Auflage: 27.6.2019
%
%   
clear
close all

%%  Bodediagramm des PT1-Gliedes
%
%  G(s) = 3/(s+1)
figure(1)
z=[3];
n=[1 1];
G1=tf(z, n);
W=logspace(-2, 2, 100);
[Betrag1,Phase1]=bode(G1, W);
subplot(2,1,1);
semilogx(W, 20*log10(Betrag1(:)), 'b');
axis([0.05, 50, -20, 20]);
grid on;
xleer;
latexylabel('$$|G_1|_{\rm dB}$$');
rotateY;
yticks([-20 0 20]);
%
subplot(2,1,2);
semilogx(W, Phase1(:), 'b');
axis([0.05, 50, -100, 20]);
grid on;
latexylabel('$$\phi_1$$');
rotateY;
xticks([0.1 1 10]);
yticks([-90  -45  0]);
latexxlabel('$$\omega$$ in rad/s');
%
%  Eintragen der betrachteten Frequenzen
subplot(2,1,1)
hold on
w1=0.1;
w2=1;
w3=10;
[B11, P11]=bode(G1, w1);
[B12, P12]=bode(G1, w2);
[B13, P13]=bode(G1, w3);
plot(w1, 20*log10(B11), 'bo');
plot(w2, 20*log10(B12), 'bo');
plot(w3, 20*log10(B13), 'bo');
hold off
%
subplot(2,1,2)
hold on
plot(w1, P11, 'bo');
plot(w2, P12, 'bo');
plot(w3, P13, 'bo');
hold off
%

%%  Stationäres Verhalten des PT1-Gliedes
%
figure(2)
dt=0.03;
T1end=240;
T2end=24;
T3end=2.4;
Time1=[0:dt:T1end];
Time2=[0:dt:T2end];
Time3=[0:dt:T3end];
ubar=2;
U1=ubar*sin(w1*Time1);
U2=ubar*sin(w2*Time2);
U3=ubar*sin(w3*Time3);
Ys11=ubar*abs(B11)*sin(w1*Time1+P11*2*pi/360);
Ys12=ubar*abs(B12)*sin(w2*Time2+P12*2*pi/360);
Ys13=ubar*abs(B13)*sin(w3*Time3+P13*2*pi/360);
%
subplot(3,1,1)
plot(Time1, U1, 'b--', Time1, Ys11, 'b');
hold on
plot([0 T1end],[0 0],'k:');
axis([0 T1end -7 7]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-7 7]);
xticks([0 50 100 150 200]);
hold off
%
subplot(3,1,2)
plot(Time2, U2, 'b--', Time2, Ys12, 'b');
hold on
plot([0 T2end],[0 0],'k:');
axis([0 T2end -5 5]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-4 4]);
xticks([0 5 10 15 20]);
hold off
%
subplot(3,1,3)
plot(Time3, U3, 'b--', Time3, Ys13, 'b');
hold on
plot([0 T3end],[0 0],'k:');
axis([0 T3end -2.4 2.4]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-2 2]);
xticks([0 0.5 1 1.5 2]);
plot([2*pi/w3, 2*pi/w3],[.4 -.4],'b',...
     [(2*pi-P13*2*pi/360)/w3, (2*pi-P13*2*pi/360)/w3],[.4 -.4],'b');
%plot(2*pi/w3, 0, 'bo', (2*pi-P13*2*pi/360)/w3, 0, 'bo');
latextext((2*pi)/w3+0.01, -1.4, '$$\phi_1$$');
hold off


%%  Erzwungene Bewegung im Vergleich zum stationären Verhalten des PT1-Gliedes
%
figure(3)
subplot(4,1,[1,2])
Y1=lsim(G1, U3, Time3);
plot(Time3, Ys13, 'b--', Time3, Y1, 'b');
hold on
plot([0 T3end],[0 0],'k:');
axis([0 T3end -1.2 1.2]);
latexylabel('$$y, y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-1 1]);
xticks([0 0.5 1 1.5 2]);
hold off



%%  Bodediagramm des I-Gliedes
%
%  G(s) = 1/s
figure(4)
z=[1];
n=[1 0];
G2=tf(z, n);
W=logspace(-2, 2, 1000);
[Betrag2,Phase2]=bode(G2, W);
subplot(2,1,1);
semilogx(W, 20*log10(Betrag2(:)), 'b');
axis([0.05, 50, -20, 20]);
grid on;
xleer;
latexylabel('$$|G_2|_{\rm dB}$$');
rotateY;
yticks([-20 0 20]);
subplot(2,1,2);
semilogx(W, Phase2(:), 'b');
axis([0.05, 50, -100, 20]);
grid on;
latexylabel('$$\phi_2$$');
rotateY;
yticks([-90  -45  0]);
xticks([0.1 1 10]);
latexxlabel('$$\omega$$ in rad/s');
%
%  Eintragen der betrachteten Frequenzen
subplot(2,1,1)
hold on
[B21, P21]=bode(G2, w1);
[B22, P22]=bode(G2, w2);
[B23, P23]=bode(G2, w3);
plot(w1, 20*log10(B21), 'bo');
plot(w2, 20*log10(B22), 'bo');
plot(w3, 20*log10(B23), 'bo');
hold off
%
subplot(2,1,2)
hold on
plot(w1, P21, 'bo');
plot(w2, P22, 'bo');
plot(w3, P23, 'bo');
hold off
%

%%  Stationäres Verhalten des I-Gliedes
%
figure(5)
Ys21=ubar*abs(B21)*sin(w1*Time1+P21*2*pi/360);
Ys22=ubar*abs(B22)*sin(w2*Time2+P22*2*pi/360);
Ys23=ubar*abs(B23)*sin(w3*Time3+P23*2*pi/360);
%
subplot(3,1,1)
plot(Time1, U1, 'b--', Time1, Ys21, 'b');
hold on
plot([0 T1end],[0 0],'k:');
axis([0 T1end -22 22]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
xticks([0 50 100 150 200]);
yticks([-20 20]);
hold off
%
subplot(3,1,2)
plot(Time2, U2, 'b--', Time2, Ys22, 'b');
hold on
plot([0 T2end],[0 0],'k:');
axis([0 T2end -2.4 2.4]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
xticks([0 5 10 15 20]);
yticks([-2 2]);
hold off
%
subplot(3,1,3)
plot(Time3, U3, 'b--', Time3, Ys23, 'b');
hold on
plot([0 T3end],[0 0],'k:');
axis([0 T3end -2.4 2.4]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
xticks([0 0.5 1 1.5 2]);
yticks([-2 2]);
hold off


%%  Erzwungene Bewegung im Vergleich zum stationären Verhalten des I-Gliedes
%
figure(6)
subplot(4,1,[1,2])
Y2=lsim(G2, U3, Time3);
plot(Time3, Ys23, 'b--', Time3, Y2, 'b');
hold on
plot([0 T3end],[0 0],'k:');
axis([0 T3end -0.3 0.5]);
latexylabel('$$y, y_{\rm s}$$');
rotateY;
yticks([-0.2  0.2 0.4]);
latexxlabel('$$t$$');
xticks([0 0.5 1 1.5 2]);
yticks([-0.2 0.2]);
hold off



%%  Bodediagramm des DT1-Gliedes
%
%  G(s) = 3s/(s+1)
figure(7)
z=[3 0];
n=[1 1];
G3=tf(z, n);
W=logspace(-2, 2, 1000);
[Betrag3,Phase3]=bode(G3, W);
subplot(2,1,1);
semilogx(W, 20*log10(Betrag3(:)), 'b');
axis([0.05, 50, -20, 20]);
grid on;
xleer;
latexylabel('$$|G_3|_{\rm dB}$$');
rotateY;
yticks([-20 0 20]);
subplot(2,1,2);
semilogx(W, Phase3(:), 'b');
axis([0.05, 50, -20 100]);
grid on;
latexylabel('$$\phi_3$$');
rotateY;
yticks([0 45 90]);
xticks([0.1 1 10]);
latexxlabel('$$\omega$$ in rad/s');
%
%  Eintragen der betrachteten Frequenzen
subplot(2,1,1)
hold on
[B31, P31]=bode(G3, w1);
[B32, P32]=bode(G3, w2);
[B33, P33]=bode(G3, w3);
plot(w1, 20*log10(B31), 'bo');
plot(w2, 20*log10(B32), 'bo');
plot(w3, 20*log10(B33), 'bo');
hold off
%
subplot(2,1,2)
hold on
plot(w1, P31, 'bo');
plot(w2, P32, 'bo');
plot(w3, P33, 'bo');
hold off
%

%%  Stationäres Verhalten des DT1-Gliedes
%
figure(8)
Ys31=ubar*abs(B31)*sin(w1*Time1+P31*2*pi/360);
Ys32=ubar*abs(B32)*sin(w2*Time2+P32*2*pi/360);
Ys33=ubar*abs(B33)*sin(w3*Time3+P33*2*pi/360);
%
subplot(3,1,1)
plot(Time1, U1, 'b--', Time1, Ys31, 'b');
hold on
plot([0 T1end],[0 0],'k:');
axis([0 T1end -3.4 3.4]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-2 2]);
xticks([0 50 100 150 200]);
hold off
%
subplot(3,1,2)
plot(Time2, U2, 'b--', Time2, Ys32, 'b');
hold on
plot([0 T2end],[0 0],'k:');
axis([0 T2end -5 5]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-4 4]);
xticks([0 5 10 15 20]);
hold off
%
subplot(3,1,3)
plot(Time3, U3, 'b--', Time3, Ys33, 'b');
hold on
plot([0 T3end],[0 0],'k:');
axis([0 T3end -8 8]);
latexylabel('$$y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-6 6]);
xticks([0 0.5 1 1.5 2]);
hold off


%%  Erzwungene Bewegung im Vergleich zum stationären Verhalten des DT1-Gliedes
%
figure(9)
subplot(4,1,[1,2])
Y3=lsim(G3, U3, Time3);
plot(Time3, Ys33, 'b--', Time3, Y3, 'b');
hold on
plot([0 T3end],[0 0],'k:');
axis([0 T3end -7 7]);
latexylabel('$$y, y_{\rm s}$$');
rotateY;
latexxlabel('$$t$$');
yticks([-5 5]);
xticks([0 0.5 1 1.5 2]);
hold off


%%  Eps-Bilder
%
Epsbildc15('BodeInterpr');
