%% Crashcourse Matlab - Part II: Inbuilt Matlab functions
% Matlab provides a large number od inbuild functions that help you do things. Here we just highlight some of the very commonly used ones. The best way to learn about functions is the Matlab documentation. The hyperlinks below will take to the documentation of the functions we discuss.
%% Basics
% In general, a function has an input and an output: out = function(in);
% For example you can determine the length of the vector as follows

a = [-10:5:20];
len_of_a = length(a)

%
% Here length() is the function, the vector "a" the input and the scalar "len_of_a" the out put. Functions can have multiple inputs.

A = rand(6,8)

% Here the function rand() produces a random matrix with 6 rows and 9 columns.  Functions can also have multiple outputs.

[r, c] = size(A)

% Here the function size() tels you that the matrix A has 6 rows and 8 columns. There are also functions that don't require an input

today = date()

% Here the function date() returns a string, i.e., a number of letters and numbers, giving todays date.
% In general, inbuilt matlab functions can have variable numbers of inputs and outputs.
%
%% Learning more about functions
% Your power in Matlab correlates directly with your knowledge of functions! So try to explore the documentation. At the bottom of every documentation page you are given a list of related functions, browsing those is a good way to explore potentially useful functions.
% The documentation takes some getting used to, but I expect that you look up the dcumentation by yourself. In class, I will only highlight certain features of functions we are using that are important in the context of the class.
% If you know the function name:
% Typically you can just google "Matlab Name" and you will get the documentation page.
% If you know what you want to do:
% Try to google something like "Matlab my activity". For example if you you want to integrate a function but don't know how. Google "Matlab numerical integration". The first hit I get is to this to the documentation page of the Matlab function integral, which not surprisingly intrgrates a function.
%
%% Basic Mathematical functions
% Matlab has a function for everything you need. Typically the names don't need much explanation. For example  all trigonometric and transcendental functions are availabe. For a full list see here and scroll down to Mathematics. A few simple examples are

sin(2*pi)
e = exp(1) % Euler's number
log(e) 
sqrt(2)
2^(1/2)

% Note that you can use "pi" to refer to the trigonometric constant \pi=3.14... in Matlab. Note, that  log() is the natural logarithm in Matlab, often otherwise denoted ln(). After getting familiar with Matlab you can guess the function name in 80% of the cases.
%
%% Basic vector functions
% Matlab is particularly good with functions for vectors

a = round((rand(5,1)-.5)*10)
length(a)   % Gives the length of the vector
max(a)      % Maximum element of the vector
min(a)      % Minimum element of the vector
abs(a)'     % takes absolute value of all elements of the vector
norm(a)     % magnitude of the vector
mean(a)     % Computes the mean of the vector elements
std(a)      % Computes the standard deviation of the vector elements
sum(a)      % Computes the sum of all vector elements, returns a scalar
cumsum(a)'  % Cumulative sum of all vector elements, returns a vector 

%
%% Basic Matrix functions
% Matlab is even better with matrix operations

A = round((rand(5)-.5)*10); 
A = A*A'               % SPD A with real integer entries
cond(A)                % Condition number
rank(A)                % Rank of matrix
Ainv = inv(A)          % computes inverse
[L,U] = lu(A)          % LU factorization
[V,D] = eig(A)         % Eigenvalue decomposition
null(A)                % Basis of nullspace of A
[U,S,V] = svd(A)       % Singular value decomposition of A
[Q,R] = qr(A)          % QR factorization

% Matlab's best feature is the backslash for direct solution of linear systems \mathbf{A} \mathbf{x} = \mathbf{b}.

b = a                  % r.h.s. vector
x = A\b
x2 = Ainv*b

% Please NEVER solve a linear system by computing the inverse of \mathbf{A}. For large systems this is very slow, very inaccurate and requires enormous memory. The latter is because \mathbf{A}^{-1}is generally a full matrix, even if \mathbf{A}only has a few entries. Instead the backslash uses Gaussian elimination to solve the linear system.
%
%