Reputation: 507
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)
My TentMap
function isn't working properly. I keep getting the error The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I don't understand how I'm supposed to use those. Basically, the TentMap
function takes a value X and returns a certain value depending on what X is. So if 0<=x<0.5 then it returns 2ax and if 0.5<=x<=1 then it returns 2a(1-x).
Upvotes: 2
Views: 3536
Reputation: 13412
You can use np.vectorize
to get around this error which occurs when using and
with a scalar value and arrray. The call looks like
np.vectorize(TentMap)(a,xaxis)
Upvotes: 3
Reputation: 69092
If you compare a numpy array with a number, you'll get another array:
>>> 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)
The problem starts when you want to and
this with something else, or use it in a boolean context:
>>> 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()
And that's what you're trying to do in the constructor of your TentMap
. Are you sure you don't need to use a
where you're using x
?
Upvotes: 3