python - 如何将此代码从 matlab 转换为 python?

标签 python numpy matlab

我正在尝试将 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 编写它:

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

两者之间的 beta 值不一致,我看不出是什么导致了差异。我将不胜感激任何协调此事的帮助。谢谢!

最佳答案

翻译 MATLAB 时,确保形状/大小正确非常重要。但是当我在 Octave 中运行代码时,我看到所有变量都是 (1,1),“标量”。因此尺寸不应该成为问题。

让我们检查一下函数值:

>> fun(1)
ans =  0.066426
>> f_deriv(1)
ans =  0.066432

您的 numpy 值看起来相同:

In [3]: p_function(1)
Out[3]: 0.06642589646577152
In [4]: p_deriv(1)
Out[4]: 0.06643181982384715

整合结果:

>> numerator
numerator =  795765635.47589

In [7]: numerator = quad(p_deriv, x1, x2)
In [8]: numerator
Out[8]: (795765635.4758892, 0.030880182071769013)

阅读文档,我们看到 quad 返回(默认)2 个值,即积分和误差估计。该元组的第一个与 MATLAB 匹配。您确实使用了numerator[0]

分母相同 - 在乘法之前注意从元组中提取值:

In [10]: denominator = 4 * quad(p_function, x1, x2)[0]
In [11]: denominator
Out[11]: 2568704689.659281
In [12]: numerator[0] / denominator
Out[12]: 0.309792573151506

>> beta
beta =  0.30979

如果不进行校正,结果会大 4 倍

In [13]: denominator = 4 * quad(p_function, x1, x2)
In [14]: denominator
Out[14]: 
(642176172.4148202,
 0.006319781838840299,
 642176172.4148202,
 0.006319781838840299,
 642176172.4148202,
 0.006319781838840299,
 642176172.4148202,
 0.006319781838840299)
In [15]: numerator[0] / denominator[0]
Out[15]: 1.239170292606024

有时(甚至经常)我们需要逐步测试值。即使从头开始开发 numpy 代码,我也会测试所有步骤。

关于python - 如何将此代码从 matlab 转换为 python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71388518/

相关文章:

python - 如何正确对 unicode 数据进行制表

python - 使用正则表达式从Python字符串中提取模式

python - 为什么 NumPy float 不给出 ZeroDivisionError?

用于信号处理的 C++ 库

python - 如何使用资源模块来衡量一个函数的运行时间?

python - 通过 numpy.random.seed 设置的随机种子是否跨子模块维护?

python - 在没有循环的情况下将 "nan"添加到 numpy 数组 20 次

matlab - (MATLAB) 线性方程的所有整数正解

matlab - 如何绘制用于分类算法的 theta 向量线 (MATLAB)

python - 如何将numpy列表输出到csv文件python 3.6