Exp 1
Exp 1
MATLAB
MATLAB is a powerful tool that is utilized by the engineers and others professionals in
development and testing of various projects. It is versatile software, with the help of which
you can solve and develop any sort of engineering problem. The name MATLAB stands for
MATRIX LABORAORY. All the work done in MATLAB is basically in the form of
matrices. Scalars are referred as 1-to-1 matrix and vectors are matrices having more than 1
row and column. MATLAB is programmable and have the same logical, relational,
conditional and loop structures as in other programming languages, such as C, Java etc. It’s
very easy to use MATLAB, all we need is to practice it and become a friend of it.
Summary:
• Scalars
• Vectors
• Matrices
• Plotting
• m-files
• functions
Getting Started:
a) Go to the start button, then programs, MATLAB and then start MATLAB. It is
preferred that you have MATLAB2015a. You can then start MATLAB by double
clicking on its icon on Desktop, if there is any.
b) The Prompt:
>>
Help in MATLAB
In order to use the built-in help of the MATLAB we use the help keyword. Write it on the
prompt and see the output.
Also try
Scalars
A scalar is a single number. A scalar is stored in the MATLAB as a 1 x 1 matrix. Try these
on the prompt.
>> A = 2;
>> B = 3;
>> C = A^B
>> C = A*B
>> C = A+B
>> C = A-B
>> C = A/B
>> C = A\B
Try to implement these two relations and show the result in the provided space
a) 25 ( 31/3 ) + 2 (2+92) =
Vectors
Vectors are also called arrays in MATLAB. Vectors are declared in the following format.
>> X = [1 2 3 4]
>> Y = [2 5 8 9]
Try these two instructions in MATLAB and see the result
>> length (X) =
>> X.*Y =
>> X.^Y =
>> X+Y =
>> X-Y =
>> X./Y =
>> X’ =
Also try some new instructions for this and notice the outputs in each case.
>> ones (1,4)
There is an important operator, the colon operator (:), it is very important operator and
frequently used during these labs. Try this one.
>> X = [0:0.1:1]
>>A= [ones(1,3), [2:2:10], zeros(1,3)] What is the length and size of this?
>> Length =
Size =
Try ‘help ones’ and ‘help zeros’ as well, and note down its important features.
MATRICES
>> B = [1,2,3;4,5,6;7,8,9]
Is there any difference between the two? Try to implement 2-to-3 matrix and 3-to-2 matrix.
Also take help on mod, rem, det, inv and eye and try to implement them. Try to use
length and size commands with these matrices as well and see the results.
7x – 2y + 3z= 5
2x + 8y -9z = 64
2. A = [2 3 4 5; 1 8 9 0; 2 3 1 3; 5 8 9 3]
Solve 6A – 2I + A2 =
PLOTTING
Plotting is very important as we have to deal with various type of waves and we have to view
them as well.
Try these and have a look on the results.
>> x = [0:0.1:10];
>> y = sin (x);
>> z = cos (x);
>> subplot (3,1,1);
>> plot (x,y);
>> grid on;
>> subplot (3,1,2);
>> plot (x,z);
>> grid on; hold on;
>> subplot (3,1,3);
Lab Manual of Analog & Digital Communication
See help on plot, figure, grid, hold, subplot, stem and other features of it.
Figure 1.1
Lab Manual of Analog & Digital Communication
M-FILES
MATLAB can execute a sequence of statements stored in disk files. Such files are called M-
files because they must have the file type ‘.m’. Lot of our work will be done with creation of
m-files.
Script Files
We can use script files in order to write long programs such as one on the previous page.
A script file may contain any command that can be entered on the prompt. Script files can
have any name but they should be saved with “.m” extension. In order to excurse an m-file
from the prompt, just type its name on the prompt. You can make an m-file by typing edit
on the prompt or by clicking on the file then new and m-file. See an example of m-file.
Write it and see the results.
% This is comment
% A comment begins with a percent symbol
% The text written in the comments is ignored by the MATLAB
%comments in your m-files.
clear;
clc;
x = [0:0.1:10];
y = sin (x);
subplot (2,2,1);
plot (x,y, ,'r');
grid on;
z = cos (x);
subplot (2,2,2);
plot (x,z);
grid on;
w = 90;
yy = 2*pi*sin
(x+w)
subplot (2,2,3);
plot (x,yy);
grid on;
zz = sin (x+2*w);
subplot (2,2,4);
stem (x,zz, ,'g');
hold on;
stem (x,y, ,'r');
grid on;
Lab Manual of Analog & Digital Communication
Figure 1.2
Function Files
MATLAB have many built-in functions including trigonometry, logarithm, calculus and
hyperbolic functions etc. In addition we can define our own functions and we can use built-
in functions in our functions files as well. The function files should be started with the
function definition and should be saved with the name of function. The general format of
the function file is
% this function computes the factorial of a number function [y] = my_func (x)
y = factorial (x);
POST LAB
Try to develop a function that will compute the maximum and minimum of two numbers.