# Optimization Toolbox 教程

• 最小化一个目标函数

• 最小化带附加参数的同一函数

• 最小化带约束的目标函数

• 通过提供梯度或黑塞矩阵，或通过更改选项来获得更高效或更准确的解。

### 无约束优化示例

`$x\mathrm{exp}\left(-\left({x}^{2}+{y}^{2}\right)\right)+\left({x}^{2}+{y}^{2}\right)/20.$`

```f = @(x,y) x.*exp(-x.^2-y.^2)+(x.^2+y.^2)/20; fsurf(f,[-2,2],'ShowContours','on')```

`fun = @(x) f(x(1),x(2));`

`x0 = [-.5; 0];`

`options = optimoptions('fminunc','Algorithm','quasi-newton');`

`options.Display = 'iter';`

`[x, fval, exitflag, output] = fminunc(fun,x0,options);`
``` First-order Iteration Func-count f(x) Step-size optimality 0 3 -0.3769 0.339 1 6 -0.379694 1 0.286 2 9 -0.405023 1 0.0284 3 12 -0.405233 1 0.00386 4 15 -0.405237 1 3.17e-05 5 18 -0.405237 1 3.35e-08 Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```

`uncx = x`
```uncx = 2×1 -0.6691 0.0000 ```

`uncf = fval`
```uncf = -0.4052 ```

`output.funcCount`
```ans = 18 ```

### 带附加参数的无约束优化示例

`$f\left(x,y\right)=x\mathrm{exp}\left(-\left({x}^{2}+{y}^{2}\right)\right)+\left({x}^{2}+{y}^{2}\right)/20.$`

`$f\left(x,y,a,b,c\right)=\left(x-a\right)\mathrm{exp}\left(-\left(\left(x-a{\right)}^{2}+\left(y-b{\right)}^{2}\right)\right)+\left(\left(x-a{\right)}^{2}+\left(y-b{\right)}^{2}\right)/c.$`

#### MATLAB 文件函数

`type bowlpeakfun`
```function y = bowlpeakfun(x, a, b, c) %BOWLPEAKFUN Objective function for parameter passing in TUTDEMO. % Copyright 2008 The MathWorks, Inc. y = (x(1)-a).*exp(-((x(1)-a).^2+(x(2)-b).^2))+((x(1)-a).^2+(x(2)-b).^2)/c; ```

```a = 2; b = 3; c = 10;```

`f = @(x)bowlpeakfun(x,a,b,c)`
```f = function_handle with value: @(x)bowlpeakfun(x,a,b,c) ```

```x0 = [-.5; 0]; options = optimoptions('fminunc','Algorithm','quasi-newton'); [x, fval] = fminunc(f,x0,options)```
```Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```
```x = 2×1 1.3639 3.0000 ```
```fval = -0.3840 ```

#### 嵌套函数

`type nestedbowlpeak`
```function [x,fval] = nestedbowlpeak(a,b,c,x0,options) %NESTEDBOWLPEAK Nested function for parameter passing in TUTDEMO. % Copyright 2008 The MathWorks, Inc. [x,fval] = fminunc(@nestedfun,x0,options); function y = nestedfun(x) y = (x(1)-a).*exp(-((x(1)-a).^2+(x(2)-b).^2))+((x(1)-a).^2+(x(2)-b).^2)/c; end end ```

```a = 2; b = 3; c = 10; x0 = [-.5; 0]; options = optimoptions('fminunc','Algorithm','quasi-newton');```

`[x,fval] = nestedbowlpeak(a,b,c,x0,options)`
```Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```
```x = 2×1 1.3639 3.0000 ```
```fval = -0.3840 ```

### 约束优化示例：不相等性

```f = @(x,y) x.*exp(-x.^2-y.^2)+(x.^2+y.^2)/20; g = @(x,y) x.*y/2+(x+2).^2+(y-2).^2/2-2; fimplicit(g) axis([-6 0 -1 7]) hold on fcontour(f) plot(-.9727,.4685,'ro'); legend('constraint','f contours','minimum'); hold off```

`x0 = [-2 1];`

`options = optimoptions('fmincon','Algorithm','interior-point','Display','iter');`

`gfun = @(x) deal(g(x(1),x(2)),[]);`

`[x,fval,exitflag,output] = fmincon(fun,x0,[],[],[],[],[],[],gfun,options);`
``` First-order Norm of Iter F-count f(x) Feasibility optimality step 0 3 2.365241e-01 0.000e+00 1.972e-01 1 6 1.748504e-01 0.000e+00 1.734e-01 2.260e-01 2 10 -1.570560e-01 0.000e+00 2.608e-01 9.347e-01 3 14 -6.629160e-02 0.000e+00 1.241e-01 3.103e-01 4 17 -1.584082e-01 0.000e+00 7.934e-02 1.826e-01 5 20 -2.349124e-01 0.000e+00 1.912e-02 1.571e-01 6 23 -2.255299e-01 0.000e+00 1.955e-02 1.993e-02 7 26 -2.444225e-01 0.000e+00 4.293e-03 3.821e-02 8 29 -2.446931e-01 0.000e+00 8.100e-04 4.035e-03 9 32 -2.446933e-01 0.000e+00 1.999e-04 8.126e-04 10 35 -2.448531e-01 0.000e+00 4.004e-05 3.289e-04 11 38 -2.448927e-01 0.000e+00 4.036e-07 8.156e-05 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. ```

`x`
```x = 1×2 -0.9727 0.4686 ```

`fval`
```fval = -0.2449 ```

`Fevals = output.funcCount`
```Fevals = 38 ```

`[c, ceq] = gfun(x)`
```c = -2.4608e-06 ```
```ceq = [] ```

`uncx`
```uncx = 2×1 -0.6691 0.0000 ```

`uncf`
```uncf = -0.4052 ```

`fval-uncf`
```ans = 0.1603 ```

### 约束优化示例：用户提供的梯度

`type onehump`
```function [f,gf] = onehump(x) % ONEHUMP Helper function for Tutorial for the Optimization Toolbox demo % Copyright 2008-2009 The MathWorks, Inc. r = x(1)^2 + x(2)^2; s = exp(-r); f = x(1)*s+r/20; if nargout > 1 gf = [(1-2*x(1)^2)*s+x(1)/10; -2*x(1)*x(2)*s+x(2)/10]; end ```

`type tiltellipse`
```function [c,ceq,gc,gceq] = tiltellipse(x) % TILTELLIPSE Helper function for Tutorial for the Optimization Toolbox demo % Copyright 2008-2009 The MathWorks, Inc. c = x(1)*x(2)/2 + (x(1)+2)^2 + (x(2)-2)^2/2 - 2; ceq = []; if nargout > 2 gc = [x(2)/2+2*(x(1)+2); x(1)/2+x(2)-2]; gceq = []; end ```

`x0 = [-2; 1];`

`options = optimoptions('fmincon','Algorithm','interior-point');`

```options = optimoptions(options,... 'SpecifyObjectiveGradient',true,... 'SpecifyConstraintGradient',true);```

`options.Display = 'iter';`

```[x,fval,exitflag,output] = fmincon(@onehump,x0,[],[],[],[],[],[], ... @tiltellipse,options);```
``` First-order Norm of Iter F-count f(x) Feasibility optimality step 0 1 2.365241e-01 0.000e+00 1.972e-01 1 2 1.748504e-01 0.000e+00 1.734e-01 2.260e-01 2 4 -1.570560e-01 0.000e+00 2.608e-01 9.347e-01 3 6 -6.629161e-02 0.000e+00 1.241e-01 3.103e-01 4 7 -1.584082e-01 0.000e+00 7.934e-02 1.826e-01 5 8 -2.349124e-01 0.000e+00 1.912e-02 1.571e-01 6 9 -2.255299e-01 0.000e+00 1.955e-02 1.993e-02 7 10 -2.444225e-01 0.000e+00 4.293e-03 3.821e-02 8 11 -2.446931e-01 0.000e+00 8.100e-04 4.035e-03 9 12 -2.446933e-01 0.000e+00 1.999e-04 8.126e-04 10 13 -2.448531e-01 0.000e+00 4.004e-05 3.289e-04 11 14 -2.448927e-01 0.000e+00 4.036e-07 8.156e-05 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. ```

`fmincon` 在前面的示例中对梯度进行了很好的估计，因此在此示例中的迭代与之相似。

`xold = x`
```xold = 2×1 -0.9727 0.4686 ```

`minfval = fval`
```minfval = -0.2449 ```

`Fgradevals = output.funcCount`
```Fgradevals = 14 ```

`Fevals`
```Fevals = 38 ```

### 约束优化示例：更改默认终止容差

$\text{subject}\text{}\text{to}\text{}xy/2+\left(x+2{\right)}^{2}+\left(y-2{\right)}^{2}/2\le 2$.

```options = optimoptions(options,... 'StepTolerance',1e-15,... 'OptimalityTolerance',1e-8);```

```[x,fval,exitflag,output] = fmincon(@onehump,x0,[],[],[],[],[],[], ... @tiltellipse,options);```
``` First-order Norm of Iter F-count f(x) Feasibility optimality step 0 1 2.365241e-01 0.000e+00 1.972e-01 1 2 1.748504e-01 0.000e+00 1.734e-01 2.260e-01 2 4 -1.570560e-01 0.000e+00 2.608e-01 9.347e-01 3 6 -6.629161e-02 0.000e+00 1.241e-01 3.103e-01 4 7 -1.584082e-01 0.000e+00 7.934e-02 1.826e-01 5 8 -2.349124e-01 0.000e+00 1.912e-02 1.571e-01 6 9 -2.255299e-01 0.000e+00 1.955e-02 1.993e-02 7 10 -2.444225e-01 0.000e+00 4.293e-03 3.821e-02 8 11 -2.446931e-01 0.000e+00 8.100e-04 4.035e-03 9 12 -2.446933e-01 0.000e+00 1.999e-04 8.126e-04 10 13 -2.448531e-01 0.000e+00 4.004e-05 3.289e-04 11 14 -2.448927e-01 0.000e+00 4.036e-07 8.156e-05 12 15 -2.448931e-01 0.000e+00 4.000e-09 8.230e-07 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. ```

`format long`

`x`
```x = 2×1 -0.972742227363546 0.468569289098342 ```

`xold`
```xold = 2×1 -0.972742694488360 0.468569966693330 ```

`x - xold`
```ans = 2×1 10-6 × 0.467124813385844 -0.677594988729435 ```

`fval`
```fval = -0.244893137879894 ```

`fval - minfval`
```ans = -3.996450220755676e-07 ```

`output.funcCount`
```ans = 15 ```

`Fgradevals`
```Fgradevals = 14 ```

### 约束优化示例：用户提供的黑塞矩阵

`fmincon` 的内点算法将黑塞矩阵作为一个单独的函数（不是目标函数的一部分）。黑塞函数 H(x,lambda) 计算拉格朗日的黑塞矩阵；请参阅 适用于 fmincon 内点算法的黑塞函数

`type hessfordemo`
```function H = hessfordemo(x,lambda) % HESSFORDEMO Helper function for Tutorial for the Optimization Toolbox demo % Copyright 2008-2009 The MathWorks, Inc. s = exp(-(x(1)^2+x(2)^2)); H = [2*x(1)*(2*x(1)^2-3)*s+1/10, 2*x(2)*(2*x(1)^2-1)*s; 2*x(2)*(2*x(1)^2-1)*s, 2*x(1)*(2*x(2)^2-1)*s+1/10]; hessc = [2,1/2;1/2,1]; H = H + lambda.ineqnonlin(1)*hessc; ```

```options = optimoptions('fmincon',... 'Algorithm','interior-point',... 'SpecifyConstraintGradient',true,... 'SpecifyObjectiveGradient',true,... 'HessianFcn',@hessfordemo);```

`options.Display = 'iter';`

```[x,fval,exitflag,output] = fmincon(@onehump,x0,[],[],[],[],[],[], ... @tiltellipse,options);```
``` First-order Norm of Iter F-count f(x) Feasibility optimality step 0 1 2.365241e-01 0.000e+00 1.972e-01 1 3 5.821325e-02 0.000e+00 1.443e-01 8.728e-01 2 5 -1.218829e-01 0.000e+00 1.007e-01 4.927e-01 3 6 -1.421167e-01 0.000e+00 8.486e-02 5.165e-02 4 7 -2.261916e-01 0.000e+00 1.989e-02 1.667e-01 5 8 -2.433609e-01 0.000e+00 1.537e-03 3.486e-02 6 9 -2.446875e-01 0.000e+00 2.057e-04 2.727e-03 7 10 -2.448911e-01 0.000e+00 2.068e-06 4.191e-04 8 11 -2.448931e-01 0.000e+00 2.001e-08 4.218e-06 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. ```

`x`
```x = 2×1 -0.972742246093537 0.468569316215571 ```

`fval`
```fval = -0.244893121872758 ```

`output.funcCount`
```ans = 11 ```

`Fgradevals`
```Fgradevals = 14 ```