
// LogisticMap.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

// https://introcs.cs.princeton.edu/java/94diffeq/LogisticMap.java.html

/* 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.

   Usage:
     > compile LogisticMap.java
     > run LogisticMap
 */


public class LogisticMap
{

  public static void main(String[] args)
  {
    int N = 800;
    StdDraw.setXscale(0.7, 1.0);
    StdDraw.setYscale(0.0, 1.0);

    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);
      }
    }
    System.out.println("Drawing finished");
  } /* main */


  private static double logistic(double y, double r)
  {  return 4.0 * r * y * (1.0 - y);  }

} // end of LogisticMap class

