# 基于问题求解非线性方程组

$\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

### 不受支持的函数要求 fcn2optimexpr

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;