使用matplotlib绘制黑体辐射曲线,但我不明白我得到的错误?

问题描述 投票:0回答:1
import math
import random
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
h=6.62607015E-34 # Planck constant, units of J*s
hbar=h/(2*math.pi) # Reduced Planck constant 
k=1.380649E-23 # Boltzmann constant, units of J/K
c=299792458.00 # speed of light M/s
sb=(2*(math.pi**5)*(k**4))/(15*(c**2)*(h**3)) # Stefan-Boltzmann constant, units of W/(m^2 K^4)
def one_star():
    mass=random.uniform(0.1,50) # random mass in relation to the sun's mass
    print("The mass of the star is "+str(mass)+" times the mass of the Sun")
    if mass <= 1.4: # given by the mass-radius relationship
        radius=mass**0.9
        print("The radius of the star is "+str(radius)+" times the radius of the Sun")
    else:
        radius=mass**0.6 # larger stars are less dense
        print("The radius of the star is "+str(radius)+" times the radius of the Sun")

    # To find the volume and surface area of the star, we want values in terms of meters^3 and meters^2
    # So we'll multiply each of the previous values by the Sun's mass and radius.
    mass_actual=mass*1.989E28 # mass in kilograms
    print("The mass of the star in kilograms: "+str(mass_actual))
    radius_actual=radius*696000000 # radius in meters
    print("The radius of the star in meters: "+str(radius_actual))   
    volume=(4/3)*math.pi*(radius_actual**3) # volume in meters cubed
    print("The volume of the star in meters cubed: "+str(volume))    
    sa=4*math.pi*(radius_actual**2) # surface area in meters squared
    print("The surface area of the star in meters squared: "+str(sa))    
    B=random.uniform(3.3,3.7)
    luminosity=mass**B # we are using the value of mass in relation to the sun's mass again for this part
    print("The luminosity of the star is "+str(luminosity)+" times the luminosity of the Sun")
    # Time to find the temperature
    # We need to convert the luminosity of the star into real units now
    l_actual=luminosity*3.828E26
    print("The luminosity of the star in watts: "+str(l_actual))
    temperature=(l_actual/(sa*sb))**(1/4) # Solved Stefan-Boltzmann law for temperature
                                          # sb is the Stefan-Boltzmann constant
    print("The surface temperature of the star in Kelvin: "+str(temperature))

    ####### Time to produce a blackbody radiation curve #######

    T=temperature
    x=np.arange(1,1000)
    Intensity=((2*h*c**2)/(x**5))(1/(exp(((h*c)/(x*k*T)-1))
    plt.plot(x, Intensity, '--', label='Intensity')
    plt.legend(loc='upper right')
    plt.title("Planck Blackbody Radiation Curve")
    plt.xlabel("Wavelength")
    plt.ylabel("Intensity")

    plt.show()

我试图绘制一条给定恒星温度的黑体辐射曲线。我得到的问题是 错误. Plot函数上面的代码都是正确的,也是可以用的,我把它留了下来,供大家参考。我相信这是一个很容易解决的问题,但我不熟悉如何绘制这样的函数?我所有的导入和定义变量都在代码上面。

python numpy matplotlib physics
1个回答
0
投票

有几个问题

Intensity=((2*h*c**2)/(x**5))(1/(exp(((h*c)/(x*k*T)-1))

小括号不平衡,有缺失。* 在封闭的小括号组之间,和 exp 函数未定义。由于 x 是一个 np.ndarray 你需要使用 np.exp 而不是 math.exp所以,应该是

Intensity = ((2 * h * c**2) / x**5) * (1 / (np.exp((h * c) / (x * k * T)) - 1))

但也应注意到,该范围 (1, 1000) 在这种情况下是相当无用的,因为 x (波长)是在 而1米和1千米波长之间的辐射并不是很有趣。它应该更像 (1e-8, 1e-6) (10纳米到1微米),可以通过以下方式产生。np.linspace(1e-8, 1e-6, 1000) 然后会给出正常的黑体曲线。

enter image description here


完整的示例代码

import matplotlib.pyplot as plt
import numpy as np
import math

h = 6.62607015e-34
k = 1.380649e-23
c = 299792458.00
sb = (2 * math.pi**5 * k**4) / (15 * c**2 * h**3)

def one_star():
    mass = np.random.uniform(0.1, 50)
    radius = mass**0.9 * 6.96e8
    B = np.random.uniform(3.3, 3.7)
    sa = 4 * math.pi * radius**2
    T = (luminosity / (sa * sb))**(1/4)
    x = np.linspace(1e-8, 1e-6, 1000)
    I = ((2 * h * c**2) / x**5) * (1 / (np.exp((h * c) / (x * k * T)) - 1))
    plt.plot(x, I)

for i in range(5):
    one_star()
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.