Execution of script varargin as a function is not supported
Afficher commentaires plus anciens
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 commentaires
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
le 18 Déc 2023
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!