jpayne@68: # File: tdemo_chaos.py jpayne@68: # Author: Gregor Lingl jpayne@68: # Date: 2009-06-24 jpayne@68: jpayne@68: # A demonstration of chaos jpayne@68: jpayne@68: from turtle import * jpayne@68: jpayne@68: N = 80 jpayne@68: jpayne@68: def f(x): jpayne@68: return 3.9*x*(1-x) jpayne@68: jpayne@68: def g(x): jpayne@68: return 3.9*(x-x**2) jpayne@68: jpayne@68: def h(x): jpayne@68: return 3.9*x-3.9*x*x jpayne@68: jpayne@68: def jumpto(x, y): jpayne@68: penup(); goto(x,y) jpayne@68: jpayne@68: def line(x1, y1, x2, y2): jpayne@68: jumpto(x1, y1) jpayne@68: pendown() jpayne@68: goto(x2, y2) jpayne@68: jpayne@68: def coosys(): jpayne@68: line(-1, 0, N+1, 0) jpayne@68: line(0, -0.1, 0, 1.1) jpayne@68: jpayne@68: def plot(fun, start, color): jpayne@68: pencolor(color) jpayne@68: x = start jpayne@68: jumpto(0, x) jpayne@68: pendown() jpayne@68: dot(5) jpayne@68: for i in range(N): jpayne@68: x=fun(x) jpayne@68: goto(i+1,x) jpayne@68: dot(5) jpayne@68: jpayne@68: def main(): jpayne@68: reset() jpayne@68: setworldcoordinates(-1.0,-0.1, N+1, 1.1) jpayne@68: speed(0) jpayne@68: hideturtle() jpayne@68: coosys() jpayne@68: plot(f, 0.35, "blue") jpayne@68: plot(g, 0.35, "green") jpayne@68: plot(h, 0.35, "red") jpayne@68: # Now zoom in: jpayne@68: for s in range(100): jpayne@68: setworldcoordinates(0.5*s,-0.1, N+1, 1.1) jpayne@68: return "Done!" jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: main() jpayne@68: mainloop()