Getting an error stating Undefined funciton or method

조회 수: 4 (최근 30일)
Adam Anderson
Adam Anderson 2012년 2월 14일
편집: Matt J 2013년 10월 12일
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
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)))
  댓글 수: 1
Adam Anderson
Adam Anderson 2012년 2월 14일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by