% Introduçao
% Exemplo resposta em regime de um SLIT para uma entrada senoidal
clear all
close all
clc
w = 3;
A = 2;
t = 0:0.01:10;   
x = A*sin(w*t);%x(t)=2*sin(3*t); 

s=j*w;
H = 10/(s+1);
modulo=abs(H)
fase=angle(H)

s = tf('s');
Hs=10/(s+1);
% simulaçao da resposta de um sistema linear com funçao de transferencia Hs
% a entrada x durante o tempo t
lsim(Hs,x,t);

y = A*modulo*sin(w.*t+fase);
hold on
plot(t,y,'r-.')
axis([5 10 -8 8])%reajuste dos eixos para analisar o regime permanente
grid on

%%
% Exemplo 1.2
%Polo real negativo
clear all
clc
close all

s = tf('s');
wc = 1;

H = wc/(s+wc)
w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wc
        A(n) = 0;
    else
        A(n) = A(n-1)-20;
    end
    if w(n)<=wc/10
        P(n) = 0;
    elseif w(n)>=10*wc
        P(n) = -90;
    else
        P(n) = -45;
    end
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode 1/(s+1)')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode 1/(s+1)')



%%
% Exemplo 1.3
%Ganho constante positivo
clear all
clc
close all

K=10;

H = tf(K,1);

w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    A(n) = 20*log10(K);
    P(n) = 0;
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode k=10')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode k=10')


%%
% Exemplo 1.4
%Ganho constante negativo
clear all
clc
close all

K=-10;

H = tf(K,1);

w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    A(n) = 20*log10(K);
    P(n) = 180;
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode k=-10')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode k=-10')

%%
% Exemplo 1.5
%Zero na origem
clear all
clc
close all

K=10;
s = tf('s');
H = K*s;

w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    A(n) = 20*log10(K)+20*log10(w(n));
    P(n) = 90;
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northwest')
title('Diagrama de Bode H(s)=10s')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northwest')
xlabel('Freq (rad/s)')
title('Diagrama de Bode H(s)=10s')

%%
% Exemplo 1.6
%Polo na origem
clear all
clc
close all

K=10;
s = tf('s');
H = K/s;

w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    A(n) = 20*log10(K)-20*log10(w(n));
    P(n) = -90;
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode H(s)=10/s')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode H(s)=10/s')

%%
% Exemplo 1.7
%Zero de ordem m na origem
clear all
clc
close all

K=1;
m = 3;
s = tf('s');
H = K*s^m;

w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    A(n) = 20*log10(K)+20*m*log10(w(n));
    P(n) = 90*m;
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northwest')
title('Diagrama de Bode H(s)=s^3')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northwest')
xlabel('Freq (rad/s)')
title('Diagrama de Bode H(s)=s^3')

%%
% Exemplo 1.8
%Polo de ordem m na origem
clear all
clc
close all

K=1;
m = 2;
s = tf('s');
H = K/s^m;

w = logspace(-2,2,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    A(n) = 20*log10(K)-20*m*log10(w(n));
    P(n) = -90*m;
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode H(s)=1/s^2')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode H(s)=1/s^2')

%%
% Exemplo 1.9
%Zero real negativo
clear all
clc
close all

s = tf('s');
H = (s+100)/100
w = logspace(-2,4,7);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end

wc = 100;


figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wc
        A(n) = 0;
    else
        A(n) = A(n-1)+20;
    end
    if w(n)<=wc/10
        P(n) = 0;
    elseif w(n)>=10*wc
        P(n) = 90;
    else
        P(n) = 45;
    end
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northwest')
title('Diagrama de Bode H(s)=(s+100)/100')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northwest')
xlabel('Freq (rad/s)')
title('Diagrama de Bode H(s)=(s+100)/100')


%%
% Exemplo 1.10
%Zero real positivo
clear all
clc
% close all

s = tf('s');
H = (s-100)/100
w = logspace(-2,4,7);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end

wc = 100;


figure(2)
hold on
for n=1:1:length(w)
    if w(n)<=wc
        A(n) = 0;
    else
        A(n) = A(n-1)+20;
    end
    if w(n)<=wc/10
        P(n) = 180;
    elseif w(n)>=10*wc
        P(n) = 90;
    else
        P(n) = 135;
    end
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northwest')
title('Diagrama de Bode H(s)=(s-100)/100')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode H(s)=(s-100)/100')

%%
% Exemplo 1.12
% Circuito RC em cascata
clear all
clc
close all

tau1=1;
wc1=1/tau1;

tau2=0.01;
wc2=1/tau2;

s = tf('s');
H1 = wc1/(s+wc1);
H2 = (wc2)/(s+wc2);
H = H1*H2;
w = logspace(-2,4,7);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end

% H(s)=(100)/(s^2+101s+100)

beta0=100        %coef do termo de menor grau do numerador
alpha0=100       %coef do termo de menor grau do denominador
m = 2             %grau do denominador
ele = 0          %grau do numerador
beta_ele = 100    %coef do termo de maior grau do numerador

wc = (alpha0*beta_ele/beta0)^(1/(m-ele))
Assintota_baixa_freq = 20*log10(beta0/alpha0)
Assintota_alta_freq = 20*log10(beta_ele)-20*(m-ele)*log10(max(w))


figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wc1
        A1(n) = 0;
    else
        A1(n) = A1(n-1)-20;
    end
    if w(n)<=wc1/10
        P1(n) = 0;
    elseif w(n)>=10*wc1
        P1(n) = -90;
    else
        P1(n) = -45;
    end
    if w(n)<=wc2
        A2(n) = 0;
    else
        A2(n) = A2(n-1)-20;
    end
    if w(n)<=wc2/10
        P2(n) = 0;
    elseif w(n)>=10*wc2
        P2(n) = -90;
    else
        P2(n) = -45;
    end
    A(n)=A1(n)+A2(n);
    P(n)=P1(n)+P2(n);
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode 100/((s+1)(s+100))')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode 100/((s+1)(s+100))')


figure(2)
subplot(2,1,1)
semilogx(w,A1,'r--')
hold on
semilogx(w,A2,'g--')
semilogx(w,A,'b','LineWidth',2)
legend('|1/(s+1)|','|100/(s+100)|','|H(s)|','Location','southwest')
ylabel('|H(j\omega)|_{dB}')
grid on
title('Soma das Assíntotas do Diagrama de Bode')

subplot(2,1,2)
semilogx(w,P1,'r--')
hold on
semilogx(w,P2,'m--')
semilogx(w,P,'b','LineWidth',2)
legend('/1/(s+1)','/100/(s+100)','/H(s)','Location','southwest')
ylabel('/H(j\omega)')
grid on
xlabel('Freq (rad/s)')
title('Soma das Assíntotas do Diagrama de Bode')

%%
% Exemplo 1.13
%Circuito passa-alta
clear all
clc
close all

tau=0.1;
wc=1/tau;


s = tf('s');
H = (tau*s)*(wc/(s+wc));
w = logspace(-1,3,5);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end



figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wc
        A1(n) = 0;
    else
        A1(n) = A1(n-1)-20;
    end
    if w(n)<=wc/10
        P1(n) = 0;
    elseif w(n)>=10*wc
        P1(n) = -90;
    else
        P1(n) = -45;
    end
    A2(n) = 20*log10(tau)+20*log10(w(n));
    P2(n) = 90;
    A(n)=A1(n)+A2(n);
    P(n)=P1(n)+P2(n);
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','northeast')
title('Diagrama de Bode s/((s+10)')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northeast')
xlabel('Freq (rad/s)')
title('Diagrama de Bode s/((s+10)')


figure(2)
subplot(2,1,1)
semilogx(w,A1,'r--')
hold on
semilogx(w,A2,'g--')
semilogx(w,A,'b','LineWidth',2)
legend('|tau*s|','|wc/(s+wc)|','|H(s)|','Location','northwest')
ylabel('|H(j\omega)|_{dB}')
grid on
title('Soma das Assíntotas do Diagrama de Bode')

subplot(2,1,2)
semilogx(w,P1,'r--')
hold on
semilogx(w,P2,'m--')
semilogx(w,P,'b','LineWidth',2)
legend('/tau*s','/wc/(s+wc)','/H(s)','Location','southwest')
ylabel('/H(j\omega)')
grid on
xlabel('Freq (rad/s)')
title('Soma das Assíntotas do Diagrama de Bode')


%%
% Exemplo 1.14
% Polos e zeros reais
clear all
clc
close all

wc1=1;
wc2=100;
wc3=1000;

s = tf('s');
H1 = wc1/(s+wc1);
H2 = (s+wc2)/wc2;
H3 = wc3/(s+wc3);
H = H1*H2*H3;
w = logspace(-2,5,8);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end

% H(s)=(10s+1000)/(s^2+1001s+1000)

beta0=1000        %coef do termo de menor grau do numerador
alpha0=1000       %coef do termo de menor grau do denominador
m = 2             %grau do denominador
ele = 1          %grau do numerador
beta_ele = 10    %coef do termo de maior grau do numerador

Assintota_baixa_freq = 20*log10(beta0/alpha0)
Assintota_alta_freq = 20*log10(beta_ele)-20*(m-ele)*log10(max(w))


figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wc1
        A1(n) = 0;
    else
        A1(n) = A1(n-1)-20;
    end
    if w(n)<=wc1/10
        P1(n) = 0;
    elseif w(n)>=10*wc1
        P1(n) = -90;
    else
        P1(n) = -45;
    end
    if w(n)<=wc2
        A2(n) = 0;
    else
        A2(n) = A2(n-1)+20;
    end
    if w(n)<=wc2/10
        P2(n) = 0;
    elseif w(n)>=10*wc2
        P2(n) = 90;
    else
        P2(n) = 45;
    end
    if w(n)<=wc3
        A3(n) = 0;
    else
        A3(n) = A3(n-1)-20;
    end
    if w(n)<=wc3/10
        P3(n) = 0;
    elseif w(n)>=10*wc3
        P3(n) = -90;
    else
        P3(n) = -45;
    end
    A(n)=A1(n)+A2(n)+A3(n);
    P(n)=P1(n)+P2(n)+P3(n);
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','southwest')
title('Diagrama de Bode 10(s+100)/((s+1)(s+1000))')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northwest')
xlabel('Freq (rad/s)')
title('Diagrama de Bode 10(s+100)/((s+1)(s+1000))')


figure(2)
subplot(2,1,1)
semilogx(w,A1,'r--')
hold on
semilogx(w,A2,'g--')
semilogx(w,A3,'m--')
semilogx(w,A,'b','LineWidth',2)
legend('|1/(s+1)|','|(s+100)/100|','|1000/(s+1000)|','|H(s)|','Location','northwest')
ylabel('|H(j\omega)|_{dB}')
grid on
title('Soma das Assíntotas do Diagrama de Bode')

subplot(2,1,2)
semilogx(w,P1,'r--')
hold on
semilogx(w,P2,'g--')
semilogx(w,P3,'m--')
semilogx(w,P,'b','LineWidth',2)
legend('/1/(s+1)','/(s+100)/100','/1000/(s+1000)','/H(s)','Location','northwest')
ylabel('/H(j\omega)')
grid on
xlabel('Freq (rad/s)')
title('Soma das Assíntotas do Diagrama de Bode')

%%
% Exemplo 1.15
% Polos complexos: Sistemas de segunda ordem subamortecidos
clear all
clc
close all

wn = 1;
xi1 = 0.1;
xi2 = 0.9;

s = tf('s');
H1 = wn^2/(s^2+2*xi1*wn*s+wn^2);
H2 = wn^2/(s^2+2*xi2*wn*s+wn^2);
w = logspace(-2,2,5);

[MAG1,PHASE1,W1] = bode(H1,{min(w),max(w)});
for k=1:length(W1)
    MAGdB1(k) = 20*log10(MAG1(1,1,k));
    fase1(k) = PHASE1(1,1,k);
end
[MAG2,PHASE2,W2] = bode(H2,{min(w),max(w)});
for k=1:length(W2)
    MAGdB2(k) = 20*log10(MAG2(1,1,k));
    fase2(k) = PHASE2(1,1,k);
end

wf = [0.0100    0.1000    1.0000   9.99 10.01  100.0000]
figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wn
        A1(n) = 0;
    else
        A1(n) = A1(n-1)-40;
    end
    if w(n)<wn
        P1(n) = 0;
    elseif w(n)>wn
        P1(n) = -180;
    else
        P1(n) = -90;
    end
%     if w(n)<=wn
%         P1(n) = 0;
%     elseif w(n)>wn
%         P1(n) = -180;
%     end
    A(n)=A1(n);
    P(n)=P1(n);
end
subplot(2,1,1)
semilogx(W1,MAGdB1,'b','LineWidth',2)
hold on
semilogx(W2,MAGdB2,'r--','LineWidth',2)
semilogx(w,A,'g:','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo \xi=0.1','Modulo \xi=0.9','Assíntotas','Location','southwest')
title('Diagrama de Bode wn^2/(s^2+2\xi*wn*s+wn^2)')
subplot(2,1,2)
semilogx(W1,fase1,'b','LineWidth',2)
hold on
semilogx(W2,fase2,'r--','LineWidth',2)
semilogx(w,P,'g:','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase \xi=0.1','Fase \xi=0.9','Assíntotas','Location','northwest')
xlabel('Freq (rad/s)')
title('Diagrama de Bode wn^2/(s^2+2\xi*wn*s+wn^2)')


%%

% Exercício
% Polos e zeros reais
clear all
clc
close all

wc1=1;
wc2=100;
wc3=10;

s = tf('s');
H1 = wc1/(s+wc1);
H2 = (s+wc2)/wc2;
H3 = wc3/(s+wc3);
H = H1*H2*H3
zpk(H)
w = logspace(-2,6,9);

[MAG,PHASE,W] = bode(H,{min(w),max(w)});
for k=1:length(W)
    MAGdB(k) = 20*log10(MAG(1,1,k));
    fase(k) = PHASE(1,1,k);
end

% H(s)=(0.1*s+10)/(s^2+11s+10)

beta0=10        %coef do termo de menor grau do numerador
alpha0=10       %coef do termo de menor grau do denominador
m = 2             %grau do denominador
ele = 1          %grau do numerador
beta_ele = 0.1    %coef do termo de maior grau do numerador

Assintota_baixa_freq = 20*log10(beta0/alpha0)
Assintota_alta_freq = 20*log10(beta_ele)-20*(m-ele)*log10(max(w))


figure(1)
hold on
for n=1:1:length(w)
    if w(n)<=wc1
        A1(n) = 0;
    else
        A1(n) = A1(n-1)-20;
    end
    if w(n)<=wc1/10
        P1(n) = 0;
    elseif w(n)>=10*wc1
        P1(n) = -90;
    else
        P1(n) = -45;
    end
    if w(n)<=wc2
        A2(n) = 0;
    else
        A2(n) = A2(n-1)+20;
    end
    if w(n)<=wc2/10
        P2(n) = 0;
    elseif w(n)>=10*wc2
        P2(n) = 90;
    else
        P2(n) = 45;
    end
    if w(n)<=wc3
        A3(n) = 0;
    else
        A3(n) = A3(n-1)-20;
    end
    if w(n)<=wc3/10
        P3(n) = 0;
    elseif w(n)>=10*wc3
        P3(n) = -90;
    else
        P3(n) = -45;
    end
    A(n)=A1(n)+A2(n)+A3(n);
    P(n)=P1(n)+P2(n)+P3(n);
end
subplot(2,1,1)
semilogx(W,MAGdB,'b','LineWidth',2)
hold on
semilogx(w,A,'r--','LineWidth',2)
ylabel('|H(j\omega)|_{dB}')
grid on
legend('Modulo','Assíntotas','Location','southwest')
title('Diagrama de Bode (s+100)/((s+1)(s+10))')
subplot(2,1,2)
semilogx(W,fase,'b','LineWidth',2)
hold on
semilogx(w,P,'r--','LineWidth',2)
ylabel('/H(j\omega)')
grid on
legend('Fase','Assíntotas','Location','northwest')
xlabel('Freq (rad/s)')
title('Diagrama de Bode (s+100)/((s+1)(s+10))')


figure(2)
subplot(2,1,1)
semilogx(w,A1,'r--')
hold on
semilogx(w,A2,'g--')
semilogx(w,A3,'m--')
semilogx(w,A,'b','LineWidth',2)
legend('|1/(s+1)|','|(s+100)/100|','|10/(s+10)|','|H(s)|','Location','southwest')
ylabel('|H(j\omega)|_{dB}')
grid on
title('Soma das Assíntotas do Diagrama de Bode')

subplot(2,1,2)
semilogx(w,P1,'r--')
hold on
semilogx(w,P2,'g--')
semilogx(w,P3,'m--')
semilogx(w,P,'b','LineWidth',2)
legend('/1/(s+1)','/(s+100)/100','/10/(s+10)','/H(s)','Location','northeast')
ylabel('/H(j\omega)')
grid on
xlabel('Freq (rad/s)')
title('Soma das Assíntotas do Diagrama de Bode')

