[R] Passing additional argument to be numerically integrated function

Amir Liu amirkliu88 at hotmail.com
Wed Dec 2 02:46:37 CET 2009



Hi,

I try to use the numerical integration functionality of R to integrate a univariate (1D) function. Below I am integrating function const1 which works nicely as expected. But for some reasons I need to pass to my function that I want to integrate an additional parameter. If it were object-oriented I just would invoke some setter methods onto a object providing functionality described in function constX and then pass this object on. And according to the help page the function can have more than one argument, just the first needs to be the argument vector for the integration function:

- f: an R function taking a numeric first argument and returning a numeric vector of the same length.  Returning a non-finite element will generate an error.

But let us say I have an additional argument called nb that I want to pass as well. How would I pass this second argument (and possibly more additional arguments) to make case 2 work, i.e. how this is expressed in R syntax?


"integ" <- function() {

  # case 1: no argument for function to be integrated
  # integrate box function of height 1 from 0 to 2
  val <- integrate(const1,0,2)
  print(val)

  # case 2: one ore more argument for function to be integrated
  # integrate box function of height nb from 0 to 2
  nb <- 5
  val1 <- integrate(constX,0,2)
  print(val1)

}

"const1" <- function(x) {
 return(rep(1,length(x)))
}

"constX" <- function(x,nb) {
 return(rep(nb,length(nb)))
}


Kind Regards,

Amir
--


 		 	   		  
_________________________________________________________________
View your other email accounts from your Hotmail inbox. Add them now.




More information about the R-help mailing list