尝试将正弦函数拟合到相位光曲线 [英] Trying to fit a sine function to phased light curve
问题描述
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model,Parameters
f2= "KELT_N16_lc_006261_V01_west_tfa.dat"
t2="TIMES" # file name
NewData2 = np.loadtxt(t2, dtype=float, unpack=True)
NewData = np.loadtxt(f2,dtype=float, unpack=True, usecols=(1,))
flux = NewData
time= NewData2
new_flux=np.hstack([flux,flux])
# fold
period = 2.0232 # period (must be known already!)
foldTimes = ((time)/ period) # divide by period to convert to phase
foldTimes = foldTimes % 1 # take fractional part of phase only (i.e. discard whole number part)
new_phase=np.hstack([foldTimes+1,foldTimes])
print len(new_flux)
print len(new_phase)
def Wave(x, new_flux,new_phase):
wave = new_flux*np.sin(new_phase+x)
return wave
model = Model(Wave)
print "Independent Vars:", model.independent_vars
print "Parameters:",model.param_names
p = Parameters()
p.add_many(('new_flux',13.42, True, None, None, None) )
p.add_many(('new_phase',0,True, None, None, None) )
result=model.fit(new_flux,x=new_phase,params=p,weights= None)
plt.scatter(new_phase,new_flux,marker='o',edgecolors='none',color='blue',s=5.0, label="Period: 2.0232 days")
plt.ylim([13.42,13.54])
plt.xlim(0,2)
plt.gca().invert_yaxis()
plt.title('HD 240121 Light Curve with BJD Correction')
plt.ylabel('KELT Instrumental Magnitude')
plt.xlabel('Phase')
legend = plt.legend(loc='lower right', shadow=True)
plt.scatter(new_phase,result.best_fit,label="One Oscillation Fit", color='red',s=60.0)
plt.savefig('NewEpoch.png')
print result.fit_report()
我正在尝试将正弦函数拟合到研究项目的相位光曲线数据中.但是,我不确定我哪里出错了,我相信这取决于我的参数.看起来拟合的幅度太高,周期太长.任何帮助,将不胜感激.谢谢!
I am trying to fit a sine function to phased light curve data for a research project. However, I am unsure as to where I am going wrong, and I believe it lays in my parameters. It appears that the fit has an amplitude that is too high, and a period that is too long. Any help would be appreciated. Thank you!
这是图形现在的样子(尝试将正弦函数拟合到我的数据集):
This is what the graph looks like now (Attempt at fitting a sine function to my dataset):
推荐答案
一些评论/建议:
首先,几乎可以肯定更换更好
First, it is almost certainly better to replace
p = Parameters()
p.add_many(('new_flux',13.42, True, None, None, None) )
p.add_many(('new_phase',0,True, None, None, None) )
与
p = Parameters()
p.add('new_flux', value=13.42, vary=True)
p.add('new_phase', value=0, vary=True)
其次,您的模型不包含 DC 偏移,但您的数据显然有一个.偏移量约为 13.4,正弦波的幅度约为 0.05.当你在做的时候,你可能想包括一个相位以及一个偏移量,这样模型是
Second, your model does not include a DC offset, but your data clearly has one. The offset is approximately 13.4 and the amplitude of the sine wave is approximately 0.05. While you're at it, you probably want to include a scale the phase as a well as an offset, so that the model is
offset + amplitude * sin(scale*x + phase_shift)
您不一定要改变所有这些,但让您的模型更通用将允许查看相移和比例是如何相关的——考虑到数据中的噪声水平,这可能很重要.
You don't necessarily have to vary all of those, but making your model more general will allow to see how the phase shift and scale are correlated -- given the noise level in your data, that might be important.
对于更通用的模型,您可以尝试几组参数值,使用 model.eval()
来评估具有一组参数的模型.一旦有了更好的模型和合理的起点,就应该得到合理的拟合.
With the more general model, you can try a few sets of parameter values, using model.eval()
to evaluate a model with a set of Parameters. Once you have a better model and reasonable starting points, you should get a reasonable fit.
这篇关于尝试将正弦函数拟合到相位光曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!