[R] Question with uniroot function

li li hannah.hlx at gmail.com
Thu Apr 16 04:20:04 CEST 2015


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]]



More information about the R-help mailing list