echo on
%%%%% lab1_matlab.m %%%%%
%
%%  Running MatLab  %%
%  $cd /usr/common/matlab-13/bin/    and when you are on this directory
%  $./matlab
%
%  Or direct:  $/usr/common/matlab-13/bin/matlab
%
disp(sprintf('**** Exercises for practicing MatLab ****\n'));

disp(sprintf('**** SOME SINTAX FORMS ****\n'));

echo off
% The echo command controls the echoing of M-files during execution.
echo on 		

a=1.2*10^-8		% define a variable and show result
a=1.2*10^-8;		% discard answer
format long		% show in 16 digits
a
format short		% show in 5 digits
a
eps			% returns the distance from 1.0 to the 
			% next largest floating-point number.
realmax			% Largest positive floating-point number 
realmin			% Smallest positive floating-point number 

disp(sprintf('**** VECTORS ****'))

a=[1,3,5,7]		% create a row vector of dimension 1 x 4 (1 row and 4 columns)
b=a'			% transpose row vector to column vector of dimension 4 x 1
			% (1 column and 4 rows)
r=rand(4,1)		% create n x 1 random vector
d=5.1*a			% A scalar can multiply a vector of any size.
d=d+a			% Vectors of same dimensions can be added or subtracted
			% d_ij = d_ij + a_ij for all i and j
d=d-a			% d_ij = d_ij - a_ij for all i and j
b=(1:2:7)'		% create a column vector; start 1, displacement 2 end 7
c=a*b			% Row and column vector multiplication (inner product), 
			% results in a scalar c=sum(a_i*b_j). The number of elements 
			% of a must equal the number of rows of b. 
C1=b*a			% The operation is not commutative! We get a matrix now.
e=C1(3,:)		% is the 3-th row of C1
e=C1(:,2)		% is the 2-nd column of C1
c=e.*b			% Column times column vector multiplication - array multiplication
			% is the element-by-element product of the arrays of the same 
			% size and shape (c_ij=e_ij*b_ij).
p=a.^2			% another variant of array multiplication


disp(sprintf('**** MATRICES ****'));

A=[1,2,3,5;1,1,1,1;3,0,3,3]	% Create a m x n = 3 x 4 matrix. 
				% element a_ij; i=1..m lines, and  j=1..n columns
B=A'			% Transposed 4x3 matrix
D=5.1*A			% A scalar can multiply a matrix of any size.
D=D+A			% Matrices of same dimensions can be added or subtracted
			% d_ij = d_ij + a_ij for all i and j
D=D-A			% d_ij = d_ij - a_ij for all i and j
E=D(2:3,2)	% vector of entries from 2 to 3 of column 2
E=D(3,2:4)	% row vector from 3th row of entries from 2 to 4
E=D(2:3,1:2)	% square submatrix from rows 2 and 3 and columns 1 and 2
R=rand(4,4)		% Create a 4 x 4 matrix with random elements. 
I=eye(4)		% Create the identity 4 x 4 matrix. 
C1 = R*I		% Identity matrix does not change the result.
C=A*B			% Matrix multiplication C=A*B is the linear algebraic product 
			% of the matrices A and B.  c_ij=sum_(k=1..n)(a_ik*b_kj). 
			% In this case C is a symmetric matrix.
			% how many flops were used to matrix multiplication?
C1=B*A			% The matrix multiplication is not commutative A*B =! B*A
			% However, it is associative A*(B*C) = (A*B)*C and
			%                distributive A*(B+C) = A*B + B*C
E=[0,1;0,0]		% Create a 2 x 2 matrix. 
F=[1,0;0,0]		% And another 2 x 2 matrix. 
Z=E*F			% Result is zero even if E and F are not zero!
C2=inv(C)		% Inverse of the square matrix C.
C3=C/C			% Slash or matrix right division A/B is roughly the same as A*inv(B).
C4=C./C			% Array right division A./B is the matrix with elements A(i,j)/B(i,j).
			% A and B must have the same size, unless one of them is a scalar. 

rank(C)			% Provides an estimate of the number of linearly independent rows 
			% or columns of a matrix.
size(A,2)		% return the size of a matrix in dimension 2
echo off		% print the size of C1 in both dimensions
disp(sprintf('A matrix is %g by %g', size(A,1), size(A,2))) 




