%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple examples of 2D plotting in Matlab.
%
% Use 'help plot' or 'doc plot' for more information about the plot 
% command.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Included for octave compatibilty.  octave's start up default is 
% to have paging enabled.
more off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example 1:  Basic plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('The following is a plot of\n\n');
fprintf('   sin(x)*sin(3x) for 0 <= x <= 2 pi, and using\n\n');
fprintf('   - 1000 points uniformly distributed in the interval\n');
fprintf('   - default plotting style (line)\n');
fprintf('   - default line color (blue)\n');
fprintf('   - default annotations (no title, axes labels or legend)\n\n');

% 'figure(<n>)' directs plotting output to the <n>-th plotting 
% window, opening that window as necessary.  By default, plots
% will appear in figure window 1, so this command doesn't do 
% anything---the subseqent calls to figure do though.
figure(1);

% 'clf' clears the graphics window.  Also not necessary at this point,
% but included for illustrative purposes.
clf;

% Generate the x vector.
x = linspace(0, 2*pi, 1000);

% Simplest invocation of 'plot': supply two vector arguments, which are 
% the x and y coordinates of the points to be plotted.  Note the use 
% of element-by-element operations to generate the y vector.
plot(x, sin(x) .* sin(3*x));

% Pause execution until user enters something.
input('Type ''Enter'' to continue: ');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example 2:  Basic plot with non-default plot style, axes labels and
%             title.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('The following is a plot of\n\n');
fprintf('   sin(x)*sin(3x) for 0 <= x <= 2 pi, using\n\n');
fprintf('   - 1000 points uniformly distributed in the interval\n');
fprintf('   - points plotting style, with small green circles for the points\n');
fprintf('   - a title and axes labels\n\n');
fprintf('Note that the new figure window that opens (Figure 2) will likely\n');
fprintf('be positioned directly on top of Figure 1.  This is the default\n');
fprintf('behaviour of Matlab---slightly annoying, but one gets used to\n');
fprintf('manually rearranging multiple windows so that they are all visible.\n');

% Open the second figure window and clear it.
figure(2);
clf;

x = linspace(0, 2*pi, 1000);
% The third (optional) argument to plot specifies the plotting style;
% in this case 'og', which means plot using points with a circle 
% symbol at each point (o), and with color green (g).
plot(x, sin(x) .* sin(3*x), 'og');

% Define axes labels.
xlabel('x');
ylabel('sin(x) sin(3x)');

% Define title.
title('A simple plot');

% Pause execution until user enters something.
input('Type ''Enter'' to continue: ');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example 3:  Multiple plots in a single figure, with title, axes 
%             labels and legend.  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf('The following is a plot of\n\n');
fprintf('   sin(x)*sin(3x) AND cos(5x)*cos(2x) for 0 <= x <= 2 pi, using \n\n');
fprintf('   - 1000 points uniformly distributed in the interval\n');
fprintf('   - red +''s for the first data set, a cyan line for the second \n');
fprintf('   - a title, axes labels and legend\n\n');

% Open the third figure window and clear it.
figure(3);
clf;

x = linspace(0, 2*pi, 1000);

% This illustrates another how to make multiple plots on a single 
% figure/graph.  The 'hold on' command tells Matlab
% NOT to clear the graphics window when a plot command is 
% issued, which it otherwise would by default.
hold on;
plot(x, sin(x) .* sin(3*x), '+r');
plot(x, cos(5*x) .* cos(2*x), '-c');

% Define axes labels.
xlabel('x');
ylabel('displacement');

% Define title.
title('A slightly more complex plot');

% Define legend, the 'location', 'best' pair positions the 
% legend so as to obscure the minimum amount of the plot.
legend('sin(x) sin(3x)', 'cos(5x) cos(2x)', 'location' , 'best');

% Pause execution until user enters something.
input('Type ''Enter'' to continue: ');
% And we're done!
fprintf('Done with the demo.\n');

% Print a cheesy little countdown, then 
% close all the figure windows.
fprintf('Closing all figure windows in 5 .. ');
% pause(<s>) pauses program execution for <s> seconds.
pause(0.9);
fprintf('4 .. ');
pause(0.9);
fprintf('3 .. ');
pause(0.9);
fprintf('2 .. ');
pause(0.9);
fprintf('1 .. ');
pause(0.9);
fprintf('\n');
close all;
