绘制理想化黑体光谱

问题描述 投票:0回答:1

所以我得到了几个错误

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
python numpy physics astropy astronomy
1个回答
3
投票

前5个值 l 太小了,这就造成了高值的 b 因此,在 exp 因为exp(1500)只是一个非常大的数字)。

事实上,在 l 是简单的零,因此 wavplanck() 变得无穷无尽 1/wav**5 是NaN。

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

还有,不 指数这不是 IDE 警告,而是 Python 警告。有一些方法可以抑制这样的警告,但是你只能在知道你要抑制的是什么以及为什么要抑制的时候才能这样做。


-3
投票

既然你的代码在运行,就忽略RuntimeWarning,因为它只是一个警告,不会影响你的代码或任何东西。你的IDE只是在警告你。我想如果你在不同的IDE中尝试,就不会有警告。但我可能是错的

© www.soinside.com 2019 - 2024. All rights reserved.