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功能无法正常工作。我不断收到错误The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(),我不明白我应该如何使用它们。基本上,该TentMap函数接受一个值 X 并根据 X 是什么返回一个特定的值。因此,如果 0<=x<0.5 则返回 2 a x,如果 0.5<=x<=1 则返回 2 a (1-x)。

4

2 回答 2

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. 你确定你不需要使用a你正在使用的地方x吗?

于 2012-12-10T01:31:44.980 回答
3

您可以使用标量值和数组np.vectorize来解决此错误。and电话看起来像

np.vectorize(TentMap)(a,xaxis)
于 2012-12-10T02:40:35.187 回答