%% MATLAB Short Course 1

%% Basic Numbers

% floaing numbers
1.3

% integer, but saved as floating number
1

% special constant pi = 3.1415926.... (format long for more digits)
pi

% special constant, e = 2.1718.... base of the natural logarithm
exp(1)

%% Basic arithmetics

1+2-3

% multiplication/division has higher precedance than addition/subtraction
1+2*3

% power
2^3

% use parenthese to reduce ambiguity
2^3^3
2^(3^3)
(2^3)^3

%% Logical comparison
1 < 2 % true (output "1" any non-zero number is "true")
2 > 3 % false (output "0")

%% machine epsilon (becareful about finite precision
eps
1+eps/2 == 1 % true, but mathematically false
1+eps == 1 % false
1e-300 == 0
1e-323 == 0 % could be different answer on different computers
1e-324 == 0

%% Use "help" for documentation
log(2) % which base is used here, 2, 10 or e?
help log % find the base from the help file

sin(30) % is "1" here degree or radian?
help sin

%% Vectors
a = [1 2 3] % row vector
b = [1; 2; 3] % column vector

%% common functions with vectors
length(a)
norm(a) % Euclidean norm
norm(a) % sum of absolute values
norm(a,'inf') % maximum of the absolute value

%% the use of colon ":"
a = 1:10
a = 1:3.5 % what if the end point is not an integer?
a = 3:2:20

%% modify the elements vi indices (start from 1, not zero
a(2) =15 % change the second element
a([2 4 6 8]) = 0
a(2:2:8) = 1
a(2:2:end) = 2 % all even indexed elements to 2

%% Special matrices
A = [1 2; 3 4]
eye(3)
zeros(2,5)

%% reshape the matrix (vector)
A = 1:9
reshape(A,3,3)
transpose(reshape(A,3,3))
reshape(A,3,3)' % ' for transpose

%% Random numbers
A = rand(3,3) % uniform 3x3
A = randn(3,3) % standard normal 3x3
b = rand(3,1)

%% Different ways to solve the linear system Ax=b
x = A\b
x = inv(A)*b
x = linsolve(A,b)