You are on page 1of 28

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 1

Lab Title: Introduction to MATLAB

Student Name: MUHAMMAD AHMED Reg. No: 190319


Objective:THE OBJECTIVE OF THIS LAB IS TO GET US FAMILIAR WITH BASICS OF
MATLAB.
LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes (5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:


EXPERIMENT 01

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.

The MATLAB Windows:


 Command Window:
When you begin a session of MATLAB, the first window displayed on the monitor is the
command window with the prompt >>, which is waiting for your command. When the command
window is inactive a blinking cursor should appear to the right of the prompt. Use the command
exit to end the session.
 The MATLAB Workspace:
If a semicolon is entered at the end of an array or a command, then the array and the output of the
command is not displayed on the command window, but the command as well as the output
variables are saved in a buffer known as the workspace. The workspace saves the variables, data,
contents of arrays and matrices, and other elements as well as a record of the commands typed by
the user.
 Figure Window:
All the plots generated by the plotting commands appear in a figure window.
 Editor Window:
Editor window allows you to place MATLAB commands in a simple text file & then tell
MATLAB to open the file & evaluate commands exactly as it would if you had typed them at the
MATLAB prompt. This window is used to create & edit MATLAB (.m type) executable files.

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.

Getting Started With MATLAB:


 Vectors, Arrays & Matrices:
Vectors and scalars are special cases of a matrix—all of which are represented as arrays in
MATLAB. A scalar is an array of 1 × 1 dimension, whereas a row vector is an array of 1 × n
dimension, and a column vector is an array of n × 1 dimension.
When elements of an array are typed in a row within square brackets, with a space between the
elements, MATLAB displays it as a row vector. For example, when you type:

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.

 Multiplication & Division:


When a scalar, vector, or matrix is multiplied (or divided) by a scalar c, every element of the
scalar, vector or matrix is multiplied (or divided) by c. When c is added to matrix, it is added to
every element of the vector or matrix.
 Addition & Subtraction:
Addition and subtraction of two matrices (and vectors) is carried out by MATLAB, according to
the rules of matrix algebra, when they have the same dimension. Multiplication of a vector or
matrix by a vector or matrix is carried out according to the rules of algebra when they are
compatible for multiplication.
The matrix operations and their corresponding notations available in MATLAB are given below:
 Addition +
 Subtraction -
 Multiplication *
 Power or exponent ^
 Transpose ’
 Left division \
 Right division /
Note that the command x=M\b gives us the solution to the equation M*x=b, where M is a square
matrix that is assumed to be non-singular. In matrix algebra, the solution is given by x = M -1b.
The left division is a more commonly used operation in application of matrix algebra. (The
command for the right division x=b/M gives the solution to the equation x*M=b, assuming that x
and M are compatible for multiplication and the solution in matrix algebra is given by x=bM -1.)
Whereas the addition and subtraction of matrices are carried out by the addition and subtraction
of corresponding positions of the elements. MATLAB gives the correct answer in all the
preceding operations.
 Matrix Multiplication:
C = A*B is the linear algebraic product of the matrices A and B.

The number of columns of A must equal the number of rows of B.


 Dot Operator:
When a dot is used before the sign for the mathematical operation between the two matrices, it
performs that matrix operator element wise on the corresponding matrix. The multiplication (.*),
division (./), and exponentiation (.^) of the terms in the corresponding positions of the two
compatible matrices is achieved by using the dot operator. For example:

 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:

To create a regularly-spaced vector x using i as the increment between elements, type:

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:

The command to return the whole matrix is:

 Deleting Rows and Columns:


If you delete a single element from a matrix, the result is not a matrix anymore. So, expressions
like X(1,2) = [ ] result in an error.

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,

 Specific Matrix Operations:


There are many other operations that can be applied on a matrix, such as A, as listed below:
Rotating a Matrix:
The command rot90(A) rotates the matrix array by 90. For example:

Flipping a Matrix’s columns:


The command fliplr(A) flips the columns left to right. For example:

Flipping a Matrix’s rows:


The command flipud(A) flips the rows up to down. For example:

Extracting Upper Triangle Part of Matrix:


The command triu(A) gives the upper triangular part of the matrix. For example:

Extracting Lower Triangle Part of Matrix:


The command tril(A) gives the lower triangular part of the matrix. For example:

 Some Special Matrices:


There are few special matrices; three of them which are often found useful in manipulating
matrices are listed below:
Generating a Ones Matrix:
The command ones(m,n) gives a matrix with the number one in all its m rows and n columns.

Generating a Zeros Matrix:


The command zeros(m,n) gives a matrix with zeros in all its m rows and n columns.

Generating an Identity Matrix of Order “m”:


The command eye(m) gives the “identity matrix” of order m × m.

 Building Tables in MATLAB:


Array operations are useful for building tables. Suppose n is the column vector:

Then, to build a table of squares and powers of 2, type:

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

3. Let x = [4 3 9 1] and y = [2 1 9 -3]


a. Add the sum of the elements of x to y
b. Raise each element of y to the power specified by the corresponding element of x.
c. Multiply each element in x by the corresponding element in y and store the result in ‘c’.
d. Evaluate y*x’ and interpret the result.

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

1.6158 + 0.0000i 1.9459 + 0.0000i

>> 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

7. Given a matrix A, such that:


Perform the following operations:
a. Extract the 3rd column of matrix A and store it in vector B.
b. Extract the 1st and 3rd columns of matrix A and store them in matrix C.
c. Add the 1st and 3rd rows of matrix A together and store the result in vector D.
d. Change the value in the 2nd row and 3rd column of A to 7 (instead of +7) and call the result
AA (do not destroy/change the original A matrix).
e. Create a matrix that contains rows 1 and 3 from A, the second row of AA, and the result of
part (c).

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.

You might also like