Review of MATLAB
Review of MATLAB
INSTRUCTIONS
• This is an individual programming exercise. Any clarifications or questions must be
directed to the instructor or student assistant.
• Save your code in an M-file with the file name CoE121_PE01_<LastName>. Submit
your code together with the required figures and functions via the submission bin in
UVLe.
• The deadline of this exercise is on January 21, 11:55 PM. A 10-point deduction per
day will be incurred for late submissions.
1 1 / 2 3 4 9 − 3
0 − 8 10
A = 6 10 1 B= C = 13 0 7
5 3 1
0 3 2 1 2 11
(2 POINTS) Write in the blank below the commands to generate matrices B and
C.
>> ____________________________;
>> ____________________________;
2. Each element of a given matrix can be accessed by specifying its indices. A(row,
column) will give you the element at the given row and column. For example, use
the command below to access the value of the matrix at the 2nd row and 2nd
column.
>> B(2,3) – C(3,1) = ______ >> B(2,[1 3]) * B([1 2],3) = __________
1 3]
(2 POINTS) How would you generate the matrix 𝐷 = [ from matrix A? Write
0 2
the command (just one) below.
>> ____________________________;
>> n = [-5:1:5]
(2 POINTS) Create a vector of time samples from t = 0 to 10 sec with 0.25 sec
interval.
>> ____________________________;
1 1/2 3
(2 POINTS) How would you generate the matrix 𝐸 = [ 5 3 1] from matrices A,
13 0 7
B and C? Write the command (just one) below.
>> ____________________________;
4. For large matrices, the find() command is useful in determining the row and
column of a certain value of the matrix. (type ‘help find’ at the MATLAB prompt
for more details)
To find the indices of matrix C that are zero, enter the command
0 9 0
(4 POINTS) Without using loops, produce the matrix 𝐹 = [13 0 0 ] from matrix
0 0 11
C only. Write in the blanks below the commands to produce F.
>> ____________________________;
>> ____________________________;
>> ____________________________;
>> ____________________________;
2. To get the inverse of a matrix, use the inv() command. The inverse of matrix A is
>> inv(A)
Solve the following system of equations using MATLAB and give the values of the
unknowns in the space provided below.
(4 POINTS) What are the commands that you use in solving the unknowns?
>> ____________________________;
>> ____________________________;
>> ____________________________;
3. (2 POINTS) Using the vector 𝑟 = [0.5 + 𝑒 −𝑗(𝜋/3) , 𝑒 −𝑗(𝜋/6) , 2], determine the difference
between the transpose() command and the ‘ operator. Write your answer in the
space provided below.
_________________________________________________________________
2. To view the signals, use the available plotting and viewing functions in MATLAB.
Some examples of these functions are the figure(), plot(), and subplot(). To view
the different samples of the sinusoid, use the stem() command.
(2 POINTS) Which of the two figures shows the correct sketch of the signal? Why?
_________________________________________________________________
Use the xlabel(), ylabel() and title() commands to add labels to your plot.
The plot() command allows you to vary the way the signal is plotted. If you want
to change the color or the plot, just add the appropriate parameters to the plot
command. For more details, use ‘help plot’ command.
To close a figure, use the close() command. This is to avoid too much cluttering
of figures in the desktop. To close all figures, just type
To make multiple plots on a single figure window, use the subplot() command.
For example, to plot on different rows or columns, specify the row and column in
the subplot() command. The following examples will help illustrate.
3. Noise signals with uniform and Gaussian distribution are generated using the
rand() and randn() commands, respectively. The randn() function by default will
generate random numbers with zero mean and unit variance. To change the
variance, just multiply the output of randn() with the square root of the desired
variance. To vary the mean, add/subtract the desired mean.
Example: Generate the tones of the octave with the following frequencies {262,
294, 330, 349.7, 392.6, 440.6, 494.5, 524} and the duration of each sinusoid is
approximately 1 second. Play each tone in succession using a simple for loop.
>> fs = 8000;
>> freq = [262, 294, 330, 349.7, 392.6, 440.6, 494.5, 524];
>> amp = ones(1,8);
>> phase = zeros(1,8);
>> t = [0:7999]’/8000;
>> octave_sig =
(ones(length(t),1)*amp).*sin(2*pi*t*freq+ones(length(t),1)*phase)
To generate a composite sinusoidal signal, sum the samples across each row (or
column). Type ‘help sum’ for more options.
(10 POINTS) Generate the DTMF of the last 7 digits of your student number. Each
digit must have a duration of 1 second, followed by a half-second silence. Save
the signals as <StudentNumber>_dtmf.wav.
Some notes:
• To save all the commands you entered, you can use the command ‘diary <filename>’
• If you need any help on any of the MATLAB commands, just type ‘help command’
from the MATLAB command prompt. If you want to search for any command that
contains a certain keyword, use the ‘lookfor’ command.