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