[R] uniroot

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Jul 30 12:22:13 CEST 2006


nurza m wrote:
> Hello,
> 
> I am struggling to find the root of a exponent 
> function.
> "uniroot" is complaining about a values at end points
> not of opposite sign?

And you think it is not the case? Why?
We cannot help because you have not given a reproducible example (What 
is w and gp?), which the posting guide asks you to do.

Please also format your code in a way that is easy for others to read, 
including some blanks and without all those ">" and "+":


saeqn <- function(s, l)
{
     p <- exp(-l$gp$lambda + s) * l$gp$c
     k11 <- (l$gp$mu * (l$gp$lambda^2) * l$gp$c -
             s * l$gp$lambda * l$gp$c * l$gp$mu +
             l$gp$mu * l$gp$lambda) * p
     k12 <- -l$gp$mu * l$gp$lambda -
            s^2 + 2 * s * l$gp$lambda - (l$gp$lambda^2)
     k13 <- k11 + k12
     k14 <- (l$gp$lambda - s) *
            (-l$gp$mu * s - s * l$gp$lambda + s^2 +
             l$gp$mu * l$gp$lambda * p)
     k1 <- -k13 / k14
     k1 - l$t
}


s <- sapply(seq(along = w), function(i) {
     uniroot(saeqn, lower = -5000, upper = 0.01036597923,
             l = list(t = w[i], gp = gp))$root
     }
)



>  
> s<- sapply(1:length(w),function(i)
> +  {
> +  
> + + 
> + 
> +
> uniroot(saeqn,lower=-5000,upper=0.01036597923,l=list(t=w[i],gp=gp))$root
> +  })
> Error in uniroot(saeqn, lower = -5000, upper =
> 0.01036597923, l = list(t = w[i],  : 
>         f() values at end points not of opposite sign
> 
> 
> 
>  
> and here is my fonction "saeqn".
> 
> 
> 
>>saeqn<-function(s,l)
> 
> +  {
> + 
> + 
> + p<- exp(-l$gp$lambda+s)*l$gp$c
> + 
> + 
> +
> k11<-(l$gp$mu*(l$gp$lambda^2)*l$gp$c-s*l$gp$lambda*l$gp$c*l$gp$mu+l$gp$mu*l$gp$lambda)*p
> + 
> + k12 <-
> -l$gp$mu*l$gp$lambda-s^2+2*s*l$gp$lambda-(l$gp$lambda^2)
> + 
> + k13 <-k11+k12
> + 
> +
> k14<-(l$gp$lambda-s)*(-l$gp$mu*s-s*l$gp$lambda+s^2+l$gp$mu*l$gp$lambda*p)
> + 
> + k1<- -k13/k14
> + 
> +  k1-l$t
> +  }
> 
>  
> 
> . There is something I must be missing since I never
> had 
> luck with "uniroot"!
> 
> Thanks,
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list