% Exemplo 16.15 - Polos complexos: sistemas de segunda ordem subamortecidos
clear all
close all
clc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plotando o gráfico do módulo
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
w = logspace(-1, 1);
xi = 0.1;
for i = 1:length(w)
    MdB(i) = mag2db(1/sqrt((1-w(i)^2)^2+(2*xi*w(i))^2)); 
end
figure
semilogx(w,MdB,'r','LineWidth',2);
axis([10^(-1) 10^1 -40 20])
grid on
hold on
xi = 0.9;
for i = 1:length(w)
    MdB(i) = mag2db(1/sqrt((1-w(i)^2)^2+(2*xi*w(i))^2)); 
end
semilogx(w,MdB,'k','LineWidth',2);


% Plotando assíntotas 
% w->0:
w = [10^(-1) 10^0]; 
M = [0 0];
plot(w,M,'b','LineWidth',2);

w = [10^(0) 10^1]; 
M = [-40*log10(w(1)) -40*log10(w(2))];
plot(w,M,'b','LineWidth',2);


title('Diagrama assintótico de módulo')
xlabel('\omega')
ylabel('M_{dB}(\omega)')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plotando o gráfico da fase
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xi = 0.1;
w = logspace(-1, 1);
for i = 1:length(w)
    if w(i)<=1
        Phi(i) = -atan(2*xi*w(i)/(1-w(i)^2))*180/pi;
    elseif w(i)>1
        Phi(i) = (-pi-atan(-2*xi*w(i)/(w(i)^2-1)))*180/pi;
    end
end
figure
semilogx(w,Phi,'r','LineWidth',2);
axis([10^(-1) 10^1 -200 20])
grid on
hold on
xi = 0.9;
w = logspace(-1, 1);
for i = 1:length(w)
    if w(i)<=1
        Phi(i) = -atan(2*xi*w(i)/(1-w(i)^2))*180/pi;
    elseif w(i)>1
        Phi(i) = (-pi-atan(-2*xi*w(i)/(w(i)^2-1)))*180/pi;
    end
end
semilogx(w,Phi,'k','LineWidth',2);


% Plotando assíntotas de fase
% (wc=1) (raízes reais - reta ligando 0.1wc a 10wc):
w = [10^(-1) 10^(-1)]; 
PA = [0 0];         
plot(w,PA,'b:','LineWidth',2);

w = [10^(-1) 10^(1)]; 
PA = [0 -180];         
plot(w,PA,'b:','LineWidth',2);

w = [10^(1) 10^(1)]; 
PA = [-180 -180];         
plot(w,PA,'b:','LineWidth',2);

% (wc=1) (raízes complexas - mudança abrupta em w=wc):
w = [10^(-1) 10^(0)]; 
PA = [0 0];         
plot(w,PA,'b','LineWidth',2);

w = [10^(0) 10^(0)]; 
PA = [0 -180];         
plot(w,PA,'b','LineWidth',2);

w = [10^(0) 10^(1)]; 
PA = [-180 -180];         
plot(w,PA,'b','LineWidth',2);



title('Diagrama assintótico de fase')
xlabel('\omega')
ylabel('\Phi(\omega)')
