Fit blackbody spectrum to data in python

In summary, the program is trying to fit a blackbody spectrum to data points and is getting an error due to an overflow. The problem may be in the exponentiation in the code, though it may also be related to the size of the data points. Additionally, due to the low probability nature of the problem, the solution may involve operating in the log domain.
  • #1
Silviu
624
11
Hi! I have to fit a blackbody spectrum to some data points. The y-axis is in mJy and the x-axis is in log_10(freq). My code looks like this:

from __future__ import division

import matplotlib.pyplot as plt

import numpy as np

from scipy.optimize import curve_fit

h = 6.63*10**(-34)

c = 3.0*10**8

k = 1.38*10**(-23)

pi = 3.14159265

scale_y = 1.5*10**13

def func(x,T):

f=scale_y*8*pi*h/(c**3)*(np.power(10,x))**3/(np.exp(h*np.power(10,x)/(k*T))-1)

return f

popt,pcov=curve_fit(func,frec, Flux)

print (popt)

freq and Flux are the lists with the x and y data and I need to find T. Just by eye T should be around 10000K. But when I run the program I get this error

RuntimeWarning: overflow encountered in exp
f=scale_y*8*pi*h/(c**3)*(np.power(10,x))**3/(np.exp(h*np.power(10,x)/(k*T))-1)

and I am not sure why, because for T=10000k the value of exp should be just fine. Any idea? Thank you!
 

Attachments

  • Plot.png
    Plot.png
    4.5 KB · Views: 1,099
Technology news on Phys.org
  • #2
Hi! I have to fit a blackbody spectrum to some data points. The y-axis is in mJy and the x-axis is in log_10(freq). My code looks like this:
Python:
from __future__ import division

import matplotlib.pyplot as plt

import numpy as np

from scipy.optimize import curve_fit

h = 6.63*10**(-34)

c = 3.0*10**8

k = 1.38*10**(-23)

pi = 3.14159265

scale_y = 1.5*10**13

def func(x,T):

    f=scale_y*8*pi*h/(c**3)*(np.power(10,x))**3/(np.exp(h*np.power(10,x)/(k*T))-1)

    return f

popt,pcov=curve_fit(func,frec, Flux)

print (popt)
freq and Flux are the lists with the x and y data and I need to find T. Just by eye T should be around 10000K. But when I run the program I get this error

RuntimeWarning: overflow encountered in exp
f=scale_y*8*pi*h/(c**3)*(np.power(10,x))**3/(np.exp(h*np.power(10,x)/(k*T))-1)

and I am not sure why, because for T=10000k the value of exp should be just fine. Any idea? Thank you!
 
Last edited by a moderator:
  • #3
Try breaking up the equation into smaller factors and print them out to see which ones may be the culprit.

Also you need to indent the equation and the subsequent return statement to be a part of the function you've defined. I did that too in your listing.

Lastly I added code tags i.e. (code=python) and (/code) around your code for syntax coloring. The parens are actually square brackets in the tags.
 
  • #4
jedishrfu said:
Try breaking up the equation into smaller factors and print them out to see which ones may be the culprit.

Also you need to indent the equation and the subsequent return statement to be a part of the function you've defined. I did that too in your listing.

Lastly I added code tags i.e. (code=python) and (/code) around your code for syntax coloring. The parens are actually square brackets in the tags.
Thank you so much. I tried to break the equation in parts and the error seems to be in the np.exp() part, which I think it means that T goes to 0, but I don't know why.
 
  • #5
So now you should print out the factors in the np.exp() call to see if there is anything out of place.

Another thing to consider is that overflows and underflows can occur from intermediate results

As an example, consider a * b * (1/c) where a, b and c are big numbers

a * b--> causes an overflow exception

but if you restructure it to: ( a * (1/c) ) * b then the factor ( a * (1/c) ) creates a not so big number so that overflow might not occur when you finally multiply by b.
 
  • Like
Likes jim mcnamara
  • #6
Did you check the argument of the exponential? What is the result of h*np.power(10,x)/(k*T)?
 
  • #7
Another option, since your function mostly involves multiplications and exponentiation, would be to operate in the log domain, I.e.take the logarithm of your input values, thus reducing all those operations to mostly additions or multiplications. At the end you exponentiate again to return to the original domain.
This trick is used a lot in machine learning where probabilities can be exceedingly low.
 

FAQ: Fit blackbody spectrum to data in python

1. What is a blackbody spectrum and why is it important?

A blackbody spectrum is the theoretical emission of electromagnetic radiation from a perfect radiator at a given temperature. It is important because it can help us understand the behavior of real-world objects, such as stars, and can be used to accurately model and predict their emissions.

2. How do I fit a blackbody spectrum to data in python?

To fit a blackbody spectrum to data in python, you can use the curve_fit function from the scipy.optimize module. This function allows you to fit a curve to your data by adjusting the parameters of a given function, such as the Planck function for a blackbody spectrum. You can then plot the fitted curve and compare it to your data to see how well it fits.

3. What are the necessary inputs for fitting a blackbody spectrum in python?

The necessary inputs for fitting a blackbody spectrum in python include the temperature of the object you are modeling, the wavelength or frequency range of your data, and the measured intensity values at each wavelength or frequency. You may also need to provide initial guesses for the parameters of the Planck function, such as the peak wavelength or frequency.

4. How do I determine the best fit parameters for my blackbody spectrum?

The best fit parameters for your blackbody spectrum can be determined by using the curve_fit function in python. This function will adjust the parameters of the Planck function to minimize the difference between the fitted curve and your data. You can also use statistical measures, such as the chi-squared test, to determine the goodness of fit for your model.

5. Are there any libraries or packages available for fitting blackbody spectra in python?

Yes, there are several libraries and packages available for fitting blackbody spectra in python, such as scipy, astropy, and lmfit. These libraries provide functions and tools for curve fitting, data manipulation, and plotting, making it easier to fit blackbody spectra to data and analyze the results.

Similar threads

Replies
9
Views
2K
Replies
4
Views
2K
Replies
17
Views
2K
Replies
2
Views
2K
Replies
6
Views
1K
Replies
3
Views
3K
Replies
7
Views
7K
Replies
1
Views
3K
Replies
1
Views
1K
Back
Top