Unrecognized function or variable 'options'. for simulannealbnd, but works for fminunc

11 次查看(过去 30 天)
When I change the optimization function to simulannealbnd instead of fminunc, MATLAB shows error " No variable named options".
What wrong am i doing?
I changed the code from :
if isempty(optimfun)
% determine whether the MATLAB Optimization toolbox is available and can be used
if ft_hastoolbox('optim')
optimfun = @fminunc;
else
optimfun = @fminsearch;
end
end
if isempty(maxiter)
% set a default for the maximum number of iterations, depends on the optimization function
if isequal(optimfun, @fminunc)
maxiter = 1000;
else
maxiter = 3000;
end
end
if isequal(optimfun, @fminunc)
options = optimset(...
'TolFun',1e-9,...
'TypicalX',scale*ones(size(param)),...
'LargeScale','off',...
'HessUpdate','bfgs',...
'MaxIter',maxiter,...
'MaxFunEvals',2*maxiter*length(param),...
'Display',display);
elseif isequal(optimfun, @fminsearch)
options = optimset(...
'MaxIter',maxiter,...
'MaxFunEvals',2*maxiter*length(param),...
'Display',display);
to:
if isempty(optimfun)
% determine whether the MATLAB Optimization toolbox is available and can be used
if ft_hastoolbox('optim')
optimfun = @simulannealbnd;
else
optimfun = @fminsearch;
end
end
if isempty(maxiter)
% set a default for the maximum number of iterations, depends on the optimization function
if isequal(optimfun, @simulannealbnd)
maxiter = 1000;
else
maxiter = 3000;
end
end
if isequal(optimfun, @simulannealbnd)
options = optimoptions(...
'TolFun',1e-9,...
'TypicalX',scale*ones(size(param)),...
'MaxIter',maxiter,...
'MaxFunEvals',2*maxiter*length(param),...
'Display',display);
end
elseif isequal(optimfun, @fminsearch)
options = optimset(...
'MaxIter',maxiter,...
'MaxFunEvals',2*maxiter*length(param),...
'Display',display);
% perform the optimization with either the fminsearch or fminunc function
[param, fval, exitflag, output] = optimfun(@dipfit_error, param, options, dat, sens, headmodel, constr, metric, checkinside, mleweight, reducerank, normalize, normalizeparam, weight, backproject);
The complete file for code is attached here
I get an error saying ' Unrecognized function or variable 'options'.' How do i solve this?

采纳的回答

Alan Weiss
Alan Weiss 2023-4-11
optimoptions requires that the first argument be the name of the solver. Something like
options = optimoptions('simulannealbnd',...
'TolFun',1e-9,...
'TypicalX',scale*ones(size(param)),...
'MaxIter',maxiter,...
'MaxFunEvals',2*maxiter*length(param),...
'Display',display);
Alan Weiss
MATLAB mathematical toolbox documentation
  4 个评论
Subrat Bastola
Subrat Bastola 2023-4-11
Update: I fixed it by:
% Define the objective function
objfun = @(param) dipfit_error(param, dat, sens, headmodel, constr, metric, checkinside, mleweight, reducerank, normalize, normalizeparam, weight, backproject);
% Call `simulannealbnd`
[param, fval, exitflag, output] = simulannealbnd(objfun, param, [], [], options);
Alan Weiss
Alan Weiss 2023-4-11
Great! If you feel that I helped, please Accept my answer.
Alan Weiss
MATLAB mathematical toolbox documentation

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-4-11
if isequal(optimfun, @simulannealbnd)
options = optimoptions(...
'TolFun',1e-9,...
'TypicalX',scale*ones(size(param)),...
'MaxIter',maxiter,...
'MaxFunEvals',2*maxiter*length(param),...
'Display',display);
end
That end concludes the entire if statement.
elseif isequal(optimfun, @fminsearch)
That elseif will generate an error if you are not already within a different if statement.
Notice that the original code did not have an end before the elseif.

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by