Professional Documents
Culture Documents
EXPERIMENT NO 1
Data presentation
Experimental results
Conclusion
Introduction to MATLAB
Objectives:
Familiarization with MATLAB
Equipment required:
MATLAB installed on PCs
MATLAB:
MATLAB is a very powerful and well-known software package that is used in science and engineering
disciplines, for numerical computation, data analysis, and graphical visualization. It is available in almost
all platforms such as personal computers, and workstations running under several operating systems.
MATLAB contains a large collection of built-in functions and commands that are used in an interactive
mode, when you are in the command window. As soon as the name of a function or a command is typed
at the prompt in the command window, with the proper syntax, the answer is displayed immediately. But
there are two other windows, the edit window and graphics window, which will be discussed later.
The software package is designed to use additional sets of functions that are more applicable disciplines
such as control systems, digital signal processing, communications engineering, and image processing.
There are more than 20 sets known as “toolboxes” (e.g., control toolbox, digital signal processing
toolbox, communication toolbox, and image processing toolbox). All of them run under MATLAB and
implement the functions based on matrix manipulation of numerical data, and that is why the software is
called MATLAB (matrix laboratory).
MATLAB allows us to construct new functions using the enormous number of built-in functions,
commands, and operations in MATLAB and in the many toolboxes, without having to know how to
compile, link, load, and create executable code, because MATLAB uses its own language to carry out all
these steps, which are invisible to the user. It carries out the steps and gives the answer very fast.
Commands:
The “save” command:
This command allows the saving of the variables in the workspace before closing the session. It
saves all the variables in a file called filename.m.
clc command:
It clears the command window during a work session.
clear command:
It deletes all the variables in the workspace. All variables are cleared & cannot be retrieved.
help command:
It is the simplest way to determine the syntax & behaviour of a function. Information is displayed
directly in the Command Window. (e.g; try help whos)
Important Points:
MATLAB is case sensitive i.e., time, Time, TIME are three different variables.
To recall previous commands MATLAB uses the up & down arrow keys.
All text after percent sign (%) is taken as a comment statement.
Multiple commands can be placed on one line if they are separated by commas (,) or semicolons
(;).
Commas tell MATLAB to display results whereas semicolons suppress printing.
If a statement does not fit on one-line use three periods, …, followed by Enter to indicate that the
statement continues in the next line.
The maximum number of characters allowed on a single line is 4096.
You can interrupt MATLAB execution at any time by pressing CTRL + c.
If the array is typed without assigning a name for the array, MATLAB creates a variable ans and
responds with:
When you type elements with a semicolon between them, the elements are displayed in the form
of a matrix, for example:
A column vector can be created by typing the elements with a semi column separating them or
creating a row vector and transposing it.
Transpose of a Matrix:
In MATLAB, the transpose of a matrix or a vector is carried out by the operator “’” that is, the
command x’ gives the transpose of the vector or matrix x. Since the vectors and matrices listed
and described above have been saved in the workspace.
Element-wise Multiplication:
To achieve element by element multiplication, dot operator is used. A.*B is the element-by
element product of A and B.
Concatenation:
A matrix can be expanded by adding new matrices, column or row vectors as illustrated by the
following examples:
The division operator ./ can be used to divide a scalar by each of the matrix element as shown
below, provided there are no zeros in the matrix:
The element in the (i,j) position of a matrix G is identified by typing:
The value of this element can be changed as:
Colon Operator:
The colon is one of the most useful operators in MATLAB. It can create vectors, subscript arrays,
and specify for iterations.
x = j:k creates a unit-spaced vector x with elements [j,j+1,...,k]. For example:
The colon sign “:” can be used to extract a sub-matrix from a matrix as shown by the following
examples:
To extract a sub-matrix with elements in all rows and the second column only, type:
To extract the elements in all columns and the third row only, type:
To extract the elements in the rows from 1 to 2 and in the columns from 2 to 3, type:
To delete rows and columns from a matrix, use empty square brackets. For example:
Using a single subscript deletes a single element, or sequence of elements, and reshapes the
remaining elements into a row vector. So,
Lab Tasks:
Write the codes for the following tasks. Paste the codes and their results in the space provided.
1. Create vectors:
a. ‘A’ of even whole numbers between 19 and 63.
b. ‘B’ of odd whole numbers between 66 and 120.
Task 1
Input
Output
Task 2
Input
Output
2. Let x = [3 -2 0 5 1 9 5 6]
a. Add 5 to each element of this vector.
b. Multiply the even indexed elements with 4.
c. Compute the square root of each element.
d. Compute the cube of each element.
Task 1
Input
Output
Task 2
Input
Output
Task 3
Input
Output
Task 4
Input
Output
Task 1
Input
Output
Task 2
Input
Output
Task 3
Input
Output
Task 4
Input
Output
4. Given a vector t = [-3:0.2:2] write down the MATLAB expressions that will correctly compute
the following:
a. ln (1−t +t¿¿ 3)¿
b. exp [1+sin (3∗t¿¿ 2)]¿
c. cos (t)2 +sin (2∗t )−3
Task 1
Input
Output
t=
Columns 1 through 16
-3.0000 -2.8000 -2.6000 -2.4000 -2.2000 -2.0000 -1.8000 -1.6000 -1.4000 -1.2000 -
1.0000 -0.8000 -0.6000 -0.4000 -0.2000 0
Columns 17 through 26
0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
ans =
Columns 1 through 8
3.1355 + 3.1416i 2.8988 + 3.1416i 2.6373 + 3.1416i 2.3441 + 3.1416i 2.0079 + 3.1416i 1.6094
+ 3.1416i 1.1092 + 3.1416i 0.4028 + 3.1416i
Columns 9 through 16
-1.0671 + 3.1416i -0.7508 + 0.0000i 0.0000 + 0.0000i 0.2531 + 0.0000i 0.3250 + 0.0000i
0.2897 + 0.0000i 0.1756 + 0.0000i 0.0000 + 0.0000i
Columns 17 through 24
-0.2132 + 0.0000i -0.4095 + 0.0000i -0.4845 + 0.0000i -0.3397 + 0.0000i 0.0000 + 0.0000i
0.4240 + 0.0000i 0.8519 + 0.0000i 1.2516 + 0.0000i
Columns 25 through 26
>> SSS
Task 2
Input
Output
Task 3
Input
Output
5. Find a short MATLAB expression to build to following matrix:
Input
Output
6. Give a MATLAB expression that uses only a single matrix multiplication with Matrix B
(obtained in problem 5) to obtain:
a. The difference of columns 2 and 3 of Matrix B
b. The last row of B
c. A version of B with rows 2 and 3 swapped
Task 1
Input
Output
Task 2
Input
Output
Task 1
Input
Output
Task 2
Input
Output
Task 3
Input
Output
Task 4
Input
Output
Task 5
Input
Output
Conclusion:
In this lab, we leart about basic of matlab and how to use a matlab and its initial stage and
apply basic rules like addition,subtraction etc. In this lab I performed basic commands to
complete my tasks.