Skip to main content

minimize

Function minimize 

Source
pub fn minimize(lo: f64, hi: f64, f: impl FnMut(f64) -> f64) -> f64
Expand description

Minimizes f on the open interval (lo, hi): a scan of 64 equal cells picks the best interior grid point, then golden-section search runs between its neighbours to full precision.

The scan guards against local minima a pure golden-section search would settle in; a minimum narrower than a cell can still be missed. The result never is an end point.

use prospicio_math::optimize::minimize;

let x = minimize(0.0, 4.0, |x| (x - 1.3).powi(2) + 0.1 * (8.0 * x).cos());
let f = |x: f64| (x - 1.3).powi(2) + 0.1 * (8.0 * x).cos();
assert!(f(x) <= f(x + 1e-6) && f(x) <= f(x - 1e-6));