I have the following dataset from a mechanical indentation test:
https://www.dropbox.com/s/jovjl55sjjyph3r/Test%20dataset.csv?dl=0
The graph shows the displacement of a spherical probe vs force recorded. I need to fit these data with specific equations (DMT model if you are familiar with it). I have produced the code below, but I am unable to get a good fitting result. The code gives no error or warnings, so I don't know if the problem is on the plotting or on the actually fitting.
Did I write the fitting code correctly? Did I pass the variables Fad and R correctly into the function? Is the code that plots the fitting curve correct?
Also, in the code you can notice 2 different fitting functions. Function1 is based on 2 equations:
a = ((R/K)*(x+Fad))^(1/3)
y = ((a^2)/R)
Function2 is the same as function1 but the 2 equations are combined in a single equation. The funny thing is that they give 2 different plots!
Importantly, I'd like to use the 2 equations method because there are other more complex models that I should use to fit the same dataset. In these models the equations cannot be combined that easily like in this case.
Any help from the Community to solve this problem would be very much appreciated.
import pandas
from matplotlib import pyplot as plt
from scipy import optimize
import numpy as np
df = pandas.read_table("Test dataset.csv", sep = ',', header=0)
df = df.astype(float) #Change data from object to float
print(df.shape)
print(df)
df_Fad = -(df.iloc[0, 0])
print("Adhesion force = {} N".format(df_Fad))
R = 280*1e-6
print("Probe radius = {} m".format(df_Fad))
df_x = df["Corr Force A [N]"].to_list()
df_y = df["Corr Displacement [m]"].to_list()
#Define fitting function1
def DMT(x, R, K, Fad):
a = ((R/K)*(x+Fad))**(1/3)
return ((a**2)/R)
custom_DMT = lambda x, K: DMT(x, R, K, df_Fad) #Fix Fad value
pars_DMT, cov_DMT = optimize.curve_fit(f=custom_DMT, xdata=df_x, ydata=df_y)
print("K = ", round(pars_DMT[0],2))
print ("E = ", round(pars_DMT[0]*(4/3),2))
ax0 = df.plot(kind='scatter', x="Corr Force A [N]", y="Corr Displacement [m]", color='lightblue')
plt.plot(df_x, DMT(np.array(df_y), pars_DMT[0], R, df_Fad), "--", color='black')
ax0.set_title("DMT fitting")
ax0.set_xlabel("Force / N")
ax0.set_ylabel("Displacement / m")
ax0.legend(['Dataset'])
plt.tight_layout()
#Define fitting function2 => function2 = funtion1 in one line
def DMT2(x, Fad, R, K):
return ((x+Fad)**(2/3))/((R**(1/3))*(K**(2/3)))
custom_DMT2 = lambda x, K: DMT2(x, df_Fad, R, K) #Fix Fad value
pars_DMT2, cov_DMT2 = optimize.curve_fit(f=custom_DMT2, xdata=df_x, ydata=df_y)
print("K = ", round(pars_DMT2[0],2))
print ("E = ", round(pars_DMT2[0]*(4/3),2))
ax1 = df.plot(kind='scatter', x="Corr Force A [N]", y="Corr Displacement [m]", color='lightblue')
plt.plot( df_x, DMT2(np.array(df_y), pars_DMT2[0], df_Fad, R), "--", color='black')
ax1.set_title("DMT fitting")
ax1.set_xlabel("Force / N")
ax1.set_ylabel("Displacement / m")
ax1.legend(['Dataset'])
plt.tight_layout()
plt.show()