Нахождение всех корней трансцендентного уравнения в Python (например, решения в связанном состоянии с потенциалом скважины конечного квадрата) - PullRequest
0 голосов
/ 21 октября 2019

Итак, я пытаюсь найти для собственных значений решения в связанном состоянии потенциал конечной квадратной ямы, который включает решение уравнения:

image

I was able to solve it graphically by plotting the two functions and figuring out where they intersect, then using the intersection as an initial guess in scipy.optimize.root. However, this seems like a fairly cumbersome process. From what I understand, root finding methods usually requires an initial guess and finds the local minima closest to the initial guess.

What I'm wondering is if there is a way to find all the roots or all local minima in a given range in Python without necessarily providing an initial guess. From the web searches I've done, there seems to be some methods in mathematica but in general, finding all roots within a good tolerance is impossible except for specific functions. What I'm wondering, though, is whether my equation happens to fall under those specific situations? Below is a graph of the two functions and the code I used using scipy.optimize.root: enter image description here

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

Ответы [ 2 ]

1 голос
/ 21 октября 2019

Нет общего пути. В этом конкретном случае все корни хорошо локализованы благодаря периодичности функции тангенса, поэтому вы можете просто запустить root-findind, например, brentq на каждом из этих интервалов.

0 голосов
/ 21 октября 2019

Это скорее взлом, а не ответ, но можно сделать процесс поиска корня немного менее утомительным, предоставив некоторую сетку начальных догадок и взяв набор решений, найденных при всех попытках.
Используя sympy (значения по умолчанию в nsolve могут обеспечить более надежный решатель), вы можете сделать

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])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...