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?
댓글 수: 0
채택된 답변
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
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
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!