Hello, I need to integrate the absolute difference between two lines measured on different points. # For example : x <- seq(0, 1, 1/100) f_x <- runif(101) + x y <- seq(0, 1, 1/23) f_y <- runif(24) + (1 - y) plot(x, f_x, type="l") lines(y, f_y) Then I would like to compute Integral( | f_x - f_y | )dx. (This is not the same as | Integral(f_x)dx - Integral(f_y)dx |.) Computing this integral looks non trivial. I guess I should interpolate the points of f_y over x and integrate both lines on these intervals. Even then I would miss points where the lines cross. There are functions to integrate below *one* line (I'm thinking about the trapz function in caTools). Do you know if there is a function to do this integration properly with two lines (and especially their absolute difference)? Regards, Xavier
g <- function(x) abs(f1(x)-f2(x)) now you have one function and you can integrate it. Rich Sent from my iPhone On Jan 25, 2011, at 7:32, Xavier Robin <Xavier.Robin at unige.ch> wrote:> Hello, > > I need to integrate the absolute difference between two lines measured > on different points. > > # For example : > x <- seq(0, 1, 1/100) > f_x <- runif(101) + x > y <- seq(0, 1, 1/23) > f_y <- runif(24) + (1 - y) > > plot(x, f_x, type="l") > lines(y, f_y) > > Then I would like to compute Integral( | f_x - f_y | )dx. > (This is not the same as | Integral(f_x)dx - Integral(f_y)dx |.) > > Computing this integral looks non trivial. I guess I should interpolate > the points of f_y over x and integrate both lines on these intervals. > Even then I would miss points where the lines cross. > > There are functions to integrate below *one* line (I'm thinking about > the trapz function in caTools). > Do you know if there is a function to do this integration properly with > two lines (and especially their absolute difference)? > > Regards, > Xavier > > ______________________________________________ > 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.
Xavier Robin <Xavier.Robin <at> unige.ch> writes:> Hello, > > I need to integrate the absolute difference between two lines measured > on different points. > > # For example : > x <- seq(0, 1, 1/100) > f_x <- runif(101) + x > y <- seq(0, 1, 1/23) > f_y <- runif(24) + (1 - y) > > plot(x, f_x, type="l") > lines(y, f_y) > > Then I would like to compute Integral( | f_x - f_y | )dx. > (This is not the same as | Integral(f_x)dx - Integral(f_y)dx |.)First define a function from those points: fx <- approxfun(x, f_x) fy <- approxfun(y, f_y) f <- function(x) abs(fx(x)-fy(x)) and now you can apply integrate() or trapz(): xx <- sort(c(x, y)) yy <- f(xx) trapz(xx, yy) trapz() should return the more accurate (i.e. exact) result. --Hans Werner> Computing this integral looks non trivial. I guess I should interpolate > the points of f_y over x and integrate both lines on these intervals. > Even then I would miss points where the lines cross. > > There are functions to integrate below *one* line (I'm thinking about > the trapz function in caTools). > Do you know if there is a function to do this integration properly with > two lines (and especially their absolute difference)? > > Regards, > Xavier > >
Le 25.01.2011 15:23, Rmh a ?crit :> g <- function(x) abs(f1(x)-f2(x)) > > now you have one function and you can integrate it.Thank you Rich. Unfortunately I have no f1 and f2 functions, only a set of observed points on two lines - and no idea about the underlying distribution to create a function. Other ideas? Xavier
Reasonably Related Threads
- Using RasterBricks
- How to filter XY pairs of inacurate gps position along track, taking into account the time index to not mix track from different days in one average track
- by output into data frame
- Timing benefits of mapply() vs. for loop was: Wrap a loop inside a function
- Help on calculating AUC with caTools trapz(a,b) command