Final 2.34 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Signals, Spectra and Signal Processing (ECE 401)

Name: Date:
Section: Rating:
Exercises #02
Matrix Operation, Complex Numbers and 2-D Plotting

Intended Learning Outcomes (ILO):


At the end of this exercise, students must be able to:
1. Define a matrix using Matlab.
2. Simulate matrix operations using Matlab.
3. Simulate complex numbers using Matlab.
4. Plot a two-dimensional graph using Matlab.
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

 2 3 1 
 
 5 7  1
  9 0 2 

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 =

1
-2 3 1
5 7 -1
-9 0 2

1. Define the following matrices in MATLAB. Note that the variable names are case sensitive.
 4 1 0  0 2 - 8  2
      e  9 0 5
B   1 3 2 C   2 0 6 d   1
0 2 5  8  6 0   4

3 2 0  2 3 0 2 6 1  5 
F  G  H  J 
 4 1 4 5   4 0 1 5  2 13 
Q1.1. Write the commands that you issued to define the matrices above.

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

Q1.3 Which of the matrices defined above is/are row matrice/s? Column matrice/s? Square
matrice/s?

2
Part 2 – Matrix operation
The following are MATLAB symbols for matrix operation
Operator Function
Element-by-element addition and subtraction, respectively. Matrices to be
+ or -
added or subtracted should be of the same dimension
Matrix multiplication and division, respectively. Matrices to be multiplied or
* or / divided should be conformable. If one of the operands is a scalar, the
operation becomes scalar.
^ Matrix exponentiation. One of the operands must be scalar
Element-by-element multiplication, division and exponentiation, respectively.
.* or ./ or .^ Matrices to be operated on an element-by-element basis should be of the
same dimension.
size(A) Determines the dimensions of matrix A.
A’ Determines the transpose of matrix A.
inv(A) Determines the inverse of matrix A. The matrix should be non-singular.
det(A) Evaluate the determinant of matrix A.
Q2.1 Using the matrices defined in part 1, perform the indicated operation. Record the output.
a. A*B, B*A b. H + J, H - J

c. C.*A, A./C d. d + e, d + e’, d’+e

e. size(d), size(e) f. det(A), det(B), det(d), det(F)

3
g. inv(C), inv(G) h. B*C, B.*C

i. H*J, H.*J j. C^2, C.^2

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

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

Part 3 – Other MATLAB capabilities – complex numbers


MATLAB can operate on complex numbers. The imaginary unit j is defined in MATLAB as i. For
example, to define a complex number 3 + j4 in MATLAB and place it in variable M, we type

>> M = 3 + 4i

4
The table below summarizes other functions of MATLAB and their syntax
Operation Function
sin(M),
Obtains the value of sine, cosine and tangent of M. M is assumed to be in radians.
cos(M),
M can be a complex number.
tan(M)

exp(M) Computes the value of e M . M can be a complex number.


log(M) Computes the logarithm of complex number M to the base e.
sinh(M),
cosh(M), Computes the hyperbolic sine, cosine and tangent of a complex number M.
tanh(M)
real(M),
Determines the real and imaginary parts of complex number M, respectively.
imag(M)
abs(M), Determines the magnitude and angle in radians of complex number M,
angle(M) respectively.
pi The value of constant   3.14159 ...

Q3.1 Let z1  4  j3 and z 2  2  j5 . Find each of the following in the form x  jy and r .

1. z1z 2 2. 3z1  z 2 2

1 25z 2
3. 4.
z1 z1

5. Re  z13  and Re z1 3


 

Q3.2 Find values in the rectangular form


1. cos1  j 2. sin j

5
2
3. cos 1   j  4. cosh 3  j6 

5. sinh4  j5 

Part 4 – Two-dimensional graphing in MATLAB


MATLAB can do 2 and 3D plotting using various tools. In 2D graphing, row-vectors of two variables must
be defined. For example to plot the line y  3x  3 for 10  x  10 , a row vector of x from -10 to 10
must be defined. You can do that by constructing a row matrix from -10 to 10 but a shorter way to do it is
through the use of increment operator. Type the line below in MATLAB and see the output

>> x = [-10:10]

Q4.1 What did this line achieved?

After defining x, we should now define y.


Q4.2 Define y in terms of x.

6
Using the plot function, a line graph can be generated. Typing the line below in MATLAB’s workspace

>> plot(x,y)

we obtain the graph below

30

20

10

-10

-20

-30

-40
-10 -5 0 5 10

Q4.3 What features are added to the plot when the following commands are issued?
a. grid on

b. axis([-10 10 -35 35])

c. xlabel(‘x’)

d. ylabel(‘y’)

e. title(‘Line’)

f. clf

Q4.4 What happens when you change the plot command to the following?
a. plot(x,y,’r’)

b. plot(x,y,’--')

7
c. plot(x,y,’—o’)

d. plot(x,y,’-.xg’)

Q4.4 Using the plot function, obtain the graph of the functions y  x 2  10 and y   x 3  2 . Plot
the curves on the space provided below.
parabola
10

-2

-4

-6

-8

-10
-10 -8 -6 -4 -2 0 2 4 6 8 10

cubic function
10

-2

-4

-6

-8

-10
-10 -8 -6 -4 -2 0 2 4 6 8 10

8
9
Laboratory Report
Signals, Spectra and Signal Processing (ECE 401)
Name: Date:
Section: Rating:
Exercises #02
Matrix Operation, Complex Numbers and 2-D Plotting

DATA

Part 1 – Defining matrices


>> A = [-2 3 1; 5 7 -1; -9 0 2]
1. Define the following matrices in MATLAB. Note that the variable names are case sensitive.
 4 1 0  0 2 - 8  2
      e  9 0 5
B   1 3 2 C   2 0 6 d   1
0 2 5  8  6 0   4

3 2 0  2 3 0 2 6 1  5 
F  G  H  J 
 4 1 4 5   4 0 1 5  2 13 
Q1.1. Write the commands that you issued to define the matrices above.
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]

Q1.2 Place a semi colon after defining one of the matrices above. What happens?
It doesn't display results and goes directly to a new line.

10
Q1.3 Which of the matrices defined above is/are row matrice/s? Column matrice/s? Square
matrice/s?

Row Matrices:
e

Column Matrices:
d

Square Matrices:
B,C F G H J

Part 2 – Matrix operation


Q2.1 Using the matrices defined in part 1, perform the indicated operation. Record the output.
b. A*B, B*A b. H + J, H - J
A*B= B*A= H+J= H-J=
-5 9 11 -3 19 3 9 1 -3 -3 -1 7
27 24 9 -5 24 2 9 -2 14 -1 2 -12
-36 -5 10 -35 14 8

c. C.*A, A./C d. d + e, d + e’, d’+e


C.*A= A./C= d+e= d+e'= d'+e=
0 6 -8 -Inf 1.5000 -0.1250 11 2 7 11 11 1 9
-10 0 -6 -2.5000 Inf -0.1667 10 1 6 1
-72 0 0 -1.1250 0 Inf 13 4 9 9

e. size(d), size(e) f. det(A), det(B), det(d), det(F)


det(A)= 32
size(d)= 3 1 det(F)= -5
size(e)=1 3 det(B)= 39

det(d)= Error using det


Matrix must be square.
g. inv(C), inv(G) h. B*C, B.*C
inv(C)= inv(G)= B*C= B.*C=
Inf Inf Inf 0.6250 0.2500
Inf Inf Inf -0.5000 0 -2 8 -26 0 2 0
Inf Inf Inf 10 -10 10 -2 0 12
36 -30 12 0 -12 0
Warning: Matrix is singular to working precision.

11
i. H*J, H.*J j. C^2, C.^2
H*J= C^2= C.^2=
Error using * -68 48 12 0 4 64
Incorrect dimensions for matrix multiplication. Check 48 -40 16 4 0 36
that the number of columns in the first matrix matches 12 16 -100 64 36 0
the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.

H.*J= 18 0 -10
20 0 13
Q2.2 Which operations above are invalid (i.e. those that returned error messages)? Explain why
each errors are returned?

det(d): Error using det. Matrix must be square. in using det function, the matrix must be a square matrix.

H*J: Error using * Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches
the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
The number of columns in the first matrix must match the number of columns of the other matrix.

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

The * symbol is defined as matrix multiplication when used on two matrices while .* symbol is used
to specify element-wise multiplication.

Part 3 – Other MATLAB capabilities – complex numbers


>> M = 3 + 4i

Q3.1 Let z1  4  j3 and z 2  2  j5 . Find each of the following in the form x  jy and r .
x = z1*z2 y= ((3*z1)-z2)^2
1. z1z 2 x = 23.0000 -14.0000i 2. 3z1  z 2 2 y = -96 + 280i
abs(x), angle(x) abs(y), angle(y)
ans = 26.9258 ans = 296
ans = -0.5468 ans = 1.9011
26.9258∠ -0.5468 296∠ 1.9011

1 25z 2 >> (25*z2)/z1


3. >> 1/z1 4. ans = -7 - 26i
z1 ans = 0.16 - 0.12i z1
abs(ans), angle(ans)
abs(ans), angle(ans) ans = 26.926
ans = 0.2 ans = -1.8338
ans = -0.6435 26.926∠ -1.8338
0.2∠ -0.6435

12
5. Re  z13  and Re z1 3
 
>> real (z1^3)
ans = -44

>> (real(z1))^3
ans = 64

Q3.2 Find values in the rectangular form


1. cos1  j 2. sin j
>> sin(i*pi)
>> cos(1+i)
ans =
ans =
0+ 11.549i
0.83373 - 0.9889i

2
3. cos 1   j  4. cosh 3  j6 

>> cosh(-3-6i)
>> cos((1/2*pi)-(i*pi))
ans =
ans =
9.6667 - 2.7991i
7.098e-16 + 11.549i

5. sinh4  j5 

>> sinh(4+5i)

ans =

7.7411 - 26.187i

Part 4 – Two-dimensional graphing in MATLAB


>> x = [-10:10]

Q4.1 What did this line achieved?

13
Q4.2 Define y in terms of x.

>> plot(x,y)

Q4.3 What features are added to the plot when the following commands are issued?
g. grid on
Enables grid view in the graph.

h. axis([-10 10 -35 35])


Adjust the desired axis length of the graph.
i. xlabel(‘x’)
Inserts a label name for X- axis.

j. ylabel(‘y’)
Inserts a label name for Y - axis.
k. title(‘Line’)
Inserts the title name of the graph.
l. clf

Clears all the command entered in the graph.

Q4.4 What happens when you change the plot command to the following?
e. plot(x,y,’r’)

Plots the graph in color red line.


f. plot(x,y,’--')
Plots the graph in dashed lines.
g. plot(x,y,’—o’)
Plots the graph in circles and lines.

14
h. plot(x,y,’-.xg’)

Plots the graph in green colored dash,dot and cross lines.

Q4.4 Using the plot function, obtain the graph of the functions y  x 2  10 and y   x 3  2 . Plot
the curves on the space provided below.

Conclusion:

(Student Name 1):

(Student Name 2): This laboratory experiment is important to students who are taking the subject
because it recalls the basic elements of the matlab software.
MATLAB has many commands that you can apply like Microsoft Excel but more applicable to
(Student Name 3): Engineers and Scientist. Because of Matrices and 2D Graphs in MATLAB has many useful
features that only Engineers can appy.

(Student Name 4): In this experiment it refresh my knowledge about MATLAB and the
different types of plot that MATLAB has. And also computing complex
numbers using matlab.

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy