【问题标题】:Plotting Idealized Blackbody Spectra绘制理想化黑体光谱
【发布时间】:2020-04-29 04:58:55
【问题描述】:

所以我遇到了几个错误

RuntimeWarning: overflow encountered in exp
intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )

和

RuntimeWarning: invalid value encountered in multiply
intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )

即使有这些错误(还有一个关于除以零的错误,我忽略了哈哈),我的图表也能以任何一种方式正确生成。我只是想知道是否有人可以帮助我清除这些错误?请和谢谢。

这里是完整的代码:

import numpy as np
import matplotlib.pyplot as plt
from astropy import constants as const


def planck(T, wav):
    a = 2.0*h*c**2
    b = h*c/(wav*k*T)
    intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )
    return intensity

# Part 1: Plotting Planck's Law

T1 = 3750
T2 = 5200
T3 = 9600 # Temperature of M0 star, the Sun, and A0 star (K)
c = const.c.value
h = const.h.value
k = const.k_B.value
l = np.linspace(0, 1.5e-6, 1500) #Array of wavlengths (meters)
IM0 = planck(T1, l) 
Isun = planck(T2, l)
IA0 = planck(T3, l) # Planck's law intensities 

plt.figure(1) # Plot of the three idealized blackbody spectra
plt.plot(l, IM0, 'k-', label = 'M0 Star')
plt.plot(l, Isun, 'r--', label = 'Sun')
plt.plot(l, IA0, 'b-.', label = 'B0')
plt.xlabel('Wavelength (meters)')
plt.ylabel('Intensity (W sr^{-1} m^{-3})')
plt.title('Idealized Blackbody Spectra')
#plt.legend('M0 Star', 'Sun', 'B0 Star')
leg = plt.legend()
plt.ticklabel_format(axis="x", style="sci", scilimits=(0,0)) # Scientific not

【问题讨论】:

  • 另请参阅我在your other question 上的回答。您可能会因为不使用正确的单位而进入热水。尝试坚持为所有数量指定单位,因为这将有助于确保您的计算都是正确的。

标签: python numpy physics astropy astronomy


【解决方案1】:

l 的前 5 个值太小,导致 b 的值较高,从而导致 exp 中的数值溢出(因为 exp(1500) 只是一个非常大的数字)。

事实上,l 中的第一个值只是零,因此planck() 中的wav 变为无限,1/wav**5 为 NaN。

因此所有的警告。设置l = np.linspace(6e-9, 1.5e-6, 1500),你会没事的。

不,index,这不是 IDE 警告,而是 Python 警告。有一些方法可以抑制此类警告,但只有在确切知道要抑制什么以及为什么要抑制时才能这样做。

【讨论】:

  • 这清楚了!还帮助我完成了下一个任务!谢谢!
猜你喜欢
  • 2014-04-20
  • 1970-01-01
  • 2013-03-25
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多