/*
   Writing to a file.
   
   Here we use the logistic map as an example.  x[n+1]=r x[n]*(1-x[n])
   where x[0]=0.5 and r is a number between 0 and 4. Chaos starts beyond r=3.56995
   For r>=4 the sequence diverges. 

   https://en.wikipedia.org/wiki/Logistic_map
      
   matlab:
   
   data=importdata("logistic.dat");
   plot(data(:,1),data(:,2),'.');
     
*/

#include <fstream>   
using namespace std;

int main()
{
   ofstream fo("logistic.dat");
   
   double rmin=3.56996;
   double rmax=4;
   
   int Neq=500;
   int N=500;
   for (double r=rmin ; r<rmax ; r+=(rmax-rmin)/5000 )
     {
        double x=0.5;
   
        // discard the first Neq of the data
        for (int i=0;i<Neq;i++)
              x=r*x*(1-x);

        // and save N of the rest.
        for (int i=0;i<N;i++)
           {
              fo << r << " " << x << endl;
              x=r*x*(1-x);
           }
     }

   fo.close();
}
