% This is a first example of a MATLAB program,
%   which is also called a MATLAB script file or a MATLAB M-file
%   MATLAB function files are similar but are slightly different.

% MATLAB programs generally act exactly the same as if the
%   same lines were entered at the cursor in the MATLAB program.

% In more detail, TO RUN A MATLAB PROGRAM (such as this one),
%  (i) Find a computer with MATLAB and run the MATLAB program,
%  (ii) Note the MATLAB Current Directory at the top of the MATLAB
%     screen. If necessary, change the Current Directory to the
%     directory that your program is located in.
%  (iii) Enter the program name without the suffix (such as firstm)
%     at the MATLAB prompt.
%  All of the MATLAB statements in the program (such as firstm.m)
%    will then execute in sequence, or at least up to the first line
%    that MATLAB thinks is an error.

% Lines beginning with % (such as this line) are COMMENTS and are
%   ignored by MATLAB (but hopefully not by you)

% BASIC VARIABLES in MATLAB are numbers, 1x1 matrices (which MATLAB
%   treats the same as numbers), nx1 column vectors, 1xn row vectors,
%   and mxn matrices. 

% To define (for example) a 2x3 matrix, enter

 [2 3 4; 5 6 7]

% Here space means `same row` and semicolon means `next row`, and
% all of the numbers must be contained within square brackets.
% MATLAB will respond
%
% ans =
%
%     2     3     4
%     5     6     7

% You will usually want to assign names to matrix. To do this, enter

 A = [2 3 4; 5 6 7]

% MATLAB will respond
%
% A =
%
%     2     3     4
%     5     6     7

% To define a matrix without having MATLAB reflect it to the screen,
%   follow its definition by a semicolon, as in

 A = [2 3 4; 5 6 7];

% This produces no display. To see the values of A later, just enter
% its name, as 

 A

% MATLAB will respond by displaying A exactly as above.

% SIZE (DIMENSIONS) OF A MATRIX
% size(A) gives the numbers of rows and columns of a matrix
%   as a 1x2 row vector, so that

S=size(A)

% returns  [2 3]  in this case. Alternatively, size(A,1) is the
% number of rows and size(A,2) the number of columns, as in

Q = [size(A,1); size(A,2)]

% MATLAB has a huge variety of built-in functions, not only
%   sin(x), cos(x), etc, but also Airy and Bessel functions.
%   The complementary error function (erfc(x)) and the incomplete
%   beta and gamma functions (betainc() and gammainc()) can be used
%   to compute normal, chi-square, Student-t, F-distribution, and
%   binomial P-values if you do not have MATLAB`s Statistics Toolbox
%   (which costs extra).

% Most functions in MATLAB are `VECTORIZED`, which means that
%   if you apply them to a vector or matrix argument, then MATLAB
%   will apply that function to each entry. For example, since
%   pi  means literal pi=3.1415926535...,

x = [0  pi/4  pi/2  3*pi/2  1]   % Could also write  (pi/4)*(0:4)

y = cos(x)

% displays as
%
% x =
%
%         0    0.7854    1.5708    2.3562    3.1416
%
%
% y =
%
%    1.0000    0.7071    0.0000   -0.7071   -1.0000


% MATRIX MULTIPLICATION: If A and B are matrices whose sizes
% allow them to be multipled, enter  C=A*B, as in

C = [1 4; -5 -5; -6 7; 4 11] * A

% Note that this defines a 4x2 matrix times the 2x3 matrix A, so
% that C will be 4x3. MATLAB responds
%
% C =
%
%    22    27    32
%   -35   -45   -55
%    23    24    25
%    63    78    93

% ARRAY ENTRIES AND SUBMATRICES
% For the matrix C just defined, C(1,3) = 32 (1st row, 3rd column)
% You can also define a submatrix

D = C(2:4, 1:2)

% MATLAB will respond
%
% D =
%
%   -35   -45
%    23    24
%    63    78
%
% Use : by itself to denote an entire row or column, so that
% the 3rd row of C is

E = C(3,:)

% MATRIX TRANSPOSE: Use a right single quote, as in

B = A'	     %'

% Note the comment with a single quote character. That is to keep
% editors that think 'xxxxx' always means a string from misbehaving.
% MATLAB will respond by displaying the transposed matrix
%
% B =
%
%     2     5
%     3     6
%     4     7

% CONSTANT MATRICES:
%   A1 = zeros(3,4) is a 3x4 matrix of all zeroes
%   A2 = ones(3,4) is a 3x4 matrix of all ones

% COMPONENTWISE MULTIPLICATION
% If A and B are matrices of the same size, then
%  C = A.*B  multiplies the matrices COMPONENT BY COMPONENT,
% so that

A = [1 2 3; 4 5 6];
B = [2 1 3; 2 3 4];
C = A.*B

% displays
%
% C =
%
%     2     2     9
%     8    15    24


