[R] Using integrate

Duncan Murdoch murdoch at stats.uwo.ca
Tue Jul 22 01:43:47 CEST 2008


On 21/07/2008 4:56 PM, Ayman Oweida wrote:
> I have a function, say:
> f<-function(x)  exp(x)
> 
> and I would like to obtain the integration of the function while adding a few operations within the integration (I need point-by-point integration), say:
> 
> t<-seq(0,40, by=1)
> z<-array(0,length(t))
> for (i in 1:40){ 
> z[i] <- integrate ( f *  (t [i+1] - t)   ,  (i-1)  ,  i   ) 
> 
> In R, I can only have the function in 'integrate'
> i.e.  
> z[i] <- integrate ( f  ,  (i-1)  ,  i   ) 
> 
> but I want to add additional operations within the 'integrate' command, which I cannot just add to the function f.

But it is very simple to define new functions.  So you could do

z[i] <- integrate( function(x) f(x) * (t [i+1] - t[i]), i-1, i )

Duncan Murdoch



More information about the R-help mailing list