我正在try 将matlab中的计算转换为python.这是matlab中的代码:
% Solving for the bandpass correction
T = 8050; % temperature in kelvin
c = 2.99e10; % speed of light in cm/s
h = 6.626e-27; % planck constant in ergs
k = 1.38e-16; % boltzmann constant in erg/K
x1 = 3e-4; % lower wavelength in cm
x2 = 13e-4; % upper wavelength in cm
fun = @(x) ((2 * h * c^2) ./ x.^5) ./ (exp((h * c) ./ (x * k * T)) - 1); % planck function
f_deriv = @(x) ((2 * h^2 * c^3) ./ (x.^6 * k)) .* (exp((h .* c) ./ (x* k * T)) ./ (T * (exp((h * c) ./ (x * k * T)) - 1).^2));
numerator = integral(f_deriv,x1,x2);
denominator = (4 * integral(fun,x1,x2));
beta = numerator / denominator;
fprintf('The numerator is %f \n',numerator)
fprintf('The denominator is %f \n',denominator)
fprintf('The bandpass value is %f \n',beta)
下面是我用python编写代码的try :
# Solving for the bandpass correction:
from scipy.integrate import quad
import numpy as np
T = 8050 # temperature in kelvin
c = 2.99e10 # speed of light in cm/s
h = 6.626e-27 # planck constant in ergs
k = 1.38e-16 # boltzmann constant in erg/K
x1 = 3e-4 # lower wavelength in cm
x2 = 13e-4 # upper wavelength in cm
def p_function(x):
return ((2 * h * c ** 2) / x ** 5) / (np.exp((h * c)/(x * k * T)) - 1) # The Planck function
def p_deriv(x):
return ((2 * h ** 2 * c ** 3) / (x ** 6 * k)) * (np.exp((h * c)/(x * k * T))/(T * (np.exp((h * c) / (x * k * T)) - 1) ** 2))
numerator = quad(p_deriv, x1, x2)
denominator = 4 * quad(p_function, x1, x2)
beta = numerator[0] / denominator[0]
print("The numerator is", numerator[0])
print("The denominator is", denominator[0])
print("The bandpass value is", beta)
两者之间的β值不一致,我看不出是什么导致了差异.我将非常感谢任何帮助来协调这一点.谢谢