View source: R/intervalUniroot.R
intervalUniroot | R Documentation |
Extend the uniroot
function from stats
by accepting intervals on which the function is constant. The function then return one of the endpoints of the interval. Note than the function must be either increasing or decreasing, although not necessarily strictly.
intervalUniroot( f, lower, upper, correction = "lower", tol = .Machine$double.eps^0.5, ... )
f |
a function for which to find the root, and passed to |
lower |
the lower end-point of the interval to be searched for the root. |
upper |
the upper end-point of the interval to be searched for the root. |
correction |
a |
tol |
|
... |
additional arguments to pass to the function |
The function first estimate the root using uniroot
. If the derivative is also zero, the function search for an interval and instead return its lowest or highest value (according to correction
). The algorithm stop when increment on the argument of f
reached absolute tolerance specified by tol
.
Return only the root (a constant), other components returned by uniroot
are dismissed.
#Define a function that reach zero at a +- delta g <- function(x, a, delta){ y <- rep(0,length = length(x)) y[x <= a - delta] <- x[x <= a - delta] - (a - delta) y[x >= a + delta] <- x[x >= a + delta] - (a + delta) return(y) } #Define parameters and range on which the function is known to be constant a <- 5 delta <- 1 lower <- -10 upper <- 10 intervalUniroot(g, lower, upper, correction = "lower", a = a, delta = delta) intervalUniroot(g, lower, upper, correction = "upper", a = a, delta = delta) uniroot(g, lower = lower, upper = upper, a = a, delta = delta)$root
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.