Randy
Randy

Reputation: 507

Can't Multiply Sequence By Non-Int of Type Float? Python

Module OneDMaps:

def LogisticMap(a,nIts,x):
    for n in xrange(0,nIts):
        return 4.*a*x*(1.-x)

Actual Program:

# Import plotting routines
from pylab import *
import OneDMaps 

def OneDMap(a,N,x,f):
    return x.append(f(a,N,x))

# Simulation parameters
# Control parameter of the map: A period-3 cycle
a = 0.98
# Set up an array of iterates and set the initital condition
x = [0.1]
# The number of iterations to generate
N = 100

#function name in OneDMaps module
func = LogisticMap

# Setup the plot
xlabel('Time step n') # set x-axis label
ylabel('x(n)') # set y-axis label
title(str(func) + ' at r= ' + str(a)) # set plot title

# Plot the time series: once with circles, once with lines
plot(OneDMap(a,N,x,func), 'ro', OneDMap(a,N,x,func) , 'b')  

The program is supposed to call a function from a module OneDMaps.py and then plot it against it's iterates. I get the error "Can't multiply sequence by non-int of type float" and i've tried using LogisticMap(float(a)...) but that didn't work. Also I want the function name to show up in the title of the plot but i get "at r=0.98 instead of it saying LogisticMap at r= 0.98.

Upvotes: 1

Views: 5417

Answers (1)

mgilson
mgilson

Reputation: 310089

You set up a list like this:

x = [0.1]

You then multipy it by a float:

return 4.*a*x*(1.-x)

You can't do that. Perhaps you wanted x to be an array instead of a list?

x = array([0.1])

(That would do the multiplication elementwise)


Note that adding lists concatenates:

[0] + [1] == [0, 1]

Multiplication by an integer is the same as concatenating that many times:

[0] * 3 == [0, 0, 0]

But that makes no sense for floats,

[0] * 3.0 #raises TypeError as you've seen

(what should you get if you multiply by 3.5 for example?)

Upvotes: 4

Related Questions