LogisticMap.java


Below is the syntax highlighted version of LogisticMap.java from §9.4 Numerical Integration.


/******************************************************************************
 *  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 standard draw
    public static void main(String[] args) {
        int N = 800;
        StdDraw.setXscale(0.7, 1.0);
        StdDraw.setYscale(0.0, 1.0);
        StdDraw.enableDoubleBuffering();

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

            // 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.point(r, y);
            }
            StdDraw.show();
            StdDraw.pause(10);
        }
    }
}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.