% tsubplot Demonstrates the use of the subplot command [PHYS210-demo]
% 
% If you need to make and display many separate plots at one time
% using a separate figure window for each can become unwieldy and 
% clutter up your desktop.
% 
% Matlab provides a nice solution to this problem which, true 
% its name and fundamental purpose, allow us to easily
% "tile" any figure window with a matrix of plots using 
% the subplot function.  
% 
% The simplest form of subplot usage is illustrated here. As usual
% use 'help subplot', 'doc subplot' or consult online documentation
% for more information.

% Plot 6 basic functions on -pi <= x <= to pi in a 3 x 2 matrix
% (grid) of plots.

% Define x-values.

nx = 1001;
x = linspace(-pi, pi, nx);

% The figure(1) call isn't necessary here, but you can 
% subplot any and all figure windows that you open, so I'm 
% including it for completeness.

figure(1);
clf;

% Arguments to subplot are: 
%
%  1. number of rows, nr,  in matrix of plots.
%  2. number of columns, nc,  in matrix of plots.
%  3. position of subplot in matrix: integer from 1 to nr*nc
%     with numbering left to right, top to bottom.

subplot(3, 2, 1);
box;
plot(x, sin(x));
xlim([min(x), max(x)]);
xlabel('x');
ylabel('sin(x)');

subplot(3, 2, 2);
box;
plot(x, cos(x));
xlim([min(x), max(x)]);
xlabel('x');
ylabel('cos(x)');

subplot(3, 2, 3);
box;
plot(x, sin(x).^2);
xlim([min(x), max(x)]);
xlabel('x');

% Note that Matlab understands quite a bit of LaTeX syntax 
% when used in strings for plot annotations.  Here, for 
% example ^2 is converted to a superscript 2, similarly 
% underscore will generate a subscript, and all of the 
% letters in the greek alphabet can be accessed using
% standard LaTeX notation, \alpha, \beta, \gamma, etc.

ylabel('sin(x)^2');

subplot(3, 2, 4);
box;
plot(x, cos(x).^2);
xlim([min(x), max(x)]);
xlabel('x');
ylabel('cos(x)^2');

subplot(3, 2, 5);
box;
plot(x, sin(x).^3);
xlim([min(x), max(x)]);
xlabel('x');
ylabel('sin(x)^3');

subplot(3, 2, 6);
box;
plot(x, cos(x).^3);
xlim([min(x), max(x)]);
xlabel('x');
ylabel('cos(x)^3');
