Find the maximum or minimum of a unimodal function over a continuous domain.
Continuous ternary search finds the maximum or minimum of a unimodal function defined over a real interval. It applies when the domain is continuous and the function has a single peak or valley.
Unlike the discrete version, there is no final scan. The algorithm stops when the interval becomes sufficiently small.
Problem
Given a function defined over a real interval , find a point such that is maximum.
Assume:
- increases up to some point
- then decreases afterward
Key Idea
Split the interval into three parts using two interior points:
Evaluate:
- If , the maximum lies in
- Otherwise, it lies in
Repeat until the interval is small enough.
Algorithm
ternary_search_continuous(f, l, r, eps):
while r - l > eps:
m1 = l + (r - l) / 3
m2 = r - (r - l) / 3
if f(m1) < f(m2):
l = m1
else:
r = m2
return (l + r) / 2Example
Consider:
genui{“math_block_widget_always_prefetch_v2”:{“content”:“f(x)=-(x-2)^2+5”}}
The function has a maximum at:
Running ternary search over an interval such as converges to this value.
Correctness
At each iteration, the algorithm discards one third of the interval while preserving the region that contains the maximum.
Because the function is unimodal:
- If , the left third cannot contain the maximum
- If , the right third cannot contain the maximum
The interval shrinks around the optimal point.
Termination
The algorithm stops when:
The returned value approximates the optimal point with error at most .
Complexity
| cost | value |
|---|---|
| iterations | |
| time | |
| space |
Here is the cost of evaluating .
Comparison
| method | domain | requirement | result |
|---|---|---|---|
| discrete ternary search | integers | unimodal | exact index |
| continuous ternary search | real numbers | unimodal | approximate value |
When to Use
- Continuous optimization problems
- Functions without closed form solutions
- Simulation based optimization
- Peak or minimum finding in real domains
Notes
- Works for minimization by reversing the comparison
- Precision depends on chosen
- Floating point errors may affect convergence
Implementation
def ternary_search_continuous(f, l, r, eps=1e-9):
while r - l > eps:
m1 = l + (r - l) / 3
m2 = r - (r - l) / 3
if f(m1) < f(m2):
l = m1
else:
r = m2
return (l + r) / 2func TernarySearchContinuous(f func(float64) float64, l, r, eps float64) float64 {
for r-l > eps {
m1 := l + (r-l)/3
m2 := r - (r-l)/3
if f(m1) < f(m2) {
l = m1
} else {
r = m2
}
}
return (l + r) / 2
}