I'm having a function of the form 1> f<-function(x){ 1+ 1+ return(x^p) 1+ 1+ } ,and I would like to integrate it with respect to x, where p should be any constant. One way would be to set a value for p globally and then call integrate function: p=2 integrate(f, lower = -1, upper = 1) However, I would like to use 'integrate' inside a function, so I could call it passing p as a parameter. I tried something like this: 1> p=1 1> integral<-function(p){ 1+ integrate(f, lower = -1, upper = 1) 1+ 1+ } 1> 1> integral(2) 0 with absolute error < 1.1e-14 ,but it doesn't work as the integral of f is evaluated with p=1 (the value of the global variable p) and not with the value of p=2 when the function integral is called. Does anyone knows how can I solve this problem? Thanks in advance santiagorf -- View this message in context: http://r.789695.n4.nabble.com/integrate-a-fuction-tp3336066p3336066.html Sent from the R help mailing list archive at Nabble.com.
On Mar 4, 2011, at 6:01 PM, santiagorf wrote:> I'm having a function of the form > > 1> f<-function(x){ > 1+ > 1+ return(x^p) > 1+ > 1+ } > > ,and I would like to integrate it with respect to x, where p should > be any > constant. > > One way would be to set a value for p globally and then call integrate > function: > p=2 > integrate(f, lower = -1, upper = 1) > > However, I would like to use 'integrate' inside a function, so I > could call > it passing p as a parameter. I tried something like this: > > 1> p=1 > 1> integral<-function(p){ > 1+ integrate(f, lower = -1, upper = 1) > 1+ > 1+ } > 1> > 1> integral(2) > 0 with absolute error < 1.1e-14 > > ,but it doesn't work as the integral of f is evaluated with p=1 (the > value > of the global variable p) and not with the value of p=2 when the > function > integral is called.Functions carry with them environments in whaich they are defined.> > Does anyone knows how can I solve this problem?Build `f` to have two parameters. -- David> Thanks in advance > santiagorf > > > > -- > View this message in context: http://r.789695.n4.nabble.com/integrate-a-fuction-tp3336066p3336066.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Heritage Laboratories West Hartford, CT
santiagorf
2011-Mar-05 14:51 UTC
[R] integrate one single variable functions with constant parameters
If f is a two-parameter function, how can I integrate it with respect to p? -- View this message in context: http://r.789695.n4.nabble.com/integrate-one-single-variable-functions-with-constant-parameters-tp3336066p3336693.html Sent from the R help mailing list archive at Nabble.com.
santiagorf
2011-Mar-05 14:57 UTC
[R] integrate one single variable functions with constant parameters
I received the solution... Hi: This is what David means: f <- function(x, p) x^p integrate(f, lower = -1, upper = 1, p = 2) 0.6666667 with absolute error < 7.4e-15 integrate(f, lower = -1, upper = 1, p = 3) 0 with absolute error < 5.6e-15 # this is correct -- View this message in context: http://r.789695.n4.nabble.com/integrate-one-single-variable-functions-with-constant-parameters-tp3336066p3336702.html Sent from the R help mailing list archive at Nabble.com.