programming, tech

Linear Programming with Matlab

I found this classical LP example online, and formulate , solve it using Matlab.

  • A calculator company produces a scientific calculator and a graphing calculator. Long-term projections indicate an expected demand of at least 100 scientific and 80 graphing calculators each day. Because of limitations on production capacity, no more than 200 scientific and 170 graphing calculators can be made daily. To satisfy a shipping contract, a total of at least 200 calculators much be shipped each day.
      x: number of scientific calculators produced
      y: number of graphing calculators produced
      R = –2x + 5y, subject to:
      100 < x < 200
      80 < y < 170

      y >x + 200
    If each scientific calculator sold results in a $2 loss, but each graphing calculator produces a $5 profit, how many of each type should be made daily to maximize net profits? 

    The question asks for the optimal number of calculators, so my variables will stand for that:

    Since they can’t produce negative numbers of calculators, I have the two constraints, x > 0 and y > 0. But in this case, I can ignore these constraints, because I already have that x > 100 and y > 80. The exercise also gives maximums: x < 200 and y < 170. The minimum shipping requirement gives me x + y > 200; in other words, y >x + 200. The revenue relation will be my optimization equation: R = –2x + 5y. So the entire system is:

    The feasibility region graphs as:

    When you test the corner points at (100, 170), (200, 170), (200, 80), (120, 80), and (100, 100), you should obtain the maximum value of R = 650 at (x, y) = (100, 170). That is, the solution is “100 scientific calculators and 170 graphing calculators”.


OK, here is the sketch of how a human could use pen and paper to solve it.
It turns out that it’s not that straightforward to convert it into Matlab LP solver, linprog().

Matlab LP solver:

Here is the formulation:
Note that constraints x>=100 and y>=80 screw things up, because the Matlab LP solver only takes Ax<=b , note “<=”.
So, we need a smart formulation:

because the problem is asking “max”, but matlab solver is for “min”, we need to inverse them.

f=[2    -5]

a =
[1     0
-1     0
0     1
0    -1
-1    -1]

b =[200  -100   170   -80  -200]

Now, we could fit into the Matlab LP solver by running


Result is the same as pen and paper solution!

>> linprog(f,a,b)

Optimization terminated.
ans =
100.0000  170.0000


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s