Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Integrating over data sets?

  1. Nov 23, 2011 #1
    I've got a Mathematica question which might be quite basic, but I couldn't find much about it in the documentation (possibly because it's so basic) so please bear with me!

    I have a set of data, call it xi(ρ), which I want to integrate over some distribution function (log-normal in this case) given by f(ρ). In particular I want to compute the integral

    ∫ x(ρ) ρ f(ρ) dρ

    from 0 to infinity (although since I don't have xi(ρ) from 0 to infinity I'd cut the calculation off at some upper and lower bound where f(ρ) becomes negligible).

    So I have a functional form for ρ f(ρ), but only have values for x(ρ) at discrete values of ρ. How can I do this integral accurately in Mathematica?
  2. jcsd
  3. Nov 23, 2011 #2


    User Avatar
    Science Advisor
    Homework Helper

    I think I don't completely understand the question..
    What is xi(ρ)? Is it a set of functions of ρ? In that case, what is x(ρ) in the integrand, is it computed from the xi(ρ)?

    Or do you mean that you have values for discrete ρ only, so that we're actually talking about x(ρi)? In that case, wouldn't you normally do something like
    [tex]\sum_{i} x(\rho_i) \rho_i f(\rho_i) \text{?}[/tex]
  4. Nov 23, 2011 #3
    Yeah, I meant x(ρi) as you said - I was typing this post in a hurry! I could do that sum myself fairly simply in C but I figured Mathematica probably has more accurate numerical integration techniques. Since the log-normal distribution has some fairly steep gradients I'm worried I might not get the most accurate answers by doing a simple sum like that.

    So the idea is there's some underlying function x(ρ) for which I want to do that integral, but the function x(ρ) is pretty complicated and has to be computed numerically by a code that I downloaded. I tell this program ρ and it gives me x, so I'm thinking of having it compute x(ρ) for some large range of ρ, importing those numbers into Mathematica and doing the integral.

    Hope that makes sense!
  5. Nov 23, 2011 #4


    User Avatar
    Science Advisor
    Homework Helper

    Yep, it makes more sense now.
    So you have a large discrete set of function values, and you would like to make a continuous function that you can use in the integration, right?

    In that case, maybe you should have a look at the InterpolatingFunction. I don't have Mathematica at hand, but you could try something like
    Code (Text):

    points = { {rho1, x1}, {rho2, x2}, ...., {rho10000, x10000}};
    x = Interpolation[points];
    NIntegrate[x[rho] rho f[rho], {rho, rho1, rho10000}]
  6. Nov 25, 2011 #5
    Thanks, this looks like exactly what I need, I'll try this!
  7. Nov 29, 2011 #6
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook