# Numerical integration of a function whose expression is unknown

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).

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

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.

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