LogisticMap.java


Below is the syntax highlighted version of LogisticMap.java from §9.4 Numerical Solutions to Differential Equations.

/*************************************************************************
 *  Compilation:  javac LogisticMap.java
 *  Execution:    java LogisticMap
 *
 *  Plot the bifurcation diagram of the logistic map. The logistic
 *  is: x[n+1] = 4 r x[n](1 - x[n]). For each value of r, discard
 *  the first 1000 iterates x[n], then plot the next 100.
 *
 *************************************************************************/

public class LogisticMap {

    static double logistic(double y, double r) {
        return 4.0 * r * y * (1.0 - y);
    }


    // plot the logistic map using turtle graphics  
    public static void main(String args[]) {
        int SIZE = 800;
        StdDraw.create(SIZE, SIZE);
        StdDraw.setScale(0.7, 0, 1.0, 1);

      for (double r = 0.7; r <= 1.0; r += 0.3/SIZE) {

         // choose random initial value
         double y = Math.random();

         // ignore first 1000 iterates
         for (int i = 0; i < 1000; i++)
            y = logistic(y, r);

         // plot next 1000 iterates
         for (int i = 0; i < 100; i++) {
            y = logistic(y, r);
            StdDraw.moveTo(r, y);
            StdDraw.spot();
         }
         StdDraw.pause(10);
      }

      StdDraw.show();
   }
   
}



Last updated: Thu Aug 25 07:03:01 EDT 2005 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.