首页 > 解决方案 > 正弦拟合数据

问题描述

老实说,我不明白这是什么机制。我已经用谷歌搜索尝试了几次,但它没有奏效。请帮助我,这段代码有什么问题。数据取自 AAVSO 网站。

import pandas as pd
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt

dg = pd.read_excel(
    open('Optical1.xlsx', 'rb'), sheet_name='Optical1', na_filter=True
)

x0 = dg["JD"]-2450000

y0 = dg["Magnitude"]

数据是这样组成的。我使用了 pandas.read (xlsx 文件)。

x0 = [2455231.965,2455265.826,2455282.773,2455388.442,2455714.7,2455794.37,2456086.7,2456156.41,2456391.451
,2456406.421,2456459.44,2456706.597,2456713.578,2456714.583,2456722.892,2456726.589,2456750.785,2456767.751
,2456808.6,2457124.799,2457166.6,2457184.6,2457194.44,2457459.91,2457475.909,2457483.813,2457488.803,2457490.777
,2457502.746,2457513.855,2457519.706,2457540.6,2457579.45,2457614.625,2457622.641,2457633.601,2457651.603,2457655.592,2457663.579
,2457859.877,2457892.6,2457900.722,2457916.6,2457916.658,2457928.671,2457941.678,2457982.622,2457983.36,2457999.606,2458016.583
,2458034.566,2458115.043,2458174.925,2458198.856,2458198.867,2458217.819,2458234.752,2458249.48,2458258.726,2458271.668,2458288.658
,2458335.673,2458343.65,2458360.606,2458379.595,2458462.039,2458476.044,2458492.991,2458508.964,2458523.902,2458548.895,2458571.798
,2458600.799,2458618.718,2458628.6,2458630.649,2458635.39,2458655.685,2458682.706,2458708.657,2458721.637,2458733.6,2458745.588,2458768.557
,2458836.037,2458856.971,2458874.941,2458888.889,2458907.902,2458926.775,2458939.784,2458954.735,2458969.808,2458982.695,2458983.701
,2458990.688,2459008.799,2459015.619,2459293.799,2459294.816,2459303.808]

y0 = [17.03,15.66,14.19,13.5,12.8,13.3,14.5,12.6,14.65,14.352,14.3,12.862,14.191,13.12,14.21,13.606,14.73,15.24,14.5
,13.35,14.5,14.8,14.7,11.94,12.125,12.28,12.37,12.403,12.54,12.601,12.72,13.1,14.3,14.517,14.722,14.919,15.316,15.337
,15.535,11.768,12.6,12.135,12.7,12.382,12.624,12.927,13.676,13.8,13.959,14.248,14.597,15.968,15.959,15.91,15.621
,14.773,13.055,12.2,11.548,11.413,11.501,12.331,12.492,12.752,13.007,14.514,14.947,15.292,15.441,15.595,15.877
,15.7,15.385,15.128,14.5,14.943,14.3,12.36,11.213,11.208,11.187,11.35,11.578,11.965,13.198,13.617,14.047
,14.38,14.905,15.215,15.506,15.763,15.911,16.38,15.916,15.83,15.797,15.97,15.1,15.15,15.48]

#我在“https://scipy-lectures.org/intro/scipy/auto_examples/plot_curve_fit.html”中得到了这段代码

def test_func(x, a, b):
    return a * np.sin(b * x)

params, params_covariance = optimize.curve_fit(test_func, x0, y0, p0=None)
                                               
print(params_covariance)
print(params)
# And plot it

fig,axes=plt.subplots()
fig.set_size_inches(10, 6)

plt.scatter(x0, y0, marker = '^', color ='firebrick',s=12, label = 'V-band')

plt.plot(x0, test_func(x0, params[0], params[1]), label='Fitted function')

axes.invert_yaxis()  

plt.xlabel('MJD(2450000+)')

plt.ylabel('Magnitude') 

plt.legend(loc='upper left')

plt.savefig("test AAVSO.png", dpi=500)

plt.show()

没看懂原理def test_func(x, a, b)。我应该为漂亮的正弦拟合做什么。

标签: pythontrigonometrydata-fitting

解决方案


推荐阅读