%% 034IN "Fundamentals of Automatic Control" - Introduction to MATLAB
%% Part 1 - Starting MATLAB
%
% This is the first MATLAB live script of the collection 034IN "Fundamentals of Automatic Control" - Introduction to MATLAB, devoted to introduce the MATLAB environment and tools for solving practical problems related to the topics of the 034IN course, i.e. performance analysis of dynamic systems, design of control law for continuous-time linear dynamical systems, approximate discretization of a continuous-time control law etc.
% Use this link to go back to the main live script of the collection.
% Table of Contents
% 034IN "Fundamentals of Automatic Control" - Introduction to MATLAB
% Part 1 - Starting MATLAB
%     Goals
%     Starting MATLAB
%         Starting MATLAB on the PCs in the Computer Lab "Aula Nettuno" - C1 Building
%         Starting MATLAB on your own PC or Laptop
%         MATLAB Online on your own PC or Laptop
%     The MATLAB Interface - MATLAB Online
%         Managing Folders and Uploading Files using MATLAB Online
%             How to create a new folder in MATLAB Online
%             How to upload files and folder in MATLAB Online
%         A First Example
%     The MATLAB Programming Language
%         Example: Using the Arrow Keys in the Command Window
%     Getting Help
%         Help Command
%             Examples
%         With a Little Help from the 'help' Command!
%         The Help Browser
%     MATLAB Scripts and Interactive Live Scripts
%         The MATLAB Editor
%         MATLAB Script: an Example
%         MATLAB Interactive Live Scripts
%         MATLAB Live Script: an Example
%     The First Code Example in an Interactive Environment: a MATLAB Live Script
%         Example: Repeating a Command in a Live Script
%     Summary
%     Back to the Index
%     Go to the Next Part: Numbers, Arithmetic Operations, Formats and Variables
%%
%% Goals
%  The aim of this live script is to make you familiar with the MATLAB environment.
% What you should know by the end of this part:
% How to start MATLAB on a computer in the University of Trieste computer laboratories in the C1 or H2bis buildings, or on your own PC;
% What is the structure of the MATLAB Desktop (i.e. the MATLAB command window and the other MATLAB windows) and how to use it proficiently;
% How to get help within MATLAB;
% How to create or open a file in the MATLAB editor.
%
%
%% Starting MATLAB
%
%% Starting MATLAB on the PCs in the Computer Lab "Aula Nettuno" - C1 Building
%
% MATLAB is installed on the computers in the Computer Labs of the Dept. of Architecture & Engineering at the University of Trieste. Moreover, the University of Trieste has subscribed to a MATLAB TAH (Total Academic Headcount) license that allows students (as well as professors and university staff) to download the software and install it on their PCs. For more details regarding the available products and how to download and activate the software, please visit the dedicated MathWorks web portal.
%
%% Starting MATLAB on your own PC or Laptop
% The MATLAB logo is similar to the image, shown below, of an L-shaped membrane
%
% MATLAB can be started on an MS Windows-based computer by clicking on the MATLAB logo icon in the Start menu or double-clicking the MATLAB logo on the desktop (if the MATLAB icon is on the desktop).
% Similarly, MATLAB can be started on a Linux-based computer by clicking the MATLAB logo icon in the Application menu or simply by typing matlab and pressing the "return/enter" key in a Terminal window.
% Finally, on a Apple Mac, you may start MATLAB by clicking the MATLAB logo icon in the Dock, or in the Applications.
%
%% MATLAB Online on your own PC or Laptop
%
% This is the preferred mode of use during the course exercises.
% Features of using MATLAB Online :
% You have access to the latest version of MATLAB with all the functionality available thanks to the license in use.
% You can use MATLAB in your web browser without any downloading, installation or maintenance.
% You can store MATLAB files (scripts, functions, live scripts, data files) online in the cloud, using the MATLAB Drive.
% To start using MATLAB Online, go to https://matlab.mathworks.com/ , sign in with your Mathworks account
%
%
% and then click the Open MATLAB Online button:
%
%
%
%% The MATLAB Interface - MATLAB Online
%
% When you start MATLAB for the first time (both MATLAB online or using your own PC/laptop) you should see the following group of windows:
%
% The most important window is the Command Window (the biggest one), in which you type MATLAB commands and see the results of the commands.
% There are also the Files and Workspace panels.
% What are these windows for?
% Specifically:
% Command Window: this is the MATLAB command interpreter window. Commands can be directly written there, or programs in MATLAB programming language can be executed.
% Files: it displays the contents of the work folder in graphic form.
% Workspace: this window displays the contents of the memory area used by MATLAB (names, types and sizes of variables).
% Other useful panels (hidden in the default layout)
% Command History is the window that contains the list of commands entered in the Command Window.
% Debugger: is the panel that allows you to handle error or warning conditions, inserting proper breakpoints and exploring data and code.
% You may modify the MATLAB user interface layout, using the "Layout " menu
%
%
% Moreover, you can customize your settings using the Preferences menu in the Environment section of the Home tab. Initially it is simplest to use MATLAB default settings. When you have decided what is the most effective way for you to work, you may close some or all of these windows. However you will always need a  Command Window.
%
%
%% Managing Folders and Uploading Files using MATLAB Online
%
%% How to create a new folder in MATLAB Online
% Use folders to keep your programs and data organized.
% To create a "new folder" using MATLAB Online, use the following procedure:
% right-click on the File Panel: in the pop-up menu select "New -> Folder " and change the proposed folder name "New Folder" in whatever you prefer. Do not use spaces in folder names!
% Suggestion: use the name "class-034IN" for the folder where you will put all the MATLAB-related material of the 034IN course.
%%
%%
%% How to upload files and folder in MATLAB Online
%
%
%
%
%% A First Example
%
% To see how MATLAB works, please type the following two lines in the MATLAB Command Window, with Enter after each command.
% x = 2
% x = x/2 + 1/x
% The first command assigns the value 2 to the variable x.
% MATLAB is an interpreted programming language. This means that each command is evaluated when you type it into the Command Window and press the Enter key.
% Note: the execution of the assignment command x=2 results in the creation of a variable named x in the Workspace, and this variable takes the value 2 (check it in the screenshot below). The result of the command (i.e. x takes the value 2) is displayed in the Command Window (it is the so-called '' command echo '').
%
% The second instruction uses the value stored in the variable x, evaluates the expression on the right-hand side of the =, and then stores the new value in the variable x.
% After entering each command, the result of the command is shown in the Command Window.
%
%
% Note: after the execution of the second assignment, the value of the variable x has been updated (check the Workspace Panel). Moreover, the Command History Panel contains the list of the command you typed in the Command Window, in chronological order.
%
%
%% The MATLAB Programming Language
%
% MATLAB is an interpreted programming language. This means that every command is evaluated when you type it into the Command Window and press the Enter key. It is possible to create sequences of commands and save them in a text file (a script) or create more complex programs using functions and also classes. Even when you execute a MATLAB script or function, the code is interpreted.
% This has the great advantage of allowing interaction with MATLAB.
% There are several ways to return to a previous command, edit it to correct any errors and then execute it again, when typing the code directly in the Command Window. The easiest way to do this is to use the arrow keys on the keyboard: the up arrow allows you to return to the previous command (pressing several times can go back several commands), while the left and right arrow keys position the cursor within the command and the Backspace or Delete keys remove a character that can be typed again. Pressing the Enter key executes the edited command.
% If you type a few characters from the the beginning of a command, for example x =, and then press the up arrow key, you go back to the most recent command that started with these characters. Alternatively you can find the command you want in the Command History sub-window, and double click on it to execute it again. You may also copy commands from the Command History sub-window using the menu opened when you right-click on them.
%% Example: Using the Arrow Keys in the Command Window
% Use the arrow keys to go back to the previous command and execute it again, several times
% x = x/2+ 1/x
% Check that every time you execute the command (i.e., every time you press the Enter key after recalling the command using the arrow key), the command itself has been stored in the Command History command list.
%
%
%% Getting Help
%
% There are several ways of getting help within MATLAB. It is essential  you become familiar with both the MATLAB help  command and the MATLAB Help browser which has extensive documentation. MATLAB also contains a number of demonstration videos that can help you  become familiar with it.
%
%% Help Command
% The quick way to get information about a MATLAB command is to type
% help command_name
% in the MATLAB Command Window. This may produce more information that will fit in one screen so you will have to scroll back to the beginning of the output.
%% Examples
% Use the help command to find out more information about the help command

help help 

%
%
% Use the help command to find out information about a MATLAB toolbox (a collection of MATLAB functions and apps, with focus on a particular topic, as Signal Processing, Control Systems,  Statistics etc.):

help control

%
%% With a Little Help from the 'help' Command!
% Here is a list of the main categories of topics available using the help command
% General purpose commands

help general

% Operators and special characters

help ops

% Programming language constructs

help lang

% Elementary matrices and matrix manipulation

help elmat

% Elementary math functions

help elfun

% Specialized math functions

help specfun

% Matrix functions - numerical linear algebra

help matfun

% Interpolation and polynomials

help polyfun

% Function functions and ODE solvers

help funfun

% Sparse matrices

help sparfun

% Two dimensional graphs

help graph2d

% Three dimensional graphs

help graph3d

% Character strings

help strfun

% File input/output

help iofun

% Time and dates

help timefun

% Control Systems Toolbox

help control

% Symbolic Math Toolbox

help symbolic

% Simulink

help simulink

%
%% The Help Browser
% More extensive information about MATLAB and its commands is available through the Help Browser using a web based interface. This can be accessed in a number of ways
% You can left-click on the ? symbol in the small circle in the top right-hand corner of the Live Editor window or the main MATLAB window. This will open a new window like the one below
%
% If you are looking for more specific information on a particular MATLAB command or toolbox, the doc command is the most efficient way to browse the MATLAB documentation:

doc sqrt

%
%% MATLAB Scripts and Interactive Live Scripts
%
% A MATLAB script is simply a plain text file containing a sequence of MATLAB commands. It is called also an M-file, because the file has extension .m
% Using M-files allows you to
% store command for later use;
% build up larger and more complicated programs;
% use the MATLAB editor to debug and then optimize your program.
% An M-file is executed (it means all the commands within the M-file are executed) simply by typing the name of the file (without the .m extension) in the Command Window.
%
%
%% The MATLAB Editor
% MATLAB provides an editor, which allows you to easily create and edit M-files. Beside the basic editing functionalities, the MATLAB Editor has some features useful specifically for writing programs:
%
%
% Syntax highlighting:
% Comments (anything in a text line after the character %) are colored green. It is very important to use comments to document your M-files, just as MATLAB does.
% MATLAB keywords are colored blue. You should not try to change a keyword.
% Smart indentation can make your program structure clearer.
% Debugging:
% Breakpoints can be set on any line of an M-file starting with code (thus, no breakpoints are allowed on lines starting with a comment). As usual in any programming language IDE, using breakpoints allows you to check the values of your variables and more easily find any mistakes.
% Profiling can help you identify where your program spends most of its time, allowing you to produce more efficient code. More details about how to use the profiler here. No more details about profiling will be provided during this course.
%% MATLAB Script: an Example
% The following command opens the existing M-file named simple_Mscript_example.m in the MATLAB Editor window

edit simple_Mscript_example

%
%  Do not worry about the meaning of the MATLAB commands for now, but you should try executing the M-file by typing simple_Mscript_example in the MATLAB Command Window.
%%
%
%% MATLAB Interactive Live Scripts
%
% MATLAB live scripts are interactive documents that combine MATLAB code with formatted text, equations and images in a single environment. In addition, live scripts store and display the output together with the code that creates it (from the MATLAB documentation). More details and examples of live scripts are available in the MATLAB documentation.
%
% The actual document is a MATLAB Live Script.
%
%% MATLAB Live Script: an Example
% This is what the live script that corresponds to the M-file seen above looks like

open simple_LiveScript_example

%
%
%% The First Code Example in an Interactive Environment: a MATLAB Live Script
%
% If not already done, open this document (the file named Intro_MATLAB_034IN_FAC_p1.mlx) directly using MATLAB. Put the mouse cursor in this document section and select the command "Run Section" from the Live Editor tab or, alternatively, click the vertical striped bar to the left of the code

x = 2
x = x/2+ 1/x 

%
% Executing this portion of code within the live script produces the same result, displayed in the same way, as code executed by entering commands directly in the Command Window.
%
%% Example: Repeating a Command in a Live Script
%
% Neither the up arrow key nor the Command History command list approach works when using a live script. If you want to repeat a command (or a piece of code) in a live script, you should use the following approach (by means of a so called Control Button - more details in the MATLAB documentation on Live Scripts and Interactive Controls):
% We have already defined the variable x in this live script, assigning it an initial value and then performing a simple calculation that assigns the result back to the variable x.
% Now, let's run the following piece of code clicking on the "Run Again" button more than once. What happens?

 

disp(x)

x = x/2 + 1/x


%%
%
%
%
%
%% Summary
%
% Using this live script, you have:
% seen how to start MATLAB and its various windows;
% learned how to type a command in the MATLAB command window;
% learned how to use the arrow keys to go back to a previous command and run it again;
% learned how to get help within MATLAB using the help command, and how to access the MATLAB help browser using the doc command;
% learned that M-files are just text files with an extension .m containing MATLAB commands;
% learned that M-files are executed by typing the name of the file (without the .m extension) in the MATLAB command window;
% learned how to open the MATLAB Editor and open and save an M-file;
% learned the difference between a simple M-file and a MATLAB Live Script;
% learned how to open the MATLAB Live Editor and open and save a Live Script.
%
%% Back to the Index
% Use this link to go back to the main live script of the collection.
%
%% Go to the Next Part: Numbers, Arithmetic Operations, Formats and Variables
% Use this link to go to the next live script (Part 2) of the collection.
%
%
%
%
%
%