使用MATLAB进行一次和二次规划问题求解

对于一次问题,可以使用linprog函数,例如

Find x that minimizes 


f(x) = –5x1 –4x2 –6x3,


subject to


x1 – x2 + x3 ≤20
3x1 +2x2 + 4x3 ≤42
3x1 +2x2 ≤ 30
0≤ x1, 0 ≤ x2,0 ≤ x3.


First, enter the coefficients
f = [-5; -4; -6];
A =  [1 -1  1
      3  2  4
      3  2  0];
b = [20; 42; 30];
lb = zeros(3,1);


Next, call a linear programming routine.
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],lb);


Examine the solution and Lagrange multipliers:
x,lambda.ineqlin,lambda.lower


x = 
     0.0000
    15.0000
     3.0000


ans =
     0.0000
     1.5000
     0.5000


ans =
     1.0000
     0.0000
     0.0000


Nonzero elements of the vectors in the fields of lambda indicateactive constraints at the solution. In this case, the second and thirdinequality constraints (in lambda.ineqlin) andthe first lower bound constraint (in lambda.lower)are active constraints (i.e., the solution is on their constraintboundaries).


对于二次问题,可以使用quadprog函数,例如

Solve a simple quadratic programming problem: find values of x thatminimize






subject to


x1 + x2 ≤2
–x1 +2x2 ≤ 2
2x1 + x2 ≤3
0 ≤ x1,0 ≤ x2.


In matrix notation this is






where 




H可以通过将关于x1,x2的二次项用syms表示,然后调用hessian函数,得到sym的H矩阵,通过double(H)转换为数值型矩阵。


Compute the Hessian of this function of three variables:
syms x y z
f = x*y + 2*z*x;
hessian(f)


ans =
[ 0, 1, 2]
[ 1, 0, 0]
[ 2, 0, 0]



1.Enter the coefficient matrices:
H = [1 -1; -1 2]; 
f = [-2; -6];
A = [1 1; -1 2; 2 1];
b = [2; 2; 3];
lb = zeros(2,1);




2.Set the options to use the 'active-set' algorithmwith no display:
opts = optimoptions('quadprog','Algorithm','active-set','Display','off');




3.Call quadprog:
[x,fval] = quadprog(H,f,A,b,[],[],lb,[],[])




4.Examine the final point, function value, and exitflag:
 x,fval,exitflag


x =
    0.6667
    1.3333


fval =
   -8.2222


exitflag =
     1




An exit flag of 1 means the resultis a local minimum. Because H is a positive definitematrix, this problem is convex, so the minimum is a global minimum.You can see H is positive definite by noting allits eigenvalues are positive:
eig(H)
ans =
    0.3820
    2.6180

你可能感兴趣的:(MATLAB)