[大谦MATLAB,dqmatlab点com]
【例7】求解下面的最优化问题:
\[\left\{ \begin{matrix} \begin{matrix} \mathrm{\mathrm{min}}{z={x}_{1}^{2}+{x}_{2}^{2}-{2x}_{1}-4{x}_{2}-6{x}_{3}} \\ {x}_{1}+{x}_{2}\leq 2 \end{matrix} \\ {x}_{2}+{x}_{3}\leq3 \\ \begin{matrix} {x}_{1}+{x}_{3}\leq 4 \\ {x}_{j}\geq0, j=1,\ldots,3 \end{matrix} \end{matrix} \right.\]
首先,目标函数可以写成下面的矩阵形式:
\(H=\left[ \begin{matrix} 2 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 0 \end{matrix} \right]\),\(f=\left[ \begin{matrix} -2 \\ -4 \\ -6 \end{matrix} \right]\),\(x=\left[ \begin{matrix} {x}_{1} \\ {x}_{2} \\ {x}_{3} \end{matrix} \right]\)
输入下列系数矩阵:
code.matlab
>> H = [2 0 0;0 2 0;0 0 0];
>> f = [-2; -4; -6];
>> A = [1 1 0; 0 1 1; 1 0 1];
>> b = [2; 3; 4];
>> lb = zeros(3,1);
然后,调用二次规划函数quadprog:
code.matlab
>> [x, fval, exitflag, output] = quadprog(H, f, A, b, [], [], lb)
得问题的解:
code.matlab
x =
1.0000
0.0000
3.0000
fval =
-19.0000
exitflag =
1
output =
包含以下字段的 struct:
message: 'Minimum found that satisfies the constraints…'
algorithm: 'interior-point-convex'
firstorderopt: 2.3467e-09
constrviolation: 0
iterations: 11
linearsolver: 'dense'
cgiterations: []