我有一个多项式,我试图找到所有根以使用nsolve进行数值计算。当我尝试使用nsolve查找最低根目录(这是我真正需要的唯一根目录,但是我不介意找到所有根目录时)时,出现一个错误,提示“无法从-0.499923944877944创建MPF”。
我尝试使用多个不同的求解器。当我使用SymPy的求解时,它仅找到5个根(当应该有6个时)。使用solve也花费了很长时间,因为我认为它最初是尝试以符号方式解决它。我尝试了Solveset,但没有给出正确的答案。
下面是我的所有代码。一切都会按预期进行,直到nsolve最底层为止。
from symengine import *
import sympy
from sympy import Matrix
from sympy import nsolve
trial = Matrix()
r, E1, E = symbols('r, E1, E')
H11, H22, H12, H21 = symbols("H11, H22, H12, H21")
S11, S22, S12, S21 = symbols("S11, S22, S12, S21")
low = 0
high = oo
integrate = lambda *args: sympy.N(sympy.integrate(*args))
quadratic_expression = (H11-E1*S11)*(H22-E1*S22)-(H12-E1*S12)*(H21-E1*S21)
general_solution = sympify(sympy.solve(quadratic_expression, E1)[0])
def solve_quadratic(**kwargs):
return general_solution.subs(kwargs)
def H(fun):
return -fun.diff(r, 2)/2 - fun.diff(r)/r - fun/r
psi0 = exp(-3*r/2)
trial = trial.row_insert(0, Matrix([psi0]))
I1 = integrate(4*pi*(r**2)*psi0*H(psi0), (r, low, high))
I2 = integrate(4*pi*(r**2)*psi0**2, (r, low, high))
E0 = I1/I2
print(E0)
for x in range(5):
f1 = psi0
f2 = r * (H(psi0)-E0*psi0)
Hf1 = H(f1).simplify()
Hf2 = H(f2).simplify()
H11 = integrate(4*pi*(r**2)*f1*Hf1, (r, low, high))
H12 = integrate(4*pi*(r**2)*f1*Hf2, (r, low, high))
H21 = integrate(4*pi*(r**2)*f2*Hf1, (r, low, high))
H22 = integrate(4*pi*(r**2)*f2*Hf2, (r, low, high))
S11 = integrate(4*pi*(r**2)*f1**2, (r, low, high))
S12 = integrate(4*pi*(r**2)*f1*f2, (r, low, high))
S21 = S12
S22 = integrate(4*pi*(r**2)*f2**2, (r, low, high))
E0 = solve_quadratic(
H11=H11, H22=H22, H12=H12, H21=H21,
S11=S11, S22=S22, S12=S12, S21=S21,
)
print(E0)
C = -(H11 - E0*S11)/(H12 - E0*S12)
psi0 = (f1 + C*f2).simplify()
trial = trial.row_insert(x+1, Matrix([[psi0]]))
# Free ICI Part
h = zeros(x+2, x+2)
HS = zeros(x+2, 1)
S = zeros(x+2, x+2)
for s in range(x+2):
HS[s] = H(trial[s]).simplify()
for i in range(x+2):
for j in range(x+2):
h[i, j] = integrate(4*pi*(r**2)*trial[i]*HS[j], (r, low, high))
for i in range(x+2):
for j in range(x+2):
S[i, j] = integrate(4*pi*(r**2)*trial[i]*trial[j], (r, low, high))
m = h - E*S
eqn = m.det()
roots = nsolve(eqn, E0)
print(roots)
最小根应该大于或等于-0.5,但是甚至还没有达到给我根的程度。
答案 0 :(得分:0)
使用nsolve时的最初猜测应该是浮点数,但是我输入了一个符号数。下面是更正的代码。
from symengine import *
import sympy
from sympy import Matrix
from sympy import nsolve
trial = Matrix()
r, E1, E = symbols('r, E1, E')
H11, H22, H12, H21 = symbols("H11, H22, H12, H21")
S11, S22, S12, S21 = symbols("S11, S22, S12, S21")
low = 0
high = oo
integrate = lambda *args: sympy.N(sympy.integrate(*args))
quadratic_expression = (H11-E1*S11)*(H22-E1*S22)-(H12-E1*S12)*(H21-E1*S21)
general_solution = sympify(sympy.solve(quadratic_expression, E1)[0])
def solve_quadratic(**kwargs):
return general_solution.subs(kwargs)
def H(fun):
return -fun.diff(r, 2)/2 - fun.diff(r)/r - fun/r
psi0 = exp(-3*r/2)
trial = trial.row_insert(0, Matrix([psi0]))
I1 = integrate(4*pi*(r**2)*psi0*H(psi0), (r, low, high))
I2 = integrate(4*pi*(r**2)*psi0**2, (r, low, high))
E0 = I1/I2
print(E0)
for x in range(6):
f1 = psi0
f2 = r * (H(psi0)-E0*psi0)
Hf1 = H(f1).simplify()
Hf2 = H(f2).simplify()
H11 = integrate(4*pi*(r**2)*f1*Hf1, (r, low, high))
H12 = integrate(4*pi*(r**2)*f1*Hf2, (r, low, high))
H21 = integrate(4*pi*(r**2)*f2*Hf1, (r, low, high))
H22 = integrate(4*pi*(r**2)*f2*Hf2, (r, low, high))
S11 = integrate(4*pi*(r**2)*f1**2, (r, low, high))
S12 = integrate(4*pi*(r**2)*f1*f2, (r, low, high))
S21 = S12
S22 = integrate(4*pi*(r**2)*f2**2, (r, low, high))
E0 = solve_quadratic(
H11=H11, H22=H22, H12=H12, H21=H21,
S11=S11, S22=S22, S12=S12, S21=S21,
)
print(E0)
C = -(H11 - E0*S11)/(H12 - E0*S12)
psi0 = (f1 + C*f2).simplify()
trial = trial.row_insert(x+1, Matrix([[psi0]]))
# Free ICI Part
h = zeros(x+2, x+2)
HS = zeros(x+2, 1)
S = zeros(x+2, x+2)
for s in range(x+2):
HS[s] = H(trial[s]).simplify()
for i in range(x+2):
for j in range(x+2):
h[i, j] = integrate(4*pi*(r**2)*trial[i]*HS[j], (r, low, high))
for i in range(x+2):
for j in range(x+2):
S[i, j] = integrate(4*pi*(r**2)*trial[i]*trial[j], (r, low, high))
m = h - E*S
eqn = m.det()
roots = nsolve(eqn, float(E0))
print(roots)
如果有人知道如何加快nsolve的速度,我很想听听。我可能只是要为二分法编写自己的脚本,因为nsolve花费的时间太长(当我在for循环中执行11次迭代时,我一直在等待几个小时才能解决它,这很荒谬)
答案 1 :(得分:0)
要考虑的两件事:如果您知道根的间隔,nsolve
将接受一个Solver ='bisect'关键字:
>>> nsolve(x**2-3,x,(0.,2.),solver='bisect')
1.73205080756888
此外,在这种情况下,real_roots
可能是获取根的有用方法。在您发布的代码中,x在range(6)范围内,则获得阶数为88的多项式,并使用以下形式对其进行了短阶求解:
>>> eq=nsimplify(eqn, rational=True).as_numer_denom()[0]
>>> [i.n(3) for i in real_roots(Poly(eq,E))][:7]
[-0.836, -0.298, -0.117, -0.0821, 0.0854, 0.181, 0.399]