Reputation: 51
I have data that looks like this:
When I plot it it looks like this:
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:
My question is: How would I use this function to interpret the value of a point at 5.111?
Upvotes: 0
Views: 552
Reputation: 3101
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; in your case, these are the lowest 1350 and highest 670000.
If you want to obtain values smaller, you should fit the data to a function that can represent your data points. 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. You should always get the lowest degree that is able to reproduce your data
the python code you should use is:
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)
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
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()
Upvotes: 1