-1

Seen below is the code for my iteration program. I want to be able to use turtle graphics to take each parameter (k) and have the equations output plotted against its corresponding k value. This should create a feigenbaum diagram if im not mistaken? My problem is, how can I get a turtle to plot these points for each k value, then connect them to the points from the neighbouring k values and so on?

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
imglueck
  • 1
  • 2

2 Answers2

0

Here is a feigenbaum diagram generated using tkinter. It is from the "open book project", visualizing chaos.

The program source is here; I converted it to python 3 and posted it hereunder. There is a lot for you to learn reading and understanding this code.

enter image description here

#
#   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()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

how can I get a turtle to plot these points for each k value

Here's a simple, crude, slow example I worked out using 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()

enter image description here

I hope it gives you some ideas about plotting functions using a turtle. (Of course, using matplotlib with numpy usually works out better in the end.)

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • since posting this, I have looked into numpy and matplotlib more and it does seem like it would have a more viable solution to my problem. I just need to figure out how to set up the darn things. Thanks for the above code aswell :D – imglueck Jul 29 '19 at 19:20