Undefined function 'bisect' for input arguments of type 'function_handle'.

11 views (last 30 days)
I don't know why the code isn't working. We copied it from the textbook and I've checked it a dozen times and I don't know why it won't work. Any suggestions?
function [root, fx, ea, iter]=bisect(func, x1, xu, es, maxit, varargin)
% bisect: root location zeroes
% [root, fx, ea, iter]=bisect(func, x1, xu, es, maxit, p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% x1, xu = lower and upper guesses
% es = desired relative error (default 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% fx = function value at root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3, error('at least 3 input arguments required'), end
test = func(x1,varargin{:})*func(xu,varargin{:});
if test>0, error('no sign change'), end
if nargin<4|isempty(es), es=0.0001; end
if nargin<5|isempty(maxit), maxit=50; end
iter = 0; xr = x1; ea = 100;
while(1) xrold = xr;
xr = (x1 + xu)/2;
iter = iter + 1;
if xr ~= 0, ea = abs((xr-xrold)/xr) * 100; end
test = func(x1,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
x1 = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit, break, end
end
root = xr; fx = func(xr, varargin{:});
end

Answers (1)

Steven Lord
Steven Lord on 17 Mar 2024
Another possibility is that the user saved it to a file not named bisect.m. If the name of a function file and the name of the main (first) function in that function file differ, MATLAB will only be able to call the function when you use the file name not the function name. That's one of the reasons why we recommend that for function files you use the same name for the file (but add the .m extension) and the function (no extension.)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!