Write a Python function called integrateSpectrum . The first few lines of its de
ID: 3732509 • Letter: W
Question
Write a Python function called
integrateSpectrum
. The first few lines of its definition
are outlined here. It’s up to you to fill in the rest of the function with code:
def integrateSpectrum(wavelengths, fluxes, w_lower, w_upper):
"""
This function performs a numerical integral of a spectrum.
Arguments:
wavelengths = a numpy array of wavelengths, in nanometers
fluxes = a numpy array of fluxes, corresponding to w
w_lower = the lower wavelength limit of the integration
w_upper = the upper wavelength limit of the integration
Returns:
the integral of fluxes, with wavelength limits from w_lower to w_upper
(this should be one floating point number)
"""
# now you write the rest
Im having issues on how to write the rest of the function.
Explanation / Answer
def IntegrateSpectrum(Wavelengths, Fluxes, LowerLim, UpperLim):
wlowind = 0
while wavelengths[wlowind] < LowerLim:
wlowind+=1
#The same must be done for the index value of the upper limit
whighind = 0
while wavelengths[whighind] < UpperLim:
whighind+=1
#Using the equation ((b-a)/N)(f[x1]+f[x2]...f[xn-1])
#Using the form h = b-a/N
num = float(UpperLim-LowerLim)
den = float(whighind-wlowind)
N = num/den
return float(np.sum((N*fluxes[wlowind:whighind])))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.