Getting an error stating Undefined funciton or method

5 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 14 Feb 2012
I do not see a problem. Perhaps you did not save the file after editing?
A hint:
find(diff(sign(f)))
  1 Comment
Adam Anderson
Adam Anderson on 14 Feb 2012
where in the code would I place this. I tried cutting it all off from the for loop but that produced a 1 row vector with xb=[].
Thanks for the help.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!