% Modified from (Lynch, 2004):
% Chapter 13 - Three-Dimensional Autonomous Systems and Chaos.
% Programs_13a - 3-D phase portrait (Lorenz).
% Copyright Birkhauser 2004. Stephen Lynch.

% Define Lorenz system as an inline function:
Lorenz=inline('[10*(x(2)-x(1));28*x(1)-x(2)-x(1)*x(3);x(1)*x(2)-(8/3)*x(3)]','t','x');
options = odeset('RelTol',1e-4,'AbsTol',1e-4);      % set solver tolerances
[t,xa]=ode45(Lorenz,[0 10],[15,20,30],options);     % Integrate differential equations

fsize=15;                      % set font size for graph
% ----- render x vs y vs z graph  -----------
figure(1)                       % set figure window
plot3(xa(:,1),xa(:,2),xa(:,3))  % plot 3-dimensional graph
title('The Lorenz Attractor')
xlabel('x(t)','Fontsize',fsize);
ylabel('y(t)','Fontsize',fsize);
zlabel('z(t)','FontSize',fsize);

% ----- render x, y, z vs t graph  -----------
figure(2)                   % set figure window
subplot(2,3,1:3)            % draw in top row of 2x3 matrix of graphs
hold on                     % retain the current plot to add more than 1 function
plot(t,xa(:,1),'r')         % plot x vs t, color red
plot(t,xa(:,2),'b')         % plot y vs t, color blue
plot(t,xa(:,3),'g')         % plot z vs t, color green
hold off                    % release hold on current plot
title('The Lorenz Attractor')
legend('x(t)','y(t)', 'z(t)')
xlabel('time','FontSize',fsize)
ylabel('x, y, z','FontSize',fsize)

% ----- render 3 phase-plane graphs (x vs y, x vs z, y vs z)  -----------
subplot(2,3,4)                  % draw in bottom-left of 2x3 matrix of graphs
plot(xa(:,1),xa(:,2))           % plot x vs y
xlabel('x','FontSize',fsize)
ylabel('y','FontSize',fsize)
subplot(2,3,5)                  % draw in bottom-center of 2x3 matrix of graphs
plot(xa(:,1),xa(:,3))           % plot x vs z
xlabel('x','FontSize',fsize)
ylabel('z','FontSize',fsize)
subplot(2,3,6)                  % draw in bottom-right of 2x3 matrix of graphs
plot(xa(:,2),xa(:,3))           % plot y vs z
xlabel('y','FontSize',fsize)
ylabel('z','FontSize',fsize)

% End of Program.

