The function need to be vectorized. Try:
> fr2.new <- function(x) ifelse(x < 1, x * x, 1)
> integrate(fr2.new, -1, 2)
1.666667 with absolute error < 3.4e-05
Andy
From: Lynette Sun>
> Hi all,
>
> Why
>
> fr2<-function(x)
> { if(x<1){x*x}else{1}
> }
>
> integrate(fr2,-1,2)
>
> gives the following wrong answer:
>
> 3 with absolute error < 3.3e-14
> Warning message:
> the condition has length > 1 and only the first element will
> be used in: if (x < 1) {
>
> while
>
> fr<-function(x){as.numeric(x<1)*(x^2-1)+1}
>
> integrate(fr,-1,2)
>
> works?
>
> 1.666667 with absolute error < 3.4e-05
>
> Thanks.
>
>
> Lynette Sun
>
> Department of Statistics
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
>