2024-10-23 17:29:32 -03:00
|
|
|
"""turtledemo/chaos.py
|
2008-06-10 01:44:07 -03:00
|
|
|
|
2024-10-23 17:29:32 -03:00
|
|
|
A demonstration of chaos.
|
|
|
|
"""
|
2008-09-21 04:32:10 -03:00
|
|
|
from turtle import *
|
2008-06-10 01:44:07 -03:00
|
|
|
|
2009-06-25 14:32:06 -03:00
|
|
|
N = 80
|
|
|
|
|
2008-06-10 01:44:07 -03:00
|
|
|
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
|
|
|
|
|
2009-06-25 14:32:06 -03:00
|
|
|
def jumpto(x, y):
|
|
|
|
penup(); goto(x,y)
|
|
|
|
|
|
|
|
def line(x1, y1, x2, y2):
|
|
|
|
jumpto(x1, y1)
|
2008-06-10 01:44:07 -03:00
|
|
|
pendown()
|
2009-06-25 14:32:06 -03:00
|
|
|
goto(x2, y2)
|
2008-06-10 01:44:07 -03:00
|
|
|
|
2009-06-25 14:32:06 -03:00
|
|
|
def coosys():
|
|
|
|
line(-1, 0, N+1, 0)
|
|
|
|
line(0, -0.1, 0, 1.1)
|
|
|
|
|
2014-10-09 19:44:32 -03:00
|
|
|
def plot(fun, start, color):
|
|
|
|
pencolor(color)
|
2008-06-10 01:44:07 -03:00
|
|
|
x = start
|
2009-06-25 14:32:06 -03:00
|
|
|
jumpto(0, x)
|
2008-06-10 01:44:07 -03:00
|
|
|
pendown()
|
|
|
|
dot(5)
|
2009-06-25 14:32:06 -03:00
|
|
|
for i in range(N):
|
2008-06-10 01:44:07 -03:00
|
|
|
x=fun(x)
|
|
|
|
goto(i+1,x)
|
|
|
|
dot(5)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
reset()
|
2009-06-25 14:32:06 -03:00
|
|
|
setworldcoordinates(-1.0,-0.1, N+1, 1.1)
|
2008-06-10 01:44:07 -03:00
|
|
|
speed(0)
|
|
|
|
hideturtle()
|
|
|
|
coosys()
|
|
|
|
plot(f, 0.35, "blue")
|
|
|
|
plot(g, 0.35, "green")
|
|
|
|
plot(h, 0.35, "red")
|
2009-06-25 14:32:06 -03:00
|
|
|
# Now zoom in:
|
2008-06-10 01:44:07 -03:00
|
|
|
for s in range(100):
|
2009-06-25 14:32:06 -03:00
|
|
|
setworldcoordinates(0.5*s,-0.1, N+1, 1.1)
|
2008-06-10 01:44:07 -03:00
|
|
|
return "Done!"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
mainloop()
|