Reputation: 21
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'
Upvotes: 0
Views: 2235
Reputation: 1046
You are using curve_fit totally wrong. Here is working example from the help of curve_fit and some additional plotting:
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. You should normally also use the optional input argument p0, which is an initial guess for the solution.
Upvotes: 1