Search code examples
pythonodeint

Python ODEINT problems with args


I am relatively new to Python and trying to use it to solve an integrator problem

x' = - L * x

Where L is the Laplacian Matrix, that is a matrix representation of a graph. This is part of my code:

def integrate_cons(x, t, l):
   xdot = -l*x
   return xdot;

t = np.linspace(0, 10, 101)

#laplacian is a 3x3 matrix
#initial_condition is a vector
solution = odeint(integrate_cons, initial_conditions, t, args=(laplacian,))
print solution

I'm having problems to pass a matrix like an argument in odeint. How can i solve?


Solution

  • import numpy as np
    from scipy.integrate import odeint
    
    def integrate_cons(x, t, l):
       # unless you use np.matrix which I never do, you have to use np.dot
       xdot = -np.dot(l,x)
       return xdot;
    
    t = np.linspace(0, 10, 101)
    # just a random matrix
    l = np.random.rand(3,3)
    # initial conditions
    x0 = np.array([1,1,1])
    #laplacian is a 3x3 matrix
    #initial_condition is a vector
    solution = odeint(integrate_cons, x0, t, args=(l,))
    print(solution)
    

    Look at the scipy cookbook for examples.