package math.diffeq;

import stdlib.StdDraw;

/**
 * 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.
 * @author Daniel Weschke
 */
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) {
    	StdDraw sd = new StdDraw();
        int n = 800;
        sd.setXscale(0.7, 1.0);
        sd.setYscale(0.0, 1.0);

        int i;
        double r;
        for(r=0.7; r<=1.0; r+=0.3/n) {	
        	double y = Math.random(); // choose random initial value
        	for(i=0; i<1000; i++)     // ignore first 1000 iterates
        		y = logistic(y, r);
        	for(i=0; i<100; i++){     // plot next 1000 iterates
        		y = logistic(y, r);
        		sd.point(r, y);
        	}
        	sd.show(10);
        }

    }
   
}