fpi <- function(expr, x = 0, k = 1000, TOL = .Machine$double.eps, print = FALSE, ...) { expr <- checkExpression(substitute(expr)) if (length(x) != 1) stop("'x' should be a singular value.") d <- getOption("digits") for (i in seq(k)) { y <- eval(expr, envir = list(x = x), enclos = parent.frame()) # Check to see if sequence is diverging if (x == Inf | y == Inf | is.nan(x) | is.nan(y)) return("Iteration Diverged. Try another start value") if (print) print(sprintf("%*i %.*f %.*f", 5, i, d, y, d, abs(y - x)), quote = FALSE) # If y is within tolerance, return it; otherwise, set it as next guess. if (stoppingCriteria(x = x, y = y, TOL = TOL, ...)) return(y) x <- y } # end for loop stop(paste("No solutions after", k, "iterations. Possible loop in root search.")) } # end fpi function