%operations (real and complex numbers)
1+1          %sum of two reals numbers
1+2i         %sum of two complex numbers
conj(ans)    %complex conjugate
%%
%vectors
vec = [1, 2, 3, 4, 5]    %row vector (comma or blank separate columns, semicolon separates rows)
vec(1)                   %indices start with 1
vec(1:4)                 %part of the vector
vec_quick = 1:5          %sequence 1 to 5 with difference 1
vec_quick_smooth = linspace(1,5,10) %sequence 1 to 5 with 10 numbers equally distributed between them
vec_quick_smoother = 1:0.1:5          %sequence 1 to 5 with difference 0.1
%excercise
vec_1 = 11:15          
vec_2 = 11:14          

vec_1(2:5)+vec_2+pi
%remark - higher precision of pi
digits(25)
vpa(pi)


%%
%matrices
mat = [1 2 3; 4 5 6; 7 8 9] %3x3 matrix
mat(1,2)                    %element of the matrix on the first row and second column
mat(1:2,1:3)                %submatrix with first two rows and all columns (dimensions 2x3)
mat_ones = eye(3)           %unit matrix
mat_magic = magic(3)        %magical square

%why magical?
sum(mat_magic)     %sum of columns
sum(mat_magic)     %sum of rows
sum(diag(mat_magic))    %sum of diagonal
trace(mat_magic)        %trace of matrix (sum of diagonal)

%excercise
trace(mat)
diag(mat)
rank(mat)

mat_new_row = [mat(1:2,1:3);[20 30 40]]
mat_new_col = [mat_new_row, [20;30;40]]

%%
%multiplication
mat*mat     %matrix multiplication
mat.*mat    %element-wise matrix multiplication
%attention: be careful either of the type of multiplication and position of
%matrices/vectors

%vector multiplication
vec*transpose(vec)  %vector multiplication
vec.*vec            %element-wise vector multiplication


%%
%functions
%anonymous functions
func_sqrt_anonym = @(x) sqrt(x-1)/x
func_sqrt_anonym(6)
%functions in the file (see my webpages)
func_sqrt_file(6)

%excercise (syntax in file will be similar)
func_excer = @(x) [sqrt(x),x^2]
func_excer(7)

b = 5
func_excer_2 = @(x,y) b*x^2 + exp(-b)*y
func_excer_2(0.5,5)

%%
%conditions
A = 3
B = -5
%and (true = 1)
A<=4 && B > -10
%or (true = 1)
A<12 || B == 7
%not equal (true)
A~=B
%equal (false)
A==B

%%
%if condition

%function y = if_condition(x)
%if x > 10 
%    y = 1;
%elseif x == 10
%    y = 0;
%else
%    y = -1;
%end

%%
%for cycle

x = 0;
for i = 1:length(vec)
  x = vec(i)+x;
end

x

%%
%for cycle excercise
x = 0;
for i = 1:length(vec)
    if mod(vec(i),2)==0 
        x(i) = vec(i)^2;
    else
        x(i) = sqrt(vec(i));
    end
end

x

%%
%Calculation speed

%for cycle - no preallocation
tic
x = 0;
for i = 1:10000000
   x(i) = rand; 
end;

toc

%for cycle - preallocated array
tic
x = 1:10000000;
for i = 1:10000000
   x(i) = rand; 
end;

toc

%in-built function
tic
x = rand(1,10000000);
toc

%%
%plot excercise
xaxis = -4:0.1:4; %x-axis interval
%plot(x, f(x), format (e.g. dashed and red '--r', or green and circles 'go')
plot(xaxis,sin(xaxis),'--r',xaxis, 1/sqrt(2*pi)*exp(-(xaxis.^2)/2),'go')
grid on
title('Two functions')
xlabel('x') % x-axis label
ylabel('f(x)') % y-axis label
legend('show')
