Getting an error stating Undefined funciton or method

4 vues (au cours des 30 derniers jours)
Adam Anderson
Adam Anderson le 14 Fév 2012
Modifié(e) : Matt J le 12 Oct 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.

Réponse acceptée

Jan
Jan le 14 Fév 2012
I do not see a problem. Perhaps you did not save the file after editing?
A hint:
find(diff(sign(f)))
  1 commentaire
Adam Anderson
Adam Anderson le 14 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by