What algorithm used in interp1d function in scipy.interpolate

我的梦境 提交于 2019-12-02 09:43:42

问题


So i was writing a python program for my numerical course, and I had to code a cubic spline program. So i implement the formula for cubic spline given in books like Numerical methods by Chapra and canale and Numerical mathematics by chenny and kincaid.

so my data is

x=[1.0,3.0,4.0,7.0]
y=[1.5,4.5,9.0,25.5]

Using this data and applying cubic spline I get for x=1.5 , y=1.79122340426

While using this same data but using the scipy function gives:

  >>> scipy.interpolate.interp1d(x, y, kind='cubic')(1.5)
array(1.265624999999932)

So, why is that difference in result? It is obvious that they are not using the same formula. What is the cubic spline formula used in that scipy function? Is it a natural cubic spline formula or something improved? Note: The value 1.2656 is more acurate.


回答1:


EDIT: @ev-br in the comments for this answer provided an important correction to my answer. In fact interp1D spline is not FITPACK based. Check the comments for the link provided by @ev-br.

The Scipy functions for curve fitting are based on FITPACK. Try seeing the documentation on the functions you are using and you'll be able to see a "References" chapter where something like this will appear:

Notes
-----
See splev for evaluation of the spline and its derivatives. Uses the
FORTRAN routine curfit from FITPACK.
If provided, knots `t` must satisfy the Schoenberg-Whitney conditions,
i.e., there must be a subset of data points ``x[j]`` such that
``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``.
References
----------
Based on algorithms described in [1]_, [2]_, [3]_, and [4]_:
.. [1] P. Dierckx, "An algorithm for smoothing, differentiation and
   integration of experimental data using spline functions",
   J.Comp.Appl.Maths 1 (1975) 165-184.
.. [2] P. Dierckx, "A fast algorithm for smoothing data on a rectangular
   grid while using spline functions", SIAM J.Numer.Anal. 19 (1982)
   1286-1304.
.. [3] P. Dierckx, "An improved algorithm for curve fitting with spline
   functions", report tw54, Dept. Computer Science,K.U. Leuven, 1981.
.. [4] P. Dierckx, "Curve and surface fitting with splines", Monographs on
   Numerical Analysis, Oxford University Press, 1993.

These references in particular where taken from the source of fitpack.py on the function "splrep". If you need to do a very thorough comparison between your algorithm and the spline from interp1D just go to the docs:

scipy.interpolate.interp1d

And you'll see a link called [source] right after the definition of the name of the function (so: scipy.interpolate.interp1D [source]). Remember that there's a lot of routine handlers on these functions so be patient while navigating the source.



来源:https://stackoverflow.com/questions/36088340/what-algorithm-used-in-interp1d-function-in-scipy-interpolate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!