I need to integrate a scalar valued function $f\left(\boldsymbol{x}\right)$ where $\boldsymbol{x}$ is a three dimensional position vector; in other words:

$\int_{\Omega} f\left(\boldsymbol{x}\right) d^{3}\boldsymbol{x}$

The integration domain $\Omega$ is a a cube of side $L$.

The problem is that i don't have the analytical function but rather its values on a cartesian cubic grid $f \left(\boldsymbol{x_{\text{grid}}}\right)$. Specifically, in my program $f \left(\boldsymbol{x_{\text{grid}}}\right)$ is a three dimensional array.

I was thinking to write a program in which i use Gaussian quadrature with $n$ points inside each element and interpolating the value of the function in the integration points and sum up the result from each element. This is however quite an intensive work, therefore my question is:

Is there a straightforward or easier way to perform such an integration?

Thanks in advance

  • $\begingroup$ Generate an InterpolatingFunction using Interpolation and then use NIntegrate. Alternatively, you could just Sum over f. $\endgroup$
    – bbgodfrey
    Commented Feb 24, 2015 at 17:01
  • $\begingroup$ Are your samples evenly spaced inside $\Omega$? $\endgroup$ Commented Feb 24, 2015 at 17:27
  • $\begingroup$ By evenly spaced you mean equidistant one to each other? If so, yes $\endgroup$
    – SSC Napoli
    Commented Feb 24, 2015 at 17:31
  • $\begingroup$ @bbgodfrey I don't think I can just sum up f, isn't there a Volume involved in each summation? $\endgroup$
    – SSC Napoli
    Commented Feb 24, 2015 at 17:40
  • $\begingroup$ If the points are uniformly spaced, the volumes are the same for each point. So, multiply the total by the volume for one point; i.e., the volume of the cube divided by the number of points. $\endgroup$
    – bbgodfrey
    Commented Feb 24, 2015 at 17:42

1 Answer 1


To elaborate on my Comment (and assuming uniform spacing of the data), consider the toy problem in 1-D:

f = Table[Sin[2 Pi ( i - .5)/10], {i, 10}]

Generating an InterpolatingFunction and then using NIntegrate yields:

g = Interpolation[f]
NIntegrate[g[x], {x, 1, 10}]
(* 3.3306690738754696*^-16 *)

Simply forming the Total yields the same result to machine precision.

(* 0. *)

This is not surprising, because using splines, etc cannot introduce more accurate information, because there is no more information. (Mathematically, the splines, etc integrate to one.)

On the other hand, if the points are not evenly spaced, then Interpolation plus NIntegrate is a straightforward (but not the only) approach.


I should add that for my toy problem the length associated with each point is one. In other cases, just multiply by that unit length.

My point here is that using more elaborate integrating procedures accomplishes nothing for uniformly spaced points.

  • $\begingroup$ +1 the Interpolation approach is also useful for handling the edge conditions that arise depending on how your grid aligns with the region boundary. $\endgroup$
    – george2079
    Commented Feb 24, 2015 at 17:53
  • $\begingroup$ actually my grid is perfectly aligned with the edge boundaries, because it is defined on the boundaries (a cube side divided in $N$ segments) $\endgroup$
    – SSC Napoli
    Commented Feb 24, 2015 at 17:58
  • $\begingroup$ @george2079 You are correct. However, summing the values of the points, weighted by the portion of their volumes that are inside the region of integration, should produce the same value. And, thanks for the +1. $\endgroup$
    – bbgodfrey
    Commented Feb 24, 2015 at 17:58
  • $\begingroup$ @user3810266 In that case, weight the points on surfaces by 1/2, on edges by 1/4, and on corners by 1/8. $\endgroup$
    – bbgodfrey
    Commented Feb 24, 2015 at 18:00
  • $\begingroup$ @bbgodfrey do you have some reference for the fact that for an uniform grid different integration schemes have the same behavior? this also means that they converge to the exact value, as the elements shrink, with the same order? $\endgroup$
    – SSC Napoli
    Commented Feb 24, 2015 at 18:01

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.