Unrecognized function or variable 'options'. for simulannealbnd, but works for fminunc
5 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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
Alan Weiss
on 11 Apr 2023
Great! If you feel that I helped, please Accept my answer.
Alan Weiss
MATLAB mathematical toolbox documentation
More Answers (1)
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!