用python拟合sin曲线

2024-09-30 10:31:58 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我有两份清单:

# on x-axis:
# list1:
[70.434654, 37.147266, 8.5787086, 161.40877, -27.31284, 80.429482, -81.918106, 52.320129, 64.064552, -156.40771, 12.37026, 15.599689, 166.40984, 134.93636, 142.55002, -38.073524, -38.073524, 123.88509, -82.447571, 97.934402, 106.28793]

# on y-axis:
# list2:
[86683.961, -40564.863, 50274.41, 80570.828, 63628.465, -87284.016, 30571.402, -79985.648, -69387.891, 175398.62, -132196.5, -64803.133, -269664.06, 36493.316, 22769.121, 25648.252, 25648.252, 53444.855, 684814.69, 82679.977, 103244.58]

我需要在使用python绘制list1(在x轴上)相对于(在y轴上)得到的数据点中拟合一条正弦曲线a+bsine(2*3.14*list1+c)。在

我不能得到任何好处结果。可以有人帮我找个合适的代码,解释。。。在

谢谢!在

这是我在绘制列表1(在x轴上)和列表2(在y轴上)之后的图形


Tags: 数据代码图形列表on绘制axislist2
1条回答
网友
1楼 · 发布于 2024-09-30 10:31:58

好吧,如果您使用lmfit设置和运行,您的fit将如下所示:

xdeg  = [70.434654, 37.147266, 8.5787086, 161.40877, -27.31284, 80.429482, -81.918106, 52.320129, 64.064552, -156.40771, 12.37026, 15.599689, 166.40984, 134.93636, 142.55002, -38.073524, -38.073524, 123.88509, -82.447571, 97.934402, 106.28793]

y = [86683.961, -40564.863, 50274.41, 80570.828, 63628.465, -87284.016, 30571.402, -79985.648, -69387.891, 175398.62, -132196.5, -64803.133, -269664.06, 36493.316, 22769.121, 25648.252, 25648.252, 53444.855, 684814.69, 82679.977, 103244.58]

import numpy as np
from lmfit import Model

import matplotlib.pyplot as plt

def sinefunction(x, a, b, c):
    return a + b * np.sin(x*np.pi/180.0 + c)

smodel = Model(sinefunction)
result = smodel.fit(y, x=xdeg, a=0, b=30000, c=0)

print(result.fit_report())

plt.plot(xdeg, y, 'o', label='data')
plt.plot(xdeg, result.best_fit, '*', label='fit')
plt.legend()
plt.show()

这是假设你的X数据是以度为单位的,并且你真的打算把它转换成弧度(正如numpy的sin()函数所要求的那样)。在

但这只是解决了如何进行拟合的机制(我将把结果的显示留给您-似乎您可能需要练习)。在

拟合结果很糟糕,因为这些数据不是正弦曲线。他们也没有很好的秩序,这不是一个问题,做适合,但确实使更难看到发生了什么。在

相关问题 更多 >

    热门问题