Learn R Programming

TukeyGH77 (version 0.1.4)

vuniroot2: Vectorised One Dimensional Root (Zero) Finding

Description

To solve a monotone function \(y = f(x)\) for a given vector of \(y\) values.

Usage

vuniroot2(
  y,
  f,
  interval = stop("must provide a length-2 `interval`"),
  tol = .Machine$double.eps^0.25,
  maxiter = 1000L
)

Value

Function vuniroot2() returns a numeric

vector

\(x\) as the solution of \(y = f(x)\) with given vector

\(y\).

Arguments

y

numeric vector of \(y\) values

f

monotone function \(f(x)\) whose roots are to be solved

interval

length-2 numeric vector

tol

double scalar, desired accuracy, i.e., convergence tolerance

maxiter

integer scalar, maximum number of iterations

Details

Function vuniroot2(), different from vuniroot, does

  • accept NA_real_ as element(s) of \(y\)

  • handle the case when the analytic root is at lower and/or upper

  • return a root of Inf (if abs(f(lower)) >= abs(f(upper))) or -Inf (if abs(f(lower)) < abs(f(upper))), when the function value f(lower) and f(upper) are not of opposite sign.

Examples

Run this code
library(rstpm2)

# ?rstpm2::vuniroot does not accept NA \eqn{y}
tryCatch(vuniroot(function(x) x^2 - c(NA, 2:9), lower = 1, upper = 3), error = identity)

# ?rstpm2::vuniroot not good when the analytic root is at `lower` or `upper`
f <- function(x) x^2 - 1:9
vuniroot(f, lower = .99, upper = 3.001) # good
tryCatch(vuniroot(f, lower = 1, upper = 3, extendInt = 'no'), warning = identity)
tryCatch(vuniroot(f, lower = 1, upper = 3, extendInt = 'yes'), warning = identity)
tryCatch(vuniroot(f, lower = 1, upper = 3, extendInt = 'downX'), error = identity)
tryCatch(vuniroot(f, lower = 1, upper = 3, extendInt = 'upX'), warning = identity)

vuniroot2(c(NA, 1:9), f = function(x) x^2, interval = c(1, 3)) # all good

Run the code above in your browser using DataLab