Estimating the error of a trapezoid method integral

Asked by Guy Koren on 1 Jan 2013
Latest activity Commented on by Roger Stafford on 5 Jan 2013

Hi,

I have some experimental data (vectors x,y) and I've estimated the area under the curve via the trapezoid method (A = trapz(y,x))

The question is, how can I determine the error of this calculation? Is there a Matlab function that can estimate it?

Thanks a lot..

0 Comments

Guy Koren

Products

No products are associated with this question.

2 Answers

Answer by Matt J on 1 Jan 2013
Edited by Matt J on 1 Jan 2013
  • If you know the true area then compute the error by direct subtraction.
  • If you don't know the true area -- i.e., because you have the continuous form of the function y=f(x), but it cannot be analytically integrated, -- then you could take finer and finer samplings of the function and see what trapz(y,x) converges to. Then, use that as an estimate of the true area.
  • If you know bounds on the derivatives of f(x), you could use error estimation formulas from here.
  • If you don't know anything about the continuous form of the function you're integrating, then what notion of "error" are you using?

2 Comments

Guy Koren on 1 Jan 2013

Thanks, the thing is, I don't really have a continues data function. My data is discrete. I only have 350 data points (x,y) with intervals of dx = 0.5, so I can't change my samplings.

Matt J on 2 Jan 2013

In that case, then why not regard the error as zero? One of the infinitely many continuous functions that connect your x,y points is the one that connects them piece-wise linearly, and trapz(y,x) is its exact, error-free integral. If there's nothing stopping you from assuming your discrete samples came from this piece-wise linear function, then voila, you're done, and your area calculation was perfect!

Matt J
Answer by Roger Stafford on 1 Jan 2013

As Matt J has indicated with his reference to

http://en.wikipedia.org/wiki/Trapezoidal_rule#Error_analysis

the error can be estimated in terms of the second derivative of your data function and the widths of your trapezoidal intervals. You can estimate the second derivative in terms of the typical second finite differences in the data divided by the square of the interval widths.

7 Comments

Roger Stafford on 4 Jan 2013

Well, that depends on how closely-spaced your intervals are in relation to the magnitude of higher derivatives. As an example I computed the integral of sin(x) from 0 to pi where the exact answer would be 2. Admittedly with matlab doing the computations the data is very precise and therefore the second differences are accurate. I divided that range into first 6 intervals and then 100 intervals. Here are the results:

 6 intervals
 actual error by trapz  - 0.04590276668629
 est. error, 2nd diff.  - 0.04363323129986
 100 intervals
 actual error by trapz  - 0.00016449611255687
 est. error, 2nd diff.  - 0.00016446634993921

In each case the estimated error is fairly accurate percentage-wise. However in the second case the data has to be very accurate to achieve this with second differencing.

The remedy when data is not sufficiently accurate is to widen the span of the area used to estimate the second derivative using appropriate filters such as those obtained from the second derivative of a normal density distribution with appropriately selected standard deviation.

Guy Koren on 5 Jan 2013

So, which numerical integration method deals best with noisy data..?

Roger Stafford on 5 Jan 2013

That is a different question from that of the accuracy of the error estimation. My point above was that estimating the trapz error with second differences is particularly sensitive to noise in data and in such cases the estimates can be made more accurate by increasing the span of points used to estimate second derivatives. If you look at the curve of the second derivative of a normal distribution, you will see how a filter can be designed to cover a span of several points in making such an estimate.

As to the question of comparing trapz with other possible methods of integration for discrete data, if the data is noisy probably trapz is the best method to use, precisely because its error depends only on the second derivative. For accurate, smooth data, other methods using higher order approximation are superior in accuracy. You will find several of these in the File Exchange.

Roger Stafford

Contact us