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
- Name the quantity to optimise (the objective) and the variables.
- Write the constraint linking the variables, and use it to reduce the objective to one variable.
- Find the domain: which values make physical sense?
- Differentiate, solve , and test critical points and endpoints.
- 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?
The parallel side is twice the perpendicular side. A quick numeric scan agrees:
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 . Minimise the metal used (surface area including top and bottom).
The optimal can is as tall as it is wide (). 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.
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 is closest to ? Minimise the squared distance, which has the same minimiser and no square root:
Closest points: , at distance . The critical point 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 | loss (error on training data) |
| variable | model parameters (weights) |
| constraint | regularisation, bounds, architecture |
| solve | usually impossible in closed form |
| numeric search | gradient descent: |
Here is gradient descent on a one-parameter loss, minimising the squared-distance function from Example 3:
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 controls the step size: too small and it crawls, too large and it overshoots and diverges. Starting at exactly it would never move, since (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
Exercise 2: Open box
Squares of side are cut from the corners of a sheet, and the sides are folded up to make an open box. Which gives the largest volume?
Answer
, volume .
Solution
Exercise 3: Minimum cost fence
A 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
Exercise 4: Learning rate too big
Run the gradient descent from the lesson with lr = 0.3 starting at , printing the first 5 steps. What happens?
Answer
It overshoots and never settles: bounces around () instead of converging to .
Solution
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 , and gradient descent is only stable there when , i.e. ; 0.3 is far beyond that. An even larger rate would make blow up completely.
Test yourself
In an optimization problem, what is the constraint used for?
- To eliminate variables so the objective depends on one variable
- To find the second derivative
- To choose the learning rate
- It is ignored
Why minimise the squared distance instead of the distance?
- It gives a different, better answer
- Squaring is required by the chain rule
- It has the same minimiser and is easier to differentiate
- Distances cannot be minimised
In gradient descent , what happens when ?
- jumps to infinity
- stops changing
- changes sign
- doubles