Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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])))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote