%% Estimate Lyapunov Exponent in the Live Editor
% Use the Estimate Lyapunov Exponent task in the Live Editor to interactively estimate the Lyapunov exponent of a uniformly sampled signal. Experiment with different values for lag, embedding dimension, expansion range and mean period to align the linear fit line with the original data plot. The task automatically generates code reflecting your selections. Open this example to see a preconfigured script containing the Estimate Lyapunov Exponent task.
% For this example, consider 'lyapExpData.mat' which contains reconstructed phase space signal phaseSpace sampled at 100 Hz.

load('lyapExpData.mat','phaseSpace')

%
% To estimate the Lyapunov exponent of the signal phaseSpace, open the Estimate Lyapunov Exponent in the Live Editor. On the Live Editor tab, select Task > Estimate Lyapunov Exponent. In the task, select signal phaseSpace.
%
% Since the selected signal is a phase space signal, select Phase space from the Signal Type dropdown menu. The signal was sampled at 100 Hz, hence specify this value in the Sampling Rate field.1
%
% The Estimate Lyapunov Exponent task automatically computes the embedding dimension and lag from the phase space data and creates the Lyapunov exponent plot with default values for expansion range and mean period.
%
% If your linear fit line does not align with the original data line using the default expansion range values, try different values in the Expansion Range Min, Expansion Range Max and Mean Period fields until the alignment is satisfactory. For this example, use the minimum value of 3 and maximum value of 7 for the best alignment. The default mean period value of 166 provides good alignment for the signal phaseSpace.
%
% You can toggle displaying the output of the Lyapunov exponent value in the Live Editor output using the Output Display option.
%
% The task generates code in your live script. The generated code reflects the parameters and options you specify. To see the generated code, click  at the bottom of the task parameter area. The task expands to display the generated code.
%
% By default, the generated code uses lyapExp as the name of the output variable. To specify a different output variable name, enter a new name in the summary line at the top of the task. For instance, change the name to lExponent.
%
% The task updates the generated code to reflect the new variable name, and the new variable lExponent appears in the MATLAB workspace. A negative Lyapunov exponent indicates convergence, while positive Lyapunov exponents demonstrate divergence and chaos. The magnitude of lExponent is an indicator of the rate of convergence or divergence of the infinitesimally close trajectories.
% Copyright 2019 The MathWorks, Inc.