jjx970523 发表于 2020-9-1 17:01:28

...求助一个简单的迭代算法

x_n+1 = r * x_n * (1 - x_n)



If I specify x_0 and r to be something (both between but not including 0 and 1), then, for example, if x_0 = 0.1, r = 0.5, then



x_1 = 0.5 * 0.1 * (1 - 0.1) = 0.09 * 0.5 = 0.045
x_2 = 0.045 * 0.1 * (1 - 0.045) = something



and so on. What I want you to do is to write a solver in Microsoft Excel that generates some iterations (say up to about x_100 or x_200), plot the resulting evolution of the iterations





我的代码是:
import numpy as np
def func(n):
    n = 0
    for n in np.arange(0,101):
      if n == 0:
            return 0.045
            n += 1
      else:
            return 0.5 * func(n-1) * (1 - func(n-1))
print(func(n))


为什么一直说我n没有定义呢????

baige 发表于 2020-9-1 17:19:02

print(func(n))
这一句n得是一个实际的值或有给n赋值

永恒的蓝色梦想 发表于 2020-9-1 21:14:12

你告诉我哪里定义了?

永恒的蓝色梦想 发表于 2020-9-1 21:20:14

x0 = 0.1
r = 0.5


def iteration(n: int) -> float:
    result = x0

    while n:
      result = r * result * (1 - result)
      n -= 1

    return result


print(iteration(100))
页: [1]
查看完整版本: ...求助一个简单的迭代算法