Code a linear programming exercise by hand
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Solving a linear programming problem by hand is a good way to understand what an optimizer is actually doing. For two-variable exercises, the usual manual method is graphical: plot the constraints, find the feasible region, identify its corner points, and evaluate the objective at those points.
This does not replace simplex or a solver. It teaches the geometry behind them. Once you see why the optimum sits on a boundary point, the automated methods stop feeling mysterious.
Write the Model Clearly First
A linear programming problem has three parts:
- decision variables
- a linear objective
- linear constraints
Consider this example:
- maximize
Z = 3x + 2y - subject to
x + y <= 6 - '
x <= 4' - '
y <= 3' - '
x >= 0andy >= 0'
The first thing to do by hand is rewrite each inequality as a boundary line:
- '
x + y = 6' - '
x = 4' - '
y = 3'
Those lines define the edges. The inequality signs tell you which side of each line is allowed.
Find the Feasible Region
Because x and y must both be non-negative, the solution must lie in the first quadrant. The other constraints carve out a polygon inside that quadrant.
For this problem, the feasible corner points are:
- '
(0, 0)' - '
(4, 0)' - '
(4, 2)' - '
(3, 3)' - '
(0, 3)'
You get these points by intersecting boundary lines and then checking which intersections satisfy every constraint. For example:
- '
x = 4andx + y = 6gives(4, 2)' - '
y = 3andx + y = 6gives(3, 3)'
Not every line intersection is feasible. That is why the checking step matters.
Evaluate the Objective at the Corner Points
For a bounded two-variable problem like this, the optimum occurs at a corner point of the feasible region. So evaluate the objective at each vertex.
The hand calculation gives:
- '
(0, 0)gives0' - '
(4, 0)gives12' - '
(4, 2)gives16' - '
(3, 3)gives15' - '
(0, 3)gives6'
So the maximum value is 16, reached at (4, 2).
The code is only a check. The real solution came from the geometry and the arithmetic.
Know Why This Method Works
Linear objectives change at a constant rate, and the feasible region formed by linear constraints is convex. In a two-variable bounded problem, that means the best value appears at one of the extreme points rather than somewhere in the middle.
That is why you do not need to test every point in the region by hand. The corner points contain all the candidates that matter.
This is also the bridge to more advanced methods. Simplex is, in effect, a systematic way of moving from one extreme point to another until it finds the optimum in higher dimensions.
Common Pitfalls
The biggest mistake is evaluating the objective at points that are not actually feasible. Every candidate point must satisfy all of the inequalities, not just the two lines used to generate it.
Another common issue is forgetting the non-negativity constraints. Those often remove regions that would otherwise look valid on the graph.
It is also easy to mix up maximization and minimization. The arithmetic can be right while the final answer is wrong because you chose the smallest value instead of the largest one.
Finally, do not assume every problem has one unique optimum at one corner. Some problems are unbounded, some are infeasible, and some have multiple optimal points along an edge.
Summary
- Start by writing the objective and constraints clearly.
- Convert inequalities into boundary lines so you can graph the feasible region.
- Find the feasible corner points and check them against all constraints.
- Evaluate the objective at those points to identify the optimum.
- Use hand calculation to understand the geometry, then use simplex or software for larger models.

