TExcelTAD
Home › Calculus in Depth › Optimization Problems

Optimization Problems

Optimization is the art of turning a word problem into a function, then finding its best value. This lesson gives a dependable procedure, explains how to prove that a critical point is the absolute optimum (not just a local one), and works through a geometry problem, a physics problem that derives Snell's law, and the problem at the heart of machine learning: minimising a loss. The survey version is Optimization.

The procedure

  1. Understand the problem: what is to be maximised or minimised, and what is given?
  2. Draw a diagram and name the quantities.
  3. Write the objective QQ as a function of the variables.
  4. Use the constraints to reduce QQ to one variable, and find its domain.
  5. Optimise: find critical numbers, then justify that the answer is an absolute extremum.

Step 5 is where proofs live. Three ways to justify it:

Situation Justification
domain is a closed interval [a,b][a, b] closed interval method
only one critical number cc, and f′f' changes from −- to ++ (or ++ to −-) there First Derivative Test for Absolute Extrema: f′<0f' < 0 left of cc and >0> 0 right means ff decreases all the way to cc and increases after, so f(c)f(c) is the absolute minimum
f′′>0f'' > 0 on the whole domain ff is convex, so any critical number is the absolute minimum

Example 1: the cheapest can

A cylindrical can must hold V=500 cm3V = 500\ \text{cm}^3. The side costs 1 unit per cm2\text{cm}^2, the top and bottom (thicker metal) cost 2 units per cm2\text{cm}^2. Minimise the cost.

Cost C=2⋅(2πr2)+1⋅(2πrh)volume πr2h=500⇒h=500πr2C(r)=4πr2+1000r,domain r>0C′(r)=8πr−1000r2=0⇒r3=125π⇒r=5π3C′′(r)=8π+2000r3>0 for all r>0⇒absolute minimum\begin{aligned} &\text{Cost } C = 2 \cdot (2\pi r^2) + 1 \cdot (2\pi r h) && \text{volume } \pi r^2 h = 500 \Rightarrow h = \frac{500}{\pi r^2} \\ &C(r) = 4\pi r^2 + \frac{1000}{r}, \quad \text{domain } r > 0 \\ &C'(r) = 8\pi r - \frac{1000}{r^2} = 0 \quad \Rightarrow \quad r^3 = \frac{125}{\pi} \quad \Rightarrow \quad r = \frac{5}{\sqrt[3]{\pi}} \\ &C''(r) = 8\pi + \frac{2000}{r^3} > 0 \text{ for all } r > 0 \quad \Rightarrow \quad \text{absolute minimum} \end{aligned}
python · try it yourself
import sympy as sp

r = sp.symbols('r', positive=True)
C = 4 * sp.pi * r**2 + 1000 / r
r_opt = sp.solve(sp.diff(C, r), r)[0]
h_opt = 500 / (sp.pi * r_opt**2)
print("r* =", r_opt, "≈", round(float(r_opt), 4), "cm")
print("h* ≈", round(float(h_opt), 4), "cm   h/r =", sp.simplify(h_opt / r_opt))
print("cost ≈", round(float(C.subs(r, r_opt)), 3))
r* = 5/pi**(1/3) ≈ 3.4139 cm
h* ≈ 13.6557 cm   h/r = 4
cost ≈ 439.378

SymPy finds h/r=4h/r = 4: the optimal can's height is twice its diameter. With equal prices for all faces you would get h=2rh = 2r, height equal to the diameter; making the ends dearer pushes material into the cheaper side.

Example 2: light takes the fastest path (Snell's law)

Light travels from point A=(0,a)A = (0, a) above a surface to B=(d,−b)B = (d, -b) below it, at speed v1v_1 above and v2v_2 below. It crosses the surface at (x,0)(x, 0). Fermat's principle says light takes the path of least time:

T(x)=a2+x2v1+b2+(d−x)2v2T′(x)=xv1a2+x2−d−xv2b2+(d−x)2=0\begin{aligned} T(x) &= \frac{\sqrt{a^2 + x^2}}{v_1} + \frac{\sqrt{b^2 + (d - x)^2}}{v_2} \\ T'(x) &= \frac{x}{v_1\sqrt{a^2 + x^2}} - \frac{d - x}{v_2\sqrt{b^2 + (d - x)^2}} = 0 \end{aligned}

The fractions are sin⁡θ1\sin\theta_1 and sin⁡θ2\sin\theta_2, where the angles are measured from the normal, so T′=0T' = 0 says sin⁡θ1v1=sin⁡θ2v2\frac{\sin\theta_1}{v_1} = \frac{\sin\theta_2}{v_2}: Snell's law. T′′>0T'' > 0 everywhere (each term is a convex function of xx), so this critical point is the absolute minimum. Numerically:

python · try it yourself
import numpy as np
from scipy.optimize import minimize_scalar

a, b, d = 2.0, 3.0, 4.0          # metres
v1, v2 = 3.0e8, 2.25e8           # air, water (refractive index 4/3)
T = lambda x: np.hypot(a, x) / v1 + np.hypot(b, d - x) / v2
res = minimize_scalar(T, bounds=(0, d), method="bounded", options={"xatol": 1e-12})
x = res.x
s1, s2 = x / np.hypot(a, x), (d - x) / np.hypot(b, d - x)
print(f"crossing point x = {x:.6f} m, time = {T(x) * 1e9:.4f} ns")
print(f"sin θ1 / v1 = {s1 / v1:.6e}")
print(f"sin θ2 / v2 = {s2 / v2:.6e}")
crossing point x = 2.075152 m, time = 25.4487 ns
sin θ1 / v1 = 2.400081e-09
sin θ2 / v2 = 2.400081e-09

Example 3: optimisation as loss minimisation

Training a model means choosing parameters that minimise a loss. The simplest case: fit y≈kxy \approx kx to data by least squares.

L(k)=∑(yi−kxi)2L′(k)=−2∑xi(yi−kxi)=0⇒k∗=∑xiyi∑xi2L′′(k)=2∑xi2>0⇒absolute minimum\begin{aligned} L(k) &= \sum (y_i - k x_i)^2 \\ L'(k) &= -2 \sum x_i(y_i - k x_i) = 0 && \Rightarrow \quad k^* = \frac{\sum x_i y_i}{\sum x_i^2} \\ L''(k) &= 2 \sum x_i^2 > 0 && \Rightarrow \quad \text{absolute minimum} \end{aligned}

Gradient descent finds the same k∗k^* without solving: repeat k←k−ηL′(k)k \leftarrow k - \eta L'(k). Because LL is a parabola in kk, each step shrinks the error by the factor 1−2η∑xi21 - 2\eta\sum x_i^2; it converges exactly when that factor lies in (−1,1)(-1, 1).

python · try it yourself
import numpy as np

x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
k_exact = float(np.sum(x * y) / np.sum(x * x))
print("closed form k* =", round(k_exact, 6))

dL = lambda k: float(-2 * np.sum(x * (y - k * x)))
for eta in (0.005, 0.02):
    k = 0.0
    for _ in range(30):
        k -= eta * dL(k)
    print(f"eta = {eta}: k after 30 steps = {k:.6g}  (factor {1 - 2 * eta * np.sum(x * x):+.2f})")
closed form k* = 2.003636
eta = 0.005: k after 30 steps = 2.00364  (factor +0.45)
eta = 0.02: k after 30 steps = -473.612  (factor -1.20)

With η=0.005\eta = 0.005 the factor is 0.45 and the iteration converges; with η=0.02\eta = 0.02 it is −1.2, so the error grows by 20% in magnitude each step and alternates sign: the classic "learning rate too high" divergence. Real networks have millions of parameters and non-convex losses, but every optimiser (SGD, Adam) is built on this one-variable picture; see Gradient Descent.

In software

  • Machine learning is optimisation: losses (squared error, cross-entropy) are minimised by gradient methods, and the learning-rate stability condition above is the 1-D version of the rule "η\eta below 2/λmax⁡2/\lambda_{\max} of the Hessian".
  • Graphics and physics: Snell's law in ray tracers comes straight from the least-time principle; inverse kinematics minimises the distance from a hand to its target over the joint angles.
  • Operations: pricing, caching and batch sizes are tuned by maximising a profit or throughput model, often with a one-variable search like minimize_scalar (golden-section or Brent's method).

Exercises

Exercise 1: Fence along a river

A farmer has 600 m of fence for a rectangular field along a straight river (no fence on the river side). Maximise the area.

Answer

150 m×300 m150\ \text{m} \times 300\ \text{m} (300 m parallel to the river), area 45 000 m245\,000\ \text{m}^2.

Solution
2x+y=600 (x=sides perpendicular to the river)⇒A(x)=x(600−2x), 0≤x≤300A′(x)=600−4x=0⇒x=150, y=300, A=45 000;A(0)=A(300)=0\begin{aligned} &2x + y = 600 \ (x = \text{sides perpendicular to the river}) \quad \Rightarrow \quad A(x) = x(600 - 2x), \ 0 \le x \le 300 \\ &A'(x) = 600 - 4x = 0 \quad \Rightarrow \quad x = 150, \ y = 300, \ A = 45\,000; \quad A(0) = A(300) = 0 \end{aligned}

Exercise 2: Closest point on a curve

Find the point on y=xy = \sqrt{x} closest to (3,0)(3, 0).

Answer

(52,5/2)≈(2.5,1.5811)\left(\frac{5}{2}, \sqrt{5/2}\right) \approx (2.5, 1.5811), at distance 112≈1.6583\frac{\sqrt{11}}{2} \approx 1.6583.

Solution

Minimise the squared distance D(x)=(x−3)2+xD(x) = (x - 3)^2 + x (since y2=xy^2 = x), x≥0x \ge 0. D′=2x−5=0⇒x=5/2D' = 2x - 5 = 0 \Rightarrow x = 5/2; D′′=2>0D'' = 2 > 0. D(5/2)=1/4+5/2=11/4D(5/2) = 1/4 + 5/2 = 11/4.

python · try it yourself
from scipy.optimize import minimize_scalar
import math
res = minimize_scalar(lambda x: (x - 3)**2 + x, bounds=(0, 10), method="bounded")
print(round(res.x, 6), round(math.sqrt(res.fun), 6))
2.5 1.658312

Exercise 3: Learning-rate limit

For the data in Example 3, what is the largest learning rate for which gradient descent on L(k)L(k) converges?

Answer

η<1/∑xi2=1/55≈0.01818\eta < 1/\sum x_i^2 = 1/55 \approx 0.01818.

Solution

The error is multiplied by 1−2η∑x21 - 2\eta\sum x^2 each step; convergence needs ∣1−110η∣<1\lvert 1 - 110\eta \rvert < 1, i.e. 0<η<2/110=1/550 < \eta < 2/110 = 1/55.

Exercise 4: Inscribed rectangle

Find the largest rectangle with its base on the x-axis and its top corners on y=12−x2y = 12 - x^2.

Answer

Half-width x=2x = 2, height 8, area 32.

Solution
A(x)=2x(12−x2)=24x−2x3,0≤x≤12A′(x)=24−6x2=0⇒x=2;A(2)=32;A(0)=A(12)=0\begin{aligned} &A(x) = 2x(12 - x^2) = 24x - 2x^3, \quad 0 \le x \le \sqrt{12} \\ &A'(x) = 24 - 6x^2 = 0 \quad \Rightarrow \quad x = 2; \quad A(2) = 32; \quad A(0) = A(\sqrt{12}) = 0 \end{aligned}

Test yourself

A function on (0,∞)(0, \infty) has one critical number cc, with f′<0f' < 0 left of cc and f′>0f' > 0 right of cc. What is f(c)f(c)?

  1. A local maximum only
  2. The absolute minimum
  3. An inflection point
  4. Not enough information

In least squares for y≈kxy \approx kx, what is k∗k^*?

  1. ∑y/∑x\sum y / \sum x
  2. ∑x2/∑xy\sum x^2 / \sum xy
  3. mean⁡(y)−mean⁡(x)\operatorname{mean}(y) - \operatorname{mean}(x)
  4. ∑xy/∑x2\sum xy / \sum x^2

Gradient descent on a parabola L(k)=A(k−k∗)2+cL(k) = A(k - k^*)^2 + c with step k←k−ηL′(k)k \leftarrow k - \eta L'(k) multiplies the error by…

  1. η\eta
  2. 2Aη2A\eta
  3. 1−2Aη1 - 2A\eta
  4. 1+η1 + \eta