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
- Understand the problem: what is to be maximised or minimised, and what is given?
- Draw a diagram and name the quantities.
- Write the objective as a function of the variables.
- Use the constraints to reduce to one variable, and find its domain.
- 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 | closed interval method |
| only one critical number , and changes from to (or to ) there | First Derivative Test for Absolute Extrema: left of and right means decreases all the way to and increases after, so is the absolute minimum |
| on the whole domain | is convex, so any critical number is the absolute minimum |
Example 1: the cheapest can
A cylindrical can must hold . The side costs 1 unit per , the top and bottom (thicker metal) cost 2 units per . Minimise the cost.
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 : the optimal can's height is twice its diameter. With equal prices for all faces you would get , 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 above a surface to below it, at speed above and below. It crosses the surface at . Fermat's principle says light takes the path of least time:
The fractions are and , where the angles are measured from the normal, so says : Snell's law. everywhere (each term is a convex function of ), so this critical point is the absolute minimum. Numerically:
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 to data by least squares.
Gradient descent finds the same without solving: repeat . Because is a parabola in , each step shrinks the error by the factor ; it converges exactly when that factor lies in .
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 the factor is 0.45 and the iteration converges; with 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 " below 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
(300 m parallel to the river), area .
Solution
Exercise 2: Closest point on a curve
Find the point on closest to .
Answer
, at distance .
Solution
Minimise the squared distance (since ), . ; . .
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 converges?
Answer
.
Solution
The error is multiplied by each step; convergence needs , i.e. .
Exercise 4: Inscribed rectangle
Find the largest rectangle with its base on the x-axis and its top corners on .
Answer
Half-width , height 8, area 32.
Solution
Test yourself
A function on has one critical number , with left of and right of . What is ?
- A local maximum only
- The absolute minimum
- An inflection point
- Not enough information
In least squares for , what is ?
Gradient descent on a parabola with step multiplies the error by…