【问题标题】:Find root of numerical integration求数值积分的根
【发布时间】:2013-02-08 06:19:53
【问题描述】:

我正在尝试用 Python 重现这个 Mathematica 程序:

它找到数值积分的根,并形成这些值的图。但是,我无法尝试运行。

当前尝试:

从 scipy.integrate 导入四边形 从 scipy 导入集成 从 scipy.optimize 导入 fsolve 将pylab导入为pl 将 numpy 导入为 np

# Variables.
boltzmann_const = 1.38e-23
planck_const = 6.62e-34
hbar = planck_const / ( 2 * np.pi )
transition_temp = 9.2
gap_energy_at_zero_kelvin = 3.528 / ( 2 * transition_temp * boltzmann_const )
debye_freq = ( 296 * boltzmann_const ) / hbar

# For subtracting from root_of_integral
a_const = np.log( ( 1.13 * hbar * debye_freq ) / ( boltzmann_const * transition_temp) )
# For simplifying function f.
b_const = ( hbar * debye_freq ) / ( 2 * boltzmann_const)


def f( coherence_length, temp ):
    # Defines the equation whose integral will have its roots found. Epsilon = coherence length. Delta = Gap energy.

    squareRoot = np.sqrt( coherence_length*coherence_length + gap_energy*gap_energy )
    return np.tanh( ( ( b_const / temp ) * squareRoot ) / squareRoot )


def integrate( coherence_length, temp ):
    # Integrates equation f with respect to E, between 0 and 1. 

    return integrate.quad( f, 0, 1, args = ( temp, ) )[0]


def root_of_integral( temp ):
    # Finds the roots of the integral with a guess of 0.01.

   return fsolve( integrate, 0.01, args = ( temp, ) )


def gap_energy_values( temp ):
    # Subtracts a_const from each root found, to obtain the gap_energy_values.

    return root_of_integral( temp ) - a_const

【问题讨论】:

  • 解释器给出的错误信息可能有用吗?
  • 请定义“无法运行”。另外,使用这种文字:1.38e-236.62e-341e-23
  • 相应编辑,谢谢!

标签: python numpy scipy physics numerical-integration


【解决方案1】:

正如在 cmets 中已经提到的(@Hristo Iliev 和 @Pavel Annosov),quadreturns a tuple of stuff。如果您假设集成没有问题,就像您在 Mathematica 中所做的那样(虽然这不是一个好主意),那么在这个元组中,您只需要第一个元素,它应该是集成结果.

但这只会给你一个数字,而不是T的函数。要获得后者,您需要自己定义相应的函数,就像您在 Mathematica 中使用 \Delta[T_]:=... 所做的一样

以下几点可以帮助您入门:

def f(E, T):
    """To be integrated over T."""
    temp = np.sqrt(E * E + T * T)
    return np.tanh(1477.92 * temp) / temp


def gap(T):
    """Integration result: \Delta(T)"""
    return quad(f, 0, 1, args=(T, ))[0]  #NB: [0] select the 1st element of a tuple

请注意,您需要使用args=(T,) 语法将T 参数发送到正在集成的函数:quad 在函数的第一个参数上集成,并且它需要其他参数才能评估@ 987654330@.

现在您可以将此gap(T) 提供给fsolvewhich also expects a function(更准确地说是callable)。

在更一般的层面上,您不应该使用玻尔兹曼常数、hbar 等数值(甚至 Mathematica 也会抱怨!)。相反,您应该做的是,您应该以无量纲形式编写公式:以能量单位测量温度(因此 k_B = 1)等,在积分中进行适当的替换,以便处理无量纲参数的无量纲函数 --- 和然后让电脑处理这些。

【讨论】:

  • 在物理学中,不能有非无量纲量的指数。双曲正切的参数已经是无量纲的并且是相同的,无论是否使用自然单位。我想说,取决于 Mathematica 如何评估 Tanh1477.92*Sqrt[e^2+x^2] 超出了双精度指数的可接受参数。例如。如果它使用Tanh[x] = (Exp[2x]-1)/(Exp[2x]+1),那么对于x >= 0.06,一个有1477.92*Sqrt[x^2+...] > 709.78,并且对于[[0,1]] 中的任何e,参数将超出范围。
  • 是的,Hristo,“自然单位”可能是也可能不是更好的措辞。我个人更喜欢认为这只是在积分等中进行适当的替换——甚至在触摸键盘之前,但这是个人喜好。看起来我们都同意在代码中包含 kB=1.23e-23 之类的常量是不对的。至于稳定性,我会先等 OP 解决单位的问题,然后再看看数值稳定性是否真的存在问题。
  • 我同意,通常使用无量纲量可以简化事情并提高数值稳定性。但不是在这种特殊情况下。
  • 你知道1477这个神奇的数字在适当的单位中会变成什么吗?我不。而且,一旦整合完成,就会有fsolve1e-23 作为(我猜)一个起点。
  • 幻数 1477.92 是德拜截止能与约 0.1K 处热能之比的一半。它不会改变,无论使用正确或不正确的单位,除非改变积分的形式。因为我不打算把它变成关于铌的 BCS 理论的讲座,并且鉴于 numpy.tanh() 在大参数的情况下评估得很好,让我们拭目以待,看看 OP 会想出什么。到目前为止,他并没有就“Python 的超导性”问题提供任何合理的反馈......
【解决方案2】:

这一行:

integral = (integrate.quad(lambda E: np.tanh(1477.92*np.sqrt(E**2+x**2))/np.sqrt(E**2+x**2), 0, 1)

有不平衡的括号:

integral = integrate.quad(lambda E: np.tanh(1477.92*np.sqrt(E**2+x**2))/np.sqrt(E**2+x**2), 0, 1)

如果你分手会更容易看到,例如

x_values = arange(0.01, 0.1, 0.0001)

delta = []
for x in x_values:
    def fun(E):
        distance = np.sqrt(E * E + x * x)
        return np.tanh(1477.92 * distance) / distance

    integral = integrate.quad(fun, 0, 1)
    delta_val = fsolve(integral, 1e-23) - a
    delta.append(delta_val)

pl.plot(delta, x_values)

【讨论】:

  • 感谢您迄今为止的回答!这看起来不错,但我收到错误:pastie.org/private/frbdw2xsurchme6s5czk1w
  • 这应该是一个单独的问题,但是integrate.quad 返回一个元组,fsolve 需要一个函数。由于我数学不好,无法解析你的mathematica程序,我不知道如何解决它。
  • 在您的情况下,integral 可能是一对数字:(result, absoluteError)。您是否期望 integrate 返回一个函数?
  • 您必须定义一个计算积分的函数并将该函数传递给根查找器fsolve
猜你喜欢
  • 2015-03-30
  • 2012-12-03
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 2014-02-27
相关资源
最近更新 更多