Fit blackbody spectrum to data in python

Click For Summary

Discussion Overview

The discussion revolves around fitting a blackbody spectrum to data points using Python. Participants are addressing issues related to the implementation of the fitting function, particularly focusing on an overflow error encountered during the computation of the exponential function in the fitting process.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant shares their code for fitting a blackbody spectrum and describes the error encountered, suggesting that the temperature T should be around 10000K.
  • Another participant suggests breaking the equation into smaller factors to identify the source of the overflow error and emphasizes the need for proper indentation in the code.
  • A later reply indicates that the error seems to originate from the np.exp() part of the equation, speculating that T may be approaching 0.
  • Further suggestions include printing the factors involved in the np.exp() call to check for anomalies and considering the potential for overflow or underflow due to large intermediate results.
  • Another participant proposes operating in the log domain to mitigate issues with large numbers, noting that this technique is commonly used in machine learning.

Areas of Agreement / Disagreement

Participants are exploring various approaches to resolve the overflow error, but there is no consensus on the exact cause or the best solution. Multiple strategies are proposed, indicating ongoing uncertainty in the discussion.

Contextual Notes

Limitations include potential missing assumptions regarding the input data and the specific values of frequency and flux. The discussion does not resolve the mathematical steps leading to the overflow error.

Silviu
Messages
612
Reaction score
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,171
Technology news on Phys.org
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:
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.
 
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.
 
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   Reactions: jim mcnamara
Did you check the argument of the exponential? What is the result of h*np.power(10,x)/(k*T)?
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K