2012-12-10 29 views
2
from numpy import * 
from pylab import * 
from math import * 

def TentMap(a,x): 
    if x>= 0 and x<0.5: 
     return 2.*a*x 
    elif x>=0.5 and x<=1.: 
     return 2.*a*(1.-x) 

# We set a = 0.98, a typical chaotic value 
a = 0.98 
N = 1.0 

xaxis = arange(0.0,N,0.01) 

Func = TentMap 

subplot(211) 

title(str(Func.func_name) + ' at a=%g and its second iterate' %a) 
ylabel('X(n+1)') # set y-axis label 
plot(xaxis,Func(a,xaxis), 'g', antialiased=True) 

subplot(212) 

ylabel('X(n+1)') # set y-axis label 
xlabel('X(n)') # set x-axis label 
plot(xaxis,Func(a,Func(a,xaxis)), 'bo', antialiased=True) 

我的TentMap函數無法正常工作。我一直得到錯誤「一個數組的真值超過一個元素是不明確的。使用a.any()或a.all()」我不明白我應該如何使用這些。基本上,TentMap函數取值爲X,並根據X是什麼返回一個確定的值。所以如果0 < = x < 0.5那麼它返回2 * a * x,並且如果0.5 < = x < = 1那麼它返回2 * a *(1-x)。具有多個元素的數組的真值是不明確的錯誤? python

+0

爲什麼直接使用它,因爲部分名稱是[「map」](http://docs.python.org/2/library/functions.html#map) ? –

回答

3

如果你比較一個numpy的陣列與一個數字,你會得到另一個數組:

>>> from numpy import arange 
>>> xaxis = arange(0.0, 0.04, 0.01) 
>>> xaxis 
array([ 0. , 0.01, 0.02, 0.03]) 
>>> xaxis <= .02 
array([ True, True, True, False], dtype=bool) 

的問題,當你想開始and這個別的東西,或在布爾上下文中使用它:

>>> xaxis <= .02 and True 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

>>> bool(xaxis <= .02) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

這就是你想在你的TentMap的構造做什麼。您確定您不需要在使用x時使用a

+0

是的,我應該使用x – Randy

3

您可以使用np.vectorize來解決使用帶標量值和arrray的and時發生的此錯誤。該調用看起來像

np.vectorize(TentMap)(a,xaxis) 
相關問題