TExcelTAD
Home › Calculus Essentials › Optimization Problems

Optimization Problems

Optimization means finding the best choice: the cheapest design, the largest area, the shortest route, the smallest error. Calculus turns a word problem into "minimise or maximise a function of one variable", which you then solve with the tools from Maxima, Minima and Curve Shape. The same pattern, at a huge scale, is how machine-learning models are trained.

The recipe

  1. Name the quantity to optimise (the objective) and the variables.
  2. Write the constraint linking the variables, and use it to reduce the objective to one variable.
  3. Find the domain: which values make physical sense?
  4. Differentiate, solve f′=0f' = 0, and test critical points and endpoints.
  5. Answer the question that was asked, with units.

Example 1: a fenced garden against a wall

You have 60 m of fencing to enclose a rectangular garden along a straight wall (no fence needed on the wall side). What dimensions maximise the area?

variables: x=the two sides perpendicular to the wall, y=the side parallel to itconstraint: 2x+y=60  ⟹  y=60−2xobjective: A(x)=x⋅y=60x−2x2,domain 0≤x≤30A′(x)=60−4x=0  ⟹  x=15, y=30A′′=−4<0→maximum;A(15)=450 m2(endpoints give A=0)\begin{aligned} &\text{variables: } && x = \text{the two sides perpendicular to the wall}, \ y = \text{the side parallel to it} \\ &\text{constraint: } && 2x + y = 60 \implies y = 60 - 2x \\ &\text{objective: } && A(x) = x \cdot y = 60x - 2x^2, \quad \text{domain } 0 \le x \le 30 \\ & && A'(x) = 60 - 4x = 0 \implies x = 15, \ y = 30 \\ & && A'' = -4 < 0 \to \text{maximum}; \quad A(15) = 450\ \text{m}^2 \quad (\text{endpoints give } A = 0) \end{aligned}

The parallel side is twice the perpendicular side. A quick numeric scan agrees:

python · try it yourself
best = max(((x / 10, (x / 10) * (60 - 2 * (x / 10))) for x in range(0, 301)), key=lambda t: t[1])
print("grid search: x =", best[0], " area =", best[1])

import sympy as sp
x = sp.symbols('x', positive=True)
A = x * (60 - 2 * x)
xc = sp.solve(sp.diff(A, x), x)[0]
print("calculus:    x =", xc, " y =", 60 - 2 * xc, " area =", A.subs(x, xc))
grid search: x = 15.0  area = 450.0
calculus:    x = 15  y = 30  area = 450

Example 2: the cheapest can

A cylindrical can must hold 500 cm3500\ \text{cm}^3. Minimise the metal used (surface area including top and bottom).

constraint: V=πr2h=500  ⟹  h=500πr2objective: S(r)=2πr2+2πrh=2πr2+1000r,r>0S′(r)=4πr−1000r2=0  ⟹  r3=250π  ⟹  r=250/π3≈4.30 cmh=500πr2=2r≈8.60 cm\begin{aligned} &\text{constraint: } && V = \pi r^2 h = 500 \implies h = \frac{500}{\pi r^2} \\ &\text{objective: } && S(r) = 2\pi r^2 + 2\pi r h = 2\pi r^2 + \frac{1000}{r}, \quad r > 0 \\ & && S'(r) = 4\pi r - \frac{1000}{r^2} = 0 \implies r^3 = \frac{250}{\pi} \implies r = \sqrt[3]{250/\pi} \approx 4.30\ \text{cm} \\ & && h = \frac{500}{\pi r^2} = 2r \approx 8.60\ \text{cm} \end{aligned}

The optimal can is as tall as it is wide (h=2rh = 2r). Real cans are usually taller, because cost also depends on seams, stacking and labels, which shows that the model matters as much as the calculus.

python · try it yourself
import sympy as sp

r = sp.symbols('r', positive=True)
S = 2 * sp.pi * r**2 + 1000 / r
rc = sp.solve(sp.diff(S, r), r)[0]
h = 500 / (sp.pi * rc**2)
print("r =", rc, "≈", round(float(rc), 3), "cm")
print("h ≈", round(float(h), 3), "cm,  h/r =", sp.simplify(h / rc))
print("S ≈", round(float(S.subs(r, rc)), 2), "cm²,  S'' > 0:", sp.diff(S, r, 2).subs(r, rc) > 0)
r = 5*2**(1/3)/pi**(1/3) ≈ 4.301 cm
h ≈ 8.603 cm,  h/r = 2
S ≈ 348.73 cm²,  S'' > 0: True

Example 3: closest point on a curve

Which point of the parabola y=x2y = x^2 is closest to (0,2)(0, 2)? Minimise the squared distance, which has the same minimiser and no square root:

D(x)=x2+(x2−2)2=x4−3x2+4D′(x)=4x3−6x=2x(2x2−3)=0  ⟹  x=0 or x=±3/2D(0)=4,D(±1.5)=2.25−4.5+4=1.75\begin{aligned} D(x) &= x^2 + (x^2 - 2)^2 = x^4 - 3x^2 + 4 \\ D'(x) &= 4x^3 - 6x = 2x(2x^2 - 3) = 0 \implies x = 0 \text{ or } x = \pm\sqrt{3/2} \\ D(0) &= 4, \quad D(\pm\sqrt{1.5}) = 2.25 - 4.5 + 4 = 1.75 \end{aligned}

Closest points: (±1.5,1.5)≈(±1.225,1.5)(\pm\sqrt{1.5}, 1.5) \approx (\pm 1.225, 1.5), at distance 1.75≈1.323\sqrt{1.75} \approx 1.323. The critical point x=0x = 0 is a local maximum of the distance. Always check what kind of critical point you found.

In software: optimization is loss minimisation

Calculus problem Machine learning
objective f(x)f(x) loss L(w)L(w) (error on training data)
variable xx model parameters ww (weights)
constraint regularisation, bounds, architecture
solve f′=0f' = 0 usually impossible in closed form
numeric search gradient descent: w←w−η⋅L′(w)w \leftarrow w - \eta \cdot L'(w)

Here is gradient descent on a one-parameter loss, minimising the squared-distance function from Example 3:

python · try it yourself
D = lambda x: x**4 - 3 * x**2 + 4
dD = lambda x: 4 * x**3 - 6 * x

x, lr = 0.3, 0.05
for step in range(1, 41):
    x -= lr * dD(x)
    if step in (1, 5, 10, 20, 40):
        print(f"step {step:2d}: x = {x:.6f}  D = {D(x):.6f}")
print(f"exact:    x = {1.5 ** 0.5:.6f}  D = 1.75")
step  1: x = 0.384600  D = 3.578128
step  5: x = 0.890239  D = 2.250521
step 10: x = 1.214723  D = 1.750598
step 20: x = 1.224744  D = 1.750000
step 40: x = 1.224745  D = 1.750000
exact:    x = 1.224745  D = 1.75

The learning rate η=0.05\eta = 0.05 controls the step size: too small and it crawls, too large and it overshoots and diverges. Starting at x=0x = 0 exactly it would never move, since D′(0)=0D'(0) = 0 (a stuck critical point). These are the same issues you meet in gradient descent for neural networks. Libraries such as SciPy's optimizers package the numeric side.

Exercises

Exercise 1: Biggest product

Two positive numbers add up to 24. What is the largest possible product?

Answer

144 (both numbers are 12).

Solution
P(x)=x(24−x)=24x−x2P′=24−2x=0  ⟹  x=12;P′′=−2<0→maxP(12)=144\begin{aligned} P(x) &= x(24 - x) = 24x - x^2 \\ P' &= 24 - 2x = 0 \implies x = 12; \quad P'' = -2 < 0 \to \text{max} \\ P(12) &= 144 \end{aligned}

Exercise 2: Open box

Squares of side xx are cut from the corners of a 30 cm×30 cm30\ \text{cm} \times 30\ \text{cm} sheet, and the sides are folded up to make an open box. Which xx gives the largest volume?

Answer

x=5 cmx = 5\ \text{cm}, volume 2000 cm32000\ \text{cm}^3.

Solution
V(x)=x(30−2x)2,0<x<15V′=(30−2x)2−4x(30−2x)=(30−2x)(30−6x)=0  ⟹  x=5(x=15 gives V=0)V(5)=5⋅202=2000\begin{aligned} V(x) &= x(30 - 2x)^2, \quad 0 < x < 15 \\ V' &= (30 - 2x)^2 - 4x(30 - 2x) = (30 - 2x)(30 - 6x) = 0 \implies x = 5 \quad (x = 15 \text{ gives } V = 0) \\ V(5) &= 5 \cdot 20^2 = 2000 \end{aligned}

Exercise 3: Minimum cost fence

A 200 m2200\ \text{m}^2 rectangular yard has one side made of brick at 30 €/m and the other three sides of wire at 10 €/m. Which dimensions minimise the cost?

Answer

Brick side 10 m, other side 20 m; cost 800 €.

Solution
x=brick side (and the opposite wire side), y=the other two sides,xy=200C=30x+10x+2⋅10y=40x+4000xC′=40−4000x2=0  ⟹  x2=100  ⟹  x=10, y=20C=400+400=800 €\begin{aligned} &x = \text{brick side (and the opposite wire side)}, \ y = \text{the other two sides}, \quad xy = 200 \\ &C = 30x + 10x + 2 \cdot 10y = 40x + \frac{4000}{x} \\ &C' = 40 - \frac{4000}{x^2} = 0 \implies x^2 = 100 \implies x = 10, \ y = 20 \\ &C = 400 + 400 = 800\ \text{€} \end{aligned}

Exercise 4: Learning rate too big

Run the gradient descent from the lesson with lr = 0.3 starting at x=0.3x = 0.3, printing the first 5 steps. What happens?

Answer

It overshoots and never settles: xx bounces around (0.81,1.63,−0.63,−1.46,−0.35,…0.81, 1.63, -0.63, -1.46, -0.35, \ldots) instead of converging to ±1.2247\pm 1.2247.

Solution
python · try it yourself
dD = lambda x: 4 * x**3 - 6 * x
x = 0.3
for step in range(1, 6):
    x -= 0.3 * dD(x)
    print(f"step {step}: x = {x:.4g}")
step 1: x = 0.8076
step 2: x = 1.629
step 3: x = -0.6275
step 4: x = -1.461
step 5: x = -0.3509

Each step is so large that it jumps right past the minimum, sometimes into the other valley. Near the minimum D′′=12D'' = 12, and gradient descent is only stable there when lr⋅D′′<2\text{lr} \cdot D'' < 2, i.e. lr<1/6\text{lr} < 1/6; 0.3 is far beyond that. An even larger rate would make xx blow up completely.

Test yourself

In an optimization problem, what is the constraint used for?

  1. To eliminate variables so the objective depends on one variable
  2. To find the second derivative
  3. To choose the learning rate
  4. It is ignored

Why minimise the squared distance instead of the distance?

  1. It gives a different, better answer
  2. Squaring is required by the chain rule
  3. It has the same minimiser and is easier to differentiate
  4. Distances cannot be minimised

In gradient descent w←w−η⋅L′(w)w \leftarrow w - \eta \cdot L'(w), what happens when L′(w)=0L'(w) = 0?

  1. ww jumps to infinity
  2. ww stops changing
  3. ww changes sign
  4. η\eta doubles