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. > # http://r.789695.n4.nabble.com/newton-method-td2306111.html#a2306111 > # R-help, 29-07-2010: newton.method > > library(nleqslv) > > f <- function(x) 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100 > > g1 <- function(x) 0.5*2.5*exp(-0.5*(2*0.045 - x)) - 1.5*2.5*exp(-1.5*x) > g2 <- function(x) matrix(0.5*2.5*exp(-0.5*(2*0.045 - x)) - 1.5*2.5*exp(-1.5*x), nrow=1,ncol=1) > > xu.1 <- uniroot(f,c(-3,0), tol=1e-8)$root > xu.2 <- uniroot(f,c( 6,8), tol=1e-8)$root > > xg1.1 <- nleqslv(-2,f,g1)$x > xg2.1 <- nleqslv(-2,f,g2)$x > > xg1.2 <- nleqslv(8,f,g1)$x > xg2.2 <- nleqslv(8,f,g2)$x > > all.equal(xg1.1, xu.1) [1] TRUE > all.equal(xg1.2, xu.2) [1] TRUE > all.equal(xg1.2, xg2.2) [1] TRUE > > all.equal(xg2.1, xu.1) [1] TRUE > all.equal(xg2.2, xu.2) [1] TRUE > all.equal(xg1.2, xg2.2) [1] TRUE > > proc.time() user system elapsed 0.15 0.04 0.18