我试图将多个数据集拟合到同一个方程中,并找到它们之间的拟合参数值。有两个自变量,我想我已经处理过了。我最终得到了一个对单个数据集有效的程序,但对多个数据集无效。代码本身有效,但合身的样子像蝴蝶结(一条直线和一条曲线在末端相连)而不仅仅是一条曲线。我希望每个数据集都有单独的曲线,参数都有共享的值。我知道我需要以某种方式分解数据,也许是通过堆叠数据和使用索引调整函数,但是我对我找到的例子感到困惑,不知道如何在这里执行它们。下面是代码:
# import things
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
## set-up data##
# Have x-data as numpy array
xfrac = [1., 0.85,0.725,0.6,0.5,0.4,0.]
x = np.concatenate((xfrac,xfrac))
# Write function to generate and populate arrays using ideal values
# data sets (I have pasted the values instead of posting the code used to calculate them)
mix_850 = [1.701 3.642865 4.6762 5.0739 5.5177 5.9923 6.9408]
mix_1000 = [1.651185 3.53359 4.4854 4.8978 5.32525 5.7388 6.792]
dat = np.concatenate((mix_850,mix_1000))
# Temperature values
c=np.repeat(850.,7.)
d=np.repeat(1000.,7.)
Temp = np.concatenate((c,d))
# Define function
def f(Z, a1, b1, a2, b2):
x1,T= Z
x2= 1.-x1
excess = a1+b1+(a2+b2*T)*(x1-(1.-x1)*(x1*(1.-x1)))
ideal = ((x1*25.939)+((1.0-x1)*314.02))/(((x1*25.939)/1.701-0.3321e-3*T)+(((1.0-x1)*314.02)/7.784-0.9920e-3*T))
mix = excess + ideal
return mix
# Fitting
popt,_ = curve_fit(f,(x,Temp),dat)
fit_a1 = popt[0]
fit_b1 = popt[1]
fit_a2 = popt[2]
fit_b2 = popt[3]
1条答案
按热度按时间oug3syen1#
将
xfrac
定义为np.array
:在绘图中使用
xfrac
而不是x