# 基于问题求解有约束非线性问题：

### 问题表示：罗森布罗克函数

`$f\left(x\right)=\mathrm{log}\left(1+100{\left({x}_{2}-{x}_{1}^{2}\right)}^{2}+\left(1-{x}_{1}{\right)}^{2}\right),$`

```rosenbrock = @(x)log(1 + 100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2); % Vectorized function figure1 = figure('Position',[1 200 600 300]); colormap('gray'); axis square; R = 0:.002:1; TH = 2*pi*(0:.002:1); X = R'*cos(TH); Y = R'*sin(TH); Z = rosenbrock([X(:),Y(:)]); % Z = f(x) Z = reshape(Z,size(X)); % Create subplot subplot1 = subplot(1,2,1,'Parent',figure1); view([124 34]); grid('on'); hold on; % Create surface surf(X,Y,Z,'Parent',subplot1,'LineStyle','none'); % Create contour contour(X,Y,Z,'Parent',subplot1); % Create subplot subplot2 = subplot(1,2,2,'Parent',figure1); view([234 34]); grid('on'); hold on % Create surface surf(X,Y,Z,'Parent',subplot2,'LineStyle','none'); % Create contour contour(X,Y,Z,'Parent',subplot2); % Create textarrow annotation(figure1,'textarrow',[0.4 0.31],... [0.055 0.16],... 'String',{'Minimum at (0.7864,0.6177)'}); % Create arrow annotation(figure1,'arrow',[0.59 0.62],... [0.065 0.34]); title("Rosenbrock's Function: Two Views") hold off```

`rosenbrock` 函数句柄同时在任意数量的二维点处计算函数 $f\left(x\right)$。这种向量化可加快函数的绘图速度，在用于加快在多个点的函数计算速度的其他环境中很有用。

### 使用优化变量定义问题

• 对于多项式或三角函数等初等函数，直接使用变量编写表达式。

• 对于其他类型的函数，使用 `fcn2optimexpr` 将函数转换为优化表达式。请参阅此示例末尾的使用 `fcn2optimexpr` 的替代表示。

`x = optimvar('x',1,2);`

`obj = log(1 + 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2);`

`obj` 为目标函数，创建名为 `prob` 的优化问题。

`prob = optimproblem('Objective',obj);`

`nlcons = x(1)^2 + x(2)^2 <= 1;`

`prob.Constraints.circlecons = nlcons;`

`show(prob)`
``` OptimizationProblem : Solve for: x minimize : log(((1 + (100 .* (x(2) - x(1).^2).^2)) + (1 - x(1)).^2)) subject to circlecons: (x(1).^2 + x(2).^2) <= 1 ```

### 求解问题

```x0.x = [0 0]; [sol,fval,exitflag,output] = solve(prob,x0)```
```Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. ```
```sol = struct with fields: x: [0.7864 0.6177] ```
```fval = 0.0447 ```
```exitflag = OptimalSolution ```
```output = struct with fields: iterations: 23 funcCount: 44 constrviolation: 0 stepsize: 7.7862e-09 algorithm: 'interior-point' firstorderopt: 8.0000e-08 cgiterations: 8 message: 'Local minimum found that satisfies the constraints....' bestfeasible: [1x1 struct] objectivederivative: "reverse-AD" constraintderivative: "closed-form" solver: 'fmincon' ```

### 检查解

• 检查在 `output` 结构体的 `constrviolation` 字段中报告的不可行性。

`infeas = output.constrviolation`
```infeas = 0 ```

• 计算在解处的不可行性。

`infeas = infeasibility(nlcons,sol)`
```infeas = 0 ```

• 计算 `x` 的范数，以确保它小于或等于 1。

`nx = norm(sol.x)`
```nx = 1.0000 ```

`output` 结构体提供有关求解过程的详细信息，例如迭代次数（23 次）、求解器 (`fmincon`) 和函数计算次数（44 次）。有关这些统计量的详细信息，请参阅容差和停止条件

### 使用 `fcn2optimexpr` 的替代表示

`type disk`
```function radsqr = disk(x) radsqr = x(1)^2 + x(2)^2; ```

`radsqexpr = fcn2optimexpr(@disk,x);`

`rosenexpr = fcn2optimexpr(rosenbrock,x);`

`convprob = optimproblem('Objective',rosenexpr,'Constraints',radsqexpr <= 1);`

`show(convprob)`
``` OptimizationProblem : Solve for: x minimize : log(((1 + (100 .* (x(2) - x(1).^2).^2)) + (1 - x(1)).^2)) subject to : (x(1).^2 + x(2).^2) <= 1 ```

`[sol,fval,exitflag,output] = solve(convprob,x0)`
```Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. ```
```sol = struct with fields: x: [0.7864 0.6177] ```
```fval = 0.0447 ```
```exitflag = OptimalSolution ```
```output = struct with fields: iterations: 23 funcCount: 44 constrviolation: 0 stepsize: 7.7862e-09 algorithm: 'interior-point' firstorderopt: 8.0000e-08 cgiterations: 8 message: 'Local minimum found that satisfies the constraints....' bestfeasible: [1x1 struct] objectivederivative: "reverse-AD" constraintderivative: "closed-form" solver: 'fmincon' ```