Anda di halaman 1dari 8

Signals, Spectra and Signal Processing (EC413L1)

Name: Date:
Section: 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

− 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 =

-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.
4 1 0  0 2 - 8 2
     
B =  1 3 2 C = − 2 0 6 d =  1 e = [9 0 5]
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)

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

3
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

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)

4
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 25 z 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.

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

6
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,’--')

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

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

7
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

Anda mungkin juga menyukai