I have a function that's represented as a long list of values that I want to integrate. I can do it procedurally with Simpson's rule. ListConvolve generally runs faster then procedural code, so i would like to use it if I can find the right kernel.

My list can be of even or odd length, and for the latter I would like to use a method that is better than the trapezoid method. Is there a higher order method for this case?

  • $\begingroup$ How about Interpolation and NIntegrate with an appropriate rule for Method? $\endgroup$ – rm -rf Jun 12 '13 at 13:06
  • $\begingroup$ @rm, even Integrate[] would work nicely for interpolating functions. $\endgroup$ – J. M.'s ennui Jun 12 '13 at 13:11
  • $\begingroup$ In any event, Bob, did you know there are two Simpson's rules? One of them (the less popular one) would work nicely when the number of intervals is a multiple of $3$... $\endgroup$ – J. M.'s ennui Jun 12 '13 at 13:16
  • 1
    $\begingroup$ Could just set up the {1,4,2,4,2,...,4,2,1} vector and use Dot. $\endgroup$ – Daniel Lichtblau Jun 12 '13 at 18:32
  • $\begingroup$ Also related: the upgrade information for ListIntegrate, an obsolete function. $\endgroup$ – Jens Jun 12 '13 at 23:41

As I mentioned in my comment, the easiest way to integrate a list of values is to use Interpolation with Integrate or NIntegrate. As Jens noted, the upgrade information for ListIntegrate, an obsolete function also mentions the same thing. For the sake of completeness, here's how you'd do it:

With[{if = Interpolation[(* list of {x, y} pairs *), InterpolationOrder -> k}, 
    Integrate[if[x], {x, Sequence @@ if["Domain"][[1]]}]

If you really do wish to use Simpson's rule specifically (perhaps, for a homework or to reproduce some other result), you can also do what Daniel suggested, which is to set up the vector for Simpson's rule (or the 3/8ths rule), $\{1,4,2,4,2,\ldots,4,2,1\}$ and use Dot.

  • $\begingroup$ Great to see this answer @rm -rf♦ Sometimes, the combination of Interpolation and NIntegrate makes the integration very slow. See Michael's answer. Is it possible to use a quadrature rule, say, trapezoidal integration, to implement the integral in that equation in order to speed up the code? Thank you. $\endgroup$ – user55777 Jul 20 '19 at 4:07

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.