Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用odeint避免发散解?射击方法_Python_Methods_Ode_Odeint - Fatal编程技术网

Python 使用odeint避免发散解?射击方法

Python 使用odeint避免发散解?射击方法,python,methods,ode,odeint,Python,Methods,Ode,Odeint,我试图用Python解一个等式。基本上我想做的是解方程: (1/x^2)*d(Gam*dL/dx)/dx)+(a^2*x^2/Gam-(m^2))*L=0 这是Schwarzschild时空中大质量标量场的Klein-Gordon方程。它假设我们知道m和Gam=x^2-2*x。我知道的初始/边界条件是L(2+epsilon)=1和L(infty)=0。注意,方程的渐近行为是 L(x-->infty)-->Exp[(m^2-a^2)*x]/x and Exp[-(m^2-a^2)*x

我试图用Python解一个等式。基本上我想做的是解方程:

(1/x^2)*d(Gam*dL/dx)/dx)+(a^2*x^2/Gam-(m^2))*L=0
这是Schwarzschild时空中大质量标量场的Klein-Gordon方程。它假设我们知道
m
Gam=x^2-2*x
。我知道的初始/边界条件是
L(2+epsilon)=1
L(infty)=0
。注意,方程的渐近行为是

L(x-->infty)-->Exp[(m^2-a^2)*x]/x and Exp[-(m^2-a^2)*x]/x 
然后,如果
a^2>m^2
我们将得到振荡解,而如果
a^2
我们将得到发散解和衰减解

我感兴趣的是衰变解,然而,当我试图解上面的方程,将其转化为一阶微分方程组,并使用打靶法,以找到能给出我感兴趣的行为的“a”时,我总是有一个发散解。我认为这是因为,
odeint
总是在寻找发散的渐近解。有没有办法避免或告诉
odeint
我对衰变解决方案感兴趣?如果没有,你知道我能解决这个问题的方法吗?也许用另一种方法来解我的微分方程组?如果是,哪种方法

基本上,我所做的是为“a”添加一个新的方程组

以使“a”作为常数。然后我考虑“a_0”的不同值,并询问我的边界条件是否满足

谢谢你抽出时间。问候,


Luis p.

考虑到辅助吸力的行为,我在无穷远处加入值,这意味着我将在场和它的导数之间建立一个关系。如果有帮助,我将为您发布代码:

from IPython import get_ipython
get_ipython().magic('reset -sf')
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from math import *
from scipy.integrate import ode
这些是Schwarzschild的初始条件。该字段在重新缩放下是不变的,那么我可以使用$L(2+\epsilon)=1$

这是我们的方程组

def F_sch(IC,r,rho_c,m,lam,l,j=0,mu=0):
    L = IC[0]
    ph = IC[1]
    om = IC[2]
    b = IC[3]

    Gam_sch=r**2.-2.*r

    dR_dr = ph
    dph_dr = (1./Gam_sch)*(2.*(1.-r)*ph+L*(l*(l+1.))-om**2.*r**4.*L/Gam_sch+(m**2.+lam*L**2.)*r**2.*L)
    dom_dr = b
    db_dr = 0.
    return [dR_dr,dph_dr,dom_dr,db_dr]
然后我尝试使用不同的“om”值,并询问是否满足边界条件。p_sch是我的模型的参数。总的来说,我想做的是更复杂一点,总的来说,我需要更多的参数,在大规模的情况下。然而,我需要从最简单的开始,这就是我在这里要问的

p_sch = (1,1,0,0) #[rho_c,m,lam,l], lam and l are for a more complicated case 
ep = 0.2
ep_r = 0.01
r_end = 500
n_r = 500000
n_omega = 1000
omega = np.linspace(p_sch[1]-ep,p_sch[1],n_omega)
r = np.linspace(2+ep_r,r_end,n_r)
tol = 0.01
a = 0

for j in range(len(omega)): 
    print('trying with $omega =$',omega[j])
    omeg = [omega[j]]
    ini = init_sch(omeg)
    Y = odeint(F_sch,ini,r,p_sch,mxstep=50000000)
    print Y[-1,0]
#Here I ask if my asymptotic behavior is fulfilled or not. This should be basically my value at infinity
    if abs(Y[-1,0]*((p_sch[1]**2.-Y[-1,2]**2.)**(1/2.)+1./(r[-1]))+Y[-1,1]) < tol:
        print(j,'times iterations in omega')
        print("R'(inf)) = ", Y[-1,0])        
        print("\omega",omega[j])
        omega_1 = [omega[j]] 
        a = 10
        break           
    if a > 1:
        break
我得到了这个错误:ValueError:
bc
return应该有形状(11,),但实际上有(8,)。
ValueError:
bc
return应该有形状(11,),但实际上有形状(8,)。

这更多的是关于物理或科学计算的问题。你用什么密码射击?如何在无穷远处合并值?
def F_sch(IC,r,rho_c,m,lam,l,j=0,mu=0):
    L = IC[0]
    ph = IC[1]
    om = IC[2]
    b = IC[3]

    Gam_sch=r**2.-2.*r

    dR_dr = ph
    dph_dr = (1./Gam_sch)*(2.*(1.-r)*ph+L*(l*(l+1.))-om**2.*r**4.*L/Gam_sch+(m**2.+lam*L**2.)*r**2.*L)
    dom_dr = b
    db_dr = 0.
    return [dR_dr,dph_dr,dom_dr,db_dr]
p_sch = (1,1,0,0) #[rho_c,m,lam,l], lam and l are for a more complicated case 
ep = 0.2
ep_r = 0.01
r_end = 500
n_r = 500000
n_omega = 1000
omega = np.linspace(p_sch[1]-ep,p_sch[1],n_omega)
r = np.linspace(2+ep_r,r_end,n_r)
tol = 0.01
a = 0

for j in range(len(omega)): 
    print('trying with $omega =$',omega[j])
    omeg = [omega[j]]
    ini = init_sch(omeg)
    Y = odeint(F_sch,ini,r,p_sch,mxstep=50000000)
    print Y[-1,0]
#Here I ask if my asymptotic behavior is fulfilled or not. This should be basically my value at infinity
    if abs(Y[-1,0]*((p_sch[1]**2.-Y[-1,2]**2.)**(1/2.)+1./(r[-1]))+Y[-1,1]) < tol:
        print(j,'times iterations in omega')
        print("R'(inf)) = ", Y[-1,0])        
        print("\omega",omega[j])
        omega_1 = [omega[j]] 
        a = 10
        break           
    if a > 1:
        break
from IPython import get_ipython
get_ipython().magic('reset -sf')
import numpy as np
import matplotlib.pyplot as plt
from math import *
from scipy.integrate import solve_bvp

def bc(ya,yb,p_sch):
    m = p_sch[1]
    om = p_sch[4]
    tol_s = p_sch[5]
    r_end = p_sch[6]

    return np.array([ya[0]-1,yb[0]-tol_s,ya[1],yb[1]+((m**2-yb[2]**2)**(1/2)+1/r_end)*yb[0],ya[2]-om,yb[2]-om,ya[3],yb[3]])

def fun(r,y,p_sch):
    rho_c = p_sch[0]
    m = p_sch[1]
    lam = p_sch[2]
    l = p_sch[3]

    L = y[0]
    ph = y[1]
    om = y[2]
    b = y[3]

    Gam_sch=r**2.-2.*r

    dR_dr = ph
    dph_dr = (1./Gam_sch)*(2.*(1.-r)*ph+L*(l*(l+1.))-om**2.*r**4.*L/Gam_sch+(m**2.+lam*L**2.)*r**2.*L)
    dom_dr = b
    db_dr = 0.*y[3]
    return np.vstack((dR_dr,dph_dr,dom_dr,db_dr))

eps_r=0.01
r_end = 500
n_r = 50000
r = np.linspace(2+eps_r,r_end,n_r)
y = np.zeros((4,r.size))
y[0]=1
tol_s = 0.0001
p_sch= (1,1,0,0,0.8,tol_s,r_end)


sol = solve_bvp(fun,bc, r, y, p_sch)