TUTORIAL QUESTIONS
Q1:
A function F has been declared as:
𝐹 (𝑦) = 2𝑦.⁄𝑦.!"#
Write the MATLAB command(s) to find a minimum number y_min
between -1 < x < 1.5
How many times will the following MATLAB commands be executed?
y = 3;
while (y < 8)
disp('Am I done yet?')
y = y + 2.5;
end
Using “fprintf” and nested “for-loop” commands, write a MATLAB
script to print the following numbers pattern:
1
12
123
1234
123456
1234567
Q2: Find a short MATLAB expression to build the matrix:
Give a MATLAB expression that uses only a single matrix
multiplication with matrix B above (Q7a) to obtain
i) The sum of columns 5 and 7 of B
ii) The last row of B
Q3
Write two ways to display the following matrix in MATLAB:
3 4 5 6 7
𝐴=* 1
13 14 15 16 17
Given a function prototype
function [out] = examfcn(in)
Write any Three (3) different calls to the function, each of which uses a
distinctly different method to provide a value for the required parameter.
Assume any variables that you need are available.
Using the MATLAB built-in functions (zeros,ones,eye), Create the
following matrix
0 0 0 1 1
0 0 0 1 1
⎛ ⎞
1 0 0 0 0
⎜ ⎟
𝐴 = ⎜0 1 0 0 0⎟
⎜0 0 1 0 0⎟
0 0 0 1 0
⎝0 0 0 0 1⎠
Q4 Let
6 4 3
𝐴 = :7 3 4<
9 6 5
Using column operator (:), create a column vector that contains all the
columns of A
Write a MATLAB commands to generate a vector Y that start at 1 and
counts by squares to 576(24^2), that is: 1 4 9 16….
Write a MATLAB commands for drawing a curve:
$ # & #
f(x, y) = − *%1 − *#1 − 16
for 5 ≤ x ≤ 5 and 5 ≤ y ≤ 5 using surf function. Label the axis and
assign title. Remove grid. Color the curve in gray.
Q5
Suppose V is a vector. Write two different MATLAB code to change all
the 1’s in V to be 2’s. That is, if V starts off as [0 1 8 1 4.4], then it should
become [0 2 8 2 4.4].
A MATLAB function ‘function_y has been declared as an inline
function. Write the MATLAB command(s) to find a zero “y_zero”
near y = 3.5.
Write MATLAB code that will produces an audio waveform (8 kHz
sampling frequency) that contains a sequence of nine tones with
frequencies 659, 622, 659, 622, 659, 494, 587, 523, and 440 Hz. Then
add to this waveform a copy of itself in which every other sample has
been multiplied by −1. The code should also be saving a .WAV file, and
include a specgram command to plot its spectrogram with correctly label
time and frequency axis.
Q6
Suppose that you have defined the function “Calc” as follows:
Function a = Calc(x)
b=2*x
a=2*z
b=a-4
What are the values return from the following calls:
(i) >> Calc (10)
(ii) >> Calc (Calc (2))
(iii) >> Calc (Calc (Calc (2)))
(iv) >> Calc (1)
Write a MATLAB commands to generate a vector Y that start at 1 and
counts by squares to 576(24^2), that is: 1 4 9 16….
Write four (4) different calls to the function, each of which uses a distinctly
different method to provide a value for the required parameter. Assume
any variables that you need are available.
Solution Hints:
Q1:
Hints:S: y_min = fminbnd(func,-1,1.5)
Hints::
2 times
Q2:
Hints:
for i=1:7 % 7 rows altogether
for j= 1:i % this is important to set the length of each row.
fprintf('%d', j)
%print the numbers on each row, num-by- num
end
fprintf('\n') % start from a new line.
End
Hints:
b = [1:7; 9:-2:-3; 2.^(2:8)]
Hints: (i) b * [0 0 0 0 1 0 1] ’
(ii) [0 0 1] * b
Q3
Hints:
i) A=[ 3 4 5 6 7 ; 13 14 15 16 17]
ii). A=[ [3:7] ; [13:17] ]
Hints:
a) Passing a constant: x = examfcn(3)
b) Passing variable: x = examfcn(y)
c) Passing an arithmetic expression: x = examfcn(2*y/3)
d) Passing another function: x = examfcn(cos(y))
Hints
>> A1= zeros(2,3);
>> A2= ones(2);
>> A3= eye(5);
>> A=[A1 A2; A3];
Q4 hints:
V=[ A(:,1) ' A(:,2) ' A(:,3) ' ] '
Hints: Y = [1:24].^2
Hints:
>> x=[-5:0.1:5];y=[-5:0.1:5];
>> [A,B]=meshgrid(x,y);
>> Z=-(A/5).^2-(B/2).^2-16;
>> surf(A,B,Z)
>> colormap gray
>> xlabel('X-Axis'); ylabel('Y-Axis');
>> title('Question-3C’)
>> grid off
Q5
Hints:
Option1:
Vones = Find(V = =1)
V(Vones) = 2
Option 2:
V(V = =1) =2
Hints:
y_zero = fzero(function_y,3.5)
Hints
f = [659 622 659 6 22 659 494 587 523 440];
fs = 8000; % sa mpling frequency
d = 0.5; % duration per tone
t = 0:1/fs:d-1/fs;
w = sin(2 * pi * f’ * t)/2;
w = w’; w = w(:)’;
w = [w, w .* (mod((1:length(w)), 2) * 2 - 1)];
wavwrite(w, fs, 16, ‘result.wav’);
specgram(w, [], fs);
Q6
Hints:
(i) 40
(ii) 32
(iii) 128
(iv) 4
Hints: Y = [1:24].^2
Hints:
i) Hardwire: x = examfx(3)
ii) Variable: x = examfx(y)
iii) Arithmetic expr: x = examfx(2*y/3)
iv) Another fcn: x = examfx(cos(y))
ASSIGNMENT
Q1
Suppose K is a new variable, with the following MATLAB statement:
K = [-10: -1: -15; -2: 3];
What are the elements that will be generated for K?
Given vector a = [3,2,1,0] and b = [5 6 7 8], write the output of each of
the following MATLAB commands:
(i) a.*b
(ii) b’
(ii) b-a(2)+4
(iii) (a.^2)/b(end-2)
(iv) b’’
Write a MATLAB code to calculate the following summation:
3∗(2+1)+
4∗(3+2+1)+5∗(4+3+2+1)+6∗(5+4+...+1)+....
+1000∗(999+...+1)
Q2 a) Write a MATLAB code to create a matrix Y that is a 5x5 array of
zeros with 1 in the middle of it.
b) Suppose that m is a positive integer already defined in your
workspace. (for instance, m might be 7). Write a for-loop that
calculates m factorial (1*2*3*….m). Indicate which variable contains
the value of m-factorial when your loop is finished.
c) MATLAB commands to plot, on the same figure, the two functions:
Where the variable t varies from 0 to 10 with step 0.5
• Draw the function f in blue 0 and the function g in red *
• Give title to your graph and label the axes.
Q3:
a) Suppose K is a new variable, with the following MATLAB
statement:
K = [-10: -1: -15; -2: 3];
What are the elements that will be generated for K?
b) Given vector a = [3,2,1,0] and b = [5 6 7 8], write the output of each
of the following MATLAB commands:
(iii) a.*b
(iv) b’
(v) b-a(2)+4
(vi) (a.^2)/b(end-2)
(vii) b’’
c) Write a user-defined MATLAB Function for the following math
function:
z(x, y) = e$ cos(y) + sin(x # − y)
The input to the function are x and y while the output is Z. Write
the function such that x can be a vector.
• Use the function to calculate Z(-3,4) and Z(5,-7).
• Use the function to make a 3D plot of the function Z(x,y)
for −π ≤ x ≤ π and −π ≤ x ≤ π