Is there a better method than the trapezoidal rule as outlined here?

P.S. not familiar with the site, sorry if this has the wrong tag.

  • $\begingroup$ If this doesn't get an answer here, it might receive a better answer on cs.stackexchange.com . It should also be asked on the Q&A forum rather than the meta forum. I will migrate it to the main site $\endgroup$
    – robjohn
    Mar 27, 2013 at 19:49
  • $\begingroup$ @mathisnotmyforte: Why can't you use the Trapezoidal Rule? Anyway, the site also lists several alternates like Rectangle method, Simpson's rule, Romberg's method, Newton–Cotes formulas and Gaussian quadrature. Is there something peculiar with what you are trying to solve that is troubling? $\endgroup$
    – Amzoti
    Mar 28, 2013 at 1:40
  • $\begingroup$ Amzoti, I'm trying to integrate data collected from an android accelerometer. Since there is no way to collect it at equal intervals, I have to use the Trapezoidal rule for non uniform grids; however the downside of this approach is that it's not as accurate as I would ideally want it to be. As for the other formulas you mentioned, I was under the impression that they were for uniformed grids only (?). $\endgroup$ Mar 28, 2013 at 6:18

1 Answer 1


I'd have to understand the nature of the data and how you expect that data to behave, but in principle, you could use a modified Simpson's Rule. The difference here is that you have to perform a quadratic interpolation for every triplet of points. That is, given a triplet of data $(x_{2 i-1},y_{2 i-1}), (x_{2 i},y_{2 i}), (x_{2 i+1},y_{2 i+1})$, where $i \in \{1,2,\ldots,N/2\}$ and $N$ is the number of data points, set

$$f_i(x) = a_i + b_i (x-x_{2 i}) + c_i (x-x_{2 i})^2$$

Plug in the triplet of data and solve for $a_i$, $b_i$, and $c_i$. Then integrate the resulting expression for $f_i(x)$ over $[x_{2 i-1},x_{2 i+1}]$:

$$\int_{x_{2 i-1}}^{x_{2 i+1}} dx \: f_i(x) = \frac{x_{2 i-1}-x_{2 i+1}}{6 (x_{2 i}-x_{2 i-1}) (x_{2 i}-x_{2 i+1})} \left[\left (-3 x_{2 i}^2 (y_{2 i-1}+y_{2 i+1})-2 x_{2 i-1} (x_{2 i+1} (y_{2 i}+y_{2 i-1}+y_{2 i+1})-x_{2 i} (y_{2 i-1}+2 y_{2 i+1}))\right) \\ \left (+2 x_{2 i} x_{2 i+1} (2 y_{2 i-1}+y_{2 i+1})+x_{2 i-1}^2 (y_{2 i}-y_{2 i+1})+x_{2 i+1}^2 (y_{2 i}-y_{2 i-1})\right)\right]$$

This is for one triplet; you will run this over all (nonoverlapping) triplets of data to get your integral. No doubt that this is more complicated and expensive to use than the trapezoidal expression, but if your data represent a piecewise smooth function, this representation should be more accurate.

  • $\begingroup$ Hey Ron, Could you give me a reference where to find such numerical integration algorithms for non uniform spacing? In an ideal world you have uniform spacing but that's not the case. ;) Thanks. $\endgroup$ Jun 6, 2016 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.