brentZero | R Documentation |
Brent's local root search
brentZero(
f,
interval,
lower = NA_real_,
upper = NA_real_,
f_lower = NULL,
f_upper = NULL,
extendInt = "no",
tol = 1e-08,
maxiter = 500L,
trace = 0L
)
f |
The function for which the root is sought. |
interval |
A length-2 vector containing the end-points of the search interval |
lower |
Scalar: the lower end point of the search interval. Not necessary if |
upper |
Scalar: the upper end point of the search interval. Not necessary if |
f_lower |
Scalar: same as f(upper). Passing this value saves time if f(lower) is slow to compute and is known. |
f_upper |
Scalar: same as f(lower). |
extendInt |
Character:
This behavior mirrors that of [uniroot()]. |
tol |
Small positive scalar: convergence tolerance. The search stops when the bracket size is smaller than
|
maxiter |
Positive integer: the maximum number of iterations before stopping. |
trace |
Integer: 0, 1, or 2. Controls the verbosity of the output.
|
A list with the following elements:
Location of the root.
Function value at the root.
Total iteration count used.
Number of initial extendInt
iterations if there were any; NA otherwise.
Estimate of the final bracket size.
f <- function (x, a) x - a
str(uniroot(f, c(0, 1), tol = 0.0001, a = 1/3))
uniroot(function(x) cos(x) - x, lower = -pi, upper = pi, tol = 1e-9)$root
# This function is faster than the base R uniroot, and this is the primary
# reason why it was written in C++
system.time(replicate(1000, { shift <- runif(1, 0, 2*pi)
uniroot(function(x) cos(x+shift) - x, lower = -pi, upper = pi)
}))
system.time(replicate(1000, { shift <- runif(1, 0, 2*pi)
brentZero(function(x) cos(x+shift) - x, lower = -pi, upper = pi)
}))
# Roughly twice as fast
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.