# [R] How do I modify uniroot function to return .0001 if error ?

From: eric <ericstrom_at_aol.com>
Date: Sun, 03 Apr 2011 15:12:31 -0500 (CDT)

d <- uniroot(k, c(.001, 250), tol=.05)
return(d\$root)

For convenience, the uniroot function is shown below:

uniroot <- function (f, interval, ..., lower = min(interval), upper = max(interval),

f.lower = f(lower, ...), f.upper = f(upper, ...), tol = .Machine\$double.eps^0.25,

maxiter = 1000)
{

if (!missing(interval) && length(interval) != 2L)

stop("'interval' must be a vector of length 2")     if (!is.numeric(lower) || !is.numeric(upper) || lower >=

```        upper)
stop("lower < upper  is not fulfilled")
if (is.na(f.lower))
stop("f.lower = f(lower) is NA")
if (is.na(f.upper))
stop("f.upper = f(upper) is NA")
if (f.lower * f.upper > 0)
stop("f.up * f.down > 0")
val <- .Internal(zeroin2(function(arg) f(arg, ...), lower,
upper, f.lower, f.upper, tol, as.integer(maxiter)))
```
iter <- as.integer(val[2L])
if (iter < 0) {
```        warning("_NOT_ converged in ", maxiter, " iterations")
iter <- maxiter
```

}
list(root = val[1L], f.root = f(val[1L], ...), iter = iter,

estim.prec = val[3L])
}

```--
View this message in context: http://r.789695.n4.nabble.com/How-do-I-modify-uniroot-function-to-return-0001-if-error-tp3424092p3424092.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help