首页 > 解决方案 > 绘制理想化黑体光谱

问题描述

所以我遇到了几个错误

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

标签: pythonnumpyphysicsastropyastronomy

解决方案


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

事实上,in 的第一个值l只是零,因此wavinplanck()变得无限并且1/wav**5是 NaN。

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

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


推荐阅读