Execution of script varargin as a function is not supported

15 views (last 30 days)
I am trying to complete a bi-section method test that displays iter,ea, and roots, but my code keeps giving error(s) in the command window.
code: (picture below)
func = @(x) x.^2 - 2;
xl = 1;
xu = 3;
es = 0.0001;
maxit = 50;
p = [1,0,-2]; %polynomial coefficients
r = roots(p);
[root,ea,iter] = mybisection (func, xl, xu, es, maxit, varargin);
function [root,ea,iter] = mybisection(func, xl, xu, es, maxit,varargin)
% mybisection: root location zeroes
% [root,ea, iter]=mybisection(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = function handle
% xl, xu = lower and upper guesses
% es = desired relative error
% maxit = maximum allowable iterations
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'), end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0, error ('no sign change'), end
if nargin<4||isempty(es), end
if nargin<5||isempty(maxit), end
iter = 0; xr = xl; root = xr;
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
disp(iter);
disp (root);
if ea <= es || iter >= maxit,break,end
end
disp(ea);
end
error message: (picture below)
>> BiSectionMethod
Execution of script varargin as a function is not supported:
C:\Program Files\MATLAB\R2023b\toolbox\matlab\lang\varargin.m
Error in BiSectionMethod (line 11)
[root,ea,iter] = mybisection (func, xl, xu, es, maxit, varargin);
>>
  2 Comments
Stephen23
Stephen23 on 18 Dec 2023
Edited: Stephen23 on 18 Dec 2023
The documentation states that "varargin is an input variable in a function definition statement .."
But here you are trying to use it when you call a function:
[root,ea,iter] = mybisection (func, xl, xu, es, maxit, varargin);
What values/arrays should it contain when you use it like that?
Christian
Christian on 18 Dec 2023
I'm new to MATLAB and programing in general, so I honestly don't know what I am doing and was trying different things to get something to work.

Sign in to comment.

Accepted Answer

Angelo Yeo
Angelo Yeo on 18 Dec 2023
You are not supposed to use varargin as function's input argument explicitly as "varargin".
varargin in MATLAB is like a magic bag that can hold an unlimited number of items. Imagine you have a bag that you can take to the grocery store, and no matter how many things you decide to buy, the bag will always have enough room for all of them. Similarly, when you're writing a function in MATLAB, sometimes you don't know in advance how many inputs someone might want to give to your function.
Let's say you're writing a function to make a smoothie, and you want to allow people to choose any combination of fruits they like. In MATLAB, you would use varargin to collect all the different fruits (inputs) that someone might want to add to their smoothie. When you look inside your varargin bag, you'll find all the fruits listed in the order they were added, and you can then use them to make the smoothie exactly how the person wants it.
In technical terms, varargin is a way for MATLAB functions to accept a variable number of input arguments. It stands for "variable-length input argument list" and is used when you want your function to be flexible in terms of how many inputs it can handle.
Below is the script without using "varargin" for "mybisection". I didn't put any for varargin because your function "func" does not need any variable-length input arguments.
func = @(x) x.^2 - 2;
xl = 1;
xu = 3;
es = 0.0001;
maxit = 50;
p = [1,0,-2]; %polynomial coefficients
r = roots(p);
[root,ea,iter] = mybisection(func, xl, xu, es, maxit);
1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 6.7435e-05
function [root,ea,iter] = mybisection(func, xl, xu, es, maxit,varargin)
% mybisection: root location zeroes
% [root,ea, iter]=mybisection(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = function handle
% xl, xu = lower and upper guesses
% es = desired relative error
% maxit = maximum allowable iterations
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'), end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0, error ('no sign change'), end
if nargin<4||isempty(es), end
if nargin<5||isempty(maxit), end
iter = 0; xr = xl; root = xr;
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
disp(iter);
disp (root);
if ea <= es || iter >= maxit,break,end
end
disp(ea);
end

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!