Dual of the diet problem: a linear programming example, solved and checked
The dual of the diet problem is a classic linear programming problem. Here it is solved by a real solver (HIGHS) and checked: every constraint, the dual, and the optimality certificate. Change the numbers below and solve your own.
Solve your own version free, no account needed
The problem
Three foods cost $3, $4 and $5 per serving. They supply 2, 1 and 1 grams of protein and 1, 2 and 3 grams of fibre per serving. A diet needs at least 8 grams of protein and 10 grams of fibre at minimum cost. Write the dual problem and find the price of each nutrient.
How the model was built
The objective as written from the problem, before the numbers are multiplied out:
- x1, x2 and x3 are servings of the three foods. 2x_1 + x2 + x3 ≥ 8: at least 8 grams of protein; x1 + 2x_2 + 3x_3 ≥ 10: at least 10 grams of fibre.
- The price of a nutrient is how much the cheapest diet's cost rises when 1 more gram of it is required, in $ per gram.
- The price of x2 ≥ 0 is how much the cost would rise for each serving of food 2 the diet were forced to include.
The model
Each line has a plain-language note on the right.
| make as small as possible: total diet cost (dollars) | |
| limit 1: left side must reach at least 8 | |
| limit 2: left side must reach at least 10 | |
| servings of food 1 (servings) cannot be negative | |
| servings of food 2 (servings) cannot be negative | |
| servings of food 3 (servings) cannot be negative |
In matrix form
With its numbers:
The dual problem
Every constraint of the original (primal) problem gets a price, and every primal variable becomes a constraint on those prices. Strong duality: primal and dual optima are equal (20.4).
| Dual variable | Prices | Value |
|---|---|---|
| y1 | price of constraint '2*x1 + x2 + x3 >= 8' (objective change per extra unit of its right-hand side) | 0.8 |
| y2 | price of constraint 'x1 + 2*x2 + 3*x3 >= 10' (objective change per extra unit of its right-hand side) | 1.4 |
What is being decided
There are 3 decision variables.
| Variable | Type | Meaning |
|---|---|---|
| x1 | real number | servings of food 1 (servings) |
| x2 | real number | servings of food 2 (servings) |
| x3 | real number | servings of food 3 (servings) |
Goal and limits
Minimize total diet cost (dollars) subject to 5 constraints, of which 3 are simple bounds on a single variable.
Problem type
LP, complexity level 1: a small linear program. Solved with HIGHS at the Medium solver tier.
The answer at a glance
The optimal value of each decision, as the solver returned it.
Which constraints hold the answer in place
Binding (no slack left): (1) 2*x1 + x2 + x3 >= 8; (2) x1 + 2*x2 + 3*x3 >= 10; (4) x2 >= 0. Changing a binding constraint changes the answer; a slack one can move a little without effect.
| # | Constraint | Value | Limit | Slack | Binding |
|---|---|---|---|---|---|
| 1 | 2*x1 + x2 + x3 >= 8 | 8 | 8 | 0 | yes |
| 2 | x1 + 2*x2 + 3*x3 >= 10 | 10 | 10 | 0 | yes |
| 3 | x1 >= 0 | 2.8 | 0 | 2.8 | |
| 4 | x2 >= 0 | 0 | 0 | 0 | yes |
| 5 | x3 >= 0 | 2.4 | 0 | 2.4 |
Why this is optimal
A proof you can check by hand. Multiply each constraint in the table by its weight and add them up: the result is a lower limit on the objective that holds for every plan meeting the constraints. Our answer reaches that limit exactly, so no plan does better. Every step was checked in exact fractions, not rounded decimals.
| # | Constraint | Weight |
|---|---|---|
| 1 | 2·x1 + x2 + x3 ≥ 8 | 4/5 |
| 2 | x1 + 2·x2 + 3·x3 ≥ 10 | 7/5 |
Algebraic model (low level)
Decision variables, objective and every constraint written out; this is exactly what the solver receives.
Standard form
As a linear program: minimize cᵀx subject to A x ≤ b (the inequality rows) and A_eq x = b_eq (the equations); a maximization is written as minimizing the negative.
Model code (cvxpy)
The same model in cvxpy, ready to run.
import cvxpy as cp
x1 = cp.Variable(name='x1') # servings of food 1 (servings)
x2 = cp.Variable(name='x2') # servings of food 2 (servings)
x3 = cp.Variable(name='x3') # servings of food 3 (servings)
objective = cp.Minimize(3*x1 + 4*x2 + 5*x3)
constraints = [
2*x1 + x2 + x3 >= 8,
x1 + 2*x2 + 3*x3 >= 10,
x1 >= 0,
x2 >= 0,
x3 >= 0,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='HIGHS')
print(problem.status, problem.value)
print('x1 =', x1.value)
print('x2 =', x2.value)
print('x3 =', x3.value)Solve your own
This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).
minimize 3*x1 + 4*x2 + 5*x3
subject to
2*x1 + 1*x2 + 1*x3 >= 8
1*x1 + 2*x2 + 3*x3 >= 10
x1, x2, x3 >= 0
# x1 = servings of food 1 (servings)
# x2 = servings of food 2 (servings)
# x3 = servings of food 3 (servings)
# objective = total diet cost (dollars)
# asked: the dual problem and the price of each nutrient (dual values of the protein and fibre constraints)Checked: the app's answer matches the reference answer in our test set, which two solvers (HIGHS, CLARABEL) agree on. Built 2026-11-10.
Related examples
Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.