【问题标题】:Finding all roots of a transcendental equation in Python (eg. Bound state solutions of the Finite Square Well potential)在 Python 中查找超越方程的所有根(例如,有限平方阱势的束缚态解)
【发布时间】:2019-10-22 04:59:25
【问题描述】:

所以我正在尝试求解有限平方阱势的束缚态解的特征值,其中涉及求解方程:

我能够通过绘制两个函数并找出它们相交的位置以图形方式解决它,然后在 scipy.optimize.root 中使用相交作为初始猜测。然而,这似乎是一个相当繁琐的过程。据我了解,寻根方法通常需要初始猜测并找到最接近初始猜测的局部最小值。

我想知道是否有一种方法可以在 Python 中找到给定范围内的所有根或所有局部最小值,而不必提供初始猜测。从我所做的网络搜索来看,mathematica 中似乎有一些方法,但总的来说,除了特定函数之外,不可能在良好的公差范围内找到所有根。不过,我想知道的是,我的方程式是否恰好属于这些特定情况?下面是两个函数的图表以及我使用 scipy.optimize.root 使用的代码:

def func(z, z0):
    return np.tan(z) - np.sqrt((z0/z)**2 - 1)

#finding z0
a = 3
V0 = 3
h_bar = 1
m = 1

z0 = a*np.sqrt(2*m*V0)/h_bar

#calculating roots
res = opt.root(func, [1.4, 4.0, 6.2], args = (z0, ))
res.x

#output
array([1.38165158, 4.11759958, 6.70483966])

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    没有通用的方法。 在这种特定情况下,由于切线函数的周期性,所有根都很好地定位,因此您可以简单地运行 root-findind,例如brentq 在这些间隔中的每一个上。

    【讨论】:

      【解决方案2】:

      这更像是一种技巧而不是答案,但是通过提供一些初始猜测网格并采用所有尝试找到的解决方案集,可以成功地使根查找过程变得不那么乏味。
      使用sympynsolve 中的默认值可能会提供更强大的求解器)你可以这样做

      from sympy.solvers import nsolve 
      from sympy import tan, sqrt, symbols 
      import numpy as np
      
      a = 3 
      V0 = 3 
      h_bar = 1 
      m = 1
      
      z0 =  a*np.sqrt(2*m*V0)/h_bar
      
      z  = symbols('z')
      
      expr = tan(z) - sqrt((z0/z)**2 - 1)
      
      # try out a grid of initial conditions and get set of found solutions
      # this may fail with some other choice of interval
      initial_guesses = np.linspace(0.2, 7, 100) 
      
      # candidate solutions
      set([nsolve(expr, z, guess) for guess in initial_guesses])
      

      【讨论】:

        猜你喜欢
        • 2015-04-27
        • 2017-08-20
        • 2017-03-22
        • 1970-01-01
        • 1970-01-01
        • 2013-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多