Search code examples
matlabmathematical-optimization

I have a bug in a MATLAB code and I cannot find it


I am trying to make a simple linear programming optimization, using MATLAB R2012b. I came up with this code:

clear;
y=1663;
f=[100; 100; 100; 100; 100; 100; 100; 100];
A=[1/867.2 1/704.7 1/619.2 1/679 1/770.3 1/658.5 1/749 1/783.1
  -105.2 -74.4 -94.3 -88.2 -108.9 999 -112.7 -91.4 
  -94.1 -71.2 -87.6 -87.8 -99.4 999 -100 -81.2 
  0.5 0.6 4.5 3 28 15 4.5 12.5
  0.78 2.11 0.01 0.01 0.56 0.09 0.02 0.9
  93.63 16.3 0.01 0.01 1.44 0.09 0.03 37.64
  0.82 3.23 0.03 0.36 3.13 29.97 0.04 16.16];
b=[y/0.775; -95*y; -85*y; 10*y; 1*y; 35*y; 18*y];
Aeq=[1; 1; 1; 1; 1; 1; 1; 1];
beq=y;
lb=zeros(8,1);
hb=[550; 250; 50; 200; 50; 350; 400];
linprog(f, A, b, Aeq, beq, lb, hb);

When I try to run this source, it says that The number of rows in A must be the same as the number of elements of b.

A has 7 rows and b has 7 elements, as it can be seen. I cannot see the mistake. Where is it?

Thanks.


Solution

  • You have to redefine Aeq. Since you have only one equality constraint, it should be 1xn. I also included x as an output of linprog.

       Aeq=[1, 1, 1, 1, 1, 1, 1, 1];
       x=linprog(f, A, b, Aeq, beq, lb, hb);
    

    The result is

    x =
    
      200.7466
       59.3486
       30.1325
       62.8837
       35.7733
        1.6669
      341.6570
      930.7914