You are on page 1of 12

Laboratory Manual

Communication Systems
Software Labs
B.Sc. Electrical Engineering
Submitted by

Submitted to

Department Of Electrical Engineering


University College of Engineering & Technology
University of Sargodha

1
Lab Contents
Lab Title Page #
1
2
3
4

2
Lab Session 01
Introduction to MATLAB

Objective:

In this lab students will get familiar with some useful features of Matlab.

MATLAB ENVIRONMENT:
Matlab environement consists of following main parts:
 Command Window
 Command History
 Workspace
 Current Folder
 Editor

Fig 1.1
Command Window:
Command Window is main window in MATLAB. Use Command window to enter variables and to
run functions and M-File scripts. You type all your commands after command prompt “>>”.
Defining Variables:
Variables are assigned numerical values by typing expression directly in the command window, for
example typing:
>>a=1+2
a=
3
>>

The answer will not be displayed when a semicolon is put at end of an expression.

3
>>a=1+2;
>>

This is very useful, because sometimes you want Matlab to respond,while in other situation that is not
necessary.
The Command “clear” will clear all variables in your workspace.

Command History:
Statements you entered in command window are logged in the Command History. From Command
History you can search and view previously run statements, as well as copy and execute selected
statements. You can also create an M-File from selected statements.

Fig 1.2
Workspace:
The Workspace window list all your variables used as long as you have MATLAB opened.

4
Fig 1.3
Save your data:
You may also save all your data and variables to a text file (.mat file), this is useful if you want to
save your data and use it for later.
Current Folder:
The “Current Folder” list all m files, etc. available in current directory.

Fig 1.4

Editor:
The editor is used to create scripts and m-files. Click “New Script” button in tool-bars.

5
M-Files:
M files are macros of MATLAB commands that are stored as ordinary text files with the extension
“.m”, that is “filename.m”. An M-file can be either a function with input and output variables or a list
of commands. MATLAB requires that the M-file must be stored either in working directory or in
directory that is specified in MATLAB path list.

Theory:
Matlab (matrix laboratory) is a fourth-generation high-level programming language and interactive
environment for numerical computation, visualization and programming.
Matlab is developed by Math works.
It allows matrix manipulations; plotting of functions and data; implementation of algorithms; creation
of user interfaces; interfacing with programs written in other languages, including C, C++, Java, and
Fortran; analyze data; develop algorithms; and create models and applications.
Matlab is case sensitive. Command X and x are not same.
Matlab Help:
Matlab provides extensive help for using MATLAB commands and features, as well as introduction
to several theoretical concepts. In order to access this help, try one of the following commands.
 Help
 Help pi
 Help sin
By typing “help” in command window following window appears.

Fig 1.5
Matlab answers with links to lots of Help topics.
Matlab allows following arithematic expression:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power operator
’ Transpose
\ Left division
/ Right division

Using above commands Matlab can be used as calculator.

6
A variable can be assigned a formula that utilizes these operators with numbers or previously defined
variables. An example of above operations on values of previously defined quantity is given below:

>>b=2*a;
>>b
b=
6
If your expression does not fit on one line, use an ellipsis (three or more periods at end of line.and
continue on next line.
>>c=1+2+3+4+…
5+6+7;
>>c
c=
28

There are several predefined variables which can be used at any time, in the same manner as
user-defined variables:

i Sqrt(-1)
j Sqrt(-1)
pi 3.1416

For example,
>>y=2*(1+4j)
y=2.0000+8.0000j

There are also a number of predefined functions that can be used when defining a variable. Some
common functions that can be used in this text are:
abs Magnitude of a number
angle Angle of complex number in radians
cos Cosine function,assumes argument is in radians
sin Sine function, assumes argument is ia radians
exp Exponential function

For example, with y defined as above,

>>c=angle(y)
c=
1.3258
>>c=abs(y)
c=
8.2462
>>c=cos(a)
c=
-0.9900
>>c=exp(a)
c=
20.0855
>>c=exp(y) %Note ‘exp’ can be used on
complex numbers
c=
-1.0751+7.3104i

7
Generating the Plots:
A list of useful commands is given below:
 plot,stem,subplot,hold
 xlabel,ylabel,title,grid,axis,axes
The command most often used is plot, which creates linear plot of vectors and matrices. There are
options on the line type and color of the plot which are obtained using plot(t,y,’option’) . The line
types options are ‘-’solid line (default), ‘--’ dashed line, ‘-.’dash dot line, ‘:’dotted line. The point in y
can be left unconnected and delineated by a variety of symbols: +.*o x. The following colors are
available: r,g,b,k,y,m etc. For example, plot(t,y, ‘--’) used a dashed line, plot(t,y,‘g’) makes a solid
green line. The options can also be used together, for example, plot(t,y,‘g:’) plots dotted green line.
To plot two or more graphs on the same set of axes, use the command: plot(t1,y1,t2,y2), which plots
y1 versus t1 and y2 versus t2. To label your axes and give the plot a title:
>>xlabel(‘time(sec)’)
>>ylabel(‘stepresponse’)
>>title(‘myplot’)
Add a grid to your plot make it easier to read by using the command grid. The problem that you will
encounter most often when plotting functions is that MATLAB will scale the axes in a way that is
different from how you want them to appear. You can easily override the auto scaling of axes by
using axis command after plotting command axis([xmin xmax ymin ymax]) , where xmin,
xmax,ymin,ymax numbers corresponding to limits you desire for axes. To return to the automatic
scaling, simply type axis. For discrete-time signals, use the command stem which plots each point
with a small open circle and straight line. To plot y(k) versus k ,type stem (k,y). You can use
stem(k,y,‘filled’) to get circles that are filled in. To plot more than one graph on the screen, use
command subplot (m,n,p) which partitions the screen into an m*n grid where p determines the
position of particular graph counting upper left corner as p=1.Hold on command is also use for
holding the value.
Different Commands on Matlab:

>>x=-pi:0.1:pi;
>>y=sin(x);
>>z=cos(x);
>>plot(x,y)

PLOT:

8
>>hold on;
>>plot(x,z);

PLOT:

Task 01:
Using MATLAB as a Calculator:
As an example of a simple interactive calculation, just type expression you want to evaluate. Let’s
start at the very beginning . For example, in order to calculate the following expression
1+2*3, one need to type it at the prompt command (>>) as follows;
>>1+2*3
ans=
7
Lab Tasks:
Try the following and record your observations:
>>3+7.5
>>18/4
>>3*7
>>3^2
Observations:

9
TASK 02:
Use subplot command to provide following three results in one figure:
 Plot 1:sin(t),sin(3t),sin(5t)
 Stem 2 :cos(t).cos(3t),cos(5t)

 Plot 3:tan(t),tan(3t),tan(5t)

For t € [-1,1]. It is important to use different color schemes to differentiate harmonics,provide


different symbols, xlabel,ylabel,and title for each plot. Paste Code as well as graph of above tasks.

10
11
Conclusion and Comments:

12

You might also like