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

9 views (last 30 days)
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?

Accepted Answer

Alan Weiss
Alan Weiss on 11 Apr 2023
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 Comments
Subrat Bastola
Subrat Bastola on 11 Apr 2023
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 on 11 Apr 2023
Great! If you feel that I helped, please Accept my answer.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 11 Apr 2023
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.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!