0

问题: Calculate the mean and standard deviation of a tightly clustered set of 1000 initial conditions as a function of iteration number. The bunch of initial conditions should be Gaussian distributed about x = 0.3 with a standard deviation of 10-3

我写的代码:

from numpy import *

def IterateMap(x,r,n):
    for i in xrange(n):
        x = r * x * (1.0 - x)
    return x

output = "data"
nIterations = 1000
r = 4.0
x0 = 0.3
delta = 0.00005

L = []

for i in xrange(nIterations):
    x = x0
    x = IterateMap(x,r,1)
    L[i] = x
    x0 = x0 + delta

A = array(L)

print 'mean: ', mean(A)

所以我的代码应该做的是获取 x (x0) 的初始值并调用 IterateMap 函数并返回 x 的新值并将其放入 list(L) 然后 x0 更改为新值,这过程持续 1000 次。我收到错误“列表分配索引超出范围”。另外,您认为我正确地解决了这个问题吗?

4

2 回答 2

2

当您处理超出当前大小的索引时,Python 列表不会自动增长。您创建了一个空列表,因此您无法处理其中的任何索引。

用于.append()将新值添加到列表末尾:

L.append(x)

或者,您必须创建一个列表来保存您想要生成的所有索引,方法是使用 None 或其他默认值预先填充它:

L = [None for _ in xrange(nIterations)]
于 2012-12-04T07:25:47.180 回答
0

问题在这里:

L = [] # the list is empty!

for i in xrange(nIterations):
    ...
    L[i] = x

要修复,请替换L[i] = xL.append(x).

于 2012-12-04T07:28:04.760 回答