Sugihara
Sugihara

Reputation: 1101

Setting up for linprog in Matlab

I am having trouble setting up the follow type of constraints for linprog in Matlab

Max 9x1 + 8x2 + 7x3
Subject to: 2 <= x1 + x2 <= 3
            4 <= x2 + x3 <= 5
            x1 >= 0, x2 >= 0, x3 >= 0

Following mathwork's doc

A = [ 1 1
      1 1
      1
      1
      1 ]
f = [ 9 8 7 ]
b = ???
lb = [ 2 4 ]
ub = [ 3 5 ]

I don't know how to set up b since the example given on the website isn't bounded.

Also how does (if needed) the lb and ub take into account of x1 >= 0, x2 >=0, and x3 >=3?

Upvotes: 2

Views: 892

Answers (2)

dubafek
dubafek

Reputation: 1113

According to the linprog function, there are 3 forms of specifying constraints:

  1. Inequality constraints. Those like x1+x2 <= 3. For these constraints you must write all of them as a linear combination between the variables less than a constant. So finally you write them like Ax <= b.

  2. Equality constrains. Those like x1+x2 = 3. This case is like the above but instead of the less than sign, you use an equal sign. So your equality constraints will be represented as Aeq x = beq.

  3. Nature of variables. In these constraints you bound your variables. So here you write 0 <= x1 <= Inf as lb <= x <= ub.

So you should write your problem as:

Max 9 x1 + 8 x2 + 7 x3
Subject to:
        - x1 - x2 <= -2
        x1 + x2   <= 3
        -x2 - x3  <= -4
        x2 + x3   <= 5

        0x1 + 0x2 + 0x2 = 0

        0 <= x1 <= Inf
        0 <= x2 <= Inf
        0 <= x3 <= Inf

Now that you rewritten it you define your Matlab variables:

A = [-1 -1 0; 1 1 0; 0 -1 -1; 0 1 1]
b = [-2; 3; -4; 5]
Aeq = zeros(1,3)
beq = 0
lb = [0; 0; 0]
ub = [Inf; Inf; Inf]
f = [9 8 7]

Hope it helps you. Greetings

EDIT: linprog minimize so f must be [-9 -8 -7]

Upvotes: 4

Phil Goddard
Phil Goddard

Reputation: 10782

The MathWorks doc that you link to shows several example of setting upper and lower bounds, and the code you show doesn't follow the doc. Your A isn't a valid matrix and your lb and ub don't have enough elements (and contain the wrong numbers).

You want

A = [1 1 0;-1 -1 0;0 1 1;0 -1 -1];
b = [3;-2;5;-4];
lb = [0;0;0];
ub = [inf;inf;inf];

Upvotes: 1

Related Questions