将余弦平方函数拟合为一组数据

时间:2018-02-12 23:54:02

标签: python optimization graph physics curve-fitting

我从实验装置中获取了一组数据,并试图模拟衍射光栅的干涉图案。目前我的功能并没有很好地覆盖数据,并且想知道是否有办法改善拟合。

[编辑]从我收到的评论中我现在试图将sin(x)** 2函数拟合到图形上,其中在这种情况下,幅度是另一个/几个函数的位置,或者适合叠加的正弦波。此外,在尝试在数据点的峰值上叠加高斯分布时,请参见图像2

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

x=sp.array([5.5,5.75,5.95,6,6.1,6.2,6.2,6.4,6.5,6.75,7,7.05,7.1,7.15,7.2,
7.3,7.35,7.5,7.55,7.7,8,8.2,8.3,8.35,8.4,8.6,8.7])

V=sp.array([0.048,0.1,0.617,0.557,0.258,0.112,0.098,0.08,0.075,0.093,0.253,
0.486,0.98,1.391,1.58,0.964,0.71,0.166,0.152,0.11,0.121,0.256,0.591,1.186,
1.552,0.787,0.283])

def my_sin(t,peroid,amplitude,phase):
           return (amplitude*(sp.sin(t*2*sp.pi/peroid+phase)))**2

guess_peroid= 2
guess_amplitude = 0.8
guess_phase = (sp.pi)/2

p0 =[guess_peroid, guess_amplitude, guess_phase]
fit = curve_fit(my_sin,x, V, p0=p0)
print ('The fit paramters are:', fit[0])
x1 = sp.linspace(5.5,8.7,100000)
data_fit = my_sin(x1,*fit[0])

print(x1)
plt.xlabel('Distance Between Minima(m)')
plt.ylabel('Voltage')
plt.plot(x1,data_fit)
plt.errorbar(x,V,fmt='x')
plt.show()

Here is a picture of the output of my code

I would like to replicate this kind of fitting for the three Maxima of Light Intensity shown in my data set

非常感谢任何帮助。

An Image of the formula i am using

0 个答案:

没有答案