uniroot {stats}R Documentation

One Dimensional Root (Zero) Finding


The function uniroot searches the interval from lower to upper for a root (i.e., zero) of the function f with respect to its first argument.


uniroot(f, interval, lower = min(interval), upper = max(interval),
        tol = .Machine$double.eps^0.25, maxiter = 1000, ...)


f the function for which the root is sought.
interval a vector containing the end-points of the interval to be searched for the root.
lower the lower end point of the interval to be searched.
upper the upper end point of the interval to be searched.
tol the desired accuracy (convergence tolerance).
maxiter the maximum number of iterations.
... additional named or unnamed arguments to be passed to f (but beware of partial matching to other arguments).


Either interval or both lower and upper must be specified: the upper endpoint must be strictly larger than the lower endpoint. The function values at the endpoints must be of opposite signs (or zero).

The function uses Fortran subroutine ‘"zeroin"’ (from Netlib) based on algorithms given in the reference below. They assume a continuous function (which then is known to have at least one root in the interval).

Convergence is declared either if f(x) == 0 or the change in x for one step of the algorithm is less than tol (plus an allowance for representation error in x).

If the algorithm does not converge in maxiter steps, a warning is printed and the current approximation is returned.

f will be called as f(x, ...) for a numeric value of x.


A list with four components: root and f.root give the location of the root and the value of the function evaluated at that point. iter and estim.prec give the number of iterations used and an approximate estimated precision for root. (If the root occurs at one of the endpoints, the estimated precision is NA.)


Based on ‘zeroin.c’ in http://www.netlib.org/c/brent.shar.


Brent, R. (1973) Algorithms for Minimization without Derivatives. Englewood Cliffs, NJ: Prentice-Hall.

See Also

polyroot for all complex roots of a polynomial; optimize, nlm.


f <- function (x,a) x - a
str(xmin <- uniroot(f, c(0, 1), tol = 0.0001, a = 1/3))
str(uniroot(function(x) x*(x^2-1) + .5, low = -2, up = 2, tol = 0.0001),
    dig = 10)
str(uniroot(function(x) x*(x^2-1) + .5, low = -2, up = 2, tol = 1e-10 ),
    dig = 10)

## Find the smallest value x for which exp(x) > 0 (numerically):
r <- uniroot(function(x) 1e80*exp(x)-1e-300, c(-1000,0), tol = 1e-15)
str(r, digits= 15) ##> around -745, depending on the platform.

exp(r$r)        # = 0, but not for r$r * 0.999...
minexp <- r$r * (1 - 10*.Machine$double.eps)
exp(minexp)     # typically denormalized

[Package stats version 2.5.0 Index]