swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: fdf16693b000f3e309c56091892c61f0ec9fd670 authored by Hans W. Borchers on 21 November 2017, 16:15:00 UTC
version 2.1.1
version 2.1.1
Tip revision: fdf1669
newtonsys.R
##
## n e w t o n s y s . R
##
# Finds a zero of a nonlinear system by Newton's method
newtonsys <- function(Ffun, x0, Jfun = NULL, ...,
maxiter = 100, tol = .Machine$double.eps^(1/2)) {
.vnorm <- function(x) { sqrt(sum(x^2)) }
if (is.null(Jfun)) Jfun <- function(x, ...) jacobian(Ffun, x, ...)
niter <- 0; err <- tol + 1
x <- x0
while (err >= tol && niter < maxiter) {
niter <- niter + 1
F <- Ffun(x, ...)
J <- Jfun(x, ...)
delta <- -1 * solve(J, F)
x <- x + delta
err <- .vnorm(delta)
}
F <- .vnorm(Ffun(x, ...))
if (niter > maxiter && err > tol) {
cat("Fails to converge within maximum number of iterations.\n",
"The iterate returned has relative residual ", F, "\n", sep="")
}
return(list(zero=x, fnorm=F, niter=niter))
}