我如何在数值上(有效地)集成和绘制我的函数?

2024-05-16 13:13:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画下这个等式的曲线图: enter image description here

问题1:你看。。。因为我的函数是一个ν的函数,所以我必须计算域中每个ν的积分。我的问题是:最好的方法是什么

我曾考虑过使用scipy进行积分,并使用for循环对每个ν进行多次计算,但这似乎是解决我的问题的一种非常不雅观的方法。有人知道更好的选择吗?有人有不同的想法吗

问题2:当我编写代码时,会出现一些错误,主要是因为我认为指数的指数非常小。你有没有什么想法,我应该如何改变它,这样我就可以用Python绘制这个图了

哦,如果你尝试另一种方法,它应该看起来like this

这是我正在编写的代码。我现在回到Python,所以可能有一些错误。我现在看到的情节和我们想象的很不一样

from scipy.integrate import quad
from scipy.constants import c, Planck, k, pi
import numpy as np
import matplotlib.pyplot as plt

def luminosity_integral(r, x):
    T_est = 4000 
    R_est = 2.5 * (696.34*1e6)

    Temp = ((2/(3*pi))**(1/4)) * T_est * ((R_est/r)**(3/4))
    termo1 = ((4 * (pi**2) * Planck * (x**4) ) / (c**2))
    termo2 = ((Planck * x) / (k*Temp))
   

    return ((termo1 * r ) / (np.exp(termo2) - 1))


freqs = np.linspace(1e10, 1e16)
y = np.array([])
for i in freqs:
I = quad(luminosity_integral, (6 * 2.5 * (696.34*1e6)), (7e4 *  2.5 * (696.34*1e6)), args = (i))
temp = np.array([I[0]])
y = np.concatenate((y, temp))


plt.loglog(freqs, y)
plt.show()

enter image description here


Tags: 方法函数代码fromimportfor错误np
1条回答
网友
1楼 · 发布于 2024-05-16 13:13:56
  • 重复使用术语R_est,而不是将其表达式编写3次(如果要更改该参数,则更好)
  • 在常量中使用了pi**2乘以整数(不影响形状)
  • 该形状类似于您作为参考放置的形状,但不在建议的范围内
  • 您正在使用T的值作为T_*,您确定吗

请尝试此版本的代码

from scipy.integrate import quad
from scipy.constants import c, Planck, k, pi
import numpy as np
import matplotlib.pyplot as plt

R_est = 2.5 * (696.34e6)
def luminosity_integral(r, x):
    T_est = 4000 

    termo1 = ((4 * pi * Planck * (x**4) ) / (c**2))
    termo2 = ((Planck * x) / (k*T_est)) * (3*pi/2 * (r/R_est)**3)**0.25
    termo3 = np.exp(-termo2)
    return ((termo1 * r ) * termo3 / (1 - termo3))


freqs = np.logspace(6, 16)
y = np.zeros_like(freqs)

for i, nu in enumerate(freqs):
    y[i] = quad(luminosity_integral, (6* R_est), (7e4 *  R_est), args = (nu))[0]
    

plt.loglog(freqs, y)
plt.ylim([1e6, 1e25])
plt.show()

相关问题 更多 >