我在一个for循环中完成了最大似然估计。
当我不收敛时,我会收到这个错误消息,
error in objective function of fmincon and solver stopped prematurely
我已经用这个答案解决了这个问题:https://uk.mathworks.com/matlabcentral/answers/131975-error-in-objective-function-of-fmincon-and-solver-stopped-prematurely
然而,
在某些情况下,我仍然不能收敛。
有没有可能(自动)计算我有多少次不收敛?
发布于 2019-10-09 17:57:09
您可以使用fmincon
函数的exitflag
输出:
[x,~,exitflag] = fmincon(fun,x0,A,b);
exitflag
指示fmincon停止的原因。
根据文档,exitflag
可以采用以下值:
All Algorithms:
1 %First-order optimality measure was less than options.OptimalityTolerance, and maximum constraint violation was less than options.ConstraintTolerance.
0 %Number of iterations exceeded options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations.
-1 %Stopped by an output function or plot function.
-2 %No feasible point was found.
All algorithms except active-set:
2 %Change in x was less than options.StepTolerance and maximum constraint violation was less than options.ConstraintTolerance.
trust-region-reflective algorithm only:
3 %Change in the objective function value was less than options.FunctionTolerance and maximum constraint violation was less than options.ConstraintTolerance.
active-set algorithm only:
4 %Magnitude of the search direction was less than 2*options.StepTolerance and maximum constraint violation was less than options.ConstraintTolerance.
5 %Magnitude of directional derivative in search direction was less than 2*options.OptimalityTolerance and maximum constraint violation was less than options.ConstraintTolerance.
interior-point, sqp-legacy, and sqp algorithms:
-3 %Objective function at current iteration went below options.ObjectiveLimit and maximum constraint violation was less than options.ConstraintTolerance.
如果fmincon成功地确定了最小值,则为exitflag == 1
。所以你可以简单地数一下有多少次exitflag ~= 1
error_counter = 0;
for ii = 1:n_iteration
[x,~,exitflag] = fmincon(fun,x0,A,b);
if exitflag ~= 1
error_counter = error_counter + 1
end
end
如果您只想检查是否没有超过迭代次数,那么可以用exitflag == 0
替换exitflag ~= 1
。
https://stackoverflow.com/questions/58237842
复制相似问题