2016-05-14 51 views
0

我是比较新的Python和试图用它来解决积分问题的Python ODEINT问题ARGS

x' = - L * x 

其中L是拉普拉斯矩阵,这是一个图的矩阵表示。这是我的代码的一部分:

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 

我有问题要传递一个矩阵,如odeint中的参数。我该如何解决?

回答

0
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) 

查看scipy cookbook的例子。

+0

这帮了我,谢谢。现在我有问题来绘制解决方案。我试着用'plt.plot(t,solution [:,0])',但是我得到错误“元组索引必须是整数,而不是元组” – oigna

+0

不能重现你的错误 – Moritz