# 将非线性函数转换为优化表达式

### 尽可能使用支持的运算

`$f\left(x,y,r\right)=100\left(y-{x}^{2}{\right)}^{2}+\left(r-x{\right)}^{2}$`

```r = 2; x = optimvar('x'); y = optimvar('y'); f = 100*(y - x^2)^2 + (r - x)^2; prob = optimproblem("Objective",f); x0.x = -1; x0.y = 2; [sol,fval] = solve(prob,x0) ```
```Solving problem using lsqnonlin. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```
```sol = struct with fields: x: 2.0000 y: 4.0000 ```
```fval = 4.4373e-31 ```

`fun = @(x,y)100*(y - x^2)^2 + (r - x)^2;`

```prob.Objective = fcn2optimexpr(fun,x,y); [sol2,fval2] = solve(prob,x0)```
```Solving problem using lsqnonlin. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```
```sol2 = struct with fields: x: 2.0000 y: 4.0000 ```
```fval2 = 4.4373e-31 ```

### 函数文件

`type expfn3.m`
```function [f,g,mineval] = expfn3(u,v) mineval = min(eig(u)); f = v'*u*v; f = -exp(-f); t = u*v; g = t'*t + sum(t) - 3; ```

```u = optimvar('u',3,3,'LowerBound',-1,'UpperBound',1); % 3-by-3 variable v = optimvar('v',3,'LowerBound',-2,'UpperBound',2); % 3-by-1 variable```

`[f,g,mineval] = fcn2optimexpr(@expfn3,u,v);`

`[f,g,mineval] = fcn2optimexpr(@expfn3,u,v,'OutputSize',[1,1],'ReuseEvaluation',true)`
```f = Nonlinear OptimizationExpression [argout,~,~] = expfn3(u, v) ```
```g = Nonlinear OptimizationExpression [~,argout,~] = expfn3(u, v) ```
```mineval = Nonlinear OptimizationExpression [~,~,argout] = expfn3(u, v) ```

### 匿名函数

```fun = @(u)min(eig(u)); funexpr = fcn2optimexpr(fun,u,'OutputSize',[1,1])```
```funexpr = Nonlinear OptimizationExpression arg1 where: anonymousFunction1 = @(u)min(eig(u)); arg1 = anonymousFunction1(u); ```

### 创建目标

```prob = optimproblem; prob.Objective = f;```

### 定义约束

`prob.Constraints.nlcons1 = g <= 0;`

```prob.Constraints.sym = u == u.'; prob.Constraints.mineval = mineval >= -1/2;```

`show(prob)`
``` OptimizationProblem : Solve for: u, v minimize : [argout,~,~] = expfn3(u, v) subject to nlcons1: arg_LHS <= 0 where: [~,arg_LHS,~] = expfn3(u, v); subject to sym: u(2, 1) - u(1, 2) == 0 u(3, 1) - u(1, 3) == 0 -u(2, 1) + u(1, 2) == 0 u(3, 2) - u(2, 3) == 0 -u(3, 1) + u(1, 3) == 0 -u(3, 2) + u(2, 3) == 0 subject to mineval: arg_LHS >= (-0.5) where: [~,~,arg_LHS] = expfn3(u, v); variable bounds: -1 <= u(1, 1) <= 1 -1 <= u(2, 1) <= 1 -1 <= u(3, 1) <= 1 -1 <= u(1, 2) <= 1 -1 <= u(2, 2) <= 1 -1 <= u(3, 2) <= 1 -1 <= u(1, 3) <= 1 -1 <= u(2, 3) <= 1 -1 <= u(3, 3) <= 1 -2 <= v(1) <= 2 -2 <= v(2) <= 2 -2 <= v(3) <= 2 ```

### 求解问题

```rng default % For reproducibility x0.u = 0.25*randn(3); x0.u = x0.u + x0.u.'; x0.v = 2*randn(3,1); [sol,fval,exitflag,output] = solve(prob,x0)```
```Solving problem using fmincon. Feasible point with lower objective function value found, but optimality criteria not satisfied. See output.bestfeasible.. 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: u: [3x3 double] v: [3x1 double] ```
```fval = -403.4288 ```
```exitflag = OptimalSolution ```
```output = struct with fields: iterations: 84 funcCount: 1233 constrviolation: 2.0777e-12 stepsize: 3.3705e-04 algorithm: 'interior-point' firstorderopt: 7.2829e-04 cgiterations: 67 message: 'Local minimum found that satisfies the constraints....' bestfeasible: [1x1 struct] objectivederivative: "finite-differences" constraintderivative: "finite-differences" solver: 'fmincon' ```

`disp(sol.u)`
``` 0.9190 0.5579 -0.8611 0.5579 0.6804 0.6225 -0.8611 0.6225 0.9835 ```
`disp(sol.v)`
``` 2.0000 -2.0000 2.0000 ```