Main Content

## 退出标志和退出消息

### 退出标志

• 退出标志为正数表示结果成功。

• 退出标志为负数表示结果不成功。

• 退出标志为零表示求解器因超过迭代限制或函数计算次数达到限制而停止（请参阅迭代和函数计算次数，另请参阅容差和停止条件）。

 `coneprog` `exitflag` `fgoalattain` `exitflag` `fminbnd` `exitflag` `fmincon` `exitflag` `fminimax` `exitflag` `fminsearch` `exitflag` `fminunc` `exitflag` `fseminf` `exitflag` `fsolve` `exitflag` `fzero` `exitflag` `intlinprog` `exitflag` `linprog` `exitflag` `lsqcurvefit` `exitflag` `lsqlin` `exitflag` `lsqnonlin` `exitflag` `lsqnonneg` `exitflag` `quadprog` `exitflag`

`[x,fval,exitflag] = fsolve(...)`

`$\begin{array}{c}2{x}_{1}-{x}_{2}={e}^{-{x}_{1}}\\ -{x}_{1}+2{x}_{2}={e}^{-{x}_{2}}.\end{array}$`

```myfcn = @(x)[2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))];```

```[xfinal fval exitflag] = fsolve(myfcn,[-5 -5]) ```
```Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. xfinal = 0.5671 0.5671 fval = 1.0e-06 * -0.4059 -0.4059 exitflag = 1```

`fsolve` `exitflag` 的表中，您会发现退出标志值 `1` 表示“函数收敛于解 `x`”。也就是说，`fsolve` 报告 `myfcn``x` = `[0.5671 0.5671]` 处接近于零。

### 退出消息

```Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.```

### 增强版退出消息

• 单词或短语链接。如果您点击此类链接，将打开一个窗口，其中显示对应项的定义或给出其他信息。新窗口可能会包含指向帮助浏览器文档的链接，以提供更详细的信息。

• 最后一行显示的链接提供 `<stopping criteria details>`。如果点击此链接，MATLAB 将显示求解器停止的详细原因。

`fminunc` 求解器提供了增强版的退出消息：

```opts = optimoptions(@fminunc,'Algorithm','quasi-newton'); % 'trust-region' needs gradient [xfinal fval exitflag] = fminunc(@sin,0,opts)```

• `<stopping criteria details>` 链接会在 MATLAB 命令行中显示以下内容：

```Optimization completed: The first-order optimality measure, 0.000000e+00, is less than options.OptimalityTolerance = 1.000000e-06.```
• 其他链接会弹出一个包含相关项定义的帮助窗口。例如，点击 `Local minimum found` 链接将打开以下窗口：

点击该窗口中的 `first-order optimality measure` 展开链接将显示 `fminunc` 的一阶最优性度量的定义：

展开链接是在同一窗口中获取详细信息的一种方式。再次点击 `first-order optimality measure` 展开链接将折叠定义。

• 其他链接可打开帮助查看器。

### 退出消息选项

`'none'` 或同义词 `'off'`
`'final'`（大多数求解器的默认值）默认值
`'final-detailed'`详细消息
`'iter'`默认值
`'iter-detailed'`详细消息
`'notify'`仅当 `exitflag` ≤ 0 时为默认值
`'notify-detailed'`仅当 `exitflag` ≤ 0 时为详细信息

```opts = optimoptions(@fminunc,'Display','iter-detailed','Algorithm','quasi-newton'); [xfinal fval] = fminunc(@cos,1,opts);```