Here is my data in excel I want to fit this data in a sine curve
here is my code,
#Fitting function
def func(x, offset, A, freq, phi):
return offset + A * np.sin(freq * x + phi)
#Experimental x and y data points
# test_df is the input excel df
x_data = test_df['x_data']
y_data = test_df['y_data']
#Plot input data points
plt.plot(x_data, y_data, 'bo', label='experimental-data')
# Initial guess for the parameters
initial_guess = [.38, 2.3, .76, 2.77]
#Perform the curve-fit
popt, pcov = curve_fit(func, x_data, y_data, initial_guess)
print(popt)
#x values for the fitted function
x_fit = np.arange(0.0, 31, 0.01)
#Plot the fitted function
plt.plot(x_fit, func(x_fit, *popt), 'r')
plt.show()
This is the graph.
I think this is not the best fit. I would like to have suggestion to improve the curve fit.