首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >fmincon和求解器过早停止的目标函数错误,MATLAB for loop

fmincon和求解器过早停止的目标函数错误,MATLAB for loop
EN

Stack Overflow用户
提问于 2019-10-04 21:56:23
回答 1查看 224关注 0票数 2

我在一个for循环中完成了最大似然估计。

当我不收敛时,我会收到这个错误消息,

代码语言:javascript
运行
复制
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

然而,

在某些情况下,我仍然不能收敛。

有没有可能(自动)计算我有多少次不收敛?

EN

Stack Overflow用户

回答已采纳

发布于 2019-10-09 17:57:09

您可以使用fmincon函数的exitflag输出:

代码语言:javascript
运行
复制
[x,~,exitflag] = fmincon(fun,x0,A,b);

exitflag指示fmincon停止的原因。

根据文档,exitflag可以采用以下值:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58237842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档