Re: [R] Integration subroutine?

2006-03-13 Thread Marco Geraci
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.67 with absolute error < 3.4e-05 Marco --- Lynette Sun <[EMAIL PROTECTED]> wrote: > Hi all, > > Why >

Re: [R] Integration subroutine?

2006-03-13 Thread Liaw, Andy
The function need to be vectorized. Try: > fr2.new <- function(x) ifelse(x < 1, x * x, 1) > integrate(fr2.new, -1, 2) 1.67 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

[R] Integration subroutine?

2006-03-13 Thread 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