[英]Solving 1st order differential equations for matrices
I'd like to code in python a coupled system of differential equations : dF/dt=A(F)
where F
is a matrix and A(F)
is a function of the matrix F
.我想在 python 中编写一个耦合的微分方程系统: dF/dt=A(F)
其中F
是一个矩阵, A(F)
是矩阵F
的函数。
When F
and A(F)
are vectors the equation is solved using scipy.integrate.odeint
.当F
和A(F)
是向量时,方程使用scipy.integrate.odeint
。
However, scipy.integrate.odeint
doesn't work for matrices, and I get an error :但是, scipy.integrate.odeint
不适用于矩阵,并且出现错误:
tmin, tmax, tstep = (0., 200., 1)
t_test=np.arange(tmin, tmax, tstep) #time vector
dydt_testm=np.array([[0.,1.],[2.,3.]])
Y0_test=np.array([[0,1],[0,1]])
def dydt_test(y,t):
return dydt_testm
result = si.odeint(dydt_test, Y0_test,t_test)
ValueError: Initial condition y0 must be one-dimensional. ValueError:初始条件 y0 必须是一维的。
As commented by Warren Weckesser in the comments, odeintw
does the job.正如 Warren Weckesser 在评论中所评论的那样, odeintw
完成这项工作。
from odeintw import odeintw
import numpy as np
Y0_test=np.array([[0,1],[0,1]])
tmin, tmax, tstep = (0., 200., 1)
t_test=np.arange(tmin, tmax, tstep) #time vector
dydt_testm=np.array([[0.,1.],[2.,3.]])
def dydt_test(y,t):
return dydt_testm
result = odeintw(dydt_test, #Computes the derivative of y at t
Y0_test, #Initial condition on y (can be a vector).
t_test)
plt.plot(t_test,result[:,0,1])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.