Not sure why my code isn't working. Please help

2 vues (au cours des 30 derniers jours)
Karina Vazquez
Karina Vazquez le 4 Mai 2022
Réponse apportée : Voss le 4 Mai 2022
I'm entering the following code and I cant seem to figure out why it won't run. Please help
%%%%%%%%%%%%%%%%******************%%%%%%%%%%%%%%
function [M,P]=fil_ters(R,C,f)% Function definition
w=2*pi*f;% Hz to rad/sec
G=1./(1j*w*R*C+1);% Transfer function
M=abs(G);% Magnitude
P=angle(G);% Phase
end
R=1000;% Resistance value
fc=10e3;% Cutoff frequency
C=1/(2*pi*R*fc);% Capacitance value
f=1:100000;% Frequency axis
[M,P]=fil_ters(R,C,f);%Function call
subplot(2,1,1);
semilogx(f,M);% Plot magnitude
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude plot');
grid;
subplot(2,1,2);
semilogx(f,rad2deg(P));% Plot phase
xlabel('Frequency');
ylabel('Phase(degree)');
title('Phase plot');
grid;
%%%%%%%%%%%%%%%%******************%%%%%%%%%%%%%%%

Réponse acceptée

Voss
Voss le 4 Mai 2022
Assuming that's a script, then depending on your version of MATLAB, you might have to move the function definition to the end.
Then it seems to run ok:
R=1000;% Resistance value
fc=10e3;% Cutoff frequency
C=1/(2*pi*R*fc);% Capacitance value
f=1:100000;% Frequency axis
[M,P]=fil_ters(R,C,f);%Function call
subplot(2,1,1);
semilogx(f,M);% Plot magnitude
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude plot');
grid;
subplot(2,1,2);
semilogx(f,rad2deg(P));% Plot phase
xlabel('Frequency');
ylabel('Phase(degree)');
title('Phase plot');
grid;
function [M,P]=fil_ters(R,C,f)% Function definition
w=2*pi*f;% Hz to rad/sec
G=1./(1j*w*R*C+1);% Transfer function
M=abs(G);% Magnitude
P=angle(G);% Phase
end

Plus de réponses (0)

Catégories

En savoir plus sur Audio Processing Algorithm Design dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by