Home | History | Annotate | Download | only in turtledemo
      1 # File: tdemo_chaos.py
      2 # Author: Gregor Lingl
      3 # Date: 2009-06-24
      4 
      5 # A demonstration of chaos
      6 
      7 from turtle import *
      8 
      9 N = 80
     10 
     11 def f(x):
     12     return 3.9*x*(1-x)
     13 
     14 def g(x):
     15     return 3.9*(x-x**2)
     16 
     17 def h(x):
     18     return 3.9*x-3.9*x*x
     19 
     20 def jumpto(x, y):
     21     penup(); goto(x,y)
     22 
     23 def line(x1, y1, x2, y2):
     24     jumpto(x1, y1)
     25     pendown()
     26     goto(x2, y2)
     27 
     28 def coosys():
     29     line(-1, 0, N+1, 0)
     30     line(0, -0.1, 0, 1.1)
     31 
     32 def plot(fun, start, color):
     33     pencolor(color)
     34     x = start
     35     jumpto(0, x)
     36     pendown()
     37     dot(5)
     38     for i in range(N):
     39         x=fun(x)
     40         goto(i+1,x)
     41         dot(5)
     42 
     43 def main():
     44     reset()
     45     setworldcoordinates(-1.0,-0.1, N+1, 1.1)
     46     speed(0)
     47     hideturtle()
     48     coosys()
     49     plot(f, 0.35, "blue")
     50     plot(g, 0.35, "green")
     51     plot(h, 0.35, "red")
     52     # Now zoom in:
     53     for s in range(100):
     54         setworldcoordinates(0.5*s,-0.1, N+1, 1.1)
     55     return "Done!"
     56 
     57 if __name__ == "__main__":
     58     main()
     59     mainloop()
     60