# eqnproblem

## 语法

``prob = eqnproblem``
``prob = eqnproblem(Name,Value)``

## 说明

``prob = eqnproblem` 用默认属性创建一个方程问题。`

``prob = eqnproblem(Name,Value)` 使用一个或多个名称-值对组参数指定附加选项。例如，您可以在构造问题时使用 `Equations` 名称来指定方程。`

## 示例

`$\begin{array}{l}\mathrm{exp}\left(-\mathrm{exp}\left(-\left({x}_{1}+{x}_{2}\right)\right)\right)={x}_{2}\left(1+{x}_{1}^{2}\right)\\ {x}_{1}\mathrm{cos}\left({x}_{2}\right)+{x}_{2}\mathrm{sin}\left({x}_{1}\right)=\frac{1}{2}\end{array}$`

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

`eq1 = exp(-exp(-(x(1) + x(2)))) == x(2)*(1 + x(1)^2);`

`eq2 = x(1)*cos(x(2)) + x(2)*sin(x(1)) == 1/2;`

```prob = eqnproblem; prob.Equations.eq1 = eq1; prob.Equations.eq2 = eq2;```

`show(prob)`
``` EquationProblem : Solve for: x eq1: exp(-exp(-(x(1) + x(2)))) == (x(2) .* (1 + x(1).^2)) eq2: ((x(1) .* cos(x(2))) + (x(2) .* sin(x(1)))) == 0.5 ```

`[0,0]` 点开始求解问题。对于基于问题的方法，将初始点指定为结构体，并将变量名称作为结构体的字段。对于此问题，只有一个变量，即 `x`

```x0.x = [0 0]; [sol,fval,exitflag] = solve(prob,x0)```
```Solving problem using fsolve. Equation solved. fsolve 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: eq1: -2.4070e-07 eq2: -3.8255e-08 ```
```exitflag = EquationSolved ```

`disp(sol.x)`
``` 0.3532 0.6061 ```

```ls1 = fcn2optimexpr(@(x)exp(-exp(-(x(1)+x(2)))),x); eq1 = ls1 == x(2)*(1 + x(1)^2); ls2 = fcn2optimexpr(@(x)x(1)*cos(x(2))+x(2)*sin(x(1)),x); eq2 = ls2 == 1/2;```

`x` 是 2×2 矩阵时，方程

`${\mathit{x}}^{3}=\left[\begin{array}{cc}1& 2\\ 3& 4\end{array}\right]$`

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

`eqn = x^3 == [1 2;3 4];`

`prob = eqnproblem('Equations',eqn);`

`[1 1;1 1]` 点开始求解问题。

```x0.x = ones(2); sol = solve(prob,x0)```
```Solving problem using fsolve. Equation solved. fsolve 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: [2x2 double] ```

`disp(sol.x)`
``` -0.1291 0.8602 1.2903 1.1612 ```

`sol.x^3`
```ans = 2×2 1.0000 2.0000 3.0000 4.0000 ```