用Python绘制分叉图

a9wyjsp7  于 5个月前  发布在  Python
关注(0)|答案(2)|浏览(77)

我是一个初学者,我的英语说得不是很好,所以很抱歉。我想画一个序列的分叉图: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)。
我这样做了:

import matplotlib.pyplot as plt
import numpy as np
P=np.linspace(0.7,4,10000)
m=0.7
Y=[m]
l=np.linspace(1000,1050,51)
for u in P:
    X=[u]
    for n in range(1001):
      m=(u*m)*(1-m)
    break 
    for l in range(1051):
      m=(u*m)*(1-m)
      Y.append(m)
plt.plot(X,Y)
plt.show()

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

0yg35tkg

0yg35tkg1#

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

import matplotlib.pyplot as plt
import numpy as np
P=np.linspace(0.7,4,10000)
m=0.7
# Initialize your data containers identically
X = []
Y = []
# l is never used, I removed it.
for u in P:
    # Add one value to X instead of resetting it.
    X.append(u)
    # Start with a random value of m instead of remaining stuck
    # on a particular branch of the diagram
    m = np.random.random()
    for n in range(1001):
      m=(u*m)*(1-m)
    # The break is harmful here as it prevents completion of
    # the loop and collection of data in Y 
    for l in range(1051):
      m=(u*m)*(1-m)
    # Collection of data in Y must be done once per value of u
    Y.append(m)
# Remove the line between successive data points, this renders
# the plot illegible. Use a small marker instead.
plt.plot(X, Y, ls='', marker=',')
plt.show()

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

mfpqipee

mfpqipee2#

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

# Bifurcation diagram of the logistic map

from PIL import Image
imgx = 1000
imgy = 500
image = Image.new("RGB", (imgx, imgy))

xa = 2.9
xb = 4.0
maxit = 1000

for i in range(imgx):
    r = xa + (xb - xa) * float(i) / (imgx - 1)
    x = 0.5
    for j in range(maxit):
        x = r * x * (1 - x)
        if j > maxit / 2:
            image.putpixel((i, int(x * imgy)), (255, 255, 255))

image.save("Bifurcation.png", "PNG")

字符串

相关问题