Main Content

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

此示例说明如何通过使用基于问题的方法尝试求解具有约束的非线性方程组。

边界约束

当您的问题只有边界约束时,求解问题的过程就会很简单。例如,求以下方程组的正分量的解

(x1+1)(10-x1)1+x221+x22+x2=0(x2+2)(20-x2)1+x121+x12+x1=0,

只需创建下界为 0 的优化变量。(这些方程有四个解:其中 x1=-1x1=10x2=-2x2=20。)

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

一般约束

当您的问题具有一般约束时,请将问题表示为优化问题,而不是方程问题。将方程设置为等式约束。例如,为了求解受限于非线性不等式约束 x210 的前述方程,请删除 x 的边界,并将问题表示为没有目标函数的优化问题。

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

在这种情况下,最小二乘目标得出与先前公式相同的解,而后者仅使用约束。

关于求解带约束的方程的更多信息

通常,solve 尝试通过最小化方程分量的平方和来求解非线性方程组。换句话说,如果 LHS(i) 是方程 i 的左侧表达式,RHS(i) 是右侧表达式,则 solve 会尝试最小化 sum((LHS RHS).^2)

相比之下,当尝试满足非线性约束表达式时,solve 一般使用 fmincon,并尝试通过使用不同策略来满足约束。

在这两种情况下,求解器都可能无法求解方程。有关在求解器失败时尝试求解的策略,请参阅 fsolve 无法求解方程

另请参阅

相关主题