绘制理想化的黑体光谱 [英] Plotting Idealized Blackbody Spectra

查看:148
本文介绍了绘制理想化的黑体光谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了几个错误

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) )

即使存在这些错误(以及除以零的更多错误,我还是忽略了大声笑),无论哪种方式,我的图都能正确生成.我只是想知道是否有人可以帮助我清除这些错误?拜托,谢谢.

Even with these errors (and one more about dividing by zero that I ignore lol) my graph gets produced correctly either way. I am just wondering if anyone can help me clear up these errors? Please and thanks.

这是完整的代码:

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

推荐答案

l的前5个值太小,这会导致b的值过高,从而导致exp中的数值溢出(例如exp(1500 )只是一个很大的数字).

First 5 values of l are too small, which causes high values of b and thus a numerical overflow in exp (as exp(1500) is just a very large number).

实际上,l中的第一个值只是零,因此planck()中的wav变为无穷大,而1/wav**5为NaN.

In fact, the first value in l is simply zero, and thus wav in planck() becomes infinite and 1/wav**5 is NaN.

因此出现所有警告.设置l = np.linspace(6e-9, 1.5e-6, 1500)就可以了.

Hence all the warnings. Set l = np.linspace(6e-9, 1.5e-6, 1500) and you'll be fine.

不是,索引,这不是IDE警告,而是Python警告.有很多方法可以抑制此类警告,但是只有在确切了解抑制的原因以及原因之后,您才能执行此操作.

And no, index, it is not IDE warning, it's Python warning. There are ways to suppress such warnings, but you can only do that once you know exactly what are you suppressing and why.

这篇关于绘制理想化的黑体光谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆