Lab Session 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

LAB SESSION 1

Introduction to MATLAB
OBJECTIVE:

This lab session is about an introduction to MATLAB.

INTRODUCTION:

MATLAB stands for matrix laboratory and it is a fourth-generation high level programming language. By
high level language we mean that the syntax of this language is easily understandable to human beings
and non-comprehensible to computers.

APPLICATIONS:

MATLAB is very efficient when it comes to manipulation of matrices, creating visualization of data and
other mathematical expressions in the form of plots, implementation of algorithms for the numerical
solution of complex problems pertaining to the field of engineering and sciences. Following are some of
the areas where MATLAB is used extensively:

Signal Processing and Communications


Image and Video Processing
Control Systems
Test and Measurement
Computational Finance

MATLAB Environment
Current Folder
This shows the files present in the current working directory.

Command Window
In command window user can enter the statements one at a time or a set of statements by pressing
SHIFT+ENTER, after the set of statements is written and ENTER key is pressed, the output of those
statements or single statement is displayed in the command window.

Work Space
All the variables which are present in the memory are displayed in workspace.
Command History
This panel shows the commands which were executed in the command history, user can access the
command history by pressing the UP-ARROW key or by adding the command history panel in the layout.
Note that command history panel is not available in default MATLAB layout.

Getting Help
For getting help about any function the command help can be used along with the function name to
know about its syntax in MATLAB.

Setting Working Environment


This layout can be changed according to the user requirements, the MATLAB screen of my desktop is as
follows. Colors have been changed from Preferences button on the home tab and the layout has been
set by dragging the panels here and there.

Output Format
There are different ways in which output can be displayed on the screen. The default output format is
short with loose spacing. There are different output formats according to the type of formatting
involved.

LAB SESSION 2
Simple Operations Related to Scalar, Vector and Matrices in MATLAB

Variable:
A variable in MATLAB is a name that is a combination of letter and numbers and numerical data can be
assigned to it. Variables can be an array or a matrix. Variables are used extensively to solve and
manipulate data. To define variables, there are some rules that must be following:

• Name of variables must begin with a letter.

• Maximum length of variable is 63 characters.

• Name of variables can contain letters, numbers and underscore.

MATLAB is case sensitive, it means variable "x" and "X" are two different variables.

Pre-defined constants cannot be used as variable names.

Assignment operator:
Variables can be assigned by using the assignment operator i.e =. It is used to assign numerical value or
results of an expression to a variable.

Constants:
A constant is something whose value does not change. It is usually a number. For example, in statement
k = 5, k is the variable and 5 is the constant.

COMMENTS:

Comments are used in MATLAB for following purposes:

• To describe the code when multiple persons are working on the same file.

• To make sure that a particular section of code is not executed by MATLAB.

Operators:

Operators are used to perform different operations on two variables.

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.

Operations in MATLAB work on both scalar and non-scalar data.

SCALAR QUANTITIES:

Scalar in MATLAB refers to a 1x1 matrix, that is a matrix with a single column is treated as a scalar. Scalar
operations include addition, subtraction, multiplication, division, exponentiation and nth root.

LAB SESSION 3
Operations related to vector and matrices in MATLAB

1.1.1 Addition and Subtraction


First, we define 2 row vectors and store them in A and B. The addition of these two row vectors is
performed and result is stored in C. The elements of B are subtracted from corresponding elements of A
and the result is stored in D. As each statement is terminated by a semi colon, nothing would be
displayed. For displaying the result of addition and subtraction, variables C and D are displayed by using
disp command.

1.1.2 Transpose Operation


Firstly, a row vector is defined and stored in r, its transpose is taken by using transpose operator ' and
assigned to variable tr. Then a column vector v is defined and its transpose is taken and assigned to
variable tv. For displaying the output disp command is used.

1.1.3 Dot Product


Firstly, we defined two row vectors representing the co-ordinates of a vector in rectangular co-ordinates.
Then their dot product is calculated using the built-in dot function and stored to variable dp. Then disp
command is used to display the dot product along with an explanatory text line.

1.1.4 Deleting Matrix Elements


A 4 by 5 matrix namely a has been defined and then array indexing has been used to delete its elements.
For deleting the 4th row of the array a, the elements of that row have been set to [], this empty array
deletes the elements. For deleting the 5th column of the array, the following command is used.

1.2 Matrix Indexing


Firstly a 3 by 5 matrix is defined and stored in a. Then the elements of a are accessed by using the array
indexing. In command v = a(:,4), all elements of 4th column of a are accessed and stored in v.

You might also like