chaos.py 951 B

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