[英]Solve system of recursive differential equation in Python
So I am trying to solve the following system of differential equations in Python.所以我试图解决 Python 中的以下微分方程组。
System of differential equations微分方程组
As you can see, for each n in {0,1,2,3,...} the system depends on the solution to the previous system.如您所见,对于 {0,1,2,3,...} 中的每个 n,系统依赖于前一个系统的解决方案。
I have tried solving the system for n=0 and found a solution R(0|t) that I can insert in R(1|t) and Python solves the system without problems.我已经尝试解决 n=0 的系统,并找到了一个解决方案 R(0|t),我可以将其插入 R(1|t) 和 Python 解决系统没有问题。 I have defined the solution R(0|t) as r0(t) and implemented the solution for n=1 as follows:
我已将解决方案 R(0|t) 定义为 r0(t) 并实现了 n=1 的解决方案,如下所示:
def model(z,t):
dxdt = -3.273*z[0] + 3.2*z[1] + r0(t)
dydt = 3.041*z[0] - 3.041*z[1]
dzdt = [dxdt, dydt]
return dzdt
z0 = [0,0]
t = np.linspace(0,90, 90)
z1 = odeint(model, z0, t)
However I would like to generalize this solution by calling the solution to the system for n-1 when solving for n.但是,我想通过在求解 n 时调用 n-1 的系统解决方案来概括此解决方案。 As the differential equations only has entry different from zero in the upper right corner of the matrix we only need the solution of z 1 from the previous solution.
由于微分方程仅在矩阵的右上角具有不同于零的条目,因此我们只需要前解中的 z 1的解。 I have tried
我努力了
def model0(z,t):
dxdt = -3.273*z[0] + 3.2*z[1]
dydt = 3.041*z[0] - 3.041*z[1]
dzdt = [dxdt, dydt]
return dzdt
z0 = [1,1]
t = np.linspace(0,90)
def model1(z,t):
dxdt = -3.273*z[0] + 3.2*z[1] + 0.071*odeint(model0, z0, t)[t,1]
dydt = 3.041*z[0] - 3.041*z[1]
dzdt = [dxdt, dydt]
return dzdt
z1 = [0,0]
z = odeint(model1, z1, t)
Without any luck.没有任何运气。 Has anyone any experience in solving these recursive systems of odes in Python?
有没有人在 Python 中解决这些递归系统的经验?
Thanks in advance.提前致谢。
Updated with code for 6x6 matrices and 6 function:更新了 6x6 矩阵和 6 function 的代码:
A = np.array([[h1h1, h1h2, h1h3, h1a1, h1a2, h1a3],
[h2h1, h2h2, h2h3, h2a1, h2a2, h2a3],
[h3h1, h2h3, h3h3, h3a1, h3a2, h3a3],
[a1h1, a1h2, a1h3, a1a1, a1a2, a1a3],
[a2h1, a2h2, a2h3, a2a1, a2a2, a2a3],
[a3h1, a3h2, a3h3, a3a1, a3a2, a3a3]
])
B = np.array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, h3a0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
])
def model0n(u,t):
Ra = u.reshape([-1,6])
n = len(Ra) - 1
dRa = np.zeros(Ra.shape)
dRa[0] = A @ Ra[0]
for i in range(1,n+1):
dRa[i] = A @ Ra[i] + B @ Ra[i-1]
return dRa.flatten()
u0 = [1,1,1,1,1,1,0,0,0,0,0,0]
t = np.linspace(0,90,90+1)
u = odeint(model0n,u0,t)
The above results in the following plot for u[:,0]: Plot for u[:,0] which is supposed to be probabilities上面的结果为 u[:,0] 的 plot: Plot for u[:,0] 这应该是概率
For n=0 it provides results doing the matrix product 'manualy':对于 n=0,它提供了“手动”矩阵乘积的结果:
def modeln0manually(z,t):
d1dt = h1h1*z[0] + h1h2 * z[1] + h1h3*z[2] + h1a1*z[3] + h1a2*z[4] + h1a3*z[5]
d2dt = h2h1*z[0] + h2h2 * z[1] + h2h3*z[2] + h2a1*z[3] + h2a2*z[4] + h2a3*z[5]
d3dt = h3h1*z[0] + h3h2 * z[1] + h3h3*z[2] + h3a1*z[3] + h3a2*z[4] + h3a3*z[5]
d4dt = a1h1*z[0] + a1h2 * z[1] + a1h3*z[2] + a1a1*z[3] + a1a2*z[4] + a1a3*z[5]
d5dt = a2h1*z[0] + a2h2 * z[1] + a2h3*z[2] + a2a1*z[3] + a2a2*z[4] + a2a3*z[5]
d6dt = a3h1*z[0] + a3h2 * z[1] + a3h3*z[2] + a3a1*z[3] + a3a2*z[4] + a3a3*z[5]
drdt = [d1dt, d2dt, d3dt, d4dt, d5dt, d6dt]
return drdt
u0 = [1,1,1,1,1,1]
t = np.linspace(0,90)
z = odeint(modeln0manually, u0, t)
Resulting in the plot for u[:,0]: Plot of u[:,0] as it is supposed to be导致 u[:,0] 的plot: u[:,0] 的 Plot 应该是
Your system is coupled, even if in triangular way.你的系统是耦合的,即使是三角形的。 So the most compact way is to solve it as a coupled system
所以最紧凑的方法就是把它作为一个耦合系统来解决
A = np.array([[-3.273, 3.2], [3.041, -3.041]])
B = np.array([[0, 0.071], [0, 0]])
def model0n(u,t):
Ra = u.reshape([-1,2])
n = len(Ra) - 1
dRa = np.zeros(Ra.shape)
dRa[0] = A @ Ra[0]
for i in range(1,n+1):
dRa[i] = A @ Ra[i] + B @ Ra[i-1]
return dRa.flatten()
u0 = [1,1,0,0]
t = np.linspace(0,90,90+1)
u = odeint(model0n,u0,t)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.