1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include "math.h"
#include "windows.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib> // for NULL
#include <deque>
#include "convert.h"
using namespace std;
double* LogisticMap(double r,double x0, unsigned long n);
void corr_dim(double* data, int buffersize, int exclusionsize);
unsigned long n;
unsigned int skip, buffersize, exclusionsize;
double total[61] = {0};
int main()
{
double r, x0;
double* logisticdata;
SYSTEMTIME st, et; //variables holding start and end time of calculation
ifstream input_stream;
ofstream output_stream;
string ifilename( "inp.txt");
string ofilename( "out.txt");
cout<<"Enter parameter r: ";
cin>>r;
cout<<"Enter starting value x0: ";
cin>>x0;
cout<<"\nEnter number of datapoints to generate (eg. \"1000000\"): ";
cin>>n;
cout<<"Enter number of datapoints to skip initially: ";
cin>>skip; //to be sure that we are on the attractor
cout<<"Enter size of circular buffer: ";
cin>>buffersize;
cout<<"Enter number of points to exclude: ";
cin>>exclusionsize; // excluding the most recent exclusionsize points
cout<<"\n\n";
ofilename = "out r = " + stringify(r) + ", n = " + stringify(n)+ ".txt";
cout<<"Results will be saved in file " + ofilename + ".\n\n";
cout<<"Iterating logistic map... ";
logisticdata = LogisticMap(r, x0, n);
cout<<"done.\n";
cout<<"Starting to compute and bin distances...\n\n";
GetLocalTime(&st); //get time before start of binning
cout<<st.wHour<<":"<<st.wMinute<<":"<<st.wSecond<<"\n";
/* call the routine that computes and bins distances skipping the specified
number of data points to be sure to be on the attractor */
corr_dim( &logisticdata[skip-1], buffersize, exclusionsize);
GetLocalTime(&et); //get end time
cout<<"\nCalculation complete: "<<et.wHour<<":"<<et.wMinute<<":"<<et.wSecond<<"\n";
output_stream.open(ofilename.c_str());
output_stream<<"Estimate the correlation dimension of the logistic map.\n";
output_stream<<"Written by Leander Hohmann, May 2009.\n\n";
output_stream <<"Calculation started at: "<<st.wHour<<":"<<st.wMinute<<":"<<st.wSecond<<"\n";
output_stream <<"Calculation completed at: "<<et.wHour<<":"<<et.wMinute<<":"<<et.wSecond<<"\n";
output_stream <<"Parameters were:\n";
output_stream <<"r = "<<r<<"\n";
output_stream <<"x0 = "<<x0<<"\n";
output_stream <<"n = "<<n<<"\n";
output_stream <<"skip = "<<skip<<"\n";
output_stream <<"buffer size: "<<buffersize<<"\n";
output_stream <<"exclusion size: "<<exclusionsize<<"\n";
output_stream <<"________________________\n";
output_stream<<"\ntotal result of binning:\n";
for(int i=0; i < 61; i++){
output_stream << total[i] <<" ";
};
output_stream.close();
return 0;
}
//takes param r, start value x0 and n and iterates the logistic map n times
double* LogisticMap(double r,double x0, unsigned long n)
{
double* list = NULL;
list = new double[n];
list[0] = x0;
unsigned long i=0;
for(i = 0; i < n; i++){
*(list + i + 1) = r * *(list+i) * (1 - *(list+i));
};
return(list);
}
/*
takes a set of values and computes and bins the distances between data points
in a circular buffer of size buffersize, ignoring the recent exclusionsize values.
*/
void corr_dim(double* data, int buffersize, int exclusionsize)
{
double current, distance = 0;
int it_buffer = 0, it_bins = 0;
long it_data = 0;
cout<<"No problem up to here.";
deque<double> buffer;
cout<<"But this does not work SOMETIMES.";
const double bins[61] = {0,3.8242466280971355e-1,0.0015034391929775724,
............,
0.0024787521766663585};
//fill the buffer initially
for(it_buffer = 0; it_buffer < buffersize; it_buffer++){
buffer.push_back(data[it_buffer]) ;
};
//iterate over all given points, starting with the first point not in the buffer
for(it_data = buffersize+1; it_data <= n - skip ; it_data++){
current = buffer.back(); //set last value in buffer as current
//compute distance to all values in buffer up to the excluded values
for(it_buffer = 0; it_buffer <= buffersize - exclusionsize; it_buffer++){
distance = pow(buffer[it_buffer] - current, 2);
//increment count of all bins having larger upper boundary
it_bins=60;
while(distance < bins[it_bins]){
total[it_bins]++;
it_bins--;
};
};
buffer.pop_front(); //delete oldest value in the buffer
buffer.push_back(data[it_data]);//append next data point at buffer's end
};
}
|