-1

下面是我的迭代程序的代码。我希望能够使用海龟图形来获取每个参数 (k),并将方程输出绘制在其对应的 k 值上。如果我没记错的话,这应该创建一个 feigenbaum 图?我的问题是,如何让乌龟为每个 k 值绘制这些点,然后将它们连接到相邻 k 值的点等等?

def iteration(xstore):
    global x0
    x0=xstore
    print (x0)

x0=float(input("x0:"))
n=float(input("max parameter value:"))
divison=float(input("divisons between parameters:"))

xv=x0
x1=0
k=0

while k<(n+divison):
    print("K VALUE:"+str(k))
    for i in range (0,20):
        x1=x0+x0*k*(1-x0)
        iteration(x1)

print ("________________________")

x0=xv

k=k+divison
4

2 回答 2

0

我怎样才能让乌龟为每个 k 值绘制这些点

这是我使用 Python turtle 编写的一个简单、粗略、缓慢的示例:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 800, 400

Kmin = 2.5
Kmax = 3.8
x = 0.6

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.setworldcoordinates(Kmin, 0.0, Kmax, 1.0)
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

k = Kmin

while k < Kmax:
    for _ in range(HEIGHT//4):
        x *= (1.0 - x) * k

        turtle.goto(k, x)
        turtle.dot(2)

        x *= 1 + 1/(HEIGHT//4)

    k *= 1 + 1/WIDTH

screen.tracer(True)
screen.exitonclick()

在此处输入图像描述

我希望它能给你一些关于使用海龟绘制函数的想法。(当然,将 matplotlib 与 numpy 一起使用通常最终效果会更好。)

于 2019-07-29T03:32:17.833 回答
0

这是使用生成的 feigenbaum 图tkinter。它来自“开书计划”,可视化混乱

程序源在这里;我将其转换为 python 3 并将其发布在下面。阅读和理解这段代码需要你学习很多东西。

在此处输入图像描述

#
#   chaos-3.py
#
#  Build Feigenbaum Logistic map. Input start and end K
#
#  python chaos-3.py 3.4 3.9  
#

canWidth=500
canHeight=500

def setupWindow () :
    global win, canvas
    from tkinter import Tk, Canvas, Frame
    win = Tk()
    canvas = Canvas(win, height=canHeight, width=canWidth)
    f = Frame (win)    
    canvas.pack()
    f.pack()

def startApp () :
    global win, canvas
    import sys
#     k1  = float(sys.argv[1])   # starting value of K
#     k2  = float(sys.argv[2])   # ending   value of K
    x = .2                     # is somewhat arbitrary
    vrng = range(200)          # We'll do 200 horz steps
    for t in range(canWidth) :
        win.update()
        k = k1 + (k2-k1)*t/canWidth
#         print("K = %.04f" % k)
        for i in vrng :
            p = x*canHeight
            canvas.create_line(t,p,t,p+1)  # just makes a pixel dot
            x = x * (1-x) * k              # next x value
            if x <=0 or x >= 1.0 :
#                 print("overflow at k", k)
                return

def main () :
    setupWindow()       # Create Canvas with Frame
    startApp()          # Start up the display  
    win.mainloop()      # Just wait for user to close graph


k1 = 2.9
k2 = 3.8
main()
于 2019-07-29T02:25:45.857 回答