创建一个名为 x 的 4×6 优化变量矩阵。
x = optimvar('x',4,6);
创建 x 的每行总和为 1 的等式。
constrsum = sum(x,2) == 1
constrsum =
4x1 Linear OptimizationEquality array with properties:
IndexNames: {{} {}}
Variables: [1x1 struct] containing 1 OptimizationVariable
See equality formulation with show.
查看等式。
show(constrsum)
(1, 1)
x(1, 1) + x(1, 2) + x(1, 3) + x(1, 4) + x(1, 5) + x(1, 6) == 1
(2, 1)
x(2, 1) + x(2, 2) + x(2, 3) + x(2, 4) + x(2, 5) + x(2, 6) == 1
(3, 1)
x(3, 1) + x(3, 2) + x(3, 3) + x(3, 4) + x(3, 5) + x(3, 6) == 1
(4, 1)
x(4, 1) + x(4, 2) + x(4, 3) + x(4, 4) + x(4, 5) + x(4, 6) == 1
要在优化问题中包含等式,请使用圆点表示法将 Constraints 属性设置为 constrsum。
prob = optimproblem;
prob.Constraints.constrsum = constrsum
prob =
OptimizationProblem with properties:
Description: ''
ObjectiveSense: 'minimize'
Variables: [1x1 struct] containing 1 OptimizationVariable
Objective: [0x0 OptimizationExpression]
Constraints: [1x1 struct] containing 1 OptimizationConstraint
See problem formulation with show.
同样,要在方程问题中包含等式,请使用圆点表示法将 Constraints 属性设置为 constrsum。
eqnprob = eqnproblem;
eqnprob.Equations.constrsum = constrsum
eqnprob =
EquationProblem with properties:
Description: ''
Variables: [1x1 struct] containing 1 OptimizationVariable
Equations: [1x1 struct] containing 1 OptimizationEquality
See problem formulation with show.