MATLAB Onramp: Report Professor: Dr. Rajesh Kandala – NVPS SENSE Lab slot: L55+L56; CB-101 Student Name: Dev Kumar Reg. No.: 22BCE9115 What is MATLAB? MATLAB is a programming and numeric computing platform that is used by millions of engineers and scientists to analyse data, develop algorithms, and create models MATLAB stands for MATrix LABoratory. A visual representation of how a MATLAB editor screen looks. Entering Commands in MATLAB: In MATLAB, commands can be executed by typing them in the command window after the prompt “>>” and pressing enter key. Unless a variable is specified, The output is stored under the variable named “ans”. When you enter a command without semicolon in MATLAB, The result is displayed, Optionally you can add a semicolon at the end of a command so that the value is stored but the result is not displayed. For example: K = 8 – 2; Here the output won’t be displayed. Previous commands can be recalled using the “up arrow” - ⬆️ on the keyboard. When you enter just a variable name in the command prompt, MATLAB displays the current value of that variable. Variables: In MATLAB, variables can be named anything as long as they start with a letter and contains only letters, numbers and underscores. All the variables get saved in the column on the righthand side of the screen called the workspace. In order to save all those variables a mat file can be created with the following command and format: >>save <filename>.mat For example: >>save datafile.mat This command saves all the variables in the workspace in a file named “datafile.mat”. To clear the workspace or delete all the variables from the work space: >>clear The “clear” command gets rid of all the variables stored in the workspace. To load our saved “.mat” file with all the variables in it the command: >>load <filename>.mat The command “load <filename>.mat”, loads all the variables stored in the file into our workspace which can be then used in our program. For example: >>load datafile.mat The above command loads the variables stored in the file named datafile. If the file is in a different directory than our MATLAB program then we need to need to mention its name along with its path. Finally, to clear our command window: >>clc The “clc” command clears out the command window. Using built in functions: MATLAB consists of many built in functions and constants which can be used to make programs simple and easy to code. For example: Built-in constant Here “pi” contains the value of mathematical function pie, where pie = 3.14 or 22/7. Built-in function Here the function “sin()” is use to find the sine function of -5 which is then stored under the variable “a”. The MATLAB editor: MATLAB web editor. The left grey side of the editor is the part of the program where we write the code also called the script, the right panel beside the script is where the output is displayed. The bottom window is the command window as it states. Lastly on the right pane there is a toggle saying “workspace”. This toggle opens a slide tab of the workspace. Vectors and Matrices: Arrays: All MATLAB variables are arrays So, each numeric variable can contain multiple numbers. You can store related data in one variable by using an array. Creating 1-D arrays: A single number called a scalar can be represented by a 1 x 1 array meaning, the array contains 1 row and 1 column. x = 4; “x” is a 1 x 1 array. (row x column) To store more than 1 columns, square braces can be used to accommodate the extra elements. (row vector) y = [7 9]; The spaces used here are used to separate the numbers in their row, a comma(“,”) can be used to do the same. Creating multi-Dimensional arrays: When you separate the numbers in an array using a semicolon, MATLAB creates a new column vector. z = [1; 3] z= 1 3 This array is of an order 2 x 1. (column vector) Following is a representation of how a matrix would be constructed in MATLAB: For long vectors, entering individual numbers is not practical. An alternative, short hand method to carry out the same would be using the colon operator, specifying the start and end values. For such conditions using the square braces is not necessary. For example: >>g = 5:8 g= 5678 The “:” operator uses a default spacing of 1, However u can alter it by providing the required parameter as shown below. <variable> = <initial>:<increment>:<final> Syntax This particular instance uses a spacing of 2 units. If you know the number of elements you want in a vector (instead of the spacing between each element), you can use the “linspace()” function. >>linspace(<first>,<last>,<number of elements>) Syntax To convert a row vector into a column vector, there exists a transpose operator (‘). The transpose vector can be used directly to create column vectors from row vectors. Similarly, the vice versa is possible. Creating arrays with functions: There are many functions in MATLAB which can be used to generate different types of matrices. For example: • rand(n) :- The function rand(n), generates a matrix of order n x n. • rand(n, m) :- The function rand(n, m), generates a matrix of order n x m. • ones(m, n) :- The function ones(m,n), generates a matrix of ones of the order of m x n. And many more such functions exist. Array indexing and modification: The data from an array can be extracted using row and column indexing. <variable name> = <array>(<row>,<column>) Syntax For example: Y = A (5,6); In this command, the values of 5th row and 6th column of the array “A” is stored in “Y”. To extract only the row or a column, we can just blank out the unnecessary parameter. <variable name> = <array>(<row>, ) Syntax The above syntax fetches only the required row of the specific array.