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