Unrecognized function or variable

91 次查看(过去 30 天)
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 个评论
Joshua Tshuma
Joshua Tshuma 2021-4-13
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 2021-4-13
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)

请先登录,再进行评论。

回答(3 个)

Gargi Patil
Gargi Patil 2021-4-16
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.

Tshiabu Angelus Dan
% 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
% 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

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by