How do i shorten this code?

2 visualizaciones (últimos 30 días)
Deepak Aswani
Deepak Aswani el 14 de Nov. de 2018
Comentada: Deepak Aswani el 14 de Nov. de 2018
Hi, i know this is a noob-grade question, but i've tried making a code for a low pass butterworth filter magnitude response. Only problem is that i can't seem to compress the code so that i can have multiple lines of factor N into the graph without repetitions.
omc=1;
om=[0:0.1:4];
x=om/omc;
N=1;
y=x.^(2*N);
z=(1+y).^(0.5);
H=1./z;
figure;plot (om,H,'b');
xlabel('frequency in radians/sec');ylabel('Magnitude');
title('Low Pass Butterworth magnitude response');
N=2;
y=x.^(2*N);
z=(1+y).^(0.5);
H=1./z;
hold on;plot(om,H,'r');hold off;
N=3;
y=x.^(2*N);
z=(1+y).^(0.5);
H=1./z;
hold on;plot(om,H,'g');hold off;
N=4;
y=x.^(2*N);
z=(1+y).^(0.5);
H=1./z;
hold on;plot(om,H,'k');hold off;
legend('N=1','N=2','N=3','N=4');

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 14 de Nov. de 2018
All you need to do is get om and N to be the same size. Meshgrid is great for this.
omc=1;
om=[0:0.1:4];
[N,om]=meshgrid(1:4,om);
x=om/omc;
y=x.^(2*N);
z=(1+y).^(0.5);
H=1./z;
plot(om,H)
legend('N=1','N=2','N=3','N=4');
xlabel('frequency in radians/sec');ylabel('Magnitude');
title('Low Pass Butterworth magnitude response');
  2 comentarios
Cris LaPierre
Cris LaPierre el 14 de Nov. de 2018
If colors are important, change your plot command to the following:
h = plot(om,H);
set(h, {'color'}, {'b'; 'r';'g';'k'});
Deepak Aswani
Deepak Aswani el 14 de Nov. de 2018
Thank you for the help !

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by