You are on page 1of 17

COMMUNICATION SYSTEMS USING MATLAB Module 02

Addition, Subtraction, Multiplication and Division of integers Write the following code in command window of MATLAB. Every time when you write a line of code, press ENTER. 3+5 ans = 8 x=3 x= 3 y=5 y= 5 x+y ans = 8 z=x+y z= 8 What you have learned from the above code. When we write 3+5, MATLAB will add the two numbers for you and store the result in a by default variable ans. When you write x=3 and y=5, MATLAB stores the integer values 3 and 5 in variables x and y respectively. Adding these 2 variables using the statement x+y, the resultant value 8 is assigned/stored in variable ans. If we want the sum of two variables to be stored into a third variable such as z, we write the statement z=x+y. this means that first addition of x and y is performed and than the result is stored in variable z, not the by default variable ans. Use * / ^ for for for for Subtraction Multiplication Division Power

Practice some subtraction, multiplication and division problems with the above parameters.

AND, OR, NOT, XOR Try some of the Logical operations as demonstrated below. c=and(1,1) c= 1 c=and(1,0) c= 0 x=1 x= 1 y=1 y= 1
2

c=or(x,y) c= 1 y=0 y= 0 c=xor(x,y) c= 1 b=not(1) b= 0 and(x,y), or(x,y), xor(x,y) and not(x) are built in functions in MATLAB. The input parameters (x,y) can be either given directly
3

as or(1,0) or indirectly in the form of variables by first declaring variables x=1, y=0 and than or (x,y). Both forms will give same result. Home Exercise 01: Implement the following and(x,y), or(x,y), not(x). using

y=not ( (1and1) or (1or0) or (not (0)) ) Home Exercise 02: The following is an example to calculate powers x=3 x= 3

y=x^2 y= 9 Implement the following equation in MATLAB and find the resultant x=4; y=5; z=3; result=(x+3)^2*4/(3*(z+y)); MATRICES If we want to construct a row matrix with 1*6 dimensions type the following m=0:1:5 m= 0 1 2 3 4 5

To convert the matrix into a column matrix with 6*1 dimensions type the following m=transpose (m) m= 0 1 2 3 4 5 A different method to construct a matrix is by giving values to each and every element of a matrix such as a(1,1)=3; a(1,2)=5; a(1,3)=7; a(1,4)=9; a
6

a= 3

The general format is a(x,y) which means matrix a with the element in x row and y column. Similarly for a row matrix b(1,1)=3; b(2,1)=5; b(3,1)=7; b(4,1)=9; b b= 3 5 7 9

For constructing a matrix with m*n dimension c(1,1)=1; c(1,2)=3; c(2,1)=3; c(2,2)=5; c c= 1 3 3 5 Type the following and see what happens d(3,2)=-5 By the above statement we have assigned a value to only one element of the matrix, and the

remaining elements of the matrix are assigned value 0. Addition, Subtraction, Multiplication of Matrices clear x=1:1:3; y=1:1:3; m(x,y)=1; m m= 1 1 1 1 1 1 1 1 1 n(x,y)=2; n n= 2 2 2 2 2 2
9

a=m+n a= 3 3 3 3 3 3 b=m-n b= -1 -1 -1 -1 -1 -1 c=a*n c= 18 18 18 18 18 18

3 3 3

-1 -1 -1

18 18 18

10

For finding matrix use det(a) ans = 0

determinant

of

Creating a Matrix with the help of Loops z=1:1:9 z= 1 2 9 n=1 n= 1


11

for i=1:3, for j=1:3, d(i,j)=z(1,n); n=n+1; end end d d= 1 2 3 4 5 6 7 8 9 Home Exercise 03 With the help of only Loops and a row or column matrix [1 2 3 4 5 6 7 8 9] design the following matrix

12

12 3 0 0 0 9 8 7 45 6 0 0 0 6 5 4 78 9 0 0 0 3 2 1 Home Exercise 04 Another way of designing a row matrix is clear x=[1 2 3 4 5 6 7 8 9] x= 1 2 3 4 5 9

Modify the matrix x in such a way that between every 2 elements of x a new element is inserted and which is the average of the two adjacent elements.

13

GRAPHS Making two dimensional graphs requires 2 vectors (row matrix), one for the x axis and one for the y axis. Write the following 3 lines of code x=0:1:10; y=x.^2; plot(x,y)
100 90 80 70 60 50 40 30 20 10 0 0 1 2 3 4 5 6 7 8 9 10

14

It gives you a graph as shown above. In order to draw a second graph on the same figure use the command, hold on. After writing the command of hold on, all the plot() statements will plot graphs in the same window. Following is an example code. hold on z=10*x; plot(x,z)
100 90 80 70 60 50 40 30 20 10 0

10

15

Sub plots: Type the following code figure subplot (1,2,1) plot(x,y) subplot (1,2,2) plot(x,z)
1 0 0 9 0 8 0 7 0 6 0 5 0 4 0 3 0 2 0 1 0 0 0 1 0 0 9 0 8 0 7 0 6 0 5 0 4 0 3 0 2 0 1 0 0 0

1 0

1 0

What have you done? Sub plot is a technique for dividing a single figure into a number of figures. In the above case subplot(m,n,p) command was used, in our case subplot (1,2,1) and subplot (1,2,2). Here m=1 means that divide the figure into 1 row,
16

n=2 means to divide the figure into 2 columns. This gives us a total of 2 subplots in one figure. Where p=1 means the window on the left (starting from row 1 and counting p=1
subplots to the right) and p=2 means the subplot on the right (starting from row 1 and counting p=2 subplots to the right). Home exercise 05 Construct a figure with 4 subplots and show the graphs of the following 4 equations in it. y=3*x.^2; z=exp(x); y1=4+5*x; z1=log(x); use x=2:0.01:5; For help on exp(x) and log(x), type the following in the command window. help exp help log

17

You might also like