乘法

2017-01-01 66 views
2

雖然在試圖計算從下面的函數x(n+1) = 1 -a*x(n)**2 + y(n); y(n+1) = b*x(n)的雅可比矩陣點積,我得到以下錯誤:vec1 = J*vec1 TypeError: can't multiply sequence by non-int of type 'list'乘法

import numpy as np 
a, b = 1.2, 0.4 
x, y = 0., 0. 

vec1 = [[1], [0]] 
vec2 = [[0], [1]] 

for i in range(500): 
    x1 = 1. - a*x**2 + y 
    y1 = b*x 
    x = x1 
    y = y1 
    J = [[-2.*a*x, 1], [b, 0]] 
    vec1 = J*vec1 
    vec2 = J*vec2 
    dotprod1 = np.dot(vec1,vec1) 
    dotprod2 = np.dot(vec1,vec2) 
print(dotprod1) 
print(dotprod2) 

回答

7

vec1vec2應該是numpy的陣列,而不是Python的清單:

import numpy as np 
from numpy import dot, exp, log, sqrt 
a, b = 1.2, 0.4 
x, y = 0., 0. 

vec1 = np.array([[1], [0]]) 
vec2 = np.array([[0], [1]]) 

for i in range(500): 
    x1 = 1. - a*x**2 + y 
    y1 = b*x 
    x = x1 
    y = y1 
    J = [[-2.*a*x, 1], [b, 0]] 
    vec1 = J*vec1 
    vec2 = J*vec2 
    dotprod1 = dot(vec1,vec1) 
    dotprod2 = dot(vec1,vec2) 
print(dotprod1) 
print(dotprod2) 

輸出:

[[ 2.32511745e+112 1.52483358e+056] 
[ 0.00000000e+000 0.00000000e+000]] 
[[ 1.07150861e-199 0.00000000e+000] 
[ 0.00000000e+000 0.00000000e+000]]