[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?

u1 <- -3
u2 <- 4
pi0 <- 0.8

f1 <- function(lambda,z,p1){

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

	[[alternative HTML version deleted]]

More information about the R-help mailing list