zeroin
- sherpa.utils.zeroin(fcn, xa, xb, fa=None, fb=None, args=(), maxfev=32, tol=0.01)[source] [edit on github]
Obtain a zero of a function of one variable using Brent’s root finder.
Return an approximate location for the root with accuracy:
4*DBL_EPSILON*abs(x) + tol
using the algorithm from [1].
- Parameters:
- fcn
callable() The function with a root. The function signature is
fcn(x, *args).- xa
float Lower limit of the bracketing interval
- xb
float Upper limit of the bracketing interval
- fa
floatorNone Function value at
xa. This parameter is optional and can be passed to save time in cases wherefcn(xa, *args)is already known and function evaluation takes a long time. IfNone, it will be calculated.- fb
floatorNone Function value at
xb. This parameter is optional and can be passed to save time in cases wherefcn(xb, *args)is already known and function evaluation takes a long time. IfNone, it will be calculated.- args
tuple Additional parameters that will be passed through to
fcn.- maxfev
int Maximal number of function evaluations
- tol
float The root finding algorithm stops if a value x with
abs(fcn(x)) < tolis found.
- fcn
- Returns:
- out
list The output has the form of a list:
[[x, fcn(x)], [x1, fcn(x1)], [x2, fcn(x2)], nfev]wherexis the location of the root, andx1andx2are the previous steps. The function value for those steps is returned as well.nfevis the total number of function evaluations. If any of those values is not available,Nonewill be returned instead.
- out
Notes
The function makes use of a bisection procedure combined with a linear or quadratic inverse interpolation.
At each step the code operates three abscissae - a, b, and c:
b - the last and the best approximation to the root
a - the last but one approximation
c - the last but one or even an earlier approximation such that:
|f(b)| <= |f(c)|f(b) and f(c) have opposite signs, i.e. b and c encompass the root
Given these abscissae, the code computes two new approximations, one by the bisection procedure and the other one from interpolation (if a,b, and c are all different the quadratic interpolation is used, linear otherwise). If the approximation obtained by the interpolation looks reasonable (i.e. falls within the current interval [b,c], not too close to the end points of the interval), the point is accepted as a new approximation to the root. Otherwise, the result of the bissection is used.
References
[1]G.Forsythe, M.Malcolm, C.Moler, Computer methods for mathematical computations. M., Mir, 1980, p.180 of the Russian edition