使用 python 拟合 sin 曲线

Fitting sin curve using python

我有两个列表:

# 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]

我需要在数据点中拟合正弦曲线 a+bsine(2*3.14*list1+c) 通过使用 python.[=14= 绘制 list1(在 x 轴上)对(在 y 轴上)获得的数据点]

我无法获得任何好处result.Can任何人都可以帮助我提供合适的代码,解释...

谢谢!

这是我绘制 list1(x 轴)和 list2(y 轴)后的图表

好吧,如果您使用 lmfit 设置并且 运行 您的合身效果将如下所示:

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() 函数的要求)。

但这只是解决了如何进行拟合的机制(我会把结果的显示留给你——看来你可能需要练习)。

拟合结果很糟糕,因为这些数据不是正弦曲线。它们也没有井井有条,这对合身来说不是问题,但确实让人更难看出发生了什么。