[R] Question with uniroot function

William Dunlap wdunlap at tibco.com
Thu Apr 16 18:33:43 CEST 2015


Use optimize() to find the minimum and feed that value into uniroot().

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Apr 16, 2015 at 7:47 AM, li li <hannah.hlx at gmail.com> wrote:

> Hi Jeff,
>   Thanks for the reply. I am aware that the sign needs to be different at
> the ends of the starting interval.
>
>    Another question:
>
> Is there a way to set the right end point ( (the "upper" argument in the
> uniroot function below) as the point where the function takes on its
> minimun, for example my function f1 below?
>
> Thanks very much!
>
>
>
> u1 <- -3
> u2 <- 4
> pi0 <- 0.8
>
> f1 <- function(lambda,z,p1){
> lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
>
>  x <- seq(-20,20, by=0.1)
> y <- numeric(length(x))
> for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
> plot(y ~ x, ylim=c(-1,1))
> abline(h=0)
>
>
> a <- uniroot(f1, lower =-10, upper = 0,
>            tol = 1e-20,p1=0.15,lambda=0.998)$root
>
>
>
>
>
> 2015-04-15 22:57 GMT-04:00 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>:
>
> > You really need to read the help page for uniroot. The sign needs to be
> > different at the ends of the starting interval. This is a typical
> > limitation of numerical root finders.
> >
> ---------------------------------------------------------------------------
> > Jeff Newmiller                        The     .....       .....  Go
> Live...
> > DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
> > Go...
> >                                       Live:   OO#.. Dead: OO#..  Playing
> > Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> > /Software/Embedded Controllers)               .OO#.       .OO#.
> rocks...1k
> >
> ---------------------------------------------------------------------------
> > Sent from my phone. Please excuse my brevity.
> >
> > On April 15, 2015 7:20:04 PM PDT, li li <hannah.hlx at gmail.com> wrote:
> > >Hi all,
> > >In the following code, I am trying to use uniroot function to solve for
> > >the root (a and b in code below) for function f1.
> > >I am not sure why uniroot function does not give the answer since when
> > >we
> > >look the graph, the function does cross 0 twice.
> > >Any suggestion?
> > >   Thanks.
> > >       Hanna
> > >
> > >u1 <- -3
> > >u2 <- 4
> > >pi0 <- 0.8
> > >
> > >f1 <- function(lambda,z,p1){
> > >lambda*(p1*exp(u1*z-u1^2/2)+(0.2-p1)*exp(u2*z-u2^2/2))-(1-lambda)*pi0}
> > >
> > >a <- uniroot(f1, lower =-10, upper = 0,
> > >           tol = 1e-20,p1=0.15,lambda=0.998)$root
> > >
> > >b <- uniroot(f1, lower =0, upper = 10,
> > >           tol = 1e-20,p1=0.15,lambda=0.998)$root
> > >
> > >x <- seq(-20,20, by=0.1)
> > >y <- numeric(length(x))
> > >for (i in 1:length(x)){y[i] <- f1(x[i],p1=0.15,lambda=0.998)}
> > >plot(y ~ x, ylim=c(-1,1))
> > >abline(h=0)
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > >______________________________________________
> > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >https://stat.ethz.ch/mailman/listinfo/r-help
> > >PLEASE do read the posting guide
> > >http://www.R-project.org/posting-guide.html
> > <http://www.r-project.org/posting-guide.html>
> > >and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list