I am trying to fit the cosine squared curve to the data that I have using curve_fit from scipy optimise. BUt unfortunately I am getting a straight line. Hoping that somebody would be able to help me.
import pylab as plb
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy import asarray as ar,exp
import numpy as np
x = ar(range(10))
y = ar([0,1,2,3,4,5,4,3,2,1])
def cosq(x,a,x0):
return a*(np.cos(np.pi*x*x0)**2)
popt,pcov = curve_fit(cosq,x,y,p0=[1,1])
plt.plot(x,y,'b+:',label='data')
plt.plot(x,cosq(x,*popt),'ro:',label='fit')
plt.legend()
plt.title('Blu Blah')
plt.xlabel('Blu')
plt.ylabel('Blah')
plt.show()
I am getting the following graph with the straight line fit as the output