python - How do I fit a a curve well using scipy.optimize.curvefit? -


i have set of data, fit using sine function. have written following code:

import numpy np scipy.optimize import curve_fit import matplotlib.pyplot plt  def sin_curve(rad, a, b, c, d):     result = []     value in rad:         outcome = float(a*np.sin(b*value + c) + d)         result.append(outcome)     return result  # x data => polangle_rad, y data => three_ara [a_300, b_300, c_300, d_300], var_300  = curve_fit(sin_curve, polangle_rad, three_ara, p0=[10,2,-0.5,75])  polplt_xrange = np.arange(0,2*np.pi+unit,unit)  # plotting plt.figure() three_line,_ = ax.plot(polangle_rad, three_ara, 'bo', polplt_xrange, np.array(sin_curve(polplt_xrange, a_300, b_300, c_300, d_300)), 'b-')  plt.show() 

the plot this; fitted curve given blue solid line, while data points given blue dots. enter image description here

from plot, can see fitted curve rotated left. there better way fit curve?

the issue not reproducible. when using generated values, fit perfect. method therefore works fine.

import numpy np scipy.optimize import curve_fit import matplotlib.pyplot plt  def sin_curve(rad, a, b, c, d):     result = []     value in rad:         outcome = float(a*np.sin(b*value + c) + d)         result.append(outcome)     return result  xval = np.linspace(0,2*np.pi,16) yval = -10*np.sin(2*xval-1.5)+75 # x data => polangle_rad, y data => three_ara [a_300, b_300, c_300, d_300], var_300  = curve_fit(sin_curve, xval,                                                      yval, p0=[10,2,-0.5,75])  print [a_300, b_300, c_300, d_300] # [-9.9999999999999556, 1.9999999999999991, -1.4999999999999984, 75.0]  polplt_xrange = np.linspace(0,2*np.pi,100)  # plotting fig = plt.figure() ax = fig.add_subplot(121, projection="polar") ax2 = fig.add_subplot(122)  p, = ax.plot(xval, yval, 'bo') y = np.array(sin_curve(polplt_xrange, a_300, b_300, c_300, d_300)) line, = ax.plot(polplt_xrange,y , 'b-')  ax.set_ylim(none, y.max()*1.1)  ax2.plot(xval, yval, 'bo') ax2.plot(polplt_xrange,y , 'b-')  plt.tight_layout() plt.show() 

enter image description here


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -