## 当求解器可能成功求解时

### 终点等于初始点

1. 从不同点开始 - 请参阅更改初始点

2. 检查您的目标和约束是否正确定义（例如，它们在某些点处是否返回了正确的值？）- 请参阅检查您的目标函数和约束函数。检查以确保不可行点不会导致函数中出现错误；请参阅迭代可能违反约束

3. 更改容差，例如 `OptimalityTolerance``ConstraintTolerance``StepTolerance` - 请参阅使用适当的容差

4. 缩放您的问题，使每个坐标具有大致相同的影响 - 请参阅重新缩放问题

5. 提供梯度和黑塞矩阵信息 - 请参阅提供解析梯度或雅可比矩阵提供黑塞矩阵

### 可能是局部最小值

 1.非平滑函数 2.以终点作为起点重新执行优化 3.尝试不同算法 4.更改容差 5.重新缩放问题 6.检查附近的点 7.更改有限差分选项 8.提供解析梯度或雅可比矩阵 9.提供黑塞矩阵

#### 2.以终点作为起点重新执行优化

```options = optimoptions('fminunc','Algorithm','quasi-newton'); funh = @(x)log(1 + (x(1) - 4/3)^2 + 3*(x(2) - (x(1)^3 - x(1)))^2); [xfinal fval exitflag] = fminunc(funh,[-1;2],options) Local minimum possible. fminunc stopped because it cannot decrease the objective function along the current search direction. xfinal = 1.3333 1.0370 fval = 8.5265e-014 exitflag = 5```

```[xfinal2 fval2 exitflag2] = fminunc(funh,xfinal,options) Local minimum found. Optimization completed because the size of the gradient is less than the default value of the function tolerance. xfinal2 = 1.3333 1.0370 fval2 = 6.5281e-014 exitflag2 = 1```

#### 3.尝试不同算法

```function [f gradf] = betopt(x) g = 1 + (x(1)-4/3)^2 + 3*(x(2) - (x(1)^3-x(1)))^2; f = log(g); gradf(1) = 2*(x(1)-4/3) + 6*(x(2) - (x(1)^3-x(1)))*(1-3*x(1)^2); gradf(1) = gradf(1)/g; gradf(2) = 6*(x(2) - (x(1)^3 -x(1)))/g;```

```options = optimoptions('fminunc','Algorithm','trust-region','SpecifyObjectiveGradient',true); [xfinal3 fval3 exitflag3] = fminunc(@betopt,[-1;2],options) Local minimum possible. fminunc stopped because the final change in function value relative to its initial value is less than the default value of the function tolerance. xfinal3 = 1.3333 1.0370 fval3 = 7.6659e-012 exitflag3 = 3```

```[xfinal4 fval4 eflag4 output4] = fminunc(@betopt,xfinal3,options) Local minimum found. Optimization completed because the size of the gradient is less than the default value of the function tolerance. xfinal4 = 1.3333 1.0370 fval4 = 0 eflag4 = 1 output4 = iterations: 1 funcCount: 2 cgiterations: 1 firstorderopt: 7.5497e-11 algorithm: 'trust-region' message: 'Local minimum found. Optimization completed because the size o...' constrviolation: []```

#### 4.更改容差

```options = optimoptions('fminunc','Algorithm','trust-region',... 'OptimalityTolerance',1e-8,'SpecifyObjectiveGradient',true); % default=1e-6 [xfinal3 fval3 eflag3 output3] = fminunc(@betopt,[-1;2],options) Local minimum found. Optimization completed because the size of the gradient is less than the selected value of the function tolerance. xfinal3 = 1.3333 1.0370 fval3 = 0 eflag3 = 1 output3 = iterations: 15 funcCount: 16 cgiterations: 12 firstorderopt: 7.5497e-11 algorithm: 'trust-region' message: 'Local minimum found. Optimization completed because the size...' constrviolation: []```

`fminunc` 比以前多进行了一次迭代，但得到的解更好。