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

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;
%%%%%%%%%%%%%%%%******************%%%%%%%%%%%%%%%

 Accepted Answer

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

More Answers (0)

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Asked:

on 4 May 2022

Answered:

on 4 May 2022

Community Treasure Hunt

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

Start Hunting!