Unrecognized function or variable

105 visualizaciones (últimos 30 días)
Joshua Tshuma
Joshua Tshuma el 13 de Abr. de 2021
Respondida: Tshiabu Angelus Dan el 16 de Mayo de 2023
Hi everyone,
I'm new to MATLAB and I'm trying square a variable, but it doesn't seem to be working (although it does on Python). When I run the code, it gives me an error saying "Unrecognized function or variable 'variableName'", and I can't figure out why.
I'll include a screenshot of the command window as well as the code I've written.
Thanks in advance!
clear all
%declaring variables
rho = 1.22;
c0 = 344;
fillRat = 0.098;
dampRat = 0.03;
Fr = 417;
K = rho*(c0)^2;
Kr = rho*(c0)^2;
H = 0.05;
freqRat = 1.5;
%wall properties
m1 = 1 ; % x - kg
m2 = 1; % x - kg
fDW = (1/(2*pi)) * sqrt((K/H)*((1/m1) + (1/m2)));
fDWSquared = fDW * ((1/(2*pi)) * sqrt((K/H)*((1/m1) + (1/m2))));
%below is the problem line
sqrd = (freqRat)^2;
  3 comentarios
Joshua Tshuma
Joshua Tshuma el 13 de Abr. de 2021
Hi,
I've removed the clear all. When you say run, do you mean just that play button at the top? I hadn't done that, I was just putting the variable I wanted to calculate the value of in the command window. Do I need to press run each time I make a new variable?
Thanks
Walter Roberson
Walter Roberson el 13 de Abr. de 2021
When you create a .m file, then MATLAB does not execute the code until you say to execute using the green button (or you save the file and invoke it by name in the command line.)
(The workflow is a bit different if you are using LiveScript)

Iniciar sesión para comentar.

Respuestas (3)

Gargi Patil
Gargi Patil el 16 de Abr. de 2021
Hi,
The given error could not be reproduced. The provided code didn't throw any errors when run on MATLAB Online and successfully assigned the value as follows:
sqrd= 2.2500 % as a double
For further information about this error message and suggestions to resolve it, you can check this thread.
  1 comentario
Walter Roberson
Walter Roberson el 16 de Abr. de 2021
User had not executed the code; see above comments.

Iniciar sesión para comentar.


Tshiabu Angelus Dan
Tshiabu Angelus Dan el 16 de Mayo de 2023
% Design the filter
fs = 2*pi*500; % highest significant frequency
Ts1 = 1/fs;
Ts2 = 2*Ts1;
Ts3 = 4*Ts1;
Rp = 2; % passband ripple (dB)
Rs = 12; % stopband attenuation (dB)
Wp = [120 300]*2*pi; % passband frequencies (rad/s)
Ws = [45 450]*2*pi; % stopband frequencies (rad/s)
[n, Wn] = cheby2(n, Rs, Ws/(fs/2)); % determine filter order and cutoff frequency
[b, a] = butter(n, Wn, 'stop'); % design filter coefficients
% Apply the filter to the input signal
t = 0:Ts1:1;
x1 = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t); % define input signal
y1 = filter(b, a, x1); % filter input signal
t = 0:Ts2:1;
x2 = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t);
y2 = filter(b, a, x2);
t = 0:Ts3:1;
x3 = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t);
y3 = filter(b, a, x3);
% Plot filtered signals
figure;
subplot(3,1,1);
plot(t, y1);
title('Filtered Signal (Ts = 1/(2*pi*500))');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,2);
plot(t, y2);
title('Filtered Signal (Ts = 2/(2*pi*500))');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,3);
plot(t, y3);
title('Filtered Signal (Ts = 4/(2*pi*500))');
xlabel('Time (s)');
ylabel('Amplitude');
% Sampling period influence
figure;
freqz(b,a);
title('Frequency Response of Filter');
xlabel('Frequency (rad/s)');
ylabel('Magnitude');
% The sampling period does influence the filtering process.
Unrecognized function or variable 'n'.
Error in QUEST2_2 (line 10)
[n, Wn] = cheby2(n, Rs, Ws/(fs/2)); % determine filter order and cutoff frequency

Tshiabu Angelus Dan
Tshiabu Angelus Dan el 16 de Mayo de 2023
% Design the filter
fs = 2*pi*500; % highest significant frequency
Ts1 = 1/fs;
Ts2 = 2*Ts1;
Ts3 = 4*Ts1;
Rp = 2; % passband ripple (dB)
Rs = 12; % stopband attenuation (dB)
Wp = [120 300]*2*pi; % passband frequencies (rad/s)
Ws = [45 450]*2*pi; % stopband frequencies (rad/s)
[n, Wn] = cheby2(n, Rs, Ws/(fs/2)); % determine filter order and cutoff frequency
[b, a] = butter(n, Wn, 'stop'); % design filter coefficients
% Apply the filter to the input signal
t = 0:Ts1:1;
x1 = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t); % define input signal
y1 = filter(b, a, x1); % filter input signal
t = 0:Ts2:1;
x2 = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t);
y2 = filter(b, a, x2);
t = 0:Ts3:1;
x3 = 1 + sin(10*pi*t) + sin(200*pi*t) + sin(400*pi*t);
y3 = filter(b, a, x3);
% Plot filtered signals
figure;
subplot(3,1,1);
plot(t, y1);
title('Filtered Signal (Ts = 1/(2*pi*500))');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,2);
plot(t, y2);
title('Filtered Signal (Ts = 2/(2*pi*500))');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(3,1,3);
plot(t, y3);
title('Filtered Signal (Ts = 4/(2*pi*500))');
xlabel('Time (s)');
ylabel('Amplitude');
% Sampling period influence
figure;
freqz(b,a);
title('Frequency Response of Filter');
xlabel('Frequency (rad/s)');
ylabel('Magnitude');
% The sampling period does influence the filtering process.
Unrecognized function or variable 'n'.
Error in QUEST2_2 (line 10)
[n, Wn] = cheby2(n, Rs, Ws/(fs/2)); % determine filter order and cutoff frequency
Please fellow can you help me fix this error

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by