intervalUniroot: Interval uniroot

View source: R/intervalUniroot.R

intervalUnirootR Documentation

Interval uniroot

Description

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.

Usage

intervalUniroot(
  f,
  lower,
  upper,
  correction = "lower",
  tol = .Machine$double.eps^0.5,
  ...
)

Arguments

f

a function for which to find the root, and passed to uniroot.

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 character indicating to which value of the interval correct the root, either lower, upper or none. The latter return the same value as uniroot.

tol

numeric, the absolute tolerance on the argument of f, specifying when to stop searching for limits of the interval. By default, correspond to the square root of the machine's precision.

...

additional arguments to pass to the function f or the function uniroot.

Details

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.

Value

Return only the root (a constant), other components returned by uniroot are dismissed.

Examples

#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


aleblancbio/timescale documentation built on Aug. 27, 2022, 3:01 p.m.