【发布时间】:2021-04-21 02:44:33
【问题描述】:
我正在尝试将一条线拟合到我的数据集的 9.0 到 10.0 um 范围内。这是我的情节:
不幸的是,这是一个散点图,x 值没有从小数索引到大数,所以我不能只将optimize.curve_fit 函数应用于特定范围的索引以获得@987654325 中的所需范围@值。
以下是我进行曲线拟合的首选程序。我将如何修改它以仅适合 9.0 到 10.0 um x-value 范围(在我的情况下为 x_dist 变量),其点随机分布在整个索引中?
def func(x,a,b): # Define your fitting function
return a*x+b
initialguess = [-14.0, 0.05] # initial guess for the parameters of the function func
fit, covariance = optimize.curve_fit( # call to the fitting routine curve_fit. Returns optimal values of the fit parameters, and their estimated variance
func, # function to fit
x_dist, # data for independant variable
xdiff_norm, # data for dependant variable
initialguess, # initial guess of fit parameters
) # uncertainty in dependant variable
print("linear coefficient:",fit[0],"+-",np.sqrt(covariance[0][0])) #print value and one std deviation of first fit parameter
print("offset coefficient:",fit[1],"+-",np.sqrt(covariance[1][1])) #print value and one std deviation of second fit parameter
print(covariance)
【问题讨论】:
-
您的意见是什么?一个numpy数组?你知道指数还是你想限制拟合的值范围?如果是前者,那么
curve_fit(func, x_dist[start:stop], xdiff_norm[start:stop], ...就足够了。 -
@Mr.T 这是一个 numpy 数组。我的问题是,如果我调用一系列索引,它将不对应于我的 x 值的范围。假设我选择 [200:250],我调用的 50 个索引将在我的整个范围内随机散布 xvalue,这没有多大帮助。我的一个想法是将索引从最小 xvalue 到最大 xvalue 排序,然后我可以调用特定的索引范围。
标签: python scipy curve-fitting scipy-optimize