You are on page 1of 14

Signals, Spectra and Signal Processing (EC42FB1)

Name: Galleno, Johny S.


Date: November 17,
2016
Section: EC42FB1
Rating:
Exercises #01
Introduction to MATLAB (Part 1)

Introduction:
The purpose of this exercise is to introduce MATLAB to students, its basic commands and functions.
MATLAB originally stood for Matrix Laboratory. Today it is regarded as high-performance language for
technical computing. This software incorporates many features that are used for many complex
mathematical computations, simulations and modeling.

Part 1 Defining matrices


MATLAB operates on matrices (hence the name matrix laboratory). Matrices are array of numbers. They
are used to represent various quantities. Vectors are one-dimensional matrices, either a row-matrix or a
column matrix.
To define a matrix in MATLAB, an identifier is used. An identifier is a name given to a variable that holds
a certain value. It is alphanumeric in form. For example to define the matrix

and place it in variable A, the following is typed in the MATLAB workspace


>> A = [-2 3 1; 5 7 -1; -9 0 2]
when entered, the following output is produced
A=
-2

-1

-9

1. Define the following matrices in MATLAB. Note that the variable names are case sensitive.

Q1.1. Write the commands that you issued to define the matrices above.
B=[4 1 0;1 3 2;0 2 5]
B=
4
1
0
1
3
2
0
2
5
C=[0,2,-8;-2,0,6;8,-6,0]
C=
0
2 -8
-2
0
6
8 -6
0
d=[2;1;4]
d=
2
1
4
e=[9 0 5]
e=
9
0
5
F=[3,2;4,1]
F=
3
2
4
1
G=[0,-2;4,5]
G=
0 -2
4
5
H=[3 0 2;4 0 1]
H=

3
4

0
0

2
1

J=[6 1 -5;5 -2 13]


J=
6
1 -5
5 -2 13

Q1.2 Place a semi colon after defining one of the matrices above. What happens?
>>
>>
>>
>>
>>
>>
>>
>>

B = [4 1 0; 1 3 2; 0 2 5;];
C = [0 2 -8; -2 0 6; 8 -6 0];
d = [2;1;4];
e = [9 0 5];
F = [3 2; 4 1;];
G = [0 -2; 4 5;];
H = [3 0 2 ; 4 0 1];
J = [6 1 -5; 5 -2 13];

If one line of code places a semi colon after defining any of the matrices above, the output
is produced but is not shown in the Command Window. One can confirm that the code has
been produced by simply checking the value of the assigned variable in the Workspace
Window.
Q1.3 Which of the matrices defined above is/are row matrice/s? Column matrice/s? Square
matrice/s?

The only row matrix listed above is the matrix e. And the only column matrix listed above is the
matrix d. And the square matrices are matrix B,C,F, and G.

Row matrice/s
e

Column matrice/s
d

Square matrice/s
B
C
F
G

Part 2 Matrix operation


The following are MATLAB symbols for matrix operation

Q2.1 Using the matrices defined in part 1, perform the indicated operation. Record the output.
a.

AB , B
ans =
-5
9 11
27 24
9
-36 -5 10
ans =
-3 19
-5 24
-35 14

b.

H +J , H J
ans =
9
1 -3
9 -2 14
ans =
-3
-1

-1
7
2 -12

3
2
8

c.

C .A , A ./C
ans =
0
-10
-72

6
0
0

-8
-6
0

ans =
-Inf 1.5000 -0.1250
-2.5000
Inf -0.1667
-1.1250
0
Inf
d.

d +e ,d + e , d + e
d+e
Error using +
Matrix dimensions must agree.
d+e',d'+e
ans =
11
1
9
ans =
11

e.

d , e
ans =
3

ans =
1

f.

det (A ),det ( B), det (d ) ,(F )


det(A),det(B)
ans =
32
ans =
39
det(d)
Error using det
Matrix must be square.
det(F)
ans =
-5

g.

inv(C), inv(G)
Warning: Matrix is singular to working precision.
ans =
Inf Inf Inf
Inf Inf Inf
Inf Inf Inf
ans =
0.6250
-0.5000

h.

0.2500
0

BC , B .C
ans =
-2
8 -26
10 -10 10
36 -30 12

ans =
0
2
-2
0
0 -12

i.

0
12
0

HJ , H .J
H*J
Error using *
Inner matrix dimensions must agree.
H.*J
ans =
18
20

j.

0 -10
0 13

C2, C . 2
ans =
-68 48 12
48 -40 16
12 16 -100

ans =
0
4
64

4
0
36

64
36
0

Q2.2 Which operations above are invalid (i.e. those that returned error messages)? Explain why
each errors are returned?

Invalid commands

d +e

det (d )
HJ

Explanation
It is impossible to add/subtract unequal
dimensions. Both matrix d and e needs
to have same dimensions (e.g. 2x2 +
2x2)
In solving/finding for the determinant of
a matrix, the matrix itself has to be a
square, dimensionally.
In multiplying matrices, both matrices
must have similar inner matrix
dimension. (e.g. 3x2 * 2x4, since they
both have 2 as their inner dimension,
they can be multiplied)

Q2.3 Explain the difference between the operation * and .*.


When multiplying matrices using *, the matrices as a whole are multiplied to
each other. While using .*, each element of one of the matrix is multiplied to its
corresponding element on the other matrix

Part 3 Other MATLAB capabilities Complex Numbers


Q3.1 Let

z 1=4 + j 3z 2=2 j 5. Find each of the following in the form

x+ jyr < .
1.

z1 z2

9.

abs(z1*z2),angle(z

1*z2)

2.
3. z1*z2
4.
5.

ans =

6.
7.
-14.0000i
8.

10.
11.
12.
13.

23.0000

ans =
26.9258

14.
15.
16.
17.

ans =
-0.5468

18.
Answer: 23-j14 or
26.9258<0.548
19.

20.

( 3 z 1z 2)

21.
22.
(3*z1-z2)^2
23.
24.
ans =
25.
26.
-9.6000e+01 +
2.8000e+02i
27.
28.
abs((3*z1z2)^2),angle((3*z1-z2)^2)
29.
30.
ans =
31.
32.
296
33.
34.
ans =
35.
36.
1.9011
37.
Answer: -96+j280 or

296<1.9011

38.
39.
40.
41.
42.
43.
44.

45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.

60.
61.

62.

-0.6435

Answer: 0.16-j0.12

or

63.
64.
65.

0.2<-0.6435
25 z 2
z1

66.
67.
(25*z2)/z1
68.
69.
ans =
70.
71.
-7.0000 -26.0000i
72.
73.
abs(((25*z2)/z1)),angle(((
25*z2)/z1))
74.
75.
ans =
76.
77.
26.9258
78.
79.
ans =
80.
81.
-1.8338

82.
Answer: -7-j26 or
83.
26.9258 <1.8338
84.
85.

1
z1
1/z1
ans =
0.1600 - 0.1200i
abs(1/z1),angle(1/z1)
ans =
0.2000
ans =

86.

(z 31 )( z 1 )3

87.
88.
real(z1^3),(real(z1))^3
89.
90.
ans =
91.
92.
-44
93.
94.
ans =
95.
96.
64
97.
98.
abs(real(z1^3)),angle(rea
l(z1^3))
99.
100.
ans =
101.

102.
44
113.
64
103.
114.
104.
115.
105.
ans =
116.
ans =
106.
117.
107.
3.1416
118.
0
108.
119.
109.
abs(((real(z1))^3)),angle(
120.
Answer: -44+j0 and
((real(z1))^3))
64+j0
110.
121.
or 44<3.1416 and
111.
ans =
64<0
112.
122.
123.
124.
125.
Q3.2 Find values in the rectangular form
126.

cos( 1+ j)

1.

2.
3. ans =
4.
5.
0.8337 - 0.9889i

6. Answer: 0.8337j0.9889
7.
8.

sin j

9.
10.
sin(j*pi)
11.
12.
ans =
13.
14.
0.0000
+11.5487i
15.
Answer: 0+j11.5487
16.
17.

1
cos ( j)
2

18.
cos(pi/2-pi*i)
19.
20.
ans =
21.
22.
0.0000
+11.5487i
23.
Answer: 0+j11.5487

24.

cosh (3 j 6)

25.
26.
cosh(-3-6i)
27.
28.
ans =
29.
30.
9.6667 - 2.7991i
31.
Answer: 9.6667-

j2.7991

32.
33.
34.
35.

sinh( 4+ j5)

36.
37.
sinh(4+5j)
38.
39.
ans =
40.
41.
7.7411 -26.1865i
42.
43.
Answer: 7.7411-

j26.1865

44.
45.
46.
47.
48.
49.
50.

51. Part 4 Two-dimensional graphing in MATLAB


52.
53.
Q4.1 What did this line (x=[-10:10]) achieved?
54.
55.
This command simply produced a row matrix having 21
elements with values from -10 to 10 respectively.
56.
57.
x=[-10:10]
58.
x=
59.
60.
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
0
1
2
3
4
5
6
7
8
9 10
61.
62.
Q4.2 Define y in terms of x.
63.
64.
y=3*x-3
65.
66.
y=
67.
68.
-33 -30 -27 -24 -21 -18 -15 -12 -9 -6 -3
0
3
6
9 12 15 18 21 24 27
69.
70.
Q4.3 What features are added to the plot when following
commands are issued?
71.
a. grid on
adds major grid lines to the current axes.
72.
b. axis([-10 10 -35 35])
the scaling of x- and y-axes were changed. In
this situation, the
73.
range of x was set to -10 up to 10, and
the range of y-axis was
74.
set to -35 up to +35.
75.
c. xlabel(x)
the x domain was labeled as x.
76.
d. ylabel(y)
the y domain was labeled as y.
77.
e. title(Line)
The title caption appeared on the top-center of the
graph,
78.
entitled as Line
79.
f. clf
the data on the graph/plot was cleared. To be more
specific,
80.
this command deletes all children of the
current figure with visible handles.
81.
82.
Q4.4 What happens when you change the plot command to the
following?
83.
a. plot(x,y,'r')
the lines color was assigned as red.

84.
b. plot(x,y,'--')
the line became a dashed line.
85.
c. plot(x,y,'-o')
the line remains a solid line but with circles.
86.
d. plot(x,y,'-.xg')
the line was assigned as green with dashed
lines and plus
87.
symbol.
88.
89.
Q4.5 Using the plot function, obtain the graph of the functions

y=x 210

and

y=x 32 . Plot the curves on the space provided

below.
90.

91.

92. Figure 4.5.1. A graph of the function


93.

y=x 210 .

94.
95.

3
Figure 4.5.2. A graph of the function y=x 2 .

You might also like