[R] Integration subroutine?

Marco Geraci marcodoc75 at yahoo.com
Tue Mar 14 00:15:38 CET 2006


Hi,
the function 'fr' evaluates each element of the vector
'x', independently of its length. 'fr2' does not.

Try with

fr2<-function(x){
ifelse(x<1, x*x, 1)
}

> integrate(fr2,-1,2)
1.666667 with absolute error < 3.4e-05


Marco


--- Lynette Sun <fuyaonv at hotmail.com> wrote:

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




More information about the R-help mailing list