https://github.com/cran/fields
Raw File
Tip revision: 8182a9a01e5ac26047a101825b76f85617a37cf8 authored by Doug Nychka on 06 February 2009, 21:36:12 UTC
version 5.02
Tip revision: 8182a9a
bisection.search.R
# fields, Tools for spatial data
# Copyright 2004-2007, Institute for Mathematics Applied Geosciences
# University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html

"bisection.search" <-
function (x1, x2, f, tol = 1e-07, niter = 25, f.extra = NA, upcross.level = 0) 
{
    f1 <- f(x1, f.extra) - upcross.level
    f2 <- f(x2, f.extra) - upcross.level
    if (f1 > f2) 
        stop(" f1 must be < f2 ")
    iter <- niter
    for (k in 1:niter) {
        xm <- (x1 + x2)/2
        fm <- f(xm, f.extra) - upcross.level
        if (fm < 0) {
            x1 <- xm
            f1 <- fm
        }
        else {
            x2 <- xm
            f2 <- fm
        }
        if (abs(fm) < tol) {
            iter <- k
            break
        }
    }
    xm <- (x1 + x2)/2
    fm <- f(xm, f.extra) - upcross.level
    list(x = xm, fm = fm, iter = iter)
}
back to top