如何将正弦曲线拟合到小数据集

发布于 2025-01-23 07:14:14 字数 2590 浏览 1 评论 0原文

我一直在努力,显然没有理由试图将犯罪功能适应类似于正弦的小数据集。我查看了许多其他问题,并尝试了不同的库,似乎找不到我的代码中的任何明显的错误。同样在许多答案中,人们将功能拟合到y = f(x)的数据上;但是我从恒星光谱中独立地检索了我的这两个列表。

这些是参考的列表:

time = np.array([2454294.5084288 , 2454298.37039515, 2454298.6022165 ,
   2454299.34790096, 2454299.60750029, 2454300.35176022,
   2454300.61361622, 2454301.36130122, 2454301.57111912,
   2454301.57540159, 2454301.57978822, 2454301.5842906 ,
   2454301.58873511, 2454302.38635047, 2454302.59553152,
   2454303.41548415, 2454303.56765036, 2454303.61479213,
   2454304.38528718, 2454305.54043812, 2454306.36761011,
   2454306.58025083, 2454306.60772791, 2454307.36686591,
   2454307.49460991, 2454307.58258509, 2454308.3698358 ,
   2454308.59468672, 2454309.40004997, 2454309.51208756,
   2454310.43078368, 2454310.6091061 , 2454311.40121502,
   2454311.5702085 , 2454312.39758274, 2454312.54580053,
   2454313.52984047, 2454313.61734047, 2454314.37609003,
   2454315.56721061, 2454316.39218499, 2454316.5672538 ,
   2454317.49410168, 2454317.6280825 , 2454318.32944441,
   2454318.56913047])
velocities = np.array([-2.08468951, -2.26117398, -2.44703149, -2.10149768, -2.09835213,
   -2.20540079, -2.4221183 , -2.1394637 , -2.0841663 , -2.2458154 ,
   -2.06177386, -2.47993416, -2.13462117, -2.26602791, -2.47359571,
   -2.19834895, -2.17976339, -2.37745005, -2.48849617, -2.15875901,
   -2.27674409, -2.39054554, -2.34029665, -2.09267843, -2.20338104,
   -2.49483926, -2.08860222, -2.26816951, -2.08516229, -2.34925637,
   -2.09381667, -2.21849357, -2.43438148, -2.28439031, -2.43506056,
   -2.16953358, -2.24405359, -2.10093237, -2.33155007, -2.37739938,
   -2.42468714, -2.19635302, -2.368558  , -2.45959665, -2.13392004,
   -2.25268181]

这些是在不同时间观察到的恒星的径向速度。绘制时,它们看起来像这样: 绘制数据

那么,这就是我使用的代码来适合数据:

x = time
y = velocities

def sin_fit(x, A, w):
    return A * np.sin(w * x)

popt, pcov = curve_fit(sin_fit,x,y) #try to calculate exoplanet parameters with these data

xfit = np.arange(min(x),max(x),0.1)

fit = sin_fit(xfit,*popt)


mod = plt.figure()
plt.xlabel("Time (G. Days)")
plt.ylabel("Radial Velocity")
plt.scatter(x,[i for i in y],color="b",label="Data")
plt.plot(x,[i for i in y],color="b",alpha=0.2)
plt.plot(xfit,fit,color="r",label="Model Fit")
plt.legend()
mod.savefig("Data with sin fit.png")
plt.show()

我认为这是对的,看来是正确的,但这就是我得到的:

带有模型正弦的数据

我在做什么错?

谢谢你

I have been struggling for apparently no reason trying to fit a sin function to a small dataset that resembles a sinusoid. I've looked at many other questions and tried different libraries and can't seem to find any glaring mistake in my code. Also in many answers people are fitting a function onto data where y = f(x); but I'm retrieving both of my lists independently from stellar spectra.

These are the lists for reference:

time = np.array([2454294.5084288 , 2454298.37039515, 2454298.6022165 ,
   2454299.34790096, 2454299.60750029, 2454300.35176022,
   2454300.61361622, 2454301.36130122, 2454301.57111912,
   2454301.57540159, 2454301.57978822, 2454301.5842906 ,
   2454301.58873511, 2454302.38635047, 2454302.59553152,
   2454303.41548415, 2454303.56765036, 2454303.61479213,
   2454304.38528718, 2454305.54043812, 2454306.36761011,
   2454306.58025083, 2454306.60772791, 2454307.36686591,
   2454307.49460991, 2454307.58258509, 2454308.3698358 ,
   2454308.59468672, 2454309.40004997, 2454309.51208756,
   2454310.43078368, 2454310.6091061 , 2454311.40121502,
   2454311.5702085 , 2454312.39758274, 2454312.54580053,
   2454313.52984047, 2454313.61734047, 2454314.37609003,
   2454315.56721061, 2454316.39218499, 2454316.5672538 ,
   2454317.49410168, 2454317.6280825 , 2454318.32944441,
   2454318.56913047])
velocities = np.array([-2.08468951, -2.26117398, -2.44703149, -2.10149768, -2.09835213,
   -2.20540079, -2.4221183 , -2.1394637 , -2.0841663 , -2.2458154 ,
   -2.06177386, -2.47993416, -2.13462117, -2.26602791, -2.47359571,
   -2.19834895, -2.17976339, -2.37745005, -2.48849617, -2.15875901,
   -2.27674409, -2.39054554, -2.34029665, -2.09267843, -2.20338104,
   -2.49483926, -2.08860222, -2.26816951, -2.08516229, -2.34925637,
   -2.09381667, -2.21849357, -2.43438148, -2.28439031, -2.43506056,
   -2.16953358, -2.24405359, -2.10093237, -2.33155007, -2.37739938,
   -2.42468714, -2.19635302, -2.368558  , -2.45959665, -2.13392004,
   -2.25268181]

These are radial velocities of a star observed at different times. When plotted they look like this:
Plotted Data

This is then the code I'm using to fit a test sine on the data:

x = time
y = velocities

def sin_fit(x, A, w):
    return A * np.sin(w * x)

popt, pcov = curve_fit(sin_fit,x,y) #try to calculate exoplanet parameters with these data

xfit = np.arange(min(x),max(x),0.1)

fit = sin_fit(xfit,*popt)


mod = plt.figure()
plt.xlabel("Time (G. Days)")
plt.ylabel("Radial Velocity")
plt.scatter(x,[i for i in y],color="b",label="Data")
plt.plot(x,[i for i in y],color="b",alpha=0.2)
plt.plot(xfit,fit,color="r",label="Model Fit")
plt.legend()
mod.savefig("Data with sin fit.png")
plt.show()

I thought this was right, and it seems right by looking at other answers, but then this is what I get:

Data with model sine

What am I doing wrong?

Thank you in advanceee

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2025-01-30 07:14:14

我想这是由于sin_fit函数完全无法拟合数据。每个默认的罪名函数围绕y = 0旋转,而您的数据则围绕y = -2.3旋转。

我尝试了您的代码,并以偏移量扩展了sin_fit,从而产生了更好的结果(尽管看起来不太完美):

def sin_fit(x, A, w, offset):
    return A * np.sin(w * x)  + offset

通过此功能至少有机会适合

I guess it's due the sin_fit function is not able to fit the data at all. The sin function per default whirls around y=0 while your data whirls somewhere around y=-2.3.

I tried your code and extended the sin_fit with an offset, yielding way better results (althought looking not too perfect):

def sin_fit(x, A, w, offset):
    return A * np.sin(w * x)  + offset

with this the function has at least a chance to fit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文