# # program to plot a chaotic function, connecting dots with lines # import turtle # allows us to use turtles # # these control the plotting and start of the function # startx = 0.26 # initial value of x for the chaotic function scale = 200 # need to scale to make the plot visible # # the particular chaotic function (see lecture 2) def chaos(x): return 3.9 * x * (1 - x) # # set up the drawing # # window stuff first wn = turtle.Screen() # draw the window for the picture wn.bgcolor("red") # make the background color red wn.title("Graph of a Chaotic Function") # put up a title # turtle (or pen) stuff next kelly = turtle.Turtle() # create a turtle, called "kelly" kelly.shape("classic") # just an arrow kelly.color("white") # white contrasts nicely with red # # draw the axes # # first, the x axis, from 0 to 1 (scaled) kelly.penup() kelly.setpos(0, 0) kelly.pendown() kelly.goto(1*scale, 0) # now the y axis, from 0 to 1 (scaled) kelly.penup() kelly.setpos(0, 0) kelly.pendown() kelly.goto(0, 1*scale) kelly.penup() # # mark the axes # # first, make a mark on the y-axis where y = 1 kelly.goto(-0.01*scale, 1*scale) kelly.pendown() kelly.goto(0.01*scale, 1*scale) kelly.penup() # now add 0 and 1 kelly.goto(-0.01*scale, 0.98*scale) kelly.write("1", False, "right") kelly.goto(-0.01*scale, -0.02*scale) kelly.write("0", False, "right") # and label the axis kelly.setpos(-0.01*scale, 0.48*scale) kelly.write("f(x)", False, "right", ("Arial", 10, "normal")) kelly.setpos(0.5*scale, -0.1*scale) kelly.write("time", False, "center", ("Arial", 10, "normal")) # now write something below the y-axis kelly.goto(0.5*scale, -0.2*scale) kelly.write("Graph of f(x) = 3.9 * x * (1-x) from x = 0.26", False, "center", ("Arial", 12, "normal")) # # move to the first point, which has x = 0 # x = startx kelly.setpos(0, chaos(x)) # start the line there kelly.pendown() for i in range(1, 200): x = chaos(x) # move to the position; this draws the line (pen is down) kelly.goto(i, x * scale) # # now hide the turtle and just sit there, displaying the result # kelly.hideturtle() # make the turtle invisible wn.mainloop() # wait until the user closes the window