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

Mathematica: Interpolation Functions

  1. Mar 2, 2012 #1
    Hi

    I have a data set of the form:

    Code (Text):
    data = {{0, 0}, {1, 1}, {2, 2}, {3, 20}, {4, 1}, {20, 1}, {21, 1}, {22, 0}};
    This data set is a probability density function (PDF), and I need to be able to integrate it from 0 to some x<22. I thought that I would use an interpolating function for this. However, there is a "gap" between 4 and 20, where the PDF is zero, but an interpolating function doesn't take that into account by itself (I think). Is there a smarter way of accounting for this other than manually inserting data points?

    The second problem is that ListInterpolation only takes a 1D-list. So I can only feed it the 2nd coordinate of the data points. However, I need somehow for the interpolation function to run from 0 to 22. I don't know how to do that.

    I appreciate any help and suggestions.

    Best regards,
    Niles.
     
  2. jcsd
  3. Mar 2, 2012 #2
    As
    http://reference.wolfram.com/mathematica/ref/Interpolation.html
    shows, Interpolation happily accepts a list of {x,f[x]}.

    For example
    In[1]:= f=Interpolation[{{0,0},{1,1},{2,2},{3,20},{4,1},{20,1},{21,1},{22,0}}]
    Out[1]= InterpolatingFunction[{{0,22}},<>]

    But the question becomes whether this is an adequate PDF.

    In[2]:= Table[{t,f[t]},{t,0,22}]
    Out[2]= {{0, 0}, {1, 1}, {2, 2}, {3, 20}, {4, 1}, {5, -709/51}, {6, -1279/51}, {7, -1678/51}, {8, -1925/51}, {9, -2039/51}, {10, -2039/51}, {11, -648/17}, {12, -591/17}, {13, -515/17}, {14, -1279/51}, {15, -994/51}, {16, -709/51}, {17, -443/51}, {18, -215/51}, {19, -44/51}, {20, 1}, {21, 1}, {22, 0}}

    That clearly shows that the interpolation fits your given points exactly, but is not PDF-aware and you need to find a radically different approach to your problem to come up with something that will be non negative and with an area=1.
     
  4. Mar 2, 2012 #3
    Yes, split your list into two sublists:

    Code (Text):

    data1 = {{0, 0}, {1, 1}, {2, 2}, {3, 20}, {4, 1}}
     
    and

    data2 = {{20, 1}, {21, 1}, {22, 0}}

    An interpolating function is given only over the range where the data set is given. That is why you introduce two interpolating functions:

    Code (Text):
    inter1= Interpolation[data1]
    and

    Code (Text):
    inter2 = Interpolation[data2]
    Then, define your function conditionally:

    Code (Text):

    f[x_] := 0 \; x < 0 || (x > 4 && x < 20) || x > 22;
    f[x_] := inter1[x] \; x >= 0 && x <= 4;
    f[x_] := inter2[x] \; x >= 20 && x <= 22;
     
    Now f[x] is a good function.
     
  5. Mar 7, 2012 #4
    Thanks for both your suggestions, that is very kind. You are right Bill Simpson, I need some other way to do this. I'll have to think about this for some time.

    Thanks for the help.

    Best regards,
    Niles.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook