使用astropy.modeling拟合负振幅高斯分布的问题

2024-05-19 01:44:57 发布

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

我正在尝试使用astropy.modeling软件包将高斯函数拟合为离散势函数。虽然我将负振幅指定给高斯,但它返回一个零高斯,即到处都是零振幅:

plot(c[0], pot_x, label='Discrete potential')
plot(c[0], g(c[0]), label='Gaussian fit') 
legend() 

enter image description here

我有以下代码行来执行装配:

g_init = models.Gaussian1D(amplitude=-1., mean=0, stddev=1.)
fit_g = fitting.LevMarLSQFitter()
g = fit_g(g_init, c[0], pot_x)

在哪里

c[0] = array([13.31381488, 13.31944489, 13.32507491, 13.33070493, 13.33633494,
   13.34196496, 13.34759498, 13.35322499, 13.35885501, 13.36448503,
   13.37011504, 13.37574506, 13.38137507, 13.38700509, 13.39263511,
   13.39826512, 13.40389514, 13.40952516, 13.41515517, 13.42078519])

pot_x = array([ -1.72620157,  -3.71811187,  -6.01282809,  -6.98874144,
    -8.36645166, -14.31787771, -23.3688849 , -26.14679496,
   -18.85970983, -10.73888697,  -7.10763373,  -5.81176637,
    -5.44146953,  -5.37165105,  -4.6454408 ,  -2.90307138,
    -1.66250349,  -1.66096343,  -1.8188269 ,  -1.41980552])

有人知道问题出在哪里吗

已解决:我只需指定一个域范围内的平均值,如13.35


Tags: 函数plotinitgaussianarraylabelfitpotential
1条回答
网友
1楼 · 发布于 2024-05-19 01:44:57

因为我不熟悉Astropy,所以我使用了scipy。以下代码提供以下输出:

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

x = np.asarray([13.31381488, 13.31944489, 13.32507491, 13.33070493, 13.33633494,
   13.34196496, 13.34759498, 13.35322499, 13.35885501, 13.36448503,
   13.37011504, 13.37574506, 13.38137507, 13.38700509, 13.39263511,
   13.39826512, 13.40389514, 13.40952516, 13.41515517, 13.42078519])

y = -np.asarray([ -1.72620157,  -3.71811187,  -6.01282809,  -6.98874144,
    -8.36645166, -14.31787771, -23.3688849 , -26.14679496,
   -18.85970983, -10.73888697,  -7.10763373,  -5.81176637,
    -5.44146953,  -5.37165105,  -4.6454408 ,  -2.90307138,
    -1.66250349,  -1.66096343,  -1.8188269 ,  -1.41980552])

mean = sum(x * y) / sum(y)
sigma = np.sqrt(sum(y * (x - mean)**2) / sum(y))

def Gauss(x, a, x0, sigma):
    return a * np.exp(-(x - x0)**2 / (2 * sigma**2))

popt,pcov = curve_fit(Gauss, x, y, p0=[max(y), mean, sigma])

plt.plot(x, y, 'b+:', label='data')
plt.plot(x, Gauss(x, *popt), 'r-', label='fit')
plt.legend()

Curve fitting

简单地说,我重用了这个answer。我不能完全确定均值和西格玛的定义,因为我不习惯在2D数据集上拟合高斯分布。但是,这并不重要,因为它只是用来定义用于启动曲线拟合算法的近似值

相关问题 更多 >

    热门问题