Why does this error occur-Unrecognized function or variable 's'.
조회 수: 2 (최근 30일)
이전 댓글 표시
function[e]= Convergent(h,m,s)
A= (32*pi)/7 ;
f = @(x) 0.*( x<25/64 & x>39/64) +cos(A*(x-0.5)).*( (25/64)<=x & x<=39/64);
g = @(x) 0.*( x<25/64 & x>39/64) +-A*sin(A*(x-0.5)).*( (25/64)<=x & x<=39/64);
x = 0:h:1;
n = length(x);
y=(f(x)).^m;
z=g(x).*m.*(f(x).^(m-1));
dy = zeros(n,1); % preallocate derivative vectors
if s==1
% 1st order(central)
for i=2:n-1
dy(i) = (y(i+1)-y(i))/(h);
end
elseif s==2
% 2nd order(central)
for i=2:n-1
dy(i) = (y(i+1)-y(i-1))/(2*h);
end
% 3rd order(forward)
elseif s==3
for i=2:n-1
dy(i) = ((-11/6)*y(i)+3*y(i+1)-(3/2)*y(i+2)+(1/3)*y(i+3))/(h) ;
end
% The error function
e=max(abs(z-dy'));
end
I'm trying to define the function called convergent for determining order of convergent. When I run this code it says "Unrecognized function or variable 's'.". Where s is a imput value depending om what order of derivative i want to use. Thanks.
댓글 수: 2
dpb
2022년 6월 23일
편집: dpb
2022년 6월 23일
I reformatted the code to ensure my eyes weren't lying -- that shows the closing end of the if...end construct is missing before the line computing e
As for the error message about s, that would have to be from somewhere else in the attempted invocation of the function it would appear; we need to see the exact code and error in context to be able to know for sure.
It looks as though
...
elseif s==3
for i=2:n-1
dy(i) = ((-11/6)*y(i)+3*y(i+1)-(3/2)*y(i+2)+(1/3)*y(i+3))/(h) ;
end
end
% The error function
e=max(abs(z-dy'));
end
was intended.
답변 (1개)
Siraj
2023년 9월 14일
Hi! It is my understanding that the error appears due to a missing "end" statement to close the "if" statement in the code. By adding the necessary "end" statement, the code was able to run successfully without any errors.
To learn more about how to properly close an "if" block in MATLAB, you can refer to the following link:
Attaching the code for your reference.
Temp = Convergent(1,1,1);
disp(Temp)
function[e]= Convergent(h,m,s)
A= (32*pi)/7 ;
f = @(x) 0.*( x<25/64 & x>39/64) +cos(A*(x-0.5)).*( (25/64)<=x & x<=39/64);
g = @(x) 0.*( x<25/64 & x>39/64) +-A*sin(A*(x-0.5)).*( (25/64)<=x & x<=39/64);
x = 0:h:1;
n = length(x);
y=(f(x)).^m;
z=g(x).*m.*(f(x).^(m-1));
dy = zeros(n,1); % preallocate derivative vectors
if s==1
% 1st order(central)
for i=2:n-1
dy(i) = (y(i+1)-y(i))/(h);
end
elseif s==2
% 2nd order(central)
for i=2:n-1
dy(i) = (y(i+1)-y(i-1))/(2*h);
end
% 3rd order(forward)
elseif s==3
for i=2:n-1
dy(i) = ((-11/6)*y(i)+3*y(i+1)-(3/2)*y(i+2)+(1/3)*y(i+3))/(h) ;
end
end
% The error function
e=max(abs(z-dy'));
end
However, the error that you have attached suggests that the variable 's' has not been defined or is not accessible within the scope of your code. To avoid the "Unrecognized function or variable 's'" error, ensure that you pass a valid value for 's' when calling the 'convergent' function. Also, double-check that you haven't accidentally overwritten the variable 's' elsewhere in your code.
Hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!