Hello everyone,
i'm modeling emission lines by setting expression to relate one amplitude to the other.
since the flux of one line is equal the third of the other, i am expressing amplitude in terms of flux. But when i use quad in integrating it brings an error of not being defined yet it is imported earlier at the start.
How can i handle this error?
the attached is the data.
thank you
Daniel.
from numpy import loadtxt
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Parameters
from lmfit.models import GaussianModel, LinearModel
from scipy.integrate import quad
import math as mt
params={'legend.fontsize':'18','axes.labelsize':'18',
'axes.titlesize':'18','xtick.labelsize':'18',
'ytick.labelsize':'18','lines.linewidth':2,'axes.linewidth':2,'animation.html': 'html5'}
plt.rcParams.update(params)
plt.rcParams.update({'figure.max_open_warning': 0})
plt.rcParams['figure.figsize'] = (12,8)
data = loadtxt('Data.txt')
x = data[:, 0]
y = data[:,1]
#selected region for fitting
mask = (x>= 6680) & (x <= 6770)
mask = (x>= 6400) & (x <= 6800)
xarray, yarray = x[mask], y[mask]
x=xarray
y=yarray
# Note: use builtin models
gmodel = ( GaussianModel(prefix='g2_')+GaussianModel(prefix='g1_')+GaussianModel(prefix='g4_')+GaussianModel(prefix='g3_')+GaussianModel(prefix='g7_')+LinearModel(prefix='off_')+GaussianModel(prefix='g8_'))
# Note: many fewer bounds:
params = Parameters()
params.add('off_intercept', value=1)
params.add('off_slope', value=0)
params.add('g2_amplitude', value=3, vary= True)
params.add('g2_center', value=6731, min=6725, max=6740)
params.add('g2_sigma', value= 3.6021,min=0)
params.add('g1_amplitude', value=1.2, vary=True)
params.add('g1_center', value=6719, min=6710, max=6724)
params.add('g1_sigma', expr='g2_sigma')
params.add('g3_amplitude', value=1.6)
params.add('g3_center', value=6585, min=6570, max=6595)
params.add('g3_sigma', expr='g2_sigma')
params.add('g7_amplitude', value=5)
params.add('g7_center', value=6567, min=6555, max=6575)
params.add('g7_sigma', expr='g2_sigma')
params.add('g4_center', value=6549, min=6540, max=6555)
params.add('g4_sigma', expr='g2_sigma')
params.add('g4_amplitude', expr='quad(1/(g3_sigma*np.sqrt(2*np.pi))*mt.exp(-(x-g3_center)**2/2*g3_sigma**2), 6500, 6650)/quad(3*mt.exp(-(x-g4_center)**2/2*g4_sigma**2), 6500, 6650)')
params.add('g8_amplitude', value=7, min=1)
params.add('g8_center', value=6565, min=6554, max=6575)
params.add('g8_sigma', value=7, min=0)
result = gmodel.fit(y, params, x=x)
print(result.fit_report())
comps = result.eval_components(x=x)
fig, axes = plt.subplots(2, 1, gridspec_kw={'hspace': 0, 'height_ratios': [3,1]}, figsize=(12, 8))
axes[0].set_title('MCG_00_29_023')
axes[0].plot(x, y, 'k', label='observed')
axes[0].set_ylim(ymin=-0.1, ymax=2)
axes[0].plot(x, result.best_fit, 'r--', label='best fit')
axes[0].plot(x, comps['g1_']+comps['off_'], 'g-', label='g1')
axes[0].plot(x, comps['g2_']+comps['off_'], 'g-', label='g2')
axes[0].plot(x, comps['g3_']+comps['off_'], 'g-', label='g3')
axes[0].plot(x, comps['g4_']+comps['off_'], 'g-', label='g4')
axes[0].plot(x, comps['g7_']+comps['off_'], 'g-', label='g7')
axes[0].plot(x, comps['g8_']+comps['off_'], 'b-', label='g8')
axes[0].plot(x, comps['off_'], 'k-', label='offset')
residual=result.best_fit-y
axes[1].set_ylim(ymin=-0.15, ymax=0.15)
axes[1].plot(x,residual)
axes[1].set_xlabel(r'wavelength [$\rm \AA$]')
axes[0].set_ylabel(r'flux [$\rm erg / (cm^{2} \, s \, \AA)$]')
plt.savefig('Ha_Data.pdf')
plt.show()