Gimpy
Gimpy

Reputation: 21

Optimization giving unexpected results MATLAB

I'm trying to optimize a function which I know the results but matlab is giving me weird results. Here's what I'm trying to do:

max: f(x)= -1815·x1 - 379·x2

subject to: 

    -1475·x1 - 112013·x2 >= -700000
    (x1,x2) <= 80
    (x1,x2) >= 0

Here is my actual code:

f  = [1815;379]
A  = [-1475 -11203]
b  = [-700000]
ub = (ones(1,2)*80)'
lb = zeros(2,1)
x  = linprog(f,A,b,[],[],lb,ub)

How would you do it?

Upvotes: 2

Views: 65

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38052

Your linear constraint has incorrect sign w.r.t. how it's expected by linprog.

As with many linear problems, it's actually easiest to just make a plot:

[x1,x2] = meshgrid(0:80);
f = -1815*x1 - 379*x2;
f(-1475*x1 - 112013*x2 < -7e5) = NaN;
surf(x1,x2,f, 'edgecolor', 'none')
xlabel('x1'), ylabel('x2')

This makes it obvious that (0,0) is the solution:

enter image description here

Upvotes: 2

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

This problem can easily be solved analitically.

As mentioned in the comments, you currently would expect 0. If however, you actually change your constraint from larger than, into smaller than, the optimum solution is actually close what matlab gives you.

It would basically be 700000/112013 = 6.248...

It is off by a factor 10, but I assume that you made a typo somewhere.


If you are struggeling with how this function works, just try a simple case first (that you can easily verify manually) and then increase the complexity. Either way, your excel solution is nowhere near what would come out of the problem description.

Upvotes: 2

Related Questions