lab 01 introduction to matlab

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

Upload: salman-khan

Post on 12-Apr-2015

42 views

Category:

Documents


4 download

DESCRIPTION

Matlab lab

TRANSCRIPT

Page 1: Lab 01 Introduction to Matlab

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+5ans = 8

» x=3x = 3

» y=5y = 5

» x+yans = 8

» z=x+yz = 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 Subtraction* for Multiplication/ for Division^ for Power

1

Page 2: Lab 01 Introduction to Matlab

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=1x = 1

» y=1y = 1

2

Page 3: Lab 01 Introduction to Matlab

» c=or(x,y)c = 1

» y=0y = 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

Page 4: Lab 01 Introduction to Matlab

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 using and(x,y), or(x,y), not(x).

y=not ( (1and1) or (1or0) or (not (0)) )

Home Exercise 02:

The following is an example to calculate powers » x=3x = 3

4

Page 5: Lab 01 Introduction to Matlab

» y=x^2y = 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:5m = 0 1 2 3 4 5

5

Page 6: Lab 01 Introduction to Matlab

To convert the matrix into a column matrix with 6*1 dimensions type the following» m=transpose (m)m = 0 1 2 3 4 5A 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

Page 7: Lab 01 Introduction to Matlab

a = 3 5 7 9

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;» bb = 3 5 7 9

7

Page 8: Lab 01 Introduction to Matlab

For constructing a matrix with m*n dimension

» c(1,1)=1;» c(1,2)=3;» c(2,1)=3;» c(2,2)=5;» cc = 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

8

Page 9: Lab 01 Introduction to Matlab

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;» mm = 1 1 1 1 1 1 1 1 1

» n(x,y)=2;» nn = 2 2 2 2 2 2

9

Page 10: Lab 01 Introduction to Matlab

2 2 2

» a=m+na = 3 3 3 3 3 3 3 3 3

» b=m-nb = -1 -1 -1 -1 -1 -1 -1 -1 -1

» c=a*nc = 18 18 18 18 18 18 18 18 18

10

Page 11: Lab 01 Introduction to Matlab

For finding determinant of a matrix use

» det(a)ans = 0

Creating a Matrix with the help of Loops

» z=1:1:9z = 1 2 3 4 5 6 7 8 9

» n=1n = 1

11

Page 12: Lab 01 Introduction to Matlab

» for i=1:3,for j=1:3,d(i,j)=z(1,n);n=n+1;endend» dd = 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

Page 13: Lab 01 Introduction to Matlab

12 3 0 0 0 9 8 745 6 0 0 0 6 5 478 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 6 7 8 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

Page 14: Lab 01 Introduction to Matlab

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)

0 1 2 3 4 5 6 7 8 9 100

10

20

30

40

50

60

70

80

90

100

14

Page 15: Lab 01 Introduction to Matlab

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)

0 1 2 3 4 5 6 7 8 9 100

10

20

30

40

50

60

70

80

90

100

15

Page 16: Lab 01 Introduction to Matlab

Sub plots: Type the following code» figure» subplot (1,2,1)» plot(x,y)» subplot (1,2,2)» plot(x,z)

0 5 100

10

20

30

40

50

60

70

80

90

100

0 5 100

10

20

30

40

50

60

70

80

90

100

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

Page 17: Lab 01 Introduction to Matlab

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