I am confused on how this function is supposed to work. I am trying to fit a line of best fit for the data sets of xdata and ydata. This code produces errors, but I do not understand how the curve_fit function is supposed to work or if I have used the right terms for what I wanted to do. xdata and ydata plotted on a graph produces a curved patter, going up, and then dropping down. Any help much appreciated.
#define xdata dnd ydata
xdata = np.array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9])
ydata = np.array([0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-
1.42001])
# get the curve fit funtion
from scipy.optimize import curve_fit
def func(xdata, p1, p2):
return p1*np.cos(p2*xdata) + p2*np.sin(p1*xdata)
popt, pcov = curve_fit(func, xdata, ydata)
#plot code for data points
plot.plot(xdata,ydata,"bo",label="Xdata and Ydata")
plot.plot(popt,pcov,"r--",label="Curve of Best Fit")
plot.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plot.show()
This currently produces a straight line which is not the curve of best fit for the data. I am trying to mirror the data pattern as best as possible, and currently I am nowhere close.

xin the function? Do you meanxdata?np.cosandnp.sininstead.