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
Tommy
2020년 4월 13일
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.
Emre Can Usengul
2020년 4월 13일
Adam Danz
2020년 4월 13일
There are lots of ways to ask users for input.
etc...
Nandini
2022년 6월 22일
편집: Walter Roberson
2024년 10월 20일
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);
Adam Danz
2022년 6월 24일
@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.
Juan David
2024년 4월 6일
이동: Voss
2024년 4월 6일
% 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));
Voss
2024년 4월 6일
Instead of
sym x;
use
syms x;
or
x = sym('x');
채택된 답변
추가 답변 (3개)
Yuyang Mao
2021년 8월 5일
1 개 추천
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
Adam Danz
2021년 8월 9일
Good advice. However, in this question, the function name is Tsin but the unrecognized variable name is x.
Jordan Wood
2021년 8월 10일
Need to reinput the values you want for x and n in the command window
syms x
solve('x+3=4',x)
카테고리
도움말 센터 및 File Exchange에서 Formula Manipulation and Simplification에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!