Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

DIGITAL SIGNAL PROCESSING

Name: Moreno, Gerhome Anthony S. Date: 09/07/2023


Section: BSECE-3 Rating:
Exercises #01
Introduction to 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

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 3 1
5 7 -1
-9 0 2

1
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.
clc
clear all

% Matrix B
B=[4 1 0;1 3 2;0 2 5]

% Matrix C
C=[0 2 -8;-2 0 6;8 -6 0]

% Matrix D
D=[2;1;4]

% Matrix E
E=[9 0 5]

% Matrix F
F=[3 2;4 1]

% Matrix G
G=[0 -2;4 5]

% Matrix H
H=[3 0 2;4 0 1]

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

Q1.2 Place a semi colon after defining one of the matrices above. What happens?
The matrices won’t appear if a semi-colon is placed after a matrix. There would be no output
because the semi-colon suppressed the output.

Q1.3 Which of the matrices defined above is/are row matrice/s? Column matrice/s? Square
matrice/s?
Matrices B, C, F, G are square matrices, D is a column matrix, and E is a row matrix

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
Ans=[-5 9 11; 27 24 9; -36 -5 10] Ans=[9 1 -3; 9 -2 14]
Ans=[-3 19 3; -5 24 2; -35 14 8] Ans=[-3 1 7; -1 2 -12]

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


Ans=[0 6 -8;-10 0 -6;-72 0 0] Ans=[11 2 7;10 1 6;13 4 9]
Ans=[-Inf 1.50 -0.1250;-2.50 Inf -0.1667;-1.1250 0 Inf]
Ans=[11;1;9] Ans=[11 1 9]
e. size(d), size(e) f. det(A), det(B), det(d), det(F)
Ans=[3 1] Ans=[32] ; Ans=[39]; Matrices
Ans=[1 3] D and F are invalid as they are not
square matrices
g. inv(C), inv(G) h. B*C, B.*C
Ans=[Inf Inf Inf; Inf Inf Inf; Inf Inf Inf] Ans=[-2 8 -26; 10 -10 10; 36 -30 12]
Ans=[0.6250 0.2500; -0.500 0] Ans=[0 2 0; -2 0 12; 0 -12 0]

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


ERROR Ans=[-68 48 12;48 -40 16; 12 16 -100]
Ans=[0 4 64;4 0 36;64 36 0]

3
Q2.2 Which operations above are invalid (i.e. those that returned error messages)? Explain why
each errors are returned.
Operations I because it is an incorrect dimension for matrix multiplication. While operation F for
det(D) and det(F) comes up as error because they are not considered square matrices only rows
and columns.

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


* is an operator that is the standard matrix product that collects the vector dot products that
combines all row vectors from one certain matrix to another. While .* is an element-by-element
multiplication of the matrices that must have the same shape.

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

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 . 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)

4
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

Q3.1 Let . Find each of the following in the form .

1. 2.
X+JY= 23.000-14.000i X+JY=(-9.6e+1)+(2.8e+02i)
r<θ=26.9258<-31.3287 r<θ=17.5983<173.4744

3. 4.

X+JY=0.1600-0.1200i X+JY=-7.000-26.000i
r<θ= 0.2<-36.8699 r<θ=26.9258<-1.8338

5. Re and

Re(z1^3)= -44
(Re(z1))^3= 64

Q3.2 Find values in the rectangular form


1. 2.
X+JY=0.8336-0.9889i X+JY=0.000+11.5487i

3. 4.
X+JY=0.000+11.5487i X+JY=9.6667-2.7991i

5.
X+JY=7.7411-26.1865i

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

5
be defined. For example to plot the line for , 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 achieve?


Created a matrix that counted starting from negative 10 to positive 10.

After defining x, we should now define y.


Q4.2 Define y in terms of x.
Y is a dependent variable while the X variable is the independent one. Meaning, that we express
Y in terms of X explicitly.

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

6
Q4.3 What features are added to the plot when the following commands are issued?
a. grid on
- a grid is added on the plot
b. axis([-10 10 -35 35])
- the line “steeps” by making a limit between -10 to 10 only
c. xlabel(‘x’)
- a label beneath the graph is added entitled “X”
d. ylabel(‘y’)
- a label besides the graph is added entitled “Y”
e. title(‘Line’)
- a title appears above the graph entitled “Line”
f. clf
- the graph is cleared

Q4.4 What happens when you change the plot command to the following?
a. plot(x,y,’r’)
- a line emerges from -10 and reaches 10 and the line is color red.
b. plot(x,y,’--')
- the line changes from a straight line to a dashed one
c. plot(x,y,’—o’)
- the code results into a line with “o”s and a line is passed
through them
d. plot(x,y,’-.xg’)
- the line that results is a broken line with X’s that appears.

Q4.4 Using the plot function, obtain the graph of the functions and .
Plot the curves on the space provided below.

7
8

You might also like