2
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.

1 Answer 1

4

You first import numpy.cos, and then import math.cos. The latter shadows the former, and doesn't know how to handle NumPy arrays. Hence the error.

To fix, try:

import numpy

def CosineMap(a,x):
    return a*numpy.cos(x/(2.*pi))

Problems of this sort are a good reason to avoid from X import *-style imports.

As to TentMap, here is one way to vectorize it correctly:

def TentMap(a,x):
    return 2.*a*numpy.minimum(x, 1.-x)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.