%% Laboratory notes: Introduction to MATLAB
% Riccardo Antonello (riccardo.antonello@unipd.it) and Giulia Michieletto (giulia.michieletto@unipd.it)
% March 04, 2024
% Dept. of Management and Engineering, University of Padova, Italy
%
%
% This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.
%
%% Contents
% Introduction
% Desktop Basics
% Vectors & Matrices
% Functions Library
%
%% 1. Introduction
% MATLAB (MATrix LABoratory) is a
% numerical computing environment
% programming language
% MATLAB easily allows matrix manipulations, data analysis and visualization, implementation of algorithms.
% MATLAB offers a large collection of readily available functions to perform several computational tasks:
%  built-in functions
% functions included in optional toolboxes
% functions provided by large user community (over 3mln users worldwide)
%
%% 2. Desktop Basics
% Current Folder panel: access to files
% Command Window:    Interactive mode: enter commands at MATLAB prompt (>>).
%                                      Non-interactive mode: launch MATLAB scripts (= text files containing a sequence of MATLAB commands and functions)
% Workspace panel: explore data defined in memory (workspace)
% Command History panel: review previously entered commands
%
%% Command Window Basics
% >>                                      MATLAB prompt.
% >> <command> ↩            Executes a command and prints the output on the command window.
% >> <command>; ↩           Executes a command, without printing the output on the command window.
% >> help <name> ↩           Displays the help text for <name> in the Command Window.
% >> doc <name> ↩            Displays the documentation for <name> in the Help Browser.
%
%% Workspace Basics
% The workspace is the area of memory containing the variables created and used in a MATLAB session
% the assignment operator is denoted with = (no data type declaration is required)
% if the result of an operation is not assigned to a variable, then it is assigned to the special variable ans
% Variables names start with a letter, followed by letters, digits, or underscores and must be different from MATLAB keywords (MATLAB is case sensitive)
% >> who                                                    Lists the variables in the current workspace.
% >> whos                                                  Lists the variables in the current workspace, including size, type, …
% >> clear <variable>                                 Removes a variable from workspace.
% >> clear all                                              Removes all the variables from workspace.
% >> save <filename> <variables list>       Stores variables into a MATLAB formatted binary data file (MAT-file, extension .dat).
% >> save <filename>                                Stores all the workspace variables into a MAT-file
% >> load <filename>                                Loads variables from a MAT-file into the workspace.
% >> load <filename> <variables list>       Loads selected variables from a MAT-file into the workspace.

a=1; b=2; c=-3
A=4; B=5; C=-6

%

help abs
doc abs

%

c=abs(c), C=abs(C)

%

save UppercaseVars A B C
save LowercaseVars a b c
clear all
who

%

load UppercaseVars
who

%

load LowercaseVars a
who

%
%% 3. Vectors & Matrices
%% Definition of vectors and matrices
% In general
% Square brackets ([ ]): enclose the elements.
% Comma (,) or space : separate elements on the same row.
% Semicolon (;) : separates the rows.

% Row vector (1×m)
a=[1 2 3]      
a=[1, 2, 3] 

%

% Column vector (n×1)
b=[1; 2; 3]     
b=[1
    2
    3] 

%

% Matrix (n×m)
C=[1 2 3; 4 5 6]        
C=[1, 2, 3; 4, 5, 6]
C=[1 2 3;
    4 5 6]

%
% Matrices with particular structure:
% >> [ ]                        Empty matrix.
% >> eye(n)                 nxn identity matrix.
% >> zeros(n,m)          nxm matrix with elements equal to 0.
% >> ones(n,m)           nxm matrix with elements equal to 1.
% >> diag(<vector>)    Diagonal matrix with elements of <vector> on the leading diagonal.
% >> rand(n,m)            nxm matrix with uniformly-distributed random real numbers in the interval (0,1).
% >> randn(n,m)         nxm matrix with normally-distributed random real numbers (mean = 0, variance = 1).
% >> randi(N,n,m)         nxm matrix with uniformly-distributed random integer numbers in the interval [1, 𝑁].

A = ones(1,3)
B = zeros(2,1)
C = diag([2 3])
D = randi(10, 1, 5)

%
%% Array Indexing
% >> A(i,j)                    Element at i-th row and j-th column of matrix A.
% >> A(i,:)                    i-th row of matrix A.
% >> A(:,j)                    j-th column of matrix A.
% >> length(X)            Length of vector X.
% >> [N,M]=size(X)    Size (rows N and columns M) of matrix X.

A = [1 2 3; 4 5 6; 7 8 9]
A(2,:)
size(A)
length(A(1,:))

%
%% Operations with matrices
% Matrix addition and subtraction:
% Operators + (plus) and – (minus) are used to add and subtract matrices of the same size.
% If one of the operands is a scalar, then the same operation is repeated for each element of the other operand.

A = [0 1; 2 3];
B = ones(2,2);
A+B
A+1

%
% Matrix multiplication (row-by-column):
% Operator * (mtimes) is used to compute the matrix multiplication (row-by-column).
% If one of the operands is a scalar, then the same operation is repeated for each element of the other operand.

A = [0 1; 2 3];
B = [0 1; 1 0];
A*B
A*2

%
% Matrix power:
% Operator ^ (mpower) is used to compute matrix powers.
% A^-1 denotes (if exists) the inverse of the (square) matrix A.
% The inverse of a square matrix can be alternatively computed with the function inv(…).

A = [0 1; 2 3];
A^2
A^-1
inv(A)

%
% Element-wise operators are obtained by preceding the operator with . (dot)

A=[0,1;2,3]; B=[1,2;3,4];
A.*B
A.^2

%
% Matrix transpose:
% The operator ’ is used to compute the transpose of a real matrix.
% With a matrix of complex numbers, the operator ’ computes its conjugate-transpose (Hermitian).

A=[1, 2; 3, 4];
A'

%

A =[1, 1+1i; 2-1i, 2];
A'

%
%% 4. Functions Library
% Trigonometric functions
% >> pi                                          Floating-point number nearest the value of 𝜋.
% >> sin(x), cos(x), tan(x)           Sine, cosine and tangent of x in [rad].
% >> asin(x), acos(x), atan(x)     Inverse sine, cosine and tangent of x; return value is in [rad].
% >> atan2(y,x)                            Four-quadrant inverse tangent of y and x; return value is in [rad].
% >> deg2rad(x), rad2deg(x)      deg-to-rad and rad-to-deg unit conversions.
% Exponential functions
% >> exp(x)                                 Exponential of x
% >> log(x), log2(x), log10(x)    Natural logarithm, base-2 logarithm and base-10 logarithm of x.
% >> expm(X)                             Matrix exponential
% >> sqrt(x)                                Square root of x.
% Linear Algebra Functions
% >> det(X)                                 Determinant of square matrix X.
% >> rank(X)                               Rank of matrix X.
% >> trace(X)                              Sum of diagonal elements (trace) of square matrix X.
% >> inv(X)                                  Inverse of non-singular square matrix X.
% >> eig(X)                                  Eigenvalues and eigenvectors of square matrix X.
% >> poly(X)                               Characteristic polynomial of square matrix X.
% >> svd(X)                                Singular values decomposition of matrix X.
%