简体   繁体   English

拉格朗日插值Python

[英]lagrange interpolation Python

I am trying to use scipy.interpolate.lagrange to interpolate the following data, which is of degree 8: 我正在尝试使用scipy.interpolate.lagrange插值以下数据,其程度为8:

x_data = [1900., 1910., 1920., 1930., 1940., 1950., 1960., 1970., 1980.]

y_data = [76212168., 92228496., 106021537., 123202624., 132164569., 151325798., 179323175., 203302031., 226542199.]

with python code: poly = scipy.interpolate.lagrange(x_data, y_data) 使用python代码: poly = scipy.interpolate.lagrange(x_data, y_data)

But the output looks not correct because even none of the (x_data[i], y_data[i]) pairs lies on the 'poly' I got from the scipy.interpolate.lagrange call. 但是输出看起来不正确,因为(x_data[i], y_data[i])对甚至都不位于我从scipy.interpolate.lagrange调用中获得的“ poly”上。

Could anybody give any hints or suggestion? 有人可以提供任何提示或建议吗? Thanks so much. 非常感谢。

Your values are poorly scaled, and, as the lagrange docstring says, "Warning: This implementation is numerically unstable." 您的值缩放比例很差,并且,正如lagrange文档字符串所说,“警告:此实现在数值上是不稳定的。” Try applying lagrange to, say, the "whitened" data (ie shift and scale the data to have mean 0 and standard deviation 1). 尝试将lagrange应用于“变白”的数据(即,对数据进行移位和缩放以使其平均值为0,标准差为1)。 For example, 例如,

xm = np.mean(x_data)
xscale = np.std(x_data)
ym = np.mean(y_data)
yscale = np.std(y_data)
x = (x_data - xm) / xscale
y = (y_data - ym) / yscale
poly = scipy.interpolate.lagrange(x, y)

( np comes from import numpy as np .) np来自import numpy as np 。)

Then to use poly on "raw" (ie unscaled) data, use the same transform on the x input when you call poly , and undo the y transform on the values returned by poly . 然后用poly对“生”(即未缩放)数据,使用在相同的转换x输入,当你调用poly ,并撤消y对返回的值转换poly Eg if xx is an array with values in the interval [1900, 1980]: 例如,如果xx是一个值在[1900,1980]之间的数组:

yy = poly((xx - xm)/xscale)*yscale + ym

Before you spend too much time on this, though, I have to ask: Why are you using Lagrange interpolation? 但是,在花太多时间之前,我必须问:为什么要使用Lagrange插值? It is an important theoretical tool, but it is not so good for practical data analysis (see http://en.wikipedia.org/wiki/Lagrange_polynomial#Notes ; in particular, note the occurrence of Runge's phenomenon). 它是一种重要的理论工具,但对实际数据分析却不太好(请参阅http://en.wikipedia.org/wiki/Lagrange_polynomial#Notes ;尤其要注意Runge现象的发生)。 Why do you need to interpolate at all? 为什么根本需要插值? What are you going to do with the interpolator? 您要如何使用插补器? If you have answers to those questions, you should include them as part of the question. 如果您对这些问题有答案,则应将其作为问题的一部分。

Here create its own function a lagrange interpolation. 在这里创建一个自己的函数lagrange插值。 Maybe you're using her 也许你在用她

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python中的拉格朗日插值 - Lagrange interpolation in Python python如何正确完成拉格朗日插值 - How to complete Lagrange Interpolation correctly by python Python中的拉格朗日插值:作为结果的马特公式 - Lagrange interpolation in Python: as a result matematical formula 用Python进行Lagrange插值做奇怪的事情 - Lagrange interpolation with Python doing something weird 如何在Python中使用Z3查找Lagrange插值多项式? - How to find Lagrange Interpolation Polynomial with Z3 in Python? 使用Python进行Lagrange多重BFGS优化 - Lagrange multipler BFGS optimization with python 如何在我的拉格朗日插值算法中获得拉格朗日多项式的 output 而不是其近似值 - How do I get output of Lagrange Polynomials instead of its approximation value on my lagrange interpolation algorithm 牛顿插值多项式的 plot 必须与区间上的原始 function 和拉格朗日插值多项式的 plot 完全匹配吗? - Must plot of Newton interpolation polynomial match completely the plot of original function and Lagrange interpolation polynomial on the interval? 在Python中使用Lagrange乘数的Numpy Arange错误 - Numpy arange error with Lagrange Multiplier in Python Statsmodels(Python):Breusch Godfrey Lagrange乘数检验 - Statsmodels (Python): Breusch Godfrey Lagrange Multiplier tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM