基于问题求解非线性方程组
要使用基于问题的方法求解非线性方程组
请首先将 x
定义为一个二元素优化变量。
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
如果方程函数不是由初等函数组成的,您必须使用 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;
请参阅Supported Operations for Optimization Variables and Expressions和Convert Nonlinear Function to Optimization Expression。