%% MATLAB Session 1 - Basics, vectors, matrices, plotting
% 2026
% Goal: Introduction to basic MATLAB operations,
% without overlap with OLS/econometrics.

clear;          % Clears all variables from the workspace
close all;      % Closes all open figure windows
clc;            % Clears the Command Window

%% 1. Basic calculations and variables

4*5             % Calculates the product of 4 and 5

a = 4*5         % Creates variable a and stores the result of 4*5
b = 2+3         % Creates variable b and stores the result of 2+3

a*b             % Calculates the product of variables a and b

%% 2. Create vectors

myVector  = [1 2 3 4 5]             % Creates a row vector with elements 1, 2, 3, 4, 5
myVector2 = 1:2:7                   % Creates a vector from 1 to 7 with step size 2
myVector3 = 3:-2:-7                 % Creates a vector from 3 to -7 with step size -2
myVector4 = linspace(1,100)         % Creates 100 equally spaced points from 1 to 100
myVector5 = linspace(1,100,200)     % Creates 200 equally spaced points from 1 to 100

%% 3. Refer to and modify vector elements

clear myVector                      % Clears only the variable myVector from the workspace

myVector = 10:10:50                 % Creates a vector from 10 to 50 with step size 10

myVector(4)                         % Displays the 4th element of the vector
myVector(3:5)                       % Displays elements from the 3rd to the 5th position
myVector(3:end)                     % Displays elements from the 3rd position to the end

myVector(4) = 1000                  % Replaces the 4th element of the vector with 1000
myVector(8) = 50                    % Adds a value in the 8th position and fills intermediate positions with zeros

myVector'                           % Transposes the row vector into a column vector

%% 4. Basic linear algebra with vectors

0.5*myVector + 1                    % Multiplies each element of the vector by 0.5 and adds 1

% The following product is not conformable because myVector is 1 x 8.
% Therefore, myVector*myVector would be 1 x 8 times 1 x 8,
% where the inner dimensions do not agree.

try
    myVector*myVector               % Tries to calculate a nonconformable matrix product
catch ME
    disp('Expected error for myVector*myVector:')   % Displays a message that the error was expected
    disp(ME.message)                            % Displays the MATLAB error message
end

myVector*myVector'                  % Inner product: 1 x 8 times 8 x 1, gives a scalar
myVector'*myVector                  % Outer product: 8 x 1 times 1 x 8, gives an 8 x 8 matrix

%% 5. Create matrices

myMatrix  = [1 2 3; 4 5 6]          % Creates a 2 x 3 matrix. The semicolon starts a new row
myMatrix
myMatrix2 = [1:4; 8:11]             % Creates a 2 x 4 matrix using sequences
myMatrix2
myMatrix3 = zeros(3)                % Creates a 3 x 3 square matrix of zeros
myMatrix3
myMatrix4 = zeros(3,2)              % Creates a 3 x 2 matrix of zeros
myMatrix4
myMatrix5 = ones(3,2)               % Creates a 3 x 2 matrix of ones
myMatrix5
%% 6. Refer to and modify matrix elements

myMatrix(2,2)                       % Displays the element in row 2 and column 2

myMatrix(1,:)                       % Displays all elements of the 1st row

myMatrix(1:2,2:3)                   % Displays rows 1 to 2 and columns 2 to 3

myMatrix(1:2,2:end)                 % Displays rows 1 to 2 and columns from the 2nd to the last

myMatrix(1,2:3) = [20 30]           % Replaces the elements of row 1 in columns 2 and 3

%% 7. Matrix operations

clear myMatrix                      % Clears the variable myMatrix from the workspace

myMatrix = [1 2 3; 4 5 6]           % Creates the matrix myMatrix again

2*myMatrix + 1                      % Multiplies each element of the matrix by 2 and adds 1
myMatrix + myMatrix                 % Adds the matrix to itself element by element

% The following product is not conformable because myMatrix is 2 x 3.
% The product myMatrix*myMatrix would be 2 x 3 times 2 x 3,
% so the inner dimensions 3 and 2 do not agree.

try
    myMatrix*myMatrix               % Tries to calculate a nonconformable matrix product
catch ME
    disp('Expected error for myMatrix*myMatrix:')   % Displays a message that the error was expected
    disp(ME.message)                            % Displays the MATLAB error message
end

myMatrix*myMatrix'                  % Valid product: 2 x 3 times 3 x 2, gives a 2 x 2 matrix

% Element-by-element operations.
% The dot before the operator means that the operation is applied element by element.

myMatrix.*myMatrix                  % Multiplies each element of the matrix by the corresponding element
myMatrix.^2                         % Squares each element of the matrix

%% 8. Matrix-vector multiplication

clear myMatrix myVector             % Clears variables myMatrix and myVector

myMatrix = [1 2 3; 4 5 6]           % Creates a 2 x 3 matrix
myVector = 1:3                      % Creates a 1 x 3 row vector

% The following product is not conformable.
% The matrix is 2 x 3 and the vector is 1 x 3.
% For matrix multiplication, the vector must be 3 x 1.

try
    myMatrix*myVector               % Tries to calculate a nonconformable product
catch ME
    disp('Expected error for myMatrix*myVector:')   % Displays a message that the error was expected
    disp(ME.message)                            % Displays the MATLAB error message
end

myMatrix*myVector'                  % Valid product: 2 x 3 times 3 x 1, gives a 2 x 1 vector

%% 9. Random matrices, size, and deleting rows

clear myMatrix                      % Clears the variable myMatrix from the workspace

rand(3)                             % Creates a 3 x 3 random matrix with values from the uniform distribution on (0,1)
rand(3,1)                           % Creates a 3 x 1 random column vector with values from the uniform distribution on (0,1)

myMatrix = [1 2 3; 4 5 6]           % Creates a 2 x 3 matrix
size(myMatrix)                      % Displays the dimensions of the matrix
[nrows,ncolumns] = size(myMatrix)   % Stores the number of rows and columns in two variables

myMatrix(1,:) = []                  % Deletes the 1st row of the matrix

%% 10. Rounding functions

myVector = [1.2 1.9]                % Creates a vector with decimal values
round(myVector)                     % Rounds each element to the nearest integer
floor(myVector)                     % Rounds each element downward
ceil(myVector)                      % Rounds each element upward

%% 11. Simple plot

clear;                              % Clears all variables from the workspace
close all;                          % Closes all open figure windows
clc;                                % Clears the Command Window

x = 0:0.1:20;                       % Creates vector x from 0 to 20 with step size 0.1
y = exp(-x/10).*sin(x);             % Calculates y = exp(-x/10)*sin(x) element by element

figure                              % Opens a new figure window
plot(x,y)                           % Plots y as a function of x
grid on                             % Adds a grid to the plot
xlabel('x')                         % Adds a label to the horizontal axis
ylabel('f(x) = e^{-x/10} sin(x)')    % Adds a label to the vertical axis
title('A simple plot')              % Adds a title to the plot

%% 12. Multiple plots with subplot

clear;                              % Clears all variables from the workspace
close all;                          % Closes all open figure windows
clc;                                % Clears the Command Window

t = linspace(0,2*pi,100);           % Creates 100 equally spaced points from 0 to 2*pi

figure                              % Opens a new figure window

subplot(2,2,1)                      % Divides the figure into a 2 x 2 grid and selects position 1
plot(t,sin(t))                      % Plots sin(t)
title('sin(t)')                     % Adds a title to the first plot
grid on                             % Adds a grid to the plot

subplot(2,2,2)                      % Selects position 2 in the 2 x 2 grid
plot(t,cos(t))                      % Plots cos(t)
title('cos(t)')                     % Adds a title to the second plot
grid on                             % Adds a grid to the plot

subplot(2,2,3)                      % Selects position 3 in the 2 x 2 grid
plot(t,sin(2*t))                    % Plots sin(2t)
title('sin(2t)')                    % Adds a title to the third plot
grid on                             % Adds a grid to the plot

subplot(2,2,4)                      % Selects position 4 in the 2 x 2 grid
plot(t,cos(2*t))                    % Plots cos(2t)
title('cos(2t)')                    % Adds a title to the fourth plot
grid on                             % Adds a grid to the plot