How do I fix the 'Invalid use of operator' Error in my anonymous function bisect code?
13 ビュー (過去 30 日間)
古いコメントを表示
function [root,fx,ea,iter]=bisect( @(x)x^7+3*x-1;0;1;0;3 )
% 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
test = func(xl)*func(xu);
if test>0,error('no sign change'),end
if nargin<4|isempty(es), es=0.0000001;end
if nargin<5|isempty(maxit), maxit=3;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)*func(xr);
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);
Then it says
"Error: ... Line: 1 Column: 36
Invalid use of operator." while underlining the '@' and last parantheses ')' for bisect in red. I'm not sure why
0 件のコメント
採用された回答
Alan Stevens
2020 年 9 月 2 日
Your syntax isn't correct. Try
f = @(x)x^7+3*x-1;
xl = 0; xu = 1; es = 10^-4; maxit = 30;
[root,fx,ea,iter]=bisect(f ,xl,xu,es,maxit );
disp(root)
function [root,fx,ea,iter]=bisect(f ,xl,xu,es,maxit )
% 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
test = f(xl)*f(xu);
if test>0,error('no sign change'),end
if nargin<4||isempty(es), es=0.0000001;end
if nargin<5||isempty(maxit), maxit=3;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 = f(xl)*f(xr);
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 = f(xr);
end
その他の回答 (1 件)
Steven Lord
2020 年 9 月 2 日
function [root,fx,ea,iter]=bisect( @(x)x^7+3*x-1;0;1;0;3 )
The values @(x)x^7+3*x-1, 0, 1, 0, and 3 are the values you should pass into bisect when you call bisect.
They are not the input argument names that you should specify when you define bisect. Those names need to be valid variable names to which MATLAB will assign the values with which the user of this function calls the function.
As you've written it, if MATLAB could somehow get past that invalid definition, when it reached this line:
test = func(xl)*func(xu);
the variable func would not exist.
If instead you had defined bisect as:
function [root, fx, ea, iter] = bisect(func, xl,xu,es,maxit,varargin)
then calling it like:
[root, fx, ea, iter] = bisect(@(x) x^7+3*x-1, 0, 1, 0, 3)
would define func as the anonymous function inside the workspace in which bisect operates, xl to be 0, etc.
参考
カテゴリ
Help Center および File Exchange で Argument Definitions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!