% intro.m

% MATLAB (matrix laboratory) is an useful computer language for displaying
% some of the mathematical concepts that we will employ in this
% introductory macroeconomics course. This first PROGRAM provides an 
% introduction to a variety of basic commands.

% Any line that begins with the 'percent' sign is a comment line in MATLAB

% Vectors and matrices

% It is easy to create vectors in MATLAB via a variety of operations

% A row vector can be created as 

x=[1 2 3]
pause

% The command pause stops the exectution of the program until the user hits
% the return key

% The comparable column vector can be created as 

x=[1
   2
   3]
pause

% or via transposition as

x=[1 2 3]'
pause

% since the symbol ' denotes the transpose of a vector or matrix with real
% elements.

% A matrix can be created as

M = [1 2 3
     4 5 6
     7 8 9]
pause

% or as 

M = [1 2 3; 4 5 6; 7 8 9]
pause

% where the symbol ';' can be used to end a line or to execute a line of
% code without displaying the result, a feature that we will use later.

% Matrix operations are easy to conduct in MATLAB. For example, to get
% y=M*x, we simply write

y=M*x
pause

% To get the eigenvalues and eigenvectors of M, we write

[P,MU]=eig(M)
pause

% To take the determinant of the matrix, we write

d=det(M)
pause

% To get the inverse of the matrix M, we can simply write 

IM=inv(M)
pause

% Note in this example, the determinant of the matrix M is zero and there
% is a zero eigenvalue, conforming to a general property of matrices, which
% is that the determinant is the product of the eigenvalues.

% MATLAB also allows for FUNCTIONS that take scalars, vectors and matrices
% as inputs and produce scalars, vectors and matrices as outputs.  The
% the functions above -- det, inv, and eig -- are functions produced by the
% creators of MATLAB.  To gain further information about these standard
% functions, you can type help functionname at the MATLAB prompt >>.
% For example, 

help inv

% brings up the following information, which defines the function and
% contains information on warning messages, function documentation, and
% related MATLAB functions

% INV    Matrix inverse.
%    INV(X) is the inverse of the square matrix X.
%    A warning message is printed if X is badly scaled or
%    nearly singular.
% 
%    See also slash, pinv, cond, condest, lsqnonneg, lscov.
%
%   Reference page in Help browser
%       doc inv

% Note that a warning message is issued because the matrix M is not
% invertible (because it is singular).

% If there is a MATLAB function, then one cannot create a USER function
% with the same name. 

% An example of a user function would be one that created a sum of squared
% values of a vector x.

%  function y=ssq(x);

%    y=0

%    for i=1:length(x)
%       y=y+x(i)^2;
%    end

% This function uses another MATLAB function length(x), which is the number
% of elements in a vector x. It also uses the MATLAB code for getting the
% ith element of x, which is x(i), and for squaring a scalar (x^a raises x
% to the a-th power).

% The input of this function is the vector x and the output of the function
% is the scalar y. 

% Using this function produces the sum of squares (since x=[1 2 3]
% it follows that the squared elements of x are [1 4 9] and there sum is
% 14). 

y=ssq(x);

% Note that one would probably not create a special function for this
% purpose. There are two simpler ways of creating the sum of squares.

% First, since x is a column vector, it follows that 

y1=x'*x;

% yields the sum of squares. Second, one can undertake operations on all
% elements of a vector with various MATLAB commands. For example, 

y2=x.^2
pause

% yields a new vector with the squared elements of x. Using the built in
% MATLAB function, we can sum the elements of this new vector, 

y2=sum(y2);

% to get a third measure of the sum of squares.
% Notice that that the last two measures are not displayed, as they are 
% follow by a ';'. To make sure that all three measures are equal, we can form

d=[y y1 y2];

% and then use the display command to display text and the vector.

disp('Three measures of the sum of squares')
disp(d)