![](/img/trans.png)
[英]Is there a method to plot and numerically integrate a sympy function with parameters?
[英]How can I numerically (and efficiently) integrate and plot my function?
問題 1:你看......因為我的 function 是 ν 的 function 我必須計算我對域中每個 ν 的積分。 我的問題是:最好的方法是什么?
我曾想過使用 scipy 來進行積分,並使用 for 循環對每個 ν 進行多次計算,但這似乎是解決我的問題的一種非常不雅的方法。 有人知道更好的選擇嗎? 有人有不同的想法嗎?
問題 2:當我編寫代碼時,我遇到了一些錯誤,主要是因為我認為指數的指數非常小。 您對我應該如何更改它有任何想法,以便我可以使用 Python 執行此 plot 嗎?
哦,如果你嘗試不同的方法,它應該看起來像這樣
這是我正在處理的代碼。 我現在回到 Python,所以可能有一些錯誤。 我得到的 plot 與應該看起來的有很大不同。
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()
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.