简体   繁体   English

矢量微分方程

[英]Vector differential equations

I am trying to solve a vector differential equation in Python and I keep getting an error I don't really understand.我正在尝试求解 Python 中的向量微分方程,但我一直收到一个我不太明白的错误。 Here is my code:这是我的代码:

    import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt

N=100

tau_s=1
tau_n=1
R=1

t0=0
t1=10

v_rest=-65

M=[]


for i in range(N):
    M.append([])
    for j in range(N):
        M[i].append(1)

M=np.array(M)

def I(t):
    I=5   
    return I

def system(u,t):
    du=np.zeros((N,)

    du=-1/tau_s*(u-v_rest)+R*(I(t)+np.dot(M,u))

    return du

u0=v_rest*np.ones(N,)

ts=(0,1000)

sol=solve_ivp(system,ts,u0)

And the error I get states:我得到的错误是:

ValueError: could not broadcast input array from shape (100,100) into shape (100)

If I understand it properly it means that one side of the diff equation has a different shape than the other one hence Python cannot solve it, but the multiplication of M and u should yield a vector with the shape (100) so I am not sure what is happening.如果我理解正确,这意味着差异方程的一侧具有与另一侧不同的形状,因此 Python 无法解决它,但是 M 和 u 的乘法应该产生一个形状为 (100) 的向量,所以我不确定怎么了。

Could you help me with that?你能帮我解决这个问题吗?

From the documentation for solve_ivp : "The calling signature is fun(t, y). Here t is a scalar".来自solve_ivp 的文档:“调用签名是 fun(t, y)。这里 t 是一个标量”。 I think you have your arguments in system swapped.我认为您已在系统中交换了 arguments。

If I change the signature of system to system(t, u) your code runs fine for me.如果我将 system 的签名更改为system(t, u)您的代码对我来说运行良好。 Although, I couldn't say whether I'm getting the expected answer or not.虽然,我不能说我是否得到了预期的答案。

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

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