[R] Passing additional argument to be numerically integrated function

David Winsemius dwinsemius at comcast.net
Wed Dec 2 15:56:20 CET 2009


You may have hidden your posting by responding to an unrelated thread.  
(Avoid that in the future.) You need to be looking at:

?"function"   #and associated topics

This will drop you into a discussion that may be germane to your task:

http://finzi.psych.upenn.edu/Rhelp08/2009-April/194648.html

The above link was found with:

RSiteSearch("match args")

(There is a function named match.arg, whose precise spelling I had  
forgotten.)

-- 
David
On Dec 1, 2009, at 8:46 PM, Amir Liu wrote:

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


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list