R Under development (unstable) (2023-11-25 r85635 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > library(nleqslv) > > print.result <- function(z, do.print.xf=FALSE) { + if( do.print.xf ) { + print(z$x) + print(z$fvec) + } + print(z$message) + print(all(abs(z$fvec)<=1e-8)) + } > > # Powell cautionary example > # M.J.D. Powell, "A Hybrid Method for Nonlinear Equations", > # in Numerical methods for Nonlinear Algebraic Equations, ed. P. Rabinowitz, 1970. > > > f <- function(x) { + y <- numeric(2) + y[1] <- x[1] + y[2] <- 10*x[1]/(x[1]+.1) + 2*x[2]^2 + + y + } > > jac <- function(x) { + fjac <- matrix(0,nrow=2,ncol=2) + + fjac[1, 1] <- 1 + fjac[1, 2] <- 0 + fjac[2, 1] <- 1/(x[1]+0.1)^2 + fjac[2, 2] <- 4*x[2] + + fjac + } > > xstart <- c(3,1) > z1 <- nleqslv(xstart,f, method="Newton",control=list(trace=0,allowSingular=TRUE)) > print.result(z1) [1] "Function criterion near zero" [1] TRUE > xstart <- c(3,0) # singular start > z2 <- nleqslv(xstart,f, method="Newton",control=list(trace=0,allowSingular=TRUE)) > print.result(z2) [1] "Function criterion near zero" [1] TRUE > > proc.time() user system elapsed 0.14 0.04 0.17