% an example based on 
% R. C. Hilborn, "Chaos and Nonlinear Dynamics - an introduction for
% scientists and engineers", second edition, Oxford University Press, 2000.
% page 27-37.

% ========================================================================
%
% The matlab code for Chaos Theory - 
%                 Nonlinear Statistical Modeling of Speech
% date : July 15, 2005 Friday.
% IES: http://www.cavs.msstate.edu/hse/ies/projects/nsf_nonlinear
%
% ========================================================================

% Numerical integration of the Lorenz system
% Demonstration for course b8: Nonlinear Systems
% M.Little, 2005
% reference for more details
% http://www.maths.ox.ac.uk/current-students/undergraduates/
% lecture-material/B/nonlinear-systems/
clear all;
clc;
close all;

% Model parameters
%
sigma  = 10;
r      = 28;
b      = 8/3;

% Initial conditions
%
x0     = -9;
y0     = -11;
z0     = 24;

% Length of integration
%
t_end  = 40;

% Integrate the system using fourth-order Runge-Kutta
%
[t, v] = ODE45('lorenzode', t_end, [x0,y0,z0], [], sigma, r, b);

% Split out the trajectory matrix into x,y,z vector variables
%
x = v(:, 1);
y = v(:, 2);
z = v(:, 3);

% Display time series plots
%
figure;
subplot(3,1,1);
plot(t, x, 'b-');
title('Lorenz time series');
xlabel('t');
ylabel(' x(t)');

subplot(3,1,2);
plot(t, y, 'b-');
xlabel('t');
ylabel(' y(t)');

subplot(3,1,3);
plot(t, z, 'b-');
xlabel('t');
ylabel(' z(t)');

% Plot trajectory in 3D state-space
%
figure;
plot3(x, y, z, 'b-');
title('Lorenz state space');
xlabel('x(t)');
ylabel('y(t)');
zlabel('z(t)');
grid on;

% specifies the view angle view, [azimuth, elevation]
%
view([37,26]);

% all the plots are combined together 
% Display time series plots
figure;
subplot(221); plot(t, x);
title('Lorenz time series X vs t');
xlabel('t');
ylabel(' x(t)');

subplot(222); plot(t, y);
title('Lorenz time series Y vs t');
xlabel('t');
ylabel(' y(t)');

subplot(223); plot(t, z);
title('Lorenz time series Z vs t');
xlabel('t');
ylabel(' z(t)');

% Plot trajectory in 3D state-space
%
subplot(224); plot3(x, y, z);
title('Lorenz state space');
xlabel('x(t)');
ylabel('y(t)');
zlabel('z(t)');
grid on;
view([37,26]);