%% 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
% Structured Programming
% Data plotting
%
%% 1. Structured Programming
% Sequence: ordered statements or subroutines executed in sequence.
% Selection: one or a number of statements executed depending on the state of the program:  ⤷ if … else … elseif … end /  ⤷ switch … case … otherwise … end
% Iteration: one or a number of statements repeatedly executed until the program reaches a certain state: ⤷ for … end / ⤷ while … end
%
%% if, elseif, else
% if expression (typically includes relational operators)
%     statements
% elseif expression
%     statements
% else
%     statements
% end

x=5.5;
if rem(x,2)==0
    disp('x is even');
elseif rem(x,2)==1
    disp('x is odd');
else
    disp('x is not a natural number');
end

%
%% switch, case, otherwise
% switch switch_expression
%     case case_expression
%         statements
%     case case_expression
%         statements
%         …
%     otherwise
%         statements
% end
% The switch block tests each case until one of the case expressions is true.
% When switch_expression == case_expression MATLAB executes the corresponding statements and exits the switch block immediatley.

switch rem(x,2)
    case 0
        disp('x is even');
    case 1
        disp('x is odd');
    otherwise
        disp('x is not a natural number');
end

%
%% for loop
% for index = values (
%     statements
% end
% values has one of the following forms: 1) init_value:end_value, 2) init_value:step:end_value, 3) array_of_values
% statements can contains other for-loops (i.e. for-loops can be nested).
% The current loop can be terminated with the command break.

s = 0;
v = rand(1,20);
for k = 1:length(v)
    s = s + v(k);
end
disp(['sum = ' s]);

%
%% while loop
% while expression (typically includes relational operators)
%     statements
% end
% statements  can contains other while-loops (i.e. while-loops can be nested).
% The current loop can be terminated with the command break.

s = 0;
v = rand(1,20);
k = 1;
while k < length(v)
    s = s + v(k);
    k = k + 1;
end
disp(['sum = ', s]);

%
%% 2. Data plotting
% Plots are displayed on dedicated windows, called MATLAB ﬁgures.
% >> figure(n)                          Finds the open figure with id-number n, and makes it the current figure. If no figure exists with that number, creates a new figure n.
% >> close(n)                          Close the figure with id-number n.
% >> close all                          Close all open figures.
% >> plot(X, Y, LineSpec)        Creates a 2-D line plot of the data in Y vs the corresponding values in X. If X is omitted, data are plotted vs the corresponding vector index value.
%                                             LineSpec is a string that specifies the line style, marker symbol, and color.

t = 0:0.05:5;
y = sin(2*pi*t);
plot(t, y);

%

x = -2:0.1:2;
y = atan(x);
plot(x, y, 'r*--');

% >> title(s)                                Adds title s to current axes.
% >> xlabel(s), ylabel(s)           Labels x-axis/y-axis of current axes.
% >> grid on/off                        Displays/hides grid lines on current axes.
% >> axis(limits)                        Specifies the limits for the current axes ([xmin, xmax,ymin, ymax]). Use xlim/ylim to set limits only for the x-axis/y-axis.
% >> axis style                           Uses a predefined style to set the limits and scaling:
%                                                         • equal: same length for the data units along each axis.
%                                                         • square: axis lines with equal lengths.
%                                                         • normal: restore the default behavior.
% >> axis mode                          Sets whether MATLAB automatically chooses the limits or not (manual/auto).

theta = 0:0.05:2*pi;
x = cos(theta);
y = sin(theta);
figure;
plot(x, y);
grid on;
title('axis: normal');

%

figure;
plot(x, y);grid on;
axis equal;
xlabel('t[s]')
title('axis: equal');

%
% >>  hold on/off                       Retains current plot when adding new plots on same axes.
% >> legend(s1,…,sN)                Creates a legend with labels s1,…,sN for the plotted data series.

theta = 0:0.05:2*pi;
x = cos(theta);
y = sin(theta);
figure;
plot(theta, x, 'o-');
hold on;
plot(theta, y, 'r*:');
legend('cos()', 'sin()');
title('Overlapped plots');

%
% >> subplot(m,n,p)            Divides the current figure into an m-by-n grid, and creates axes in the position specified by p.

t = linspace(0,2*pi,400);
x = cos(t); y = sin(t);

subplot(2,2,1); % Top-left plot
plot(t,x); hold on;
plot(t,y,'k:');
grid on;
title('cos(t)');
xlabel('t'); ylabel('cos(t)');

subplot(2,2,2); % Top-right plot
plot(t,y); hold on;
plot(t,x,'k:');
grid on;
title('sin(t)');
xlabel('t'); ylabel('sin(t)');

subplot(2,2,3); % Bottom-left plot
plot(x,y); grid on;
axis equal;
title('axis: equal');
xlabel('cos(t)'); ylabel('sin(t)');