/*
   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
      
   1) Implement outer loop where r is varied rmin=0 to rmax=4,
      Output the sequence for each r in the same file, plot it
      
   2) Change code so the first 500 iterations are not shown, and only show the next 500 iterations
    
   3) Trying outputting rmin=3.56 to rmax=4
      
   matlab:
   
   data=importdata("logistic.dat");
   plot(data(:,1),data(:,2),'.');
     
*/

#include <fstream>   
#include <cassert>
using namespace std;

int main()
{
   ofstream fo("logistic.dat");      // Attempt to open file.
   assert( fo.is_open() );           // Halts if file couldn't be opened
   
   int N=500;
   double r=3.2;
   double x=0.5;
   
   for (int i=0;i<N;i++)
      {
         fo << i << " " << x << endl;
         x=r*x*(1-x);
      }
   
   fo.close();                       // Close file
}
