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

조회 수: 1 (최근 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.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by