0
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Generate an InterpolatingFunction using Interpolation and then use NIntegrate. Alternatively, you could just Sum over f. $\endgroup$ – bbgodfrey Feb 24 '15 at 17:01
  • $\begingroup$ Are your samples evenly spaced inside $\Omega$? $\endgroup$ – 2012rcampion Feb 24 '15 at 17:27
  • $\begingroup$ By evenly spaced you mean equidistant one to each other? If so, yes $\endgroup$ – SSC Napoli Feb 24 '15 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 Feb 24 '15 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 Feb 24 '15 at 17:42
2
$\begingroup$

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.

Total[f]
(* 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.

Addendum

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.

$\endgroup$
  • $\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 Feb 24 '15 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 Feb 24 '15 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 Feb 24 '15 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 Feb 24 '15 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 Feb 24 '15 at 18:01

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.