[R] Question with uniroot function

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Apr 16 04:57:33 CEST 2015


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
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list