【发布时间】:2020-02-16 23:15:51
【问题描述】:
我有一个如下图所示的振荡数据,并希望对其拟合正弦曲线。但是,我的结果不正确。
我想拟合这条曲线的函数是:
def radius (z,phi, a0, k0,):
Z = z.reshape(z.shape[0],1)
k = np.array([k0,])
a = np.array([a0,])
r0 = 110
rs = r0 + np.sum(a*np.sin(k*Z +phi), axis=1)
return rs
正确的解决方案可能如下所示:
r_fit = radius(z, phi=np.pi/.8, a0=10,k0=0.017)
plt.plot(z, r, label='data')
plt.plot(z, r_fit, label='fitted curve')
plt.legend()
然而,我拟合曲线的结果是:
from scipy.optimize import curve_fit
popt, pcov = curve_fit(radius, xdata=z, ydata=r)
r_fit = radius(z, *popt)
plt.plot(z, r, label='data')
plt.plot(z, r_fit, label='fitted curve')
plt.legend()
我的数据也如下:
r = np.array([100.09061214, 100.17932773, 100.45526772, 102.27891728,
113.12440802, 119.30644014, 119.86570527, 119.75184665,
117.12160143, 101.55081608, 100.07280857, 100.12880236,
100.39251753, 103.05404178, 117.15257288, 119.74048706,
119.86955437, 119.37452005, 112.83384329, 101.0507198 ,
100.05521567])
z = np.array([-407.90074345, -360.38004677, -312.99221012, -266.36934609,
-224.36240585, -188.55933945, -155.21242348, -122.02778866,
-87.84335638, -47.0274899 , 0. , 47.54559191,
94.97469981, 141.33801462, 181.59490575, 215.77219256,
248.95956379, 282.28027286, 318.16440024, 360.7246922 ,
407.940799 ])
由于我的函数仅表示傅立叶级数,因此我也尝试了 scipy.fftpack.fft(r),但无法重现与我计算 fft 的信号接近的信号。
【问题讨论】:
-
我收到
name 'curve_fit' is not defined。你能添加你所做的所有导入吗? -
@peer:这里:从 scipy.optimize 导入曲线拟合
标签: python scipy regression curve-fitting non-linear-regression