2012-12-09 85 views
1

模塊OneDMaps:不能通過非整型浮點乘法序列? Python的

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

實際的程序:

# 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') 

程序應該調用從模塊OneDMaps.py一個函數,然後繪製它反對它的迭代。我得到的錯誤「不能乘以non-int的float類型的序列」,我嘗試過使用LogisticMap(float(a)...),但是沒有奏效。此外,我希望函數名中的情節的標題出現,但我得到「在R = 0.98,而不是它說,在r LogisticMap = 0.98

+0

那麼,是什麼問題是什麼呢?我很清楚這個錯誤是什麼。 ''不能用類型爲float的非int類型的乘法序列「 - 這意味着你不能執行'4。*'s''如果你只是想乘,從'4. * a中移除'.' * x *(1.-x)' –

回答

4

你建立一個list這樣的:

x = [0.1] 

然後,通過一個float multipy它:

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

你不能這樣做,也許你想x是一個array代替list

x = array([0.1]) 

(那會做乘法的elementwise)


注意添加列表會連接:

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

乘以整數相同串聯,很多時候:

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

但是這對浮動沒有意義S,

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

(你應該怎樣,如果你用3.5例如乘得到什麼?)

+0

+1給你和OP - 我不知道你可以用這種方式定義花車:) – RocketDonkey

+0

@RocketDonkey - 定義花車是什麼方式? – mgilson

+0

使用'4.'並省略'0' - 雖然有意義但很酷。 – RocketDonkey

相關問題