# Experiment Name: Introduction To MATLAB

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

# Experiment Name: Introduction to MATLAB

# Abstract:
MATLAB is a very useful and widely used tool in numerous divisions of
engineering. In Digital Signal Processing, MATLAB is an integral part. It plays an important
role in analyzing and performing various operations on signal processing. The purpose of this
experiment was to gain familiarity with MATLAB and build some basic skills in MATLAB
language. The very basics of MATLAB, i.e. writing codes, computing mathematical
calculations, the concept of arrays, plotting data, creating functions etc. were described in this
experiment. The name MATLAB stands for matrix laboratory. MATLAB was originally written
to provide easy access to matrix.

# Theory & Methodology:


In this first lab, we learned how to perform basic mathematical operations on
simple variables, vectors, matrices and complex numbers, generate 2-D plots, use and write
script files (MATLAB programs). MATLAB script files have a file extension name .m and are,
therefore, usually referred as M-files. We learned to write commands to do simple calculations
like addition, subtraction, multiplication etc. using arithmetic operators and special characters.
We observed how to use array operators to indicate any array power etc. and the use of relational
and logical operators. We learned some basic file commands such as, cd, clc, clear (all) etc. and
observed how some of the predefined variables and math constants are implemented. We learned
the differences between matrices and array commands and how to properly write them in the
command window and used that knowledge to solve some linear equations.

Table 1: Arithmetic operators and special characters

Character Description
+ Addition
− Subtraction
* Multiplication (scalar and array)
/ Division (right)
ˆ Power or exponentiation
: Colon; creates vectors with equally spaced elements Semicolon;
; suppresses display; ends row in array Comma;
, separates array subscripts
... Continuation of lines
% Percent; denotes a comment; specifies output format Single quote;
= creates string; specifies matrix transpose Assignment operator
() Parentheses; encloses elements of arrays and input arguments
[] Brackets; encloses matrix elements and output arguments
# Codes:

1. >> xx=sin(pi/5);

>> yy=sqrt(1-xx*xx)

yy =0.8090

2. >> zz=3+4i;

>> conj(zz)

ans = 3.0000 - 4.0000i

>> abs(zz)

ans =5

>> angle(zz)

ans = 0.9273

>> real(zz)

ans = 3

>> imag(zz)

ans =4

3. >>jkl = 2 : 4 : 17
jkl = 99 : -1 : 88
ttt = 2 : (1/9) : 4
tpi = pi * [ 2 : (-1/9) : 0 ]

jkl = 2 6 10 14

jkl = 99 98 97 96 95 94 93 92 91 90 89 88

ttt = Columns 1 through 12

2.0000 2.1111 2.2222 2.3333 2.4444 2.5556 2.6667 2.7778 2.8889 3.0000 3.1111
3.2222

Columns 13 through 19

3.3333 3.4444 3.5556 3.6667 3.7778 3.8889 4.0000

tpi = Columns 1 through 12

6.2832 5.9341 5.5851 5.2360 4.8869 4.5379 4.1888 3.8397 3.4907 3.1416 2.7925
2.4435

Columns 13 through 19
2.0944 1.7453 1.3963 1.0472 0.6981 0.3491 0

4. >> xx = [ ones(1,4), [2:2:11], zeros(1,3) ]

xx(3:7)

length(xx)

xx = 1 1 1 1 2 4 6 8 10 0 0 0

ans =1 1 2 4 6

ans = 12

>> xx(2:2:length(xx))

ans = 1 1 4 8 0 0

>> xx(3:7) = pi*(1:5)

xx =1.0000 1.0000 3.1416 6.2832 9.4248 12.5664 15.7080 8.0000 10.0000 0 0 0

5. >> kset = -3:11;

>> cos( pi*kset/4 )

ans = Columns 1 through 12

-0.7071 0.0000 0.7071 1.0000 0.7071 0.0000 -0.7071 -1.0000 -0.7071 -0.0000 0.7071
1.0000

Columns 13 through 15

1.70710.0000 -0.7071

6. a) >> kset=-3:11

kset =

Columns 1 through 11

-3 -2 -1 0 1 2 3 4 5 6 7

Columns 12 through 15

8 9 10 11

>> kset

kset =

Columns 1 through 11

-3 -2 -1 0 1 2 3 4 5 6 7
Columns 12 through 15

8 9 10 11

>> cos(pi*kset/4)

ans = Columns 1 through 6

-0.7071 0.0000 0.7071 1.0000 0.7071 0.0000

Columns 7 through 12

-0.7071 -1.0000 -0.7071 -0.0000 0.7071 1.0000

Columns 13 through 15

0.7071 0.0000 -0.7071

b) >> xx=[];%<--- initialize the x vector to a null

>> for k=0:7

xx(k+1)=sin(k*pi/4)%--xx(0)would fail

end

xx = 0

xx = 0 0.7071

xx = 0 0.7071 1.0000

xx = 0 0.7071 1.0000 0.7071

xx = 0 0.7071 1.0000 0.7071 0.0000

xx = 0 0.7071 1.0000 0.7071 0.0000 -0.7071

xx = Columns 1 through 6

0 0.7071 1.0000 0.7071 0.0000 -0.7071

Column 7

-1.0000

xx = Columns 1 through 6

0 0.7071 1.0000 0.7071 0.0000 -0.7071

Columns 7 through 8

-1.0000 -0.7071

7. >> tt=-2:0.05:3;

xx=sin(2*pi*0.789*tt);
plot(tt,xx),grid on

title('TEST PLOT OF SINUSOID')

xlabel('TIME(sec)')

8. function x= cosgen(f,dur)
t=0:1/(20*f):dur;
x=cos(2*pi*f*t);
plot(x)
>> x=cosgen(.2,15)

x =

Columns 1 through 12

1.0000 0.9511 0.8090 0.5878 0.3090 0.0000


-0.3090 -0.5878 -0.8090 -0.9511 -1.0000 -0.9511

Columns 13 through 24

-0.8090 -0.5878 -0.3090 -0.0000 0.3090 0.5878


0.8090 0.9511 1.0000 0.9511 0.8090 0.5878

Columns 25 through 36
0.3090 0.0000 -0.3090 -0.5878 -0.8090 -0.9511
-1.0000 -0.9511 -0.8090 -0.5878 -0.3090 -0.0000

Columns 37 through 48

0.3090 0.5878 0.8090 0.9511 1.0000 0.9511


0.8090 0.5878 0.3090 0.0000 -0.3090 -0.5878

Columns 49 through 60

-0.8090 -0.9511 -1.0000 -0.9511 -0.8090 -0.5878


-0.3090 -0.0000 0.3090 0.5878 0.8090 0.9511

Column 61

1.0000

9.
function [sum,prod]=sumprod(x1,x2)
sum=x1+x2
prod=x1*x2

>> [sum,prod]=sumprod(3,3)

sum =

6
prod =
9
10. >> A=randn(6,3)
A=A.*(A>0)

A =
0.3252 0.3192 1.0933
-0.7549 0.3129 1.1093
1.3703 -0.8649 -0.8637
-1.7115 -0.0301 0.0774
-0.1022 -0.1649 -1.2141
-0.2414 0.6277 -1.1135

A =

0.3252 0.3192 1.0933


0 0.3129 1.1093
1.3703 0 0
0 0 0.0774
0 0 0
0 0.6277 0
11. >> x=randn(4,5)

x =

-0.0068 -0.2256 0.5525 -1.4916 -0.6156


1.5326 1.1174 1.1006 -0.7423 0.7481
-0.7697 -1.0891 1.5442 -1.0616 -0.1924
0.3714 0.0326 0.0859 2.3505 0.8886

>> a=(x>=0)

a =

0 0 1 0 0
1 1 1 0 1
0 0 1 0 0
1 1 1 1 1

>> p=a.*x

p =

0 0 0.5525 0 0
1.5326 1.1174 1.1006 0 0.7481
0 0 1.5442 0 0
0.3714 0.0326 0.0859 2.3505 0.8886
>> b=(x<0)

b =

1 1 0 1 1
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0

>> y=b.*77

y =

77 77 0 77 77
0 0 0 77 0
77 77 0 77 77
0 0 0 0 0
12. >> xx=[-3 -1 0 1 3];

yy=xx.*xx-3*xx;

plot(xx,yy)% Real number plot

zz=xx+yy*sqrt(-1)

zz =

-3.0000 +18.0000i -1.0000 + 4.0000i 0.0000 + 0.0000i 1.0000 - 2.0000i 3.0000 + 0.0000i

>> plot(zz)%complex number plot


# Lab Review Questions and Answers:

1. You saw how it easy it is for Matlab to generate and manipulate vectors (i.e., 1-dimensional
arrays of numbers). For example, consider the following:
>>yy = 0:10;
>>yy = zeros(1,25);
>>yy = 1:.25:5;
(a) How would you modify one of the above lines of Matlab code to create a vector that steps
from 0 to 10 in steps of 1/2?
Sol: >>yy= 0: 0.5 : 10
(b) How would you modify one of the lines in the code to create a vector of one hundred 100's?
Sol: >>yy= 1 : 100;

2. You also learned that Matlab has no problem handling complex numbers . Consider the
following line of code:
>>yy = 3 + 5j;
(a) How do you get Matlab to return the magnitude of the complex number yy?
Sol: >>abs (yy)
(b) How do you get Matlab to return the phase of the complex number yy? What are the
units of the answer?
Sol: >> angle(yy)

3. In Section 2.3, you learned that multiple lines of Matlab code can be stored in a file with a .m
extension. Matlab then executes the code in the order that it appears in the file. Consider the
following file, named example.m:
Sol:
f = 200;
tt = [0:1/(20*f):1];
z = exp(j*2*pi*f*tt);
subplot(211)
plot(real(z))
title('Real part of exp(j*2*pi*200*tt)')
subplot(212)
plot(imag(z))
title('Imaginary part of exp(j*2*pi*200*tt)')

(a) How do you execute the file from the Matlab prompt?
Sol: Press the run button.
(b) Suppose the file were named example.dog. Would it run? How could you change it to make
it work in Matlab?
Sol: No, it will not run. It will run after we make it a .m extension file.
(c) Assuming the M-file runs, what do you expect the plots to look like? If you're not sure type
in the code and run it.
Sol: The real part looks like a cosine graph and the imaginary part looks like a sine graph.

# Discussion:
In this experiment, basic skills in MATLAB software was developed, each
code was checked by our supervisor while practicing which solved many of the mistakes inside
the code and helped to grab a better understanding of writing codes in MATLAB. MATLAB
software really makes it easier to solve complex problems within seconds.
#Reference:
[1] John G. Proakis, and Dimitris G. Manolakis, Digital Signal Processing, New Delhi, Prentice
Hall of India, 2003.
[2] http://www.math.mtu.edu/~msgocken/intro/intro.html

You might also like