简体   繁体   English

使用python的curve_fit()

[英]curve_fit() using python

def model(A, x, mu, sigma):
return A*exp(-((x-mu)**2)/(2*sigma**2))
from scipy.optimize import curve_fit
mu=np.mean(d_spacing_2)
sigma=np.std(d_spacing_2)
f=intensity_2
x=d_spacing_2
popt, pcov = curve_fit(model, A, x, mu, sigma)

TypeError: model() missing 2 required positional arguments: 'mu' and 'sigma' TypeError:model()缺少2个必需的位置参数:“ mu”和“ sigma”

You are using curve_fit totally wrong. 您正在使用curve_fit完全错误。 Here is working example from the help of curve_fit and some additional plotting: 这是来自curve_fit和一些其他绘图的工作示例:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(x, a, b, c):
    return a * np.exp(-b * x) + c

xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
ydata = y + 0.2 * np.random.normal(size=len(xdata))

popt, pcov = curve_fit(func, xdata, ydata,p0=[2,1,1])

plt.ion()
plt.plot(xdata,ydata,'o')
xplot = np.linspace(0,4,100)
plt.plot(xplot,func(xplot,*popt))

The first input argument of curve_fit is the function the second the x values of the data and the third the y values. curve_fit的第一个输入自变量是函数,第二个是数据的x值,第三个是y值。 You should normally also use the optional input argument p0, which is an initial guess for the solution. 通常,您还应该使用可选的输入参数p0,这是对解决方案的初步猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM