Inputting polynomial as parameter

Hi, newbie to MATLAB here, I currently get "Error using error Function is not defined for 'struct' inputs." I am wondering how to input a polynomial function into bisect2 (and fcnchk) properly. Appreciate any help!
function [x, out] = bisect2(FunFcnIn, Intv, params)
TOL = params.tol;
NO = params.MaxIt;
[FunFcn,msg] = fcnchk(FunFcnIn,0);
if ~isempty(msg)
error('InvalidFUN',msg);
end
...

Respuestas (1)

Steven Lord
Steven Lord el 5 de Sept. de 2017
The second output of fcnchk is a struct array. The error function can accept a struct array like that second output of fcnchk OR it can accept an error message string. Don't combine the two, it won't work.
function [x, out] = bisect2(FunFcnIn, Intv, params)
TOL = params.tol;
NO = params.MaxIt;
[FunFcn,msg] = fcnchk(FunFcnIn,0);
if ~isempty(msg)
error(msg);
end
To confirm that this works, run this code. You will receive an error.
bisect2(1:10, 1, struct('tol', 1, 'MaxIt', 2))
Alternately, just let fcnchk throw the error itself.
function [x, out] = bisect2(FunFcnIn, Intv, params)
TOL = params.tol;
NO = params.MaxIt;
FunFcn = fcnchk(FunFcnIn,0);
And again, check:
bisect2(1:10, 1, struct('tol', 1, 'MaxIt', 2))

Categorías

Preguntada:

el 5 de Sept. de 2017

Respondida:

el 5 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by