简体   繁体   English

Python - 求解瞬态矩阵微分方程

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

I am facing some problems solving a time-dependent matrix differential equation.我在求解瞬态矩阵微分方程时遇到了一些问题。 The problem is that the time-dependent coefficient is not just following some time-dependent shape, rather it is the solution of another differential equation.问题是时间相关系数不仅仅是遵循某种时间相关的形状,而是另一个微分方程的解。

Up until now, I have considered the trivial case where my coefficient G(t) is just G(t)=pulse(t) where this pulse(t) is a function I define.到目前为止,我已经考虑了我的系数 G(t) 只是 G(t)=pulse(t) 的简单情况,其中这个脉冲(t) 是我定义的 function。 Here is the code:这是代码:

# 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,))

And I get a nice result.我得到了一个不错的结果。 The problem is that is it not exactly what I need.问题是这不正是我需要的。 Want I need is that dot(G(t))=-kappa*G(t)+pulse(t), ie the coefficient is the solution of a differential equation.我需要的是dot(G(t))=-kappa*G(t)+pulse(t),即系数是微分方程的解。

I have tried to implement this differential equation in a sort of vectorized way in Leq by returning another parameter G(t) that would be fed to M, but I was getting problems with the dimensions of the arrays.我试图在 Leq 中以一种矢量化的方式实现这个微分方程,方法是返回另一个将被馈送到 M 的参数 G(t),但我遇到了 arrays 的尺寸问题。

Any idea of how should I proceed?知道我应该如何进行吗?

Thanks,谢谢,

In principle you have the right idea, you just have to split and join the state and derivative vectors.原则上你有正确的想法,你只需要拆分并加入 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()])

The initial vector has likewise to be such a composite.初始向量也必须是这样的复合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM