【发布时间】:2021-03-26 00:32:10
【问题描述】:
我有以下来自机械压痕测试的数据集:
https://www.dropbox.com/s/jovjl55sjjyph3r/Test%20dataset.csv?dl=0
该图显示了球形探头的位移与记录的力的关系。我需要用特定的方程拟合这些数据(如果你熟悉 DMT 模型)。 我已经生成了下面的代码,但我无法得到一个好的拟合结果。代码没有给出错误或警告,所以我不知道问题出在绘图上还是实际拟合上。
我是否正确编写了拟合代码?我是否将变量 Fad 和 R 正确传递到函数中?绘制拟合曲线的代码是否正确?
此外,在代码中,您可以注意到 2 个不同的拟合函数。 Function1 基于 2 个方程:
a = ((R/K)*(x+Fad))^(1/3)
y = ((a^2)/R)
Function2 与 function1 相同,但两个方程组合在一个方程中。有趣的是,他们给出了 2 个不同的情节!
重要的是,我想使用 2 方程方法,因为我应该使用其他更复杂的模型来拟合相同的数据集。在这些模型中,方程不能像这种情况那样容易地组合起来。
非常感谢社区为解决此问题提供的任何帮助。
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()
【问题讨论】:
标签: python pandas matplotlib curve-fitting model-fitting