---
title: Optimization in one and several variables
heading: Finding the lowest point, with and without derivatives
eyebrow: A13 · Lesson 38 · Optimization in one and several variables
xp: 40
---

::: hook
Choose resistor values to bring a filter's response closest to a template. Adjust a solar charger's operating voltage for maximum power. Tune a controller's gains to minimize overshoot, or a network's weights to minimize prediction error. Each is a minimization, and the methods mirror the root finders: a bracketing method that can't fail (golden-section search), derivative-based methods that are fast near the answer (gradient descent, Newton), and derivative-free ones for noisy measurements. The one new idea is the step size: too small and progress crawls, too large and the iteration oscillates or blows up.
:::

::: play
- Gradient descent on the bowl $f = \frac12(x^2 + \kappa y^2)$ from (1, 1), with a fixed step size η. The path joins the iterates $x_{k+1} = x_k - \eta\nabla f(x_k)$.
- With a small η the path creeps down the gentle x direction. Raise η: the steep y direction starts to overshoot and zigzag, and past $\eta = 2/\kappa$ it diverges.
- The best fixed step, $\eta = \frac{2}{1 + \kappa}$, balances the two directions; the error then shrinks by $\frac{\kappa - 1}{\kappa + 1}$ per step, the same as steepest descent's worst case.
:::
:::

```sim plot
view: flow
x: x = -0.3..1.2
y: y = -1.2..1.2
slider: k = 2..20 step 1 = 8
slider: e = 0.01..0.3 step 0.005 = 0.1
shade: (x^2 + k*y^2)/2 | contours: 14 | scale: 1.5
path: (1 - e)^floor(u)*(1 - (u - floor(u))) + (1 - e)^(floor(u) + 1)*(u - floor(u)), (1 - e*k)^floor(u)*(1 - (u - floor(u))) + (1 - e*k)^(floor(u) + 1)*(u - floor(u)) | over: u = 0..40
dot: 1, 1 | label: start
dot: 0, 0 | label: minimum
readout: factor per step along x = abs(1 - e)
readout: factor per step along y = abs(1 - e*k)
readout: step where it turns unstable = 2/k
readout: best fixed η = 2/(1 + k)
```

::: predict
## A function's curvature (second derivative) along its steepest direction is 50. Above what fixed step size does gradient descent diverge?

- [ ] 0.5
- [ ] 0.02
- [x] 0.04
- [ ] 50

::: reveal
0.04, which is 2/50. Along a direction with curvature λ, each step multiplies the error by $1 - \eta\lambda$, which passes −1 when $\eta > 2/\lambda$. The step size is limited by the steepest direction, even when the slow direction needs big steps: that mismatch, κ, is what makes ill-conditioned problems slow.
:::
:::

::: nameit
## One variable

- **Golden-section search:** with a bracket $a < c < b$ where $f(c)$ is below both ends, evaluate at a point in the larger part and keep the sub-bracket that still has a low interior point. Placing points at the golden ratio shrinks the bracket by $\varphi - 1 = 0.618$ per evaluation, guaranteed for a unimodal f.
- **Parabolic interpolation:** fit a parabola through three points and jump to its vertex; superlinear near a smooth minimum. **Brent's minimizer** combines the two, as Brent's root finder does.
- **Newton:** find a zero of f′: $x \leftarrow x - \frac{f'(x)}{f''(x)}$.

A minimum's location can only be found to about $\sqrt\varepsilon$ relative accuracy (about 8 digits in double), because near it f changes only quadratically: $f(x^* + \delta) - f(x^*) \approx \frac12f''\delta^2$ falls below rounding when δ is about $\sqrt\varepsilon$.

## Several variables

- **Gradient descent:** $x \leftarrow x - \eta\nabla f$. On a quadratic with Hessian eigenvalues from $\lambda_{min}$ to $\lambda_{max}$, it's stable for $\eta < 2/\lambda_{max}$, and the best fixed step $\eta = \frac{2}{\lambda_{min} + \lambda_{max}}$ gives the factor $\frac{\kappa - 1}{\kappa + 1}$.
- **Newton:** solve $H\Delta = -\nabla f$ with the Hessian H; quadratic convergence near a minimum, but each step needs second derivatives and a linear solve.
- **Quasi-Newton (BFGS):** build up an approximate Hessian from successive gradients; the usual choice for smooth problems of modest size.
- **Derivative-free (Nelder–Mead, perturb-and-observe):** compare function values only; slow but robust to noise, as when the "function" is a measurement.

If f is **convex**, any local minimum is the global one. Otherwise, start from several points.
:::

::: example
## How many golden-section evaluations shrink a bracket of width 5 to $10^{-6}$?

::: solution
1. **Count the shrink factors.**
   $$\frac{\ln(10^{-6}/5)}{\ln 0.618} = 32.05$$
   so 33 evaluations.
2. **Compare with bisection.** That's slower than bisection for roots (0.5 per step), but it needs no derivatives and can't be fooled by a unimodal function.

**Answer:** 33 evaluations.
:::
:::

::: example
## Minimize $f(x) = x^4 - 3x$ by Newton's method from x = 1.

::: solution
1. **The derivatives.**
   $$\begin{aligned} f' &= 4x^3 - 3 \\ f'' &= 12x^2 \end{aligned}$$
2. **Newton's iterates.** They are 1, 0.91667, 0.90863, 0.9085603, 0.908560296.
3. **The exact minimum.**
   $$(3/4)^{1/3} = 0.9085603$$
   reached quadratically.

**Answer:** the minimum is at $x = (3/4)^{1/3} = 0.9085603$.
:::
:::

::: example
## Minimize $f = x^2 + 10y^2$ by gradient descent with the best fixed step. How fast does it converge?

::: solution
1. **The Hessian.** It is diag(2, 20).
2. **The best fixed step.**
   $$\begin{aligned} \eta &= \frac{2}{\lambda_\min + \lambda_\max} \\ &= \frac{2}{22} \\ &= 0.091 \end{aligned}$$
3. **The rate.** The error then shrinks per step by
   $$\begin{aligned} \frac{\kappa - 1}{\kappa + 1} &= \frac{10 - 1}{10 + 1} \\ &= 0.818 \end{aligned}$$
4. **Steps for six digits.**
   $$\frac{\ln 10^{-6}}{\ln 0.818} = 69$$
   and the path zigzags across the long, narrow valley.
5. **Better methods.** Newton's method, which uses the Hessian, would finish this quadratic in one step; conjugate gradients in two.

**Answer:** with the best fixed step, 0.091, the error shrinks by 0.818 per step, so six digits take 69 steps.
:::
:::

::: check
given: L = 1 | 5 | 20; d = 0.001 | 0.000001
answer: ceil(ln(d/L)/ln(0.6180339887))
tolerance: 0

## How many golden-section evaluations shrink a bracket of width {L} below {d}?

::: right
Right. {answer}.
:::

::: misses
- [ceil(ln(d/L)/ln(0.5))] That's bisection's rate; golden section keeps 0.618 of the bracket each time.
- [any] Not yet. Solve $L(0.618)^n \le \delta$.
:::
:::

::: check
given: l = 4 | 50 | 1000
answer: 2/l
tolerance: 0.5%

## A quadratic's largest Hessian eigenvalue is {l}. Above what fixed step size does gradient descent diverge?

::: right
Right. $2/\lambda_{max}$ = {answer}.
:::

::: misses
- [1/l] At $1/\lambda$ the steep direction is solved in one step; divergence starts at $2/\lambda$.
- [any] Not yet. The factor $1 - \eta\lambda$ must stay above −1.
:::
:::

::: check
given: a = 1 | 2; b = 10 | 100
answer: 2/(a + b)
tolerance: 0.5%

## A quadratic's Hessian eigenvalues run from {a} to {b}. What's the best fixed gradient-descent step?

::: right
Right. $\frac{2}{\lambda_{min} + \lambda_{max}}$ = {answer}.
:::

::: misses
- [2/b] That's where it becomes unstable, not the best step.
- [any] Not yet. Balance $1 - \eta\lambda_{min}$ against $\eta\lambda_{max} - 1$.
:::
:::

::: check
given: c = 2 | 3 | 5; x0 = 1 | 2
answer: x0 - (4*x0^3 - c)/(12*x0^2)
tolerance: 0.01%

## One Newton step toward the minimum of $x^4 - \given{c}x$ from {x0}: what's the new x?

::: right
Right. $x - f'/f''$ = {answer}.
:::

::: misses
- [x0 - (x0^4 - c*x0)/(4*x0^3 - c)] That's Newton for a root of f; for a minimum, find a root of f′.
- [any] Not yet. $x - \frac{4x^3 - c}{12x^2}$.
:::
:::

::: check
given: p = 11 | 16 | 24 | 53 | 64 | 113
answer: sqrt(2^(1 - p))
tolerance: 1%

## To about what relative accuracy can a minimum's location be found in a {p}-bit format?

::: right
Right. About $\sqrt\varepsilon$ = {answer}.
:::

::: misses
- [2^(1 - p)] Near a minimum f changes quadratically, so you only get the square root of epsilon.
- [any] Not yet. $\frac12f''\delta^2 \approx \varepsilon\lvert f\rvert$ gives $\delta \approx \sqrt\varepsilon$.
:::
:::

::: check
kind: choice

## A solar charger must find its panel's maximum-power voltage from noisy power readings, with no formula for the panel. Which approach fits?

- [ ] Newton's method with the analytic Hessian
- [x] A derivative-free hill climb (perturb and observe): nudge the voltage, keep going if power rises, reverse if it falls
- [ ] Solving the normal equations
- [ ] Bisection on the power

::: misses
- [1] There's no formula to differentiate, and noise ruins finite differences.
- [3] That's for fitting, not for maximizing a measurement.
- [4] Bisection finds where a function changes sign, not where it peaks.
:::
:::

::: check
kind: parts
given: c = 4 | 10 | 100

## Gradient descent with the best fixed step minimizes $x^2 + \given{c}y^2$.

::: part
answer: (c - 1)/(c + 1)
tolerance: 0.5%

### By what factor does the error shrink per step?

::: misses
- [1/c] The rate is $\frac{\kappa - 1}{\kappa + 1}$ with κ = c.
- [any] $\frac{\lambda_\max - \lambda_\min}{\lambda_\max + \lambda_\min}$.
:::
:::

::: part
answer: ln(1e-6)/ln((c - 1)/(c + 1))
tolerance: 1%

### About how many steps gain 6 digits?

::: misses
- [any] Solve $\rho^k = 10^{-6}$.
:::
:::
:::

::: check
kind: derivation
vars: a = 1..3, b = 0..0.9, c = 1..3, h = 0.1..1

## A parabola goes through $(-h, a)$, $(0, b)$, $(h, c)$, with $a, c > b$.

::: step
answer: h*(a - c)/(2*(a - 2*b + c))

### Where is its minimum?
:::

::: right
Right. That's one step of parabolic interpolation, the fast half of Brent's minimizer.
:::
:::

::: explain
## Why does gradient descent with a fixed step converge slowly on an ill-conditioned problem?

::: model
Along each eigendirection of the Hessian, a fixed step multiplies the error by $1 - \eta\lambda$. The steepest direction, with the largest curvature $\lambda_{max}$, forces η below $2/\lambda_{max}$ or the iteration diverges. With η that small, the gentlest direction, curvature $\lambda_{min}$, shrinks only by $1 - \eta\lambda_{min}$, which is close to 1 when $\lambda_{min} \ll \lambda_{max}$. The best compromise still leaves a factor $\frac{\kappa - 1}{\kappa + 1}$, close to 1 for large κ, so the number of steps grows in proportion to κ. Methods that use curvature (Newton, quasi-Newton, conjugate gradients) rescale the directions and avoid that dependence.
:::
:::

::: practice

::: check
given: L = 2 | 50; d = 0.0001 | 0.00000001
answer: ceil(ln(d/L)/ln(0.6180339887))
tolerance: 0

## Golden-section evaluations, width {L} to {d}?

::: misses
- [any] Smallest n with $L(0.618)^n \le \delta$.
:::
:::

::: check
given: l = 2 | 8 | 20 | 50 | 300 | 1000
answer: 2/l
tolerance: 0.5%

## Gradient-descent stability limit for $\lambda_{max}$ = {l}?

::: misses
- [any] $2/\lambda_{max}$.
:::
:::

::: check
given: a = 0.5 | 3; b = 20 | 60
answer: 2/(a + b)
tolerance: 0.5%

## Best fixed step for eigenvalues {a} to {b}?

::: misses
- [any] $\frac{2}{\lambda_{min} + \lambda_{max}}$.
:::
:::
:::
