簡體   English   中英

Python - 求解瞬態矩陣微分方程

[英]Python - Solve time-dependent matrix differential equation

我在求解瞬態矩陣微分方程時遇到了一些問題。 問題是時間相關系數不僅僅是遵循某種時間相關的形狀,而是另一個微分方程的解。

到目前為止,我已經考慮了我的系數 G(t) 只是 G(t)=pulse(t) 的簡單情況,其中這個脈沖(t) 是我定義的 function。 這是代碼:

# Matrix differential equation
def Leq(t,v,pulse): 
    v=v.reshape(4,4) #covariance matrix 
    M=np.array([[-kappa,0,E_0*pulse(t),0],\.  #coefficient matrix 
                [0,-kappa,0,-E_0*pulse(t)],\
                [E_0*pulse(t),0,-kappa,0],\
                [0,-E_0*pulse(t),0,-kappa]])
    
    Driff=kappa*np.ones((4,4),float) #constant term
    
    dv=M.dot(v)+v.dot(M)+Driff #solve dot(v)=Mv+vM^(T)+D
    return dv.reshape(-1)  #return vectorized matrix

#Pulse shape
def Gaussian(t):
    return np.exp(-(t - t0)**2.0/(tau ** 2.0))

#scipy solver
cov0=np.zeros((4,4),float) ##initial vector
cov0 = cov0.reshape(-1);   ## vectorize initial vector
Tmax=20 ##max value for time
Nmax=10000 ##number of steps
dt=Tmax/Nmax  ##increment of time
t=np.linspace(0.0,Tmax,Nmax+1)

Gaussian_sol=solve_ivp(Leq, [min(t),max(t)] , cov0, t_eval= t, args=(Gaussian,))

我得到了一個不錯的結果。 問題是這不正是我需要的。 我需要的是dot(G(t))=-kappa*G(t)+pulse(t),即系數是微分方程的解。

我試圖在 Leq 中以一種矢量化的方式實現這個微分方程,方法是返回另一個將被饋送到 M 的參數 G(t),但我遇到了 arrays 的尺寸問題。

知道我應該如何進行嗎?

謝謝,

原則上你有正確的想法,你只需要拆分並加入 state 和衍生向量。

def Leq(t,u,pulse): 
    v=u[:16].reshape(4,4) #covariance matrix 
    G=u[16:].reshape(4,4) 
    ... # compute dG and dv
    return np.concatenate([dv.flatten(), dG.flatten()])

初始向量也必須是這樣的復合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM