# Integrate a function from samples using computer codes

I have a function $$c ( I (\vec{r}) )$$. Not a constant, $$c$$ doesn't denote a constant. So $$c$$ is a function of $$I$$ which is a function of $$\vec{r}$$. $$I$$ is an intensity (W/cm2).

This $$c$$ is hard to sample and I have sampled it for 10,000 values of $$I$$.

I need to integrate $$c$$ across all the space, i.e. a 3Dimensional integral: $$d^3 r$$.

$$\int\int\int c(I(\vec{r})) d^3 r$$

I want to use the already sampled 10,000 $$c$$'s.

Is there a method to numerically integrate my $$c$$ using those samples I already have? The values of $$I$$ at which $$c$$ is sampled are equally spaced in logspace. $$c$$ dies (goes to 0) apart from a very small region in the $$d^3 r$$ space.

I only have $$c(I)$$ and not $$c(\vec{r})$$. I can create a routine which outputs $$I$$ from a vector $$\vec{r}$$ if needed. Again, the sampling of $$c(I)$$ is hard and I cannot sample $$c(\vec{r})$$, i.e. directly sample $$c$$ from a vector $$\vec{r}$$, but only from a value of $$I$$.

A picture to show how $$c(I)$$ looks like is:

• Have you attemted to change integration variables in the analytic expression? What happens? – Norbert Schuch Mar 8 at 14:07
• There's no $\phi$ dependence in $I(\vec{r})$ in cylindrical coordinates, so the integral is 2Dimensional because the $\phi$ integrates to $2*pi$. However, this doesn't change the problem by much, I still need to use the already sampled $c$'s and not to sample any more $c$'s (at any other location in $d^3 r$ space, i.e at any other $I(\vec{r})$) – velenos14 Mar 8 at 14:11
• You should change the integration variable to $I$, obviously! if you can compute the dI/dr arising in the change of variables, you are fine. – Norbert Schuch Mar 8 at 14:28
• Would Computational Science be a better home for this question? – Qmechanic Mar 8 at 15:01
• Do you know anything about the spatial distribution of the points? Maybe they're distributed randomly but uniformly in a box, or sphere, or in a Gaussian fashion, etc. In that case the suggestion of @nicoguaro below is the right path. Otherwise I guess I'd ask how you know all your readings of $I$ didn't come from the same single spatial point. I hope this isn't tax money at work. – A rural reader Mar 10 at 23:11

It sounds like you want to do a numeric integration of the function, c, over volume. Start by choosing the approximate number of volume elements you want to use. These should be in the same coordinate system that defines the function, I. Then the location of each volume element defines the corresponding, I. For each of these you will need to scan your data to find an approximate corresponding value for, c. You may want to do an interpolation between values of, c, which are too high or too low. For the integration, do a cumulative sum with the results of these calculations.

• Thank you, interpolation was the ''direction'' I need to pursue/read about. – velenos14 Mar 8 at 17:06
• Before scanning you might want to sort your data in terms of increasing, I. (A sprad-sheet will do this for you). – R.W. Bird Mar 8 at 20:09
• It is , it is sorted :) Many thanks! I was trying very hard to use just those already computed samples, not even thought that it is so easy to interpolate because it's such a smooth function, so basically I can find my function at any given input I need... I now have a splines interpolator and I query it at the ''locations'' I need in the integration routine. – velenos14 Mar 10 at 18:28

If your samples were done following a grid, then you could integrate it using a quadrature method. These methods are available in several programming languages, such as Python or Matlab.

Also, as mentioned by R.W Bird, you could interpolate your data and integrate this polynomials. If the data follows a grid, this is the same method as before. If not, then you would need to build the interpolator that can be global or local.

Another option, if you chose uniformly distributed random points, is to use a Monte Carlo method to compute the integral. In that case, the integral would be

$$\frac{V}{N} \sum_{n=1}^{N} c(I(\mathbf{r}_i)) \, ,$$

where $$V$$ is the volume of the region, and $$N$$ the number of samples.

• thanks! I built a splines interpolator and I am querying it at the locations I need. I am doing a mid-point rule in 2Dims (my problem is azimuthally symmetric, thus the reduction from 3D->2D) using this interpolator – velenos14 Mar 10 at 18:29