2016-10-10 280 views
0

我想解決這個方程:解決使用odeint二階ODE在Python

Y '' + A Y」 - B y = 0的

其中y,A和B是的功能同樣的變量「a」

我嘗試下面的代碼:

import numpy as np 
import matplotlib.pyplot as pyplot 
from scipy.integrate import odeint 


x0 = 0.,0.1 # initial conditions 
oe = 0.0 # cosmological constant density parameter 
om = 1. # matter density parameter 
h = 2.3E-18 # Hubble constant 
w = -1. 

a = np.linspace(0.1,1,10) # scale factor 


def H(a): # Hubble rate equation 
    return h*np.sqrt((om/(a**3.))+(oe/(a**(3.*(1.+w))))) 

def A(a): # differential equation term 
    return (-3./(2.*a)) 

def B(a): # differential equation term 
    return (3.*om*(h**2.))/((2.*(a**5.))*(H(a)**2.)) 



def system(X,a): # differential equation system 
    X0 = X[0] 
    X1 = X[1] 
    X2 = -A(a)*X1 + B(a)*X0 

    return X1,X2 



x = odeint(system,x0,a) 



pyplot.semilogx(a,x, linestyle='-', c="k", linewidth="2") 

它返回一個情節是沒有意義的。我應該只得到一個情節,最大值「x」在a = 1時爲1。但我得到了以下情節:

情節,我得到:

Plot that I get

和預期的結果是一樣的連續線下圖中:

enter image description here

任何建議?

+0

那你得到了什麼?爲什麼不添加一個數字?請閱讀[問]。另外,H^2對數值精度不會有好處。如果可能,我建議重寫您的方程,使得數量在1的數量級上, –

+0

只需按照示例一步一步http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate。 odeint.html – outoftime

+0

該代碼的工作原理應該如此,您應該將問題轉移到物理論壇,討論要更改的模型以使模型符合您的期望。 – LutzL

回答

0

請嘗試在功能系統(X,a)中的B(a)* X0之前更改符號。根據你的初始方程,它應該是負值。

+0

我的不好。原方程是y「+ A * y' - B * y –