Getting an error stating Undefined funciton or method
5 次查看(过去 30 天)
显示 更早的评论
I wrote this code directly out of a text as directed and then I try to run it as directed and keep getting an error that says, ??? Undefined function or method 'incsearch' for input arguments of type 'function_handle'. The code written looks like this.
function xb = incsearch(func,xmin,xmax,ns)
%Incremental search root locator
% xb = incsearch(func,xmin,xmax,ns):
% finds brackets of x that contain sign changes
% of a function on an interval
%input:
% func=name of function
% xmin,xmax=endpoints of interval
% ns=number of subintervals
%output:
% xb(k,1) is the lower bound of the kth sign change
% xb(k,2) is the upper bound of the kth sign change
% if no brackets found, xbb=[]
if nargin < 3 , error('at least 3 input arguments required')
end
if nargin < 4, ns=50; end % if ns is blank set to 50
% Incremental search
x=linspace(xmin,xmax,ns);
f=func(x);
nb=0;xb=[]; % xb is null unless sign change is detected
for k = 1:length(x)-1
if sign(f(k)) ~= sign(f(k+1)) % check for sign change
nb=nb+1;
xb(nb,1)=x(k);
xb(nb,2)=x(k+1);
end
end
if isempty(xb) %display no brackets were found
disp('no brackets found')
disp('check interval or increase ns')
else
disp('number of brackets:') %display number of brackets
disp(nb)
end
The calling of it is
incsearch (@(x) sin(10*x) + cos(3*x),3,6)
and the error I keep getting is
??? Undefined function or method 'xb' for input arguments of type 'function_handle'.
I find this very funny as this is exactly how the author says to do it in the book. No wonder I am not learning much. Any help would be appreciated.
采纳的回答
Jan
2012-2-14
I do not see a problem. Perhaps you did not save the file after editing?
A hint:
find(diff(sign(f)))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!