Linear Programming in Matlab help

113 Views Asked by At

I wrote the following code in Matlab in order to solve a problem:

% Problem design.

C = [6 ; 4 ; 7; 5];

A = [1 2 1 2 ; 6 5 3 2 ; 3 4 9 12];

B = [20 ; 100 ; 75 ];

% Our variables X are positive (thus lower bounded by zeros)

% but not upper bounded.

LB = zeros(size(C));

UB = inf(size(C));

% Solve using linprog. Use empty matrices for Aeq and beq because

% we do not have additional equality constraints.

[ X , val ] = linprog(C,A,B,[],[],LB,UB);

% Output results.

disp(X)

disp(val)

However, X becomes the LB vector and as such the solution is 0. This happens for whatever value i set the LB vector to be, the function seems to substitute X for the LB vector. Any help would be appreciated as i thought this code was fine.

2

There are 2 best solutions below

2
On

You are trying to solve the problem

$$\min c^Tx $$

subject to

$$Ax \le b$$

$$x \ge l$$

where every entry of $A$ and $b$ and $c$ are nonnegative.

Hence the smaller each component of $x$ is, the more optimal is the solution.

Edit:

note that $\max c^Tx = - \min (-c)^Tx$

0
On

So the linear program function in matlab always finds the minimum of your objective function as seen here. Looks like you set up everything right to find the minimum optimal answer, but if you are trying to find the maximum, you need to multiply your objective function by a negative because:

$max$ $c^Tx = -min -c^Tx$