R: Solve a nonlinear equation system with multiple roots from...
searchZeros
R Documentation
Solve a nonlinear equation system with multiple roots
from multiple initial estimates
Description
This function solves a system of nonlinear equations with nleqlsv
for multiple initial estimates of the roots.
Usage
searchZeros(x, fn, digits=4, ... )
Arguments
x
A matrix with each row containing an initial guess of the roots.
fn
A function of x returning a vector of function values
with the same length as the vector x.
digits
integer passed to round
for locating and removing duplicate the rounded solutions.
...
Further arguments to be passed to nleqslv, fn and jac.
Details
Each row of x is a vector of initial estimates for the argument
x of nleqslv.
The function runs nleqslv for each row of the matrix x.
The first initial value is treated separately and slightly differently from the
other initial estimates. It is used to check if all
arguments in ... are valid arguments for nleqslv and the function
to be solved. This is done by running nleqslv with no condition handling.
If an error is then detected an error message is issued and the function stops.
For the remaining initial estimates nleqslv is executed silently.
Only solutions for which the nleqslv termination code tcode equals 1
are regarded as valid solutions. The rounded solutions (after removal of duplicates)
are used to order the solutions in increasing order.
They are not returned in the return value of the function.
Value
A list containing components
x
a matrix with each row containing a unique solution (unrounded)
xfnorm
a vector of the function criterion associated with each row of
the solution matrix x.
fnorm
a vector containing the function criterion for every converged result
idxcvg
a vector containing the row indices of the matrix
of initial estimates for which function value convergence
was achieved
idxxtol
a vector containing the row indices of the matrix
of initial estimates for which x-value convergence was achieved
idxnocvg
a vector containing the row indices of the matrix
of initial estimates which lead to an nleqslv
termination code > 2
idxfatal
a vector containing the row indices of the matrix
of initial estimates for which a fatal error occurred
that made nleqslv stop
xstart
a matrix of the initial estimates
corresponding to the solution matrix
cvgstart
a matrix of all initial estimates
for which convergence was achieved
Examples
# Dennis Schnabel example 6.5.1 page 149 (two solutions)
set.seed(123)
dslnex <- function(x) {
y <- numeric(2)
y[1] <- x[1]^2 + x[2]^2 - 2
y[2] <- exp(x[1]-1) + x[2]^3 - 2
y
}
xstart <- matrix(runif(50, min=-2, max=2),ncol=2)
ans <- searchZeros(xstart,dslnex, method="Broyden",global="dbldog")
ans
# more complicated example
# R. Baker Kearfott, Some tests of Generalized Bisection,
# ACM Transactions on Methematical Software, Vol. 13, No. 3, 1987, pp 197-220
# A high-degree polynomial system (section 4.3 Problem 12)
# There are 12 real roots (and 126 complex roots to this system!)
hdp <- function(x) {
f <- numeric(length(x))
f[1] <- 5 * x[1]^9 - 6 * x[1]^5 * x[2]^2 + x[1] * x[2]^4 + 2 * x[1] * x[3]
f[2] <- -2 * x[1]^6 * x[2] + 2 * x[1]^2 * x[2]^3 + 2 * x[2] * x[3]
f[3] <- x[1]^2 + x[2]^2 - 0.265625
f
}
N <- 40 # at least to find all 12 roots
set.seed(123)
xstart <- matrix(runif(3*N,min=-1,max=1), N, 3) # N initial guesses, each of length 3
ans <- searchZeros(xstart,hdp, method="Broyden",global="dbldog")
ans$x