Getting an error stating Undefined funciton or method
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Adam Anderson
am 14 Feb. 2012
Bearbeitet: Matt J
am 12 Okt. 2013
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.
Akzeptierte Antwort
Jan
am 14 Feb. 2012
I do not see a problem. Perhaps you did not save the file after editing?
A hint:
find(diff(sign(f)))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!