%% Visualize and Estimate Correlation Dimension of Data
% In this example, consider a Lorenz Attractor describing a unique set of chaotic solutions.
% Load the data set and visualize the Lorenz Attractor in 3D.

load('lorenzAttractorExampleData.mat','data');
plot3(data(:,1),data(:,2),data(:,3));

%
% For this example, use only x-direction data of the Lorenz Attractor. Since lag is unknown, estimate the delay using phaseSpaceReconstruction. Set 'Dimension' to 3 since the Lorenz Attractor is a three-dimensional system. The dim and lag parameters are required to create the correlation integral versus the neighborhood radius plot.

xdata = data(:,1);
dim = 3;
[~,lag] = phaseSpaceReconstruction(xdata,[],dim)

%
% Create the correlation integral versus neighborhood radius plot for the Lorenz Attractor, using the lag value obtained in the previous step. Set an appropriate value for 'NumPoints' to determine a good resolution for the neighborhood radius.

Np = 100;
correlationDimension(xdata,lag,dim,'NumPoints',Np);

% The first dashed, vertical green line (on the left) indicates the value of MinRadius, while the second vertical green line (on the right), represents MaxRadius. The dashed red line indicates the linear fit line for the correlation integral versus neighborhood radius data, within the computed range of radius.
%
% To compute correlation dimension, you first need to determine the MinRadius and MaxRadius values needed for accurate estimation.
% In the plot, drag the two dashed, vertical green lines to 'best fit' the linear fit line to the original data line to obtain the range of radius.
%
% Note the new values of MinRadius and MaxRadius after dragging the two vertical lines for an appropriate fit.
%
% Find the correlation dimension of the Lorenz Attractor, using the new MinRadius and MaxRadius values obtained in the previous step.

MinR = 0.05656;
MaxR = 2.516;
corDim = correlationDimension(xdata,[],dim,'MinRadius',MinR,'MaxRadius',MaxR,'NumPoints',Np)

% The value of correlation dimension is directly proportional to the level of chaos in the system, that is, a higher value of corDim represents a high level of chaotic complexity in the system.
%
% Copyright 2018 The MathWorks, Inc.
%
%