newtonsys: Newton Method for Nonlinear Systems

View source: R/newtonsys.R

newtonsysR Documentation

Newton Method for Nonlinear Systems

Description

Newton's method applied to multivariate nonlinear functions.

Usage

newtonsys(Ffun, x0, Jfun = NULL, ...,
    	  maxiter = 100, tol = .Machine$double.eps^(1/2))

Arguments

Ffun

m functions of n variables.

Jfun

Function returning a square n-by-n matrix (of partial derivatives) or NULL, the default.

x0

Numeric vector of length n.

maxiter

Maximum number of iterations.

tol

Tolerance, relative accuracy.

...

Additional parameters to be passed to f.

Details

Solves the system of equations applying Newton's method with the univariate derivative replaced by the Jacobian.

Value

List with components: zero the root found so far, fnorm the square root of sum of squares of the values of f, and iter the number of iterations needed.

Note

TODO: better error checking, e.g. when the Jacobian is not invertible.

References

Quarteroni, A., R. Sacco, and F. Saleri (2007). Numerical Mathematics. Second Edition, Springer-Verlag, Berlin Heidelberg.

See Also

newtonRaphson, broyden

Examples

##  Example from Quarteroni & Saleri
F1 <- function(x) c(x[1]^2 + x[2]^2 - 1, sin(pi*x[1]/2) + x[2]^3)
newtonsys(F1, x0 = c(1, 1))  # zero: 0.4760958 -0.8793934

##  Find the roots of the complex function sin(z)^2 + sqrt(z) - log(z)
F2 <- function(x) {
    z  <- x[1] + x[2]*1i
    fz <- sin(z)^2 + sqrt(z) - log(z)
    c(Re(fz), Im(fz))
}
newtonsys(F2, c(1, 1))
# $zero   0.2555197 0.8948303 , i.e.  z0 = 0.2555 + 0.8948i
# $fnorm  2.220446e-16
# $niter  8

##  Two more problematic examples
F3 <- function(x)
        c(2*x[1] - x[2] - exp(-x[1]), -x[1] + 2*x[2] - exp(-x[2]))
newtonsys(F3, c(0, 0))
# $zero   0.5671433 0.5671433
# $fnorm  0
# $niter  4

## Not run: 
F4 <- function(x)  # Dennis Schnabel
        c(x[1]^2 + x[2]^2 - 2, exp(x[1] - 1) + x[2]^3 - 2)
newtonsys(F4, c(2.0, 0.5))
# will result in an error ``missing value in  ... err<tol && niter<maxiter''
## End(Not run)

pracma documentation built on March 19, 2024, 3:05 a.m.