Why does this error occur-Unrecognized function or variable 's'.

2 ビュー (過去 30 日間)
Zoe Westcott
Zoe Westcott 2022 年 6 月 23 日
回答済み: Siraj 2023 年 9 月 14 日
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
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.
Torsten
Torsten 2022 年 6 月 23 日
How do you call the function "Convergent" ?

サインインしてコメントする。

回答 (1 件)

Siraj
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)
0
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.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by