2
$\begingroup$

I want to compute the value of an integral of a function.

This function, however, is not given by a formula, say $f(x) \: \forall x \in [0,1]$, but is only known through its values on some given points, say $f \left(\frac{k}{n} \right) \: \forall k \in \{0,\cdots,n\}$. (is the name for that scattered data ?)

How should I do so ? I don't want to implement a quadrature method by hand (I know how to do so in a basic way). Methods refering to a scientific library would be prefered (e.g. GSL).

$\endgroup$
2
  • $\begingroup$ Welcome to SciComp Exchange. What software are you using (or planning to use) for this integration? $\endgroup$
    – nicoguaro
    Commented Apr 24, 2015 at 17:06
  • $\begingroup$ @nicoguaro I am coding in Fortran right now, but I have knowledge of Matlab, Scilab and some other maths tools. $\endgroup$
    – bela83
    Commented Apr 25, 2015 at 0:54

1 Answer 1

3
$\begingroup$

I want to compute the value of an integral of a function.

This function, however, is not given by a formula, say $f(x)$ $ \forall{x}\in[0,1]$, but is only known through its values on some given points, say $f(k/n)$ $\forall{k}\in{0,⋯,n}$. (is the name for that scattered data ?)

In general, if you have data as collections of points $(x_{k}, f(x_{k})$, the absolute simplest method of integrating the "curve" through those points (there are many such curves) is to assume a linear interpolant between adjacent points, which gives you a trapezoidal rule. As far as I know, the trapezoidal rule is not in GSL (or at least, not in the numerical integration section). However, there are subroutines available in some libraries or software packages; trapz & cumtrapz in MATLAB come to mind. At worst, a trapezoidal rule wouldn't be onerous to implement by hand.

Higher-order methods assume higher order interpolants, and also typically do not equispace the quadrature points due to Runge's phenomenon, which is one motivation for using methods with non-equispaced points (for instance, quadrature based at Chebyshev points, or adaptive quadrature methods). If you have the ability to query your function anywhere (it need not be given by a formula, but it must be computable for arbitrary inputs within your interval of integration), you should use one of these methods. If not, then I doubt there is much advantage to trying to use higher-order piecewise interpolants (e.g., splines), but I am not an expert on numerical integration.

$\endgroup$
3
  • $\begingroup$ Thanks ! My function is only known at equidistant points because it is the solution to a numerical scheme involving finite differences. $\endgroup$
    – bela83
    Commented Apr 25, 2015 at 8:42
  • $\begingroup$ Trapezoidal rule was indeed the way to go. For higher dimensions, should I post a new question ? $\endgroup$
    – bela83
    Commented May 4, 2015 at 14:30
  • $\begingroup$ Yes, you should post a new question. $\endgroup$ Commented May 4, 2015 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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