2016-05-14 66 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