https://github.com/cran/pracma
Tip revision: 7ba95550c2066826c77bc78b974b7cb8cd416301 authored by Hans W. Borchers on 11 January 2017, 00:43:13 UTC
version 1.9.9
version 1.9.9
Tip revision: 7ba9555
laguerre.R
##
## l a g u e r r e . R Laguerre Method
##
laguerre <- function(p, x0, nmax = 25, tol = .Machine$double.eps^(1/2)) {
if (!is.numeric(p) && !is.complex(p))
stop("Argument 'p' must be a numeric or complex vector.")
if ( (!is.numeric(x0) && !is.complex(x0)) || length(x0) != 1)
stop("Argument 'x0' must be a real or complex number.")
n <- length(p) - 1
p1 <- polyder(p)
p2 <- polyder(p1)
y0 <- polyval(p, x0)
if (abs(y0) < tol) return(x0)
for (m in 1:nmax) {
a <- polyval(p1, x0) / y0
a2 <- a^2
b <- a2 - polyval(p2, x0) / y0
x <- x0 - n/(a + a/abs(a) * sqrt((n-1)*(n*b - a2)))
y <- polyval(p, x)
if (abs(y) < tol) break
x0 <- x
y0 <- y
}
if (m > nmax)
warning("Root finding process might not have converged.")
return(x)
}