swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Raw File
Tip revision: 113add4c7a9bfd37466a9b41cf2dc060b7b87091 authored by HwB on 06 December 2012, 00:00:00 UTC
version 1.3.1
Tip revision: 113add4
aitken.R
##
##  a i t k e n . R  Aitken's acceleration method
##


aitken <- function(f, x0, nmax = 12, tol = 1e-8, ...) {
    if (!is.numeric(x0) || length(x0) != 1)
        stop("Argument 'x0' must be a numeric scalar.")
    fun <- match.fun(f)
    f <- function(x) fun(x, ...)

    x <- x0
    diff <- 1 + tol
    niter <- 0
    while (diff > tol && niter <= nmax) {
        gx <- f(x)
        ggx <- f(gx)
        xnew <- (x*ggx - gx^2) / (ggx - 2*gx + x)
        diff <- abs(x - xnew)
        x <- xnew
        niter <- niter + 1
    }
    if (niter > nmax)
        warning("Maximum number of iterations exceeded.")
    return(x)
}
back to top