Search code examples
pythonmatrixvectorodedifferential-equations

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. 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.

Could you help me with that?


Solution

  • From the documentation for solve_ivp: "The calling signature is fun(t, y). Here t is a scalar". I think you have your arguments in system swapped.

    If I change the signature of system to system(t, u) your code runs fine for me. Although, I couldn't say whether I'm getting the expected answer or not.