Hi All, Can any one help to explain why min and max function couldn't work in the integrate function directly. For example, if issue following into R: integrand <- function(x) {min(1-x, x^2)} integrate(integrand, lower = 0, upper = 1) it will return this: Error in integrate(integrand, lower = 0, upper = 1) : evaluation of function gave a result of wrong length However, as min(U,V) = (U+V)/2-abs(U-V)/2 Below code working; integrand <- function(x){(1-x+x^2)/2-0.5*abs(1-x-x^2)}> integrate(integrand, lower = 0, upper = 1)0.151639 with absolute error < 0.00011 I am confused... Dan [[alternative HTML version deleted]]
use pmin instead of min. hth Matthias On 12.02.2013 16:41, dan wang wrote:> Hi All, > > Can any one help to explain why min and max function couldn't work in the > integrate function directly. > > For example, if issue following into R: > > integrand <- function(x) {min(1-x, x^2)} > integrate(integrand, lower = 0, upper = 1) > > it will return this: > Error in integrate(integrand, lower = 0, upper = 1) : > evaluation of function gave a result of wrong length > > > However, as min(U,V) = (U+V)/2-abs(U-V)/2 > > Below code working; > > integrand <- function(x){(1-x+x^2)/2-0.5*abs(1-x-x^2)} >> integrate(integrand, lower = 0, upper = 1) > 0.151639 with absolute error < 0.00011 > > I am confused... > > > Dan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Prof. Dr. Matthias Kohl www.stamats.de
integrate() want a vectorized function, one such that length(f(x))==length(x). Use pmin or pmax instead of min or max. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of dan wang > Sent: Tuesday, February 12, 2013 7:41 AM > To: r-help at r-project.org > Subject: [R] integrate function > > Hi All, > > Can any one help to explain why min and max function couldn't work in the > integrate function directly. > > For example, if issue following into R: > > integrand <- function(x) {min(1-x, x^2)} > integrate(integrand, lower = 0, upper = 1) > > it will return this: > Error in integrate(integrand, lower = 0, upper = 1) : > evaluation of function gave a result of wrong length > > > However, as min(U,V) = (U+V)/2-abs(U-V)/2 > > Below code working; > > integrand <- function(x){(1-x+x^2)/2-0.5*abs(1-x-x^2)} > > integrate(integrand, lower = 0, upper = 1) > 0.151639 with absolute error < 0.00011 > > I am confused... > > > Dan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Possibly Parallel Threads
- Questions regarding "integrate" function
- Problem with Integrate for NEF-HS distribution
- Integration + Normal Distribution + Directory Browsing Processing Questions
- convolution of the double exponential distribution
- Taking Integral and Optimization using Integrate, Optim and maxNR