Python-Matplotlib中的曲线拟合

问题描述 投票:0回答:1

仅想说明一下我是python的初学者。我的大部分知识是我在物理实验室中用于数据分析/绘图的知识。

我基本上收集到的电容数据是温度的函数,我应该用函数拟合绘制的数据以找到其最佳参数。

请注意,我为初始值输入了随机数,然后运行了代码。我的POpt函数为我提供了新的最佳参数,我用其替换为初始随机数

fig, plot = plt.subplots(figsize=(40,40))

def cap(T, Cmax, K, TC, gamma): #this is the function I am fitting to my data
    return Cmax/(1+K*(T-TC)**gamma)
CmaxInit = 5.16061523 #these are the optimal parameters it gave me
KInit = 3.87292298e-05
TCInit = 3.00020150e+01
gammaInit =  2.74812849
fmodel = np.linspace(0, 100, 1000)
plt.plot(fmodel, cap(fmodel, CmaxInit, KInit, TCInit, gammaInit), linewidth=8, label='Fitted model')

plot.errorbar(temperature, capacitance, linewidth=8, label='Data Points') #this is my data of temperature vs capacitance

pOpt, pCov = curve_fit(cap, temperature, capacitance, p0=[CmaxInit, KInit, TCInit, gammaInit], absolute_sigma=True) #this is what I use to curve-fit/get my optimal parameters
print("[CmaxOpt KOpt TCOpt gammaOpt] =", pOpt)
print()
print("pCov =") #This is a co-variant matrix function that calculates my error values
print(pCov)

plt.xticks(fontsize=60)
plt.yticks(fontsize=60)
plt.grid()
plt.legend(fontsize=80)
plt.show()

但是绘制我的拟合模型时,它给了我:

电容与温度的关系

“”

POpt函数在某种程度上确实符合总体外观,但显然与之相去甚远。我不明白为什么,但是我的猜测可能是我要优化的参数数量。

编辑:将初始参数更改为

CmaxInit = 6 
KInit = 0.01
TCInit = 50
gammaInit =  2

产生的“

但是现在在计算最佳参数时产生了错误。

[CmaxOpt KOpt TCOpt gammaOpt] = [nan nan nan nan]

编辑2:切碎我的数据后,我现在尝试调整“

但是我仍然得到

[CmaxOpt KOpt TCOpt gammaOpt] = [nan nan nan nan]

一个指数函数似乎比我应该建模的方程更好地拟合。也许这就是为什么我没有获得最佳参数的原因?

python matplotlib data-modeling curve-fitting
1个回答
0
投票

拟合函数存在一些数字问题:

  • 该算法似乎很难具有指数(gamma)作为拟合参数。这似乎也适用于exp(gamma)
  • 我将拟合函数替换为Cmax*np.exp(-K*(T-TC)**2) + c。取决于x范围(数据窗口)拟合度如何。
  • 出于数字原因,最好使用截距c
  • 通常,通过转换将数据带入拟合函数的“容易区域”是一个好主意,因此峰值约为零,x范围约为[-5 .. + 5]。拟合后,将结果转换回原始数据范围。
  • 适合(data)的log(data)有时也会有所帮助。

...

def cap(T, Cmax, K, TC, gamma):         
    return Cmax/(1+K*(T-TC)**gamma)

def func_1(T, Cmax, K, TC, c):
    return Cmax*np.exp(-K*(T-TC)**2) + c

#--- generate data --------------------
CmaxInit =  5.0         # 5.16061523      
KInit =     3.0e-3      # 3.87292298e-05
TCInit =    30.0         # 3.00020150e+01
gammaInit = 2.0         # 2.74812849
cInit =     0.0         # intercept

fmodel = np.linspace(20, 50, 280)
x0 = fmodel.copy()

y0 = cap   (x0, CmaxInit, KInit, TCInit, gammaInit)
y1 = func_1(x0, CmaxInit, KInit, TCInit, cInit)
y_noise = 0.05 * np.random.normal(size=x0.size)
Y0 = y0 + y_noise
Y1 = y1 + y_noise

#--- fit the data by a function ------------
pOpt, pCov = curve_fit(func_1, x0, Y0,  p0=[CmaxInit, KInit, TCInit, cInit], absolute_sigma=True)
Yfit = func_1(x0, *pOpt)

#--- print the results ---------------------------
print("CmaxInit, KInit, TCInit, c", CmaxInit, KInit, TCInit, c)
print("[CmaxOpt KOpt TCOpt cOpt] =", pOpt); print()
print("pCov ="); print(pCov)

#---- graphics --------------------------------------------
fig, plot = plt.subplots(figsize=(12, 12))
plt.plot(x0, Y0, linewidth=2, label='simulated data with CAP')
plt.plot(x0, Y1, ls=':', lw=1,label='simulated data with EXP')
plt.plot(x0, Yfit,  label='Yfit: Cmax=%5.3f, K=%5.3f, TC=%5.3f, c=%5.3f' % tuple(pOpt))
plt.legend(); plt.savefig('pic3.jpg'); plt.show()

..enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.