Unrecognized function or variable 'x'.

function y = Tsin(x,n)
x=input('Degrees: ');
y=input('Terms: ');
%Tsin calculates the sin using Taylor formula.
%Input arguments:
%x The angle in degrees, n number of terms.
z=x*pi/180;
y=0;
for k=0:n-1
y=y+(-1)^k*z^(2*k+1)/factorial(2*k+1);
end
RUN then
>> Tsin(x, n)
Unrecognized function or variable 'x'.

10 Comments

When you run
>> Tsin(x,n)
you need to provide values for both x and n. However, it looks like you are obtaining x within the function via this line:
x=input('Degrees: ');
If so, you do not need to supply x to your function. I would recommend changing the function definition to match
function y = Tsin(n)
and then calling
>> Tsin(n)
for whatever n value you want.
I want to ask to users degrees and terms. if on workspace values for x and n the code is run but give it same result which number ı type.
PenaltyFactor=100;
LMUpdateRate=0.0100;
AbsoluteTolerance=5.0000e-06;
RelativeTolerance=0.0050;
MaxIterations=1000;
InitialIMFs=zeros(length(x),5);
InitialLM=zeros(length(x)+1,1);
CentralFrequencies= [];
InitializeMethod='peaks';
FFTLength=2*length(x);
NumIMFs= 5;
SignalLength= length(x);
HalfSignalLength=length(x)/2;
MirroredSignalLength=2*length(x);
DataType= 'double';
NumHalfFreqSamples= length(x)+1;
Display= 0;
%%
nfft = FFTLength;
penaltyFactor = PenaltyFactor;
numIMFs = NumIMFs;
relativeDiff = inf;
absoluteDiff = relativeDiff;
tau = LMUpdateRate; % Lagrange multiplier update rate
xr = [x(HalfSignalLength:-1:1); x; x(SignalLength:-1:ceil(SignalLength/2)+1)];
y = fft(xr,FFTLength);
sigFDFull = y;
% Get half of the bandwidth
sigFD = sigFDFull(1:NumHalfFreqSamples);
initIMFfdFull = fft(InitialIMFs,nfft);
initIMFfd = initIMFfdFull(1:NumHalfFreqSamples,:) + eps;
IMFfd = initIMFfd;
sumIMF = sum(IMFfd,2);
LM = InitialLM(:); % Lagrange Multiplier
%% Frequency vector from [0,0.5) for odd nfft and [0,0.5] for even nfft
f = (0:(nfft/2))/nfft;
%% Get the initial central frequencies
x=abs(sigFD);
BW = 2/FFTLength; % bandwidth of signal
minBWGapIndex = 2*BW/f(2);
x(x<mean(x)) = mean(x);
TF = islocalmax(x,'MinSeparation',minBWGapIndex);
pkst = x(TF);
locst = f(TF);
numpPeaks = length(pkst);
% Check for DC component
if x(1) >= x(2)
pks = zeros(numpPeaks+1,1);
locs = pks;
pks(2:length(pkst)+1) = pkst;
locs(2:length(pkst)+1) = locst;
pks(1) = x(1);
locs(1) = f(1);
else
pks = zeros(numpPeaks,1);
locs = pks;
pks(1:length(pkst)) = pkst;
locs(1:length(pkst)) = locst;
end
[~,index] = sort(pks,'descend');
centralFreq = 0.5*rand(NumIMFs,1);
% Check if the number of peaks is less than number of IMFs
if length(locs) < NumIMFs
centralFreq(1:length(locs(index))) = locs;
else
centralFreq(1:NumIMFs) = locs(index(1:NumIMFs));
end
%%
iter = 0;
f=f';
initIMFNorm = abs(initIMFfd).^2;
normIMF = zeros(size(initIMFfd,1),size(initIMFfd,2));
while (iter < MaxIterations && (relativeDiff > RelativeTolerance ||...
absoluteDiff > AbsoluteTolerance))
for kk = 1:numIMFs
sumIMF = sumIMF - IMFfd(:,kk);
IMFfd(:,kk) = (sigFD - sumIMF + LM/2)./...
(1+penaltyFactor*(f - centralFreq(kk)).^2);
normIMF(:,kk) = abs(IMFfd(:,kk)).^2;
centralFreq(kk) = (f.'*normIMF(:,kk))/sum(normIMF(:,kk));
sumIMF = sumIMF + IMFfd(:,kk);
end
LM = LM + tau*(sigFD-sumIMF);
absDiff = mean(abs(IMFfd-initIMFfd).^2);
absoluteDiff = sum(absDiff);
relativeDiff = sum(absDiff./mean(initIMFNorm));
% Sort IMF and central frequecies in descend order
% In ADMM, the IMF with greater power will be substracted first
[~,sortedIndex] = sort(sum(abs(IMFfd).^2),'descend');
IMFfd = IMFfd(:,sortedIndex);
centralFreq = centralFreq(sortedIndex(1:length(centralFreq)));
initIMFfd = IMFfd;
initIMFNorm = normIMF;
iter = iter + 1;
end
%%--------------------- Step 08 --------------------------------
%% Convert to time domain signal
% Transform to time domain
IMFfdFull = complex(zeros(nfft,numIMFs));
IMFfdFull(1:size(IMFfd,1),:) = IMFfd;
if ~mod(FFTLength,2)
IMFfdFull(size(IMFfd,1)+1:end,:) = conj(IMFfd(end-1:-1:2,:));
else
IMFfdFull(size(IMFfd,1)+1:end,:) = conj(IMFfd(end:-1:2,:));
end
[~,index] = sort(centralFreq,'descend');
%%
z=IMFfdFull(:,index);
xr = real(ifft(z,FFTLength));
IMFs_without_inbuild = xr(HalfSignalLength+1:MirroredSignalLength-HalfSignalLength,:);
residual_without_inbuild = PPGblr1 - sum(IMFs_without_inbuild,2);
@Patrick's answer moved here as a comment
-----------------------------------------------------------------
function Simpson1 (f1,a,b,M)
%F es el integrando como una cadena de caracteres
f=inline(f1);
h=(b-a)/(2*M);
s1=0;
s2=0;
for k=1:M
x=a+h*(2*k-1);
s1=s1+feval(f,x);
end
for k=1:M-1
x=a+h*2*k;
s2=s2+feval(f,x);
end
s=(h/3)*(feval(f,a)+feval(f,b)+4*s1+2*s2);
syms x
sv=int (f(x),a,b);
error=eval(abs(s-sv)*/abs(sv))*100;
disp('rpta simpson error true%')
fprintf('/t%0.5f/t%0.5f/n',s,error);
end
Console
Simpson1((9.8*67/12.5)*(1-exp(-12.5*x/67)),0,8,10)
Unrecognized function or variable 'x'.
@Patrick The error message tells you which variable is causing the problem: "x".
When you call the function using
Simpson1((9.8*67/12.5)*(1-exp(-12.5*x/67)),0,8,10)
% ^
the variable x is not defined.
Diwitha
Diwitha on 1 Feb 2023
Moved: Adam Danz on 9 Mar 2023
clear all
sym x
f(x)=x^ 4+2*x^ 3-8*x^ 2
Diwitha
Diwitha on 1 Feb 2023
Moved: Adam Danz on 9 Mar 2023
It says Unrecognized function or variable 'x'.
% Función para calcular el valor de Lagrange
function y = lagrange2(X, Y)
n=length(X);
sym x;
for i=1:n
w=1;
for j=1:n
if j~=1
w = w * (x - X(j)) / (X(i) - X(j));
end
end
end
y = 0;
for i=1:n
y = y + w(i) * Y(i);
end
y=simplify(expand(ecuacion));
end
RUN then
Unrecognized function or variable 'x'.
Error in lagrange2 (line 10)
w = w * (x - X(j)) / (X(i) - X(j));

Instead of

sym x;

use

syms x;

or

x = sym('x');

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 13 Apr 2020
Edited: Adam Danz on 13 Apr 2020
You need to define the input variables. You cannot simply run a function that has undefined input variables.
x = 45
n = 8
Tsin(x,n)
____________________________________
Copy of question:
function y = Tsin(x,n)
x=input('Degrees: ');
y=input('Terms: ');
%Tsin calculates the sin using Taylor formula.
%Input arguments:
%x The angle in degrees, n number of terms.
z=x*pi/180;
y=0;
for k=0:n-1
y=y+(-1)^k*z^(2*k+1)/factorial(2*k+1);
end
RUN then
>> Tsin(x, n)
Unrecognized function or variable 'x'.

2 Comments

can't define a variable for fiding a root :\
@Dylan Radey I don't know what that means. All variables are defined either directly by the user or from computations within the function/script.

Sign in to comment.

More Answers (3)

Yuyang Mao
Yuyang Mao on 5 Aug 2021
I got the same problem before.
Explaination: Please make sure that you have add your function to the path!
solution:
  • Click run, it jumps out a window
  • click 'add to path', is shows error in red color which is fine
  • now give the name of your function again, in your case is 'Tsin(x,n)'
And this should work.
Best,
Yuyang

2 Comments

Good advice. However, in this question, the function name is Tsin but the unrecognized variable name is x.
Need to reinput the values you want for x and n in the command window

Sign in to comment.

syms x
solve('x+3=4',x)
Error using sym/solve>getEqns (line 418)
List of equations must not be empty.

Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
f=inline('x^2','x')
f = Inline function: f(x) = x^2
diff(f(x),x)
Unrecognized function or variable 'x'.

Categories

Find more on Descriptive Statistics and Insights in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!