求解矩阵的一阶微分方程
Solving 1st order differential equations for matrices
我想在 python 耦合微分方程组中编写代码:dF/dt=A(F)
其中 F
是矩阵,A(F)
是矩阵的函数F
.
当 F
和 A(F)
是向量时,使用 scipy.integrate.odeint
求解方程。
但是,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.
正如 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()
我想在 python 耦合微分方程组中编写代码:dF/dt=A(F)
其中 F
是矩阵,A(F)
是矩阵的函数F
.
当 F
和 A(F)
是向量时,使用 scipy.integrate.odeint
求解方程。
但是,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.
正如 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()