% MATLAB Recitation Demo for Monday, September 15.
% File: rdemo2c
%
% *** Plotting of simple functions ***
%
% MATLAB is also good for plotting functions, such as
%
%   y(x) = sin(2*pi*x)  or  y(x) = x^2
%
% where x is a finite number of points in an interval (say, [0, 1])
% and   y is a vector giving the function value at each point in x.
%
>> diary rdemo2c

>> help linspace
 LINSPACE Linearly spaced vector.
 	LINSPACE(x1, x2) generates a row vector of 100 linearly
 	equally spaced points between x1 and x2.
 	LINSPACE(x1, x2, N) generates N points between x1 and x2.
 	See also LOGSPACE, :.

>> x = linspace(0, 1, 21)
x =
  Columns 1 through 7 
         0    0.0500    0.1000    0.1500    0.2000    0.2500    0.3000
  Columns 8 through 14 
    0.3500    0.4000    0.4500    0.5000    0.5500    0.6000    0.6500
  Columns 15 through 21 
    0.7000    0.7500    0.8000    0.8500    0.9000    0.9500    1.0000

% The constant pi is predefined in MATLAB.
>> pi
ans =
    3.1416

%
% Let's define the function y(x) = sin(2*pi*x) for the x in [0, 1].
%
>> y1 = sin(2*pi*x)
y1 =
  Columns 1 through 7 
         0    0.3090    0.5878    0.8090    0.9511    1.0000    0.9511
  Columns 8 through 14 
    0.8090    0.5878    0.3090         0   -0.3090   -0.5878   -0.8090
  Columns 15 through 21 
   -0.9511   -1.0000   -0.9511   -0.8090   -0.5878   -0.3090         0

%
% Let's define the function y(x) = x^2 for the x in [0, 1]. 
>> y2 = x.^2
y2 =
  Columns 1 through 7 
         0    0.0025    0.0100    0.0225    0.0400    0.0625    0.0900
  Columns 8 through 14 
    0.1225    0.1600    0.2025    0.2500    0.3025    0.3600    0.4225
  Columns 15 through 21 
    0.4900    0.5625    0.6400    0.7225    0.8100    0.9025    1.0000

%
% Plot sin(x) in red using dashed lines, superimpose a grid,
% label the vertical and horizontal axes, give a title,
% print the graph to an Athena cluster printer, and also print
% the graph to a file in your directory.
%
>> plot(x, y1, 'r-')
>> grid
>> ylabel('y(x)')
>> xlabel('x')
>> title('Plot of function y(x) = sin(x) for x in [0,1]')
%
% Print the graph at LaserPrinter in Bldg 2, 2nd Floor.
%
>> print -dps -Pceline
%
% Save the graph as a postscript file called 'figure1.ps'
% in my directory.
%
>> print -dps figure1.ps
%
% Clear the graph. 
% Plot sin(x) and x^2 with red dashes and green dots, 
% respectively.
% Superimpose a grid, and give the graph a title.
%
>> clg
>> plot(x, y1, 'r-', x, y2, 'g.')
>> grid
>> title('This is another graph')
>> diary off