Simple Interpolation

Question

How do you do simple interpolation?

Answer

This can be done with the module arrayfns. Didn't know you had it, did you? It comes with the Numeric package, even though I couldn't find any documentation of it in the Numeric documentation.

To do this, use the arrayfns.interp function. Assume you have y points located at x and you want to find interpolated y-values (yint) at another set of x-values (xint). This looks like:

>>> import arrayfns
>>> import Numeric
>>> x = Numeric.array([1., 2., 3., 4., 5.])
>>> y = Numeric.array([3., 6., 2.,-5.,-3.])
>>> xint = Numeric.array([3.4, 2.3])
>>> yint = arrayfns.interp(y, x, xint)
>>> yint
array([-0.8, 4.8])

Notes: Thanks to Python FAQTS for showing me how to do this.

Return to the Tips and Examples index page.

Updated: November 15, 2003 by Johnny Lin <email address>. License.