【发布时间】: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