Length-1 arrays can be converted to python scalars error? python
- by Randy
from numpy import *
from pylab import *
from math import *
def LogisticMap(a,x):
return 4.*a*x*(1.-x)
def CosineMap(a,x):
return a*cos(x/(2.*pi))
def TentMap(a,x):
if x>= 0 or x<0.5:
return 2.*a*x
if x>=0.5 or x<=1.:
return 2.*a*(1.-x)
a = 0.98
N = 40
xaxis = arange(0.0,N,1.0)
Func = CosineMap
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 program is supposed to take any of the three defined functions and plot it. They all take in a value x from the array xaxis from 0 to N and then return the value. I want it to plot a graph of xaxis vs f(xaxis) with f being any of the three above functions. The logisticmap function works fine, but for CosineMap i get the error "only length-1 arrays can be converted to python scalars" and for TentMap i get error "The truth value of an array with more than one element is ambiguous, use a.any() or a.all()". My tent map function is suppose to return 2*a*x if 0<=x<0.5 and it's suppose to return 2*a*(1-x) if 0.5<=0<=1.