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