Hello,
I am having trouble with a code in MATLAB. I have the full code that is supposed to work (below) but when I run the code I get the error on line 17 'at least 3 inputs required' (bolded row below). Does this mean I need to actually put three number values in there and then run it? Thanks in advance!
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, 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 %MARKED
test = func(xl,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 = xl; ea = 100;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr; fx = func(xr, varargin{:});

 採用された回答

Walter Roberson
Walter Roberson 2022 年 2 月 10 日

0 投票

That code cannot be run by pressing the green Run button. You need to either go down to the command line and invoke it, or else you need to have another piece of code invoke it.
When the function is invoked, it needs to be passed a minimum of three inputs. The first input needs to be a function handle of the function that is to be bisected. The second input needs to be the lower bound of a search (a numeric scalar.) The third input needs to be the upper bound of the search (a numeric scalar.)
Example:
bisect(@(x) x.^2 - cos(x), -1, pi/2)

4 件のコメント

Heather Reed
Heather Reed 2022 年 2 月 10 日
Thanks so much!!! I am trying to follow the book examples (Applied Mathematical Numerical Methods with MATLAB), but some of the conversational nuance needed by students not used to coding at all (like me) is missing from the book examples. I appreciate your response and taking the time to explain why it wasn't working for me!
Heather Reed
Heather Reed 2022 年 2 月 12 日
Hi Walter,
I tried to run your example function above to see what happens and I'm now getting the error below (that the inputs need to be numeric). I read through your answer a few times, and I'm thinking one the two of the three inputs are numeric, right? The first input is the actual function, right? Thanks in advance for your help!
Walter Roberson
Walter Roberson 2022 年 2 月 12 日
I do not get that mesage.
bisect(@(x) x.^2 - cos(x), -1, 1/2)
ans = -0.8241
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, 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 %MARKED
test = func(xl,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 = xl; ea = 100;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr; fx = func(xr, varargin{:});
end
Heather Reed
Heather Reed 2022 年 3 月 4 日
Thanks Walter! I apologize for the delayed response. I have been struggling with entering the function handles correctly. Hopefully with practice, I'll get the hang of it :-). Have a great day!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by