Hi, Is this method broken in R? I am using it to find roots of the following function: f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100 It is giving an answer of -38.4762403 which is not even close (f(x) 2.903809e+25 for x=-38.4762403). The answer should be around 0.01-0.1. This function should converge.. Even for a simple function like f(x) = exp(-x) * x, it gives answer as 8.89210984 for which f(x) = 0.001222392 and I set tolerance to 10^-12.. Also, is there a non graphical version of newton method? I looked at nleqslv but have no idea how to use it.. Thanks for your help. -- View this message in context: http://r.789695.n4.nabble.com/newton-method-tp2306111p2306111.html Sent from the R help mailing list archive at Nabble.com.
On Jul 29, 2010, at 4:32 AM, sammyny wrote:> > Hi, > Is this method broken in R?> ?newton.method No documentation for 'newton.method' in specified packages and libraries: you could try '??newton.method' If you are asking about a non-base function, you are asked by the Posting Guide to supply the package name. You can do that in the text of you message or by providing code that starts out with: require(package) You are further asked to provide the code and data you used. You are finally asked to contact the package maintainer if you believe that a package function is not providing correct results. So .... read the Posting Guide. -- David.> I am using it to find roots of the following > function: > f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + > 2.5*exp(-1.5*x) - 100 > > It is giving an answer of -38.4762403 which is not even close (f(x) > 2.903809e+25 for x=-38.4762403). The answer should be around > 0.01-0.1. This > function should converge.. > > Even for a simple function like f(x) = exp(-x) * x, it gives answer > as > 8.89210984 for which f(x) = 0.001222392 and I set tolerance to > 10^-12.. > > Also, is there a non graphical version of newton method? I looked at > nleqslv > but have no idea how to use it.. > > Thanks for your help. > --
Hi: Interesting. Try the following; f is copied and pasted directly from your e-mail: f <- function(x) 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100 curve(f, -3, 8) # plot it in a localized region abline(0, 0, lty = 2) There are two places where the function crosses the x-axis, but not where you were expecting. The graph suggests one root between -3 and -2 and another between 7 and 8: Note that uniroot() takes an object of class function as its first argument and a search interval as its second. It is the R function that finds a zero of a univariate function. See ?uniroot for details. (And f is a function object; try class(f).) On Thu, Jul 29, 2010 at 1:32 AM, sammyny <sjain@caa.columbia.edu> wrote:> > Hi, > Is this method broken in R? I am using it to find roots of the following > function: > f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - > 100 >Which method? It is never (conveniently) mentioned... Function? Package?> > It is giving an answer of -38.4762403 which is not even close (f(x) > 2.903809e+25 for x=-38.4762403). The answer should be around 0.01-0.1. This > function should converge.. >> Even for a simple function like f(x) = exp(-x) * x, it gives answer as > 8.89210984 for which f(x) = 0.001222392 and I set tolerance to 10^-12.. >I see handwaving and fulminating, but no code...> > Also, is there a non graphical version of newton method? I looked at > nleqslv > but have no idea how to use it.. >> Thanks for your help. >HTH, Dennis> -- > View this message in context: > http://r.789695.n4.nabble.com/newton-method-tp2306111p2306111.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Yes, there are two roots. Try this to get the 2 roots: require(BB) p0.mat <- matrix(rnorm(10), 10, 1) # 10 random starting values ans <- multiStart(par=p0, fn=f) ans$par You can see that the 2 roots are -2.438285 and 7.419378. Hope this helps, Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Dennis Murphy <djmuser at gmail.com> Date: Thursday, July 29, 2010 8:28 am Subject: Re: [R] newton.method To: sammyny <sjain at caa.columbia.edu> Cc: r-help at r-project.org> Hi: > > Interesting. Try the following; f is copied and pasted directly from > your > e-mail: > > f <- function(x) 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + > 2.5*exp(-1.5*x) - 100 > curve(f, -3, 8) # plot it in a localized region > abline(0, 0, lty = 2) > > There are two places where the function crosses the x-axis, but not where > you were expecting. The graph suggests one root between -3 and -2 and > another between 7 and 8: Note that uniroot() takes an object of class > function as its first argument and a search interval as its second. > It is > the R function that finds a zero of a univariate function. See > ?uniroot for > details. (And f is a function object; try class(f).) > > On Thu, Jul 29, 2010 at 1:32 AM, sammyny <sjain at caa.columbia.edu> wrote: > > > > > Hi, > > Is this method broken in R? I am using it to find roots of the following > > function: > > f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + > 2.5*exp(-1.5*x) - > > 100 > > > > Which method? It is never (conveniently) mentioned... Function? Package? > > > > > It is giving an answer of -38.4762403 which is not even close (f(x) > > > 2.903809e+25 for x=-38.4762403). The answer should be around > 0.01-0.1. This > > function should converge.. > > > > > Even for a simple function like f(x) = exp(-x) * x, it gives > answer as > > 8.89210984 for which f(x) = 0.001222392 and I set tolerance to 10^-12.. > > > > I see handwaving and fulminating, but no code... > > > > > Also, is there a non graphical version of newton method? I looked at > > nleqslv > > but have no idea how to use it.. > > > > > Thanks for your help. > > > > HTH, > Dennis > > > -- > > View this message in context: > > > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > R-help at r-project.org mailing list > > > > PLEASE do read the posting guide > > > > and provide commented, minimal, self-contained, reproducible code. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Oops, it should have been: ans <- multiStart(par=p0.mat, fn=f) ans$par Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Ravi Varadhan <rvaradhan at jhmi.edu> Date: Thursday, July 29, 2010 11:22 am Subject: Re: [R] newton.method To: Dennis Murphy <djmuser at gmail.com> Cc: r-help at r-project.org, sammyny <sjain at caa.columbia.edu>> Yes, there are two roots. Try this to get the 2 roots: > > require(BB) > > p0.mat <- matrix(rnorm(10), 10, 1) # 10 random starting values > > ans <- multiStart(par=p0, fn=f) > > ans$par > > > You can see that the 2 roots are -2.438285 and 7.419378. > > > Hope this helps, > > Ravi. > ____________________________________________________________________ > > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology > School of Medicine > Johns Hopkins University > > Ph. (410) 502-2619 > email: rvaradhan at jhmi.edu > > > ----- Original Message ----- > From: Dennis Murphy <djmuser at gmail.com> > Date: Thursday, July 29, 2010 8:28 am > Subject: Re: [R] newton.method > To: sammyny <sjain at caa.columbia.edu> > Cc: r-help at r-project.org > > > > Hi: > > > > Interesting. Try the following; f is copied and pasted directly > from > > your > > e-mail: > > > > f <- function(x) 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + > > 2.5*exp(-1.5*x) - 100 > > curve(f, -3, 8) # plot it in a localized region > > abline(0, 0, lty = 2) > > > > There are two places where the function crosses the x-axis, but > not where > > you were expecting. The graph suggests one root between -3 and -2 > and > > another between 7 and 8: Note that uniroot() takes an object of class > > function as its first argument and a search interval as its > second. > > It is > > the R function that finds a zero of a univariate function. See > > ?uniroot for > > details. (And f is a function object; try class(f).) > > > > On Thu, Jul 29, 2010 at 1:32 AM, sammyny <sjain at caa.columbia.edu> > wrote: > > > > > > > > Hi, > > > Is this method broken in R? I am using it to find roots of the following > > > function: > > > f(x) = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + > > 2.5*exp(-1.5*x) - > > > 100 > > > > > > > Which method? It is never (conveniently) mentioned... Function? Package? > > > > > > > > It is giving an answer of -38.4762403 which is not even close > (f(x) > > > > > 2.903809e+25 for x=-38.4762403). The answer should be around > > 0.01-0.1. This > > > function should converge.. > > > > > > > > Even for a simple function like f(x) = exp(-x) * x, it gives > > answer as > > > 8.89210984 for which f(x) = 0.001222392 and I set tolerance to 10^-12.. > > > > > > > I see handwaving and fulminating, but no code... > > > > > > > > Also, is there a non graphical version of newton method? I > looked at > > > nleqslv > > > but have no idea how to use it.. > > > > > > > > Thanks for your help. > > > > > > > HTH, > > Dennis > > > > > -- > > > View this message in context: > > > > > > Sent from the R help mailing list archive at Nabble.com. > > > > > > ______________________________________________ > > > R-help at r-project.org mailing list > > > > > > PLEASE do read the posting guide > > > > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list > > > > PLEASE do read the posting guide > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
newton.method is in package 'animation'. Thanks Ravi. BBSolve/BBOptim seems to work very well although I am not familiar with the optimization methods being used there. Is there a way to specify a tolerance in the function to get the required precision. I did something like this to use newton method. require(animation) newton.method(f, init=2, tol=10*exp(-8)) But it gives bogus results. If someone could point me a correct working version of newton method for finding roots and its usage, that would be helpful. cheers, Sam -- View this message in context: http://r.789695.n4.nabble.com/newton-method-tp2306111p2306895.html Sent from the R help mailing list archive at Nabble.com.
sammyny wrote:> > > If someone could point me a correct working version of newton method for > finding roots and its usage, that would be helpful. >You mentioned in your original post that you had no idea how to use nleqslv. nleqslv provides a Broyden and Newton method with several different global search strategies for "difficult" functions. Here it is an example using your function (which is not particulary difficult if you look at the plot): library(nleqslv) # starting value -2 nleqslv(-2,f) # starting value 8 nleqslv(8,f) # manual multistart for( x in seq(-3,8,1)) { z <- nleqslv(x,f); print(c(z$x,z$fvec))} These example all use the Broyden method. If you want the Newton method then you can do for( x in seq(-3,8,1)) { z <- nleqslv(x,f,method="Newton"); print(c(z$x,z$fvec))} /Berend -- View this message in context: http://r.789695.n4.nabble.com/newton-method-tp2306111p2306973.html Sent from the R help mailing list archive at Nabble.com.
Sometimes it is easier to just write it. See below. On 10-07-30 06:00 AM, r-help-request at r-project.org wrote:> Date: Thu, 29 Jul 2010 11:15:05 -0700 (PDT) > From: sammyny<sjain at caa.columbia.edu> > To:r-help at r-project.org > Subject: Re: [R] newton.method > Message-ID:<1280427305687-2306895.post at n4.nabble.com> > Content-Type: text/plain; charset=us-ascii > > > newton.method is in package 'animation'. > > Thanks Ravi. > BBSolve/BBOptim seems to work very well although I am not familiar with the > optimization methods being used there. Is there a way to specify a tolerance > in the function to get the required precision. > > I did something like this to use newton method. > require(animation) > newton.method(f, init=2, tol=10*exp(-8)) > But it gives bogus results. > > If someone could point me a correct working version of newton method for > finding roots and its usage, that would be helpful. > > cheers, > > Sam >tfn<-function(x) { f = 2.5*exp(-0.5*(2*0.045 - x)) + 2.5*exp(-0.045) + 2.5*exp(-1.5*x) - 100 return(f) } tgr<-function(x) { g = 0.5*2.5*exp(-0.5*(2*0.045 - x)) -1.5*2.5*exp(-1.5*x) return(g) } newt<-function(start, fun, grad) { x<-start newx<-x+100 # to avoid stopping while( 1 != 0) { f<-fun(x) g<-grad(x) newx<-x-f/g cat("x, newx, f, g:",x,' ',newx,' ',f,' ',g,"\n") if ((100+x) == (100+newx)) return(newx) tmp<-readline("continue?") x<-newx } } You can try newt(7,tfn, tgr) newt(-7,tfn,tgr) and get both roots quite quickly. However, I'd probably used uniroot by preference as a general tool. The scripts above are meant for learning purposes. Best, John Nash PS. I did check tgr with numDeriv -- always worth doing.