

%==========================================================================
%REU Tutorial:  (Example One)
%
%matlabCalculator.m
%
%Elements of the matlab programing language.
%
%
%==========================================================================



%==========================================================================
%MatLab as a scientific calculator:

%Try entering the following expressions by hand: (hit enter after each line)
2 + 2
2*10
5^3
4.1^(-2)
3.33^(1.9)
cos(1.334)
exp(3.4444)
factorial(6)

%Special Functions: For complete list check help for mfun, and mfunlist

mfun('Airy', 2.224)
mfun('binomial', 4, 2)
mfun('binomial', 4.002, 2.5)
mfun('bessel', 2.3, 0.123)

%Special numbers:
pi
exp(1)

format long 
pi
exp(1)

%Multiple Precision:
vpa(pi, 10)
vpa(pi, 25)
vpa(pi, 1000)


%==========================================================================
%Short computations with named variables:

a = 1
b = pi
c = exp(1)

d = c^(i*b) + 1     %i is, by default, the imaginary unit.

%Complex arethemetic:

z_0 = 2.5 + i*4.5
z_1 = 1.4 + i*3.2;

z = z_0 + z_1
z = z_0*z_1
z = z_0/z_1

z_0Bar = conj(z_0)    %complex conjugation
z = z_0 + z_0Bar


%==========================================================================
%Matrices: This is what MatLab is really about:
%==========================================================================

%Matrix instantiation
A = [1,  3, 4;
     3,  2, 3;
     7, -2, 5]

B = [0.453, 0.555, -1.224;
     0.445, 2.001,  3.333;
     pi,    5,      -2]

%Vectors.   
x = [2.5;
     3.3;
     4.4]   %Column Vector
 
y = [1.2, 0, -3.3]   %Row vector 

%Special Matrices
I = eye(3)
Z = zeros(3,3)


%The colon operator.
subMatrix = A(1:2, 1:2)
subMatrix = A(1, :)

bigMatrix = [1:5, 2:6]

%=========================================================================
%Matrix Linear Algebra:
A + B
B - A


A*B
A*A
A^(2)
B*Z

A_inv = inv(A)   %Inversion
A*A_inv
A_inv*A
A_inv*A - I

A*x
y*B
y*A*B*x

det(B)    %determinants
det(A*B)

%Solution of Linear Syatems:

b = [0.441;
     1.223;
     -0.335];
 
%Solve Ax = b 
check_det = det(A)
if (check_det ~= 0)
  x = A\b;
  disp('Solution is: ')
  x
else 
  disp('Warning: Matrix is singular.') 
end    

%Eigenvalues and Eigenvectors:
[V, D] = eigs(A);
V
D

%==========================================================================
%Non-Square Matrices:

C = [2, 3, 4, 5;
     3, 3, 3, 3;
     2, 1, 7, 5];
 
 %Singular Value Decomposition:
 [U, S, V] = svd(C);
 
y = [2;
    -2;
     1];
 
 %Moore Penrose Inverse:
 C_mp = pinv(C);
 x = C_mp*y
 
 %Check:
 C*x
 
 %=========================================================================
 %Matrices as scalar containers:

 X = [2, 2;
      2, 2]
 Y = [1, 2;
      3, 4]
 
 %element by element operations: 
 X + Y
 X .* Y
 X ./ Y
 sin(X)
 exp(Y)
 
 %For operator theory exponentation use
 expm(X)
 
 
 %Discretize [0, 10] in 500 pieces.
 t = linspace(0.0000001, 2, 5000);
 
 %Evaluate f(t) = sqrt(t)sin(1/t) two ways:
 
 %for loop:
 for n = 1:5000
    x1(n) = sqrt(t(n))*sin(1/t(n)); 
 end    
 
 figure
 hold on
 plot(t(:), x1(:), 'b.')
 
 %matlab element by element operations; more elegant and more efficient.
 x2 = sqrt(t).*sin(t.^(-1));
 plot(t(:), x2(:), 'r')
 
 

















