optimize_parameter

emlib.mathlib.optimize_parameter(func, val, paraminit, maxerror=0.001, maxiterations=100)[source]

Optimize one parameter to arrive to a desired value.

Will raise NotFoundError if the maximum iterations are reached without converging

Parameters:
  • func – a function returning a value which will be compared to val

  • val (float) – the desired value to arrive to

  • paraminit (float) – the initial value of param

  • maxerror – the max. relative error (0.001 is 0.1%)

  • maxiterations – max. number of iterations

Return type:

tuple[float, int]

Returns:

a tuple (value, number of iterations)

Example

# find the exponent of a bpf were its value at 0.1 is 1.25
# (within the given relative error)
>>> func = lambda param: bpf.expon(0, 1, 1, 6, exp=param)(0.1)
>>> expon, numiter = optimize_parameter(func=func, val=1.25, paraminit=2)
>>> bpf.expon(0, 1, 1, 6, exp=expon)(0.1)
1.25