www.gusucode.com > optim 案例源码 matlab代码程序 > optim/ObtainSolutionAndLagrangeMultipliersExample.m

    %% Obtain Solution and Lagrange Multipliers
% Solve a simple linear program and examine the solution and the Lagrange
% multipliers.
%%
% Use the objective function
%
% $$f(x) = -5x_1-4x_2-6x_3.$$

% Copyright 2015 The MathWorks, Inc.

f = [-5; -4; -6];

%%
% Use the linear inequality constraints
%
% $$ x_1 - x_2 + x_3 \le 20$$
%
% $$ 3x_1 + 2x_2 + 4x_3 \le 42$$
%
% $$ 3x_1 + 2x_2 \le 30.$$
A =  [1 -1  1
      3  2  4
      3  2  0];
b = [20;42;30];

%%
% Constrain all variables to be positive:
%
% $$x_1\ge 0$$
%
% $$x_2\ge 0$$
%
% $$x_3\ge 0.$$
lb = zeros(3,1);

%%
% Set |Aeq| and |beq| to |[]|, indicating that there are no linear equality
% constraints.
Aeq = [];
beq = [];

%%
% Call |linprog|, obtaining the Lagrange multipliers.
[x,fval,exitflag,output,lambda] = linprog(f,A,b,Aeq,beq,lb);
%%
% Examine the solution and Lagrange multipliers.
x,lambda.ineqlin,lambda.lower

%%
% |lambda.ineqlin| is nonzero for the second and third components of |x|.
% This indicates that the second and third linear inequality constraints
% are satisfied with equalities:
%
% $$ 3x_1 + 2x_2 + 4x_3 = 42$$
%
% $$ 3x_1 + 2x_2 = 30.$$
%
% Check that this is true:
A*x

%%
% |lambda.lower| is nonzero for the first component of |x|. This
% indicates that |x(1)| is at its lower bound of 0.