简体   繁体   English

如何使用非线性插值确定曲线上的单个值

[英]How do I use nonlinear interpolation to determine a single value on a curve

I have data that looks like this:我的数据如下所示:

非线性数据

When I plot it it looks like this:当我 plot 它看起来像这样:

在此处输入图像描述

I have been able to use code like this to fit a non-linear curve to my data:我已经能够使用这样的代码来拟合我的数据的非线性曲线:

df = pd.read_csv('data.csv')

X = df['Molecular Weight']
y = df['Time 1']

f = interpolate.interp1d(X, y)
f2 = interpolate.interp1d(X, y, kind='cubic')

plt.plot(X, y, 'o', X, f2(X), '-')
plt.legend(['data', 'cubic spline'], loc='best')
plt.show()

Gives me a plot like this:给我一个 plot 像这样:

在此处输入图像描述

My question is: How would I use this function to interpret the value of a point at 5.111?我的问题是:我将如何使用这个 function 来解释 5.111 点的值?

you will never be able to use the function you have used to interpolate the point to obtain values outside of the maximum and minimum of the points used to interpolate;您将永远无法使用您用来插值点的 function 来获得用于插值的点的最大值和最小值之外的值; in your case, these are the lowest 1350 and highest 670000.在您的情况下,这些是最低的 1350 和最高的 670000。

If you want to obtain values smaller, you should fit the data to a function that can represent your data points.如果您想获得更小的值,您应该将数据拟合到可以表示您的数据点的 function。 In your case, I think a second-order polynomial is enough, and you can't go over a fourth-order because you only have 5 points.在您的情况下,我认为二阶多项式就足够了,并且您不能 go 超过四阶,因为您只有 5 个点。 You should always get the lowest degree that is able to reproduce your data您应该始终获得能够重现数据的最低程度

the python code you should use is:您应该使用的 python 代码是:


X = [670000, 158000, 44000, 17000, 1350]
y = [4.19, 5.469, 6.554, 7.293, 9.109]

polynom = np.polyfit(X, y, 2)
f2 = np.poly1d(polynom)

plt.plot(X, y, 'o', X, f2(X), '-')
plt.legend(['data', 'cubic spline'], loc='best')
plt.show()

在此处输入图像描述

from this f2 you are able to get the values at 5.11从这个 f2 你可以得到 5.11 的值

f2(5.11)

Things a part, I think is better when you plot the fit to overexpress the vector x to have a smooth curve when you plot the data事情的一部分,我认为当你 plot 适合过度表达向量 x 以在你 plot 数据时具有平滑曲线时会更好

x_fit = np.linspace(1350, 670000, 1000)
plt.plot(X, y, 'o', x_fit, f2(x_fit), '-')
plt.legend(['data', 'cubic spline'], loc='best')
plt.show()

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM