用Python绘制分叉图

a9wyjsp7  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(177)

我是一个初学者,我的英语说得不是很好,所以很抱歉。我想画一个序列的分叉图:x(n+1)=ux(n)(1-x(n)),其中x(0)=0.7,u在0.7和4之间。
我应该得到这样的东西:
x1c 0d1x的数据
所以,对于u的每个值,我想计算这个序列的累加点。这就是为什么我想编写一些代码,可以显示每个u值的每个点(u;x1001),(u;x1002).(u;x1050)。
我这样做了:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. P=np.linspace(0.7,4,10000)
  4. m=0.7
  5. Y=[m]
  6. l=np.linspace(1000,1050,51)
  7. for u in P:
  8. X=[u]
  9. for n in range(1001):
  10. m=(u*m)*(1-m)
  11. break
  12. for l in range(1051):
  13. m=(u*m)*(1-m)
  14. Y.append(m)
  15. plt.plot(X,Y)
  16. plt.show()

字符串
我得到一个空白的图形。
这是我尝试编写的第一个代码,我对Python一无所知,所以我需要帮助。

0yg35tkg

0yg35tkg1#

你的代码中有一些问题,虽然你遇到的问题是代码审查问题,但生成分叉图是一个普遍感兴趣的问题(它可能需要在scicomp上重新定位,但我不知道如何正式请求)。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. P=np.linspace(0.7,4,10000)
  4. m=0.7
  5. # Initialize your data containers identically
  6. X = []
  7. Y = []
  8. # l is never used, I removed it.
  9. for u in P:
  10. # Add one value to X instead of resetting it.
  11. X.append(u)
  12. # Start with a random value of m instead of remaining stuck
  13. # on a particular branch of the diagram
  14. m = np.random.random()
  15. for n in range(1001):
  16. m=(u*m)*(1-m)
  17. # The break is harmful here as it prevents completion of
  18. # the loop and collection of data in Y
  19. for l in range(1051):
  20. m=(u*m)*(1-m)
  21. # Collection of data in Y must be done once per value of u
  22. Y.append(m)
  23. # Remove the line between successive data points, this renders
  24. # the plot illegible. Use a small marker instead.
  25. plt.plot(X, Y, ls='', marker=',')
  26. plt.show()

字符串
此外,X在这里是无用的,因为它包含P的副本。

展开查看全部
mfpqipee

mfpqipee2#

以png格式保存分叉图,可以尝试this简单代码。

  1. # Bifurcation diagram of the logistic map
  2. from PIL import Image
  3. imgx = 1000
  4. imgy = 500
  5. image = Image.new("RGB", (imgx, imgy))
  6. xa = 2.9
  7. xb = 4.0
  8. maxit = 1000
  9. for i in range(imgx):
  10. r = xa + (xb - xa) * float(i) / (imgx - 1)
  11. x = 0.5
  12. for j in range(maxit):
  13. x = r * x * (1 - x)
  14. if j > maxit / 2:
  15. image.putpixel((i, int(x * imgy)), (255, 255, 255))
  16. image.save("Bifurcation.png", "PNG")

字符串

展开查看全部

相关问题