Qu

minimize

Builtin index · Signal processing

result = minimize(f, x0, [grad=], [max_iter=200], [tol=1e-8])

Input: f, x0, grad, max_iter, tol · Output: result

What it is

f: a string naming a user-defined function f(params) (a length-P vector) returning a scalar; x0: a length-P vector, the starting point; grad (optional string naming a gradient function, else the gradient is numerical/central-differences); max_iter/tol (optional numbers).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = minimize(f, x0, [grad=], [max_iter=200], [tol=1e-8])
f string, vector, or scalarf: a string naming a user-defined function f(params) (a length-P vector) returning a scalar.
x0 vectorx0: a length-P vector, the starting point.
grad string, optionalgrad (optional string naming a gradient function, else the gradient is numerical/central-differences).
max_iter number, optional (default 200)max_iter/tol (optional numbers).
tol number, optional (default 1e-8)max_iter/tol (optional numbers).
Model —kind "minimize"
params vectorlength-P vector
value numbera number
converged booleana boolean
Returns a Model (kind "minimize") with fields params (length-P vector), value (a number), converged (a boolean). L-BFGS quasi-Newton multivariate minimizer.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

bowl(p) := (p[0]-3)^2 + (p[1]+2)^2
m = minimize("bowl", [0, 0])
print("minimum at {m.params}, value {m.value}")
output
minimum at [3, -2], value 2.029121e-19

from numerics-dsp

bowl_grad(p) := [2*(p[0]-3), 2*(p[1]+2)]
m2 = minimize("bowl", [0, 0], grad="bowl_grad")   # same minimum, fewer

                                                    # evaluations per step

from numerics-dsp

wavy(p) := -0.5*exp(-(p[0]+2)^2) - 2*exp(-(p[0]-3)^2/2)  # shallow local

                                                            # min at -2,

                                                            # deeper global

                                                            # min at 3

stuck = minimize("wavy", [-2])            # stays at the shallow one

found = basin_hopping("wavy", [-2], n_iter=60, step_size=3)  # finds the deep one

from numerics-dsp

How it works

minimize(f, x0, [grad=], [max_iter=200], [tol=1e-8]) — L-BFGS, a quasi-Newton ("pseudo-Newton") multivariate minimizer: f takes a single params vector and returns a scalar. By default the gradient is computed numerically (central differences); passing grad="gradfn" (a function params -> gradient vector, same shape as f) uses that exact analytical gradient instead — cheaper (one call instead of 2n) and not subject to finite-difference error. Model kind "minimize", fields params, value, converged.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.