chaos.py 953 Bytes
Newer Older
1 2 3
# File: tdemo_chaos.py
# Author: Gregor Lingl
# Date: 2009-06-24
4

5
# A demonstration of chaos
6

7
from turtle import *
8

9 10
N = 80

11 12 13 14 15 16 17 18 19
def f(x):
    return 3.9*x*(1-x)

def g(x):
    return 3.9*(x-x**2)

def h(x):
    return 3.9*x-3.9*x*x

20 21 22 23 24
def jumpto(x, y):
    penup(); goto(x,y)

def line(x1, y1, x2, y2):
    jumpto(x1, y1)
25
    pendown()
26
    goto(x2, y2)
27

28 29 30 31 32 33
def coosys():
    line(-1, 0, N+1, 0)
    line(0, -0.1, 0, 1.1)

def plot(fun, start, colour):
    pencolor(colour)
34
    x = start
35
    jumpto(0, x)
36 37
    pendown()
    dot(5)
38
    for i in range(N):
39 40 41 42 43 44
        x=fun(x)
        goto(i+1,x)
        dot(5)

def main():
    reset()
45
    setworldcoordinates(-1.0,-0.1, N+1, 1.1)
46 47 48 49 50 51
    speed(0)
    hideturtle()
    coosys()
    plot(f, 0.35, "blue")
    plot(g, 0.35, "green")
    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 56 57 58 59
    return "Done!"

if __name__ == "__main__":
    main()
    mainloop()