Unrecognized function or variable

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 个评论

To verify: did you run whatever .m file that you stored the code in?
By the way: why did you put "clear all" in as part of your code? "clear all" inside a program asks the program to try to do its best to remove all state from MATLAB -- including the state that is recording the fact that the program is running. It is Wile E. Coyote blowing up the bridge that he is standing on, but expecting not to fall down.
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
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 个)

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.
% 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
% 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

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by