Mathematica: Interpolation Functions

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 5K views
Niles
Messages
1,834
Reaction score
0
Hi

I have a data set of the form:

Code:
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.


Niles.
 
Physics news on Phys.org
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.
 
Yes, split your list into two sublists:

Code:
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:
inter1= Interpolation[data1]

and

Code:
inter2 = Interpolation[data2]

Then, define your function conditionally:

Code:
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.
 
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.Niles.