34. Optimization: building the model, not just solving it

📖 Reading · 15 min
💡 Every code box below is live — edit it and hit Run.

Find the best design, the cheapest route, the largest volume. This is what calculus is for outside mathematics, and it's the direct ancestor of §14's gradient descent.

The calculus part — set the derivative to zero — is the easy part. Setting up the problem is the hard part, and where nearly all the errors live.

The procedure

  1. Identify what's being optimized. Write it as the objective function.
  2. Identify the constraint. There is always one, or the answer is infinite.
  3. Reduce to one variable by substituting the constraint into the objective.
  4. State the domain. Physically meaningful values only.
  5. Differentiate, set to zero, solve.
  6. Check it's the extremum you want — second derivative test, or endpoints.
  7. Answer the question that was asked.

Steps 2–4 are the modelling. Step 5 is one line. Step 7 sounds trivial and is routinely botched — the question often asks for a dimension when you solved for a different one.

The box

A 20 cm square of cardboard has squares of side x cut from each corner; the flaps fold up into an open box. What x maximises the volume?

Objective. Base is (20-2x) on a side, height is x:

V(x) = x(20-2x)^2

Constraint is already absorbed — the sheet's size fixed the base in terms of x.

Domain. x>0 for a box to exist, and 20-2x>0 so the base has positive size. So 0 < x < 10. Including the endpoints [0,10] is convenient: both give V=0, so the EVT applies and the maximum must be interior.

Differentiate. Expand first:

V = x(400 - 80x + 4x^2) = 400x - 80x^2 + 4x^3

V' = 400 - 160x + 12x^2 = 4(3x^2 - 40x + 100) = 4(3x-10)(x-10)

Solve. x = \frac{10}{3} or x=10. The second is on the boundary and gives V=0, so:

x = \frac{10}{3} \approx 3.33 \text{ cm}

Verify. V'' = -160 + 24x, and V''(10/3) = -160+80 = -80 < 0 → maximum ✓.

V_{\max} = \frac{10}{3}\left(20 - \frac{20}{3}\right)^2 = \frac{10}{3}\cdot\frac{1600}{9} = \frac{16000}{27} \approx 592.6\text{ cm}^3

The general result is prettier than the number. For a square sheet of side L, the optimum is always x = \frac L6 — check: \frac{20}{6} = \frac{10}{3} ✓. Whenever a specific answer looks arbitrary, redo it with a letter; the structure usually appears.

The can

A cylindrical can must hold 355 mL. What dimensions minimise the material?

Objective: surface area A = 2\pi r^2 + 2\pi rh (two ends plus the side).

Constraint: V = \pi r^2h = 355.

Reduce. From the constraint, h = \frac{355}{\pi r^2}:

A(r) = 2\pi r^2 + 2\pi r\cdot\frac{355}{\pi r^2} = 2\pi r^2 + \frac{710}{r}

Domain: r>0, unbounded. No endpoints, so the second derivative test is required rather than optional.

Differentiate:

A'(r) = 4\pi r - \frac{710}{r^2} = 0 \implies r^3 = \frac{710}{4\pi} \implies r = \sqrt[3]{\frac{710}{4\pi}} \approx 3.837\text{ cm}

h = \frac{355}{\pi(3.837)^2} \approx 7.675\text{ cm}

Verify: A'' = 4\pi + \frac{1420}{r^3} > 0 for all r>0 — concave up everywhere, so this is the global minimum ✓.

The structure: h = 2r. The optimal can is exactly as tall as it is wide — its height equals its diameter. Doing it symbolically shows this holds for any volume, which the decimals conceal.

Real cans aren't shaped this way, and that's informative: the model assumed material cost is proportional to area with the same cost per unit everywhere. Actual cans have thicker, double-seamed ends, and shelf presentation matters. The mathematics is right; the model is a simplification. Knowing which is which is the point of modelling.

Where these go wrong

Forgetting the constraint. "Maximise the volume of a box" has no answer without one. If your objective has no upper bound, you've dropped a constraint.

Not reducing to one variable. Two variables and one equation means you haven't substituted yet. (Two variables and no way to eliminate one means you need §10.7's Lagrange multipliers.)

Ignoring the domain. Negative lengths, and x > 10 in the box problem, are solutions to the algebra that aren't solutions to the problem.

Skipping the verification. f'=0 finds critical points, not maxima. §3.5 was explicit that a horizontal tangent proves nothing on its own.

Answering the wrong question. "What's the minimum cost?" wants a cost, not the radius that achieves it.

Doing it in Python

The box, solved symbolically:

import sympy as sp

x = sp.Symbol('x', positive=True)
V = x * (20 - 2*x)**2

crit = sp.solve(sp.diff(V, x), x)
print(f"V(x)  = {sp.expand(V)}")
print(f"V'(x) = {sp.factor(sp.diff(V, x))}")
print(f"critical points: {crit}\n")

for c in crit:
    second = sp.diff(V, x, 2).subs(x, c)
    print(f"  x={c}: V={V.subs(x,c)} ({float(V.subs(x,c)):.2f}), "
          f"V''={second} -> {'max' if second < 0 else 'min'}")

L = sp.Symbol('L', positive=True)
Vgen = x * (L - 2*x)**2
print(f"\nfor a sheet of side L: x = {sp.solve(sp.diff(Vgen, x), x)}")

The can, and its hidden structure:

import sympy as sp

r, V = sp.symbols('r V', positive=True)
h = V / (sp.pi * r**2)
A = 2*sp.pi*r**2 + 2*sp.pi*r*h

r_opt = sp.solve(sp.diff(A, r), r)[0]
h_opt = sp.simplify(h.subs(r, r_opt))

print(f"A(r)      = {sp.simplify(A)}")
print(f"optimal r = {r_opt}")
print(f"optimal h = {h_opt}")
print(f"h / r     = {sp.simplify(h_opt / r_opt)}   <- height equals the diameter")

print(f"\nfor V = 355 mL:")
print(f"  r = {float(r_opt.subs(V, 355)):.4f} cm")
print(f"  h = {float(h_opt.subs(V, 355)):.4f} cm")
print(f"  A = {float(A.subs({r: r_opt, V: 355}).subs(V, 355)):.4f} cm^2")

Scanning the objective to see the optimum is real:

def volume(x):
    return x * (20 - 2*x)**2

print(f"{'x':>7} {'V(x)':>12}")
for x in (1, 2, 3, 10/3, 3.5, 4, 5, 7, 9):
    marker = "  <- optimum" if abs(x - 10/3) < 1e-9 else ""
    print(f"{x:>7.3f} {volume(x):>12.3f}{marker}")

print("\nthe peak really is at x = 10/3, and the curve is flat near it --")
print("which is why a 5% error in x costs less than 1% of the volume")

Why the optimum is insensitive — a genuinely useful engineering fact:

def volume(x):
    return x * (20 - 2*x)**2

best = 10/3
vbest = volume(best)

print(f"{'x error':>10} {'x':>10} {'V':>12} {'V shortfall':>14}")
for pct in (0.01, 0.05, 0.10, 0.20):
    x = best * (1 + pct)
    print(f"{pct:>9.0%} {x:>10.4f} {volume(x):>12.3f} {1 - volume(x)/vbest:>13.4%}")

print("\na 10% error in x costs 1% of the volume. near a maximum f' = 0,")
print("so the loss is second order -- exactly the linearization fact from 3.1")

Worked example

Find the point on the parabola y = x^2 closest to the point (0, 3).

Objective: distance from (x, x^2) to (0,3):

D = \sqrt{x^2 + (x^2-3)^2}

Simplify before differentiating. Minimising D is the same as minimising D^2, since \sqrt{\cdot} is increasing — the same x achieves both. That kills the square root and all the chain-rule mess:

S(x) = x^2 + (x^2-3)^2

This trick is worth internalising. Whenever the objective is a distance, optimize the squared distance. It's used everywhere from least squares (§14.5) to nearest-neighbour search.

Differentiate:

S'(x) = 2x + 2(x^2-3)(2x) = 2x\left[1 + 2(x^2-3)\right] = 2x(2x^2-5)

Solve: x=0 or x^2 = \frac52, i.e. x = \pm\sqrt{2.5} \approx \pm1.581.

Evaluate all three:

  • x=0: S = 0 + 9 = 9, so D = 3
  • x = \pm\sqrt{2.5}: S = 2.5 + (2.5-3)^2 = 2.5+0.25 = 2.75, so D = \sqrt{2.75}\approx1.658

The closest points are \left(\pm\sqrt{2.5}, 2.5\right), at distance \approx1.658.

Two things worth noticing.

There are two closest points — the problem is symmetric in x, so the answer must be. If you'd found only one you'd have missed a solution.

And x=0, the vertex, is a local maximum of the distance among the critical points — the point directly below (0,3) is the farthest nearby point, not the nearest. Anyone who assumed the vertex was closest without checking would have been confidently wrong, which is exactly why step 6 exists.

Your turn

1. Find two positive numbers with sum 20 whose product is maximal.

2. A rectangular pen against a straight river wall (no fence needed on that side) uses 100 m of fence. What are the dimensions of maximum area?

3. Minimise the cost of a rectangular open-top box with volume 32 m³ and a square base, if the base costs $4/m² and the sides $2/m².

Solutions

1. With x + y = 20, so y = 20-x:

P(x) = x(20-x) = 20x - x^2, \qquad 0 < x < 20

P'(x) = 20-2x = 0 \implies x = 10

P'' = -2 < 0 → maximum ✓. So \boxed{x=y=10}, product 100.

The general fact: for a fixed sum, the product is maximised when the numbers are equal. That's the AM–GM inequality, and this is its calculus proof in one variable.

2. Let x be the two sides perpendicular to the river and y the side parallel. Only three sides are fenced:

2x + y = 100 \implies y = 100-2x

A(x) = x(100-2x) = 100x - 2x^2, \qquad 0<x<50

A'(x) = 100-4x = 0 \implies x = 25, \quad y = 50

A''=-4<0 → maximum ✓. Dimensions \boxed{25 \times 50} m, area 1250 m².

Note y = 2x: the side parallel to the river is twice each perpendicular side. Without the river you'd get a square; the free side shifts the optimum to a 2:1 rectangle. The optimum shape depends on which constraint you're spending.

3. Base x\times x, height h:

V = x^2h = 32 \implies h = \frac{32}{x^2}

Cost: base 4x^2, four sides at 2 \cdot xh each:

C = 4x^2 + 8xh = 4x^2 + 8x\cdot\frac{32}{x^2} = 4x^2 + \frac{256}{x}

C' = 8x - \frac{256}{x^2} = 0 \implies x^3 = 32 \implies x = \sqrt[3]{32} \approx 3.1748

h = \frac{32}{32^{2/3}} = 32^{1/3} \approx 3.1748

C'' = 8 + \frac{512}{x^3} > 0 → minimum ✓.

So \boxed{x = h = \sqrt[3]{32} \approx 3.17\text{ m}}, and the minimum cost is

C = 4(32^{2/3}) + \frac{256}{32^{1/3}} \approx 40.3 + 80.6 = bfdollar6f55b26402n2z120.95

The base and height coming out equal is a consequence of the specific 4:2 price ratio, not a general truth — halve the base's price and the box gets wider and flatter. Worth re-deriving with symbols if you want to see how.

Check yourself in code

Solve the open-box problem symbolically.

For V(x) = x(20-2x)^2, print V' factored, the critical points, and for each one the volume and V'' with a max/min verdict.

Print exactly this:

V'(x) = 4*(x - 10)*(3*x - 10)
critical points: [10/3, 10]
x=10/3  V=16000/27  V''=-80  max
x=10  V=0  V''=80  min
import sympy as sp

x = sp.Symbol('x')
V = x * (20 - 2*x)**2

print(f"V'(x) = {sp.factor(sp.diff(V, x))}")
crit = sorted(sp.solve(sp.diff(V, x), x))
print(f"critical points: {crit}")

for c in crit:
    # volume there, second derivative, and the verdict
    print(f"x={c}  ...")
import sympy as sp

x = sp.Symbol('x')
V = x * (20 - 2*x)**2

print(f"V'(x) = {sp.factor(sp.diff(V, x))}")
crit = sorted(sp.solve(sp.diff(V, x), x))
print(f"critical points: {crit}")

for c in crit:
    second = sp.diff(V, x, 2).subs(x, c)
    print(f"x={c}  V={V.subs(x, c)}  V''={second}  "
          f"{'max' if second < 0 else 'min'}")

Write the objective, write the constraint, substitute to one variable, state the physical domain, differentiate, solve, verify, and answer what was asked. The calculus is one line; the modelling is everything else. Optimize squared distances rather than distances. Redo the problem with letters when the number looks arbitrary — that's how you find that the best can is as tall as it is wide and the best box uses a sixth of the sheet. And a critical point is a candidate, never a conclusion.

Next: the one place in this module where the mathematics is exactly right and the arithmetic still betrays you.