# 基于问题的有约束的非线性方程组

### 边界约束

`$\begin{array}{l}\left({\mathit{x}}_{1}+1\right)\left(10-{\mathit{x}}_{1}\right)\frac{1+{\mathit{x}}_{2}^{2}}{1+{\mathit{x}}_{2}^{2}+{\mathit{x}}_{2}}=0\\ \left({\mathit{x}}_{2}+2\right)\left(20-{\mathit{x}}_{2}\right)\frac{1+{\mathit{x}}_{1}^{2}}{1+{\mathit{x}}_{1}^{2}+{\mathit{x}}_{1}}=0,\end{array}$`

```x = optimvar('x',2,"LowerBound",0); expr1 = (x(1) + 1)*(10 - x(1))*((1 + x(2)^2))/(1 + x(2)^2 + x(2)); expr2 = (x(2) + 2)*(20 - x(2))*((1 + x(1)^2))/(1 + x(1)^2 + x(1)); eqn1 = expr1 == 0; eqn2 = expr2 == 0; prob = eqnproblem; prob.Equations.eqn1 = eqn1; prob.Equations.eqn2 = eqn2; x0.x = [15,15]; [sol,fval,exitflag] = solve(prob,x0)```
```Equation problem has bound constraints. Reformulating as a least squares problem. Solving problem using lsqnonlin. Equation solved. lsqnonlin 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. ```
```sol = struct with fields: x: [2x1 double] ```
```fval = struct with fields: eqn1: 0 eqn2: 0 ```
```exitflag = EquationSolved ```

`sol.x`
```ans = 2×1 10 20 ```

### 一般约束

```x.LowerBound = []; circlecons = x(1)^2 + x(2)^2 <= 10; prob2 = optimproblem; prob2.Constraints.circlecons = circlecons; prob2.Constraints.eqn1 = eqn1; prob2.Constraints.eqn2 = eqn2; [sol2,fval2,exitflag2] = solve(prob2,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. ```
```sol2 = struct with fields: x: [2x1 double] ```
```fval2 = 0 ```
```exitflag2 = OptimalSolution ```

`sol2.x`
```ans = 2×1 -1.0000 -2.0000 ```

### 使用最小二乘目标的一般约束

```prob3 = optimproblem; prob3.Objective = expr1^2 + expr2^2; prob3.Constraints.circlecons = circlecons; [sol3,fval3,exitflag3] = solve(prob3,x0)```
```Solving problem using lsqnonlin. 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. ```
```sol3 = struct with fields: x: [2x1 double] ```
```fval3 = 8.5058e-16 ```
```exitflag3 = OptimalSolution ```

`sol3.x`
```ans = 2×1 -1.0000 -2.0000 ```