You are on page 1of 14

Assignment # 1

EMI-Lab
By: -
Hussain Ali (FA20-EEE-008)
Abdul Hanan (FA20-EEE-016)
Ali Ahmed Tariq (FA20-EEE-028)

Q1. Identify if the following variable is a scalar, a row vector, a column vector,
or a matrix and write your findings in the blank space provided in front of each
part.
a. var = [1 2 3 4 5]: - Row vector.
b. var = [100]: - Scalar.
c. var = [10:10:50]: - Row vector.
d. var = [10:15]: - Row vector.
e. var = [5: -1:1]: - Row vector.
f. var = [1;2;3]: - Column vector.
g. var = [0 -1; 2 4]: - Matrix.
h. var = [1 2 3 4 5]’: - Column vector.
Q2. Create a 3 × 2 matrix containing arbitrary data. Explore using the following
MATLAB functions. (Where N = 3 and M = 2 for this example).
a. zeros (N, M): -
Ans = 0 0
0 0
0 0

b. ones (N, M): -


Ans = 1 1
1 1
1 1

c. eye(N): -
Ans = 1 0 0
0 1 0
0 0 1

d. rand (N, M): -


Ans = 0.8147 0.9134
0.9058 0.6324
0.1270 0.0975

e. Find a way to use the square matrix eye (2) as part of your 3 × 2 matrix. Verify
the sizes of your arrays using size () and length () functions.
var = [1 2; eye (2)]
var = 1 2
1 0
0 1

f. Give a one-line explanation of each function in the space below.


zeros (): -
It is used to make matrix of zeros.

Ones (): -
It is used to make matrix of ones.

eye (): -
It is used to make diagonal matrix.

rand (): -
It is used to make matrix of random number.

size (): -
It is used to get the size of matrix.
length (): -
It is used to get longest dimension of matrix (row or column).

Q3. Use the functions size () and length () to compute the size of the arrays
given by array1 = -1: 0.10.1:1 and array2 = 10: 2: 28. Consider general array
constructor patter, i.e., array = start: step: stop. Answer in the space provided
after evaluate ng each expression in MATLAB.
a. How many elements are in each array? array1: - 12_&_array2: - 10.
b. Can you add these arrays? Give reason. No, because dimensions are not same.
c. Now concatenate ones such that the number of columns in array1 are
increased by 2, (i.e., says array3 = [ ones (1,2), array2]. Can you add array1 and
array3? Give reason to your answer. Yes, because now both have same number of
dimensions.

d. Multiply array1 and array2 without the element-by-element operator (“.”).


Write the error prompted as well as the reason for the error. No, because number of
row elements in array1 are not equal to number of columns in array2.

e. Multiply array1 and the transpose of array2 (i.e., array 1 * array2’) as in part
d. Explain your findings? No, because dimension problem exists here.
f. Repeat the same with the expression given by array2’ * array1. What re the
dimensions of your output now? No, because dimension problem exists here.
Q4: -
a. Following the same procedure, compute the values of the following function
with input given along. (Write the first five values of the outputs in table
below).

MATLAB Domain Range (first five values)


Function
log2() 2: 2: 14 1.0000 2.0000 2.5850 3.0000 3.3219
sin () {0, 𝜋/10, 2π} 0 0.3090 -0.0000
sec () {0, 𝜋⁄10, 2π} 1.0000 1.0515 1.0000
Tan () {0.25𝜋, 𝜋⁄10, 0.25π} 1.0000 0.3249 1.0000
atan () 0: 0.1: 1 0 0.0997 0.1974 0.2915 0.3805

b. Use the following functions write down the results in the following table.
MATLAB Domain Range (first five values)
Function
round () 2: 2: 14 2 4 6 8 10
ceil () {0, 𝜋⁄10, 2π} 0 32 7
floor () {0, 𝜋⁄10, 2π} 0 0 6
c. What does each function do to the input?
round (): -
It is used to round off decimal number to any nearest integer.
ceil (): -
It is also used to round off number but towards larger integer.
floor (): -
As its name indicate it round off number towards smaller integer.
Q5. Let’s visualize(plot) a sinusoid represented by the following expression,

x(t) = sin(2𝜋𝑓𝑡) ∀ 𝑡 ∈ [0,5] and 𝑓 = 1 𝐻𝑧


After solving as indicated in question we get: -

Now open the variable “x” from


the command window. Insert a value at the 52nd place and execute the “plot
()” function again. (You may use arrow
key up from your keyboard to recall the
code line-by-line) and answer the
following.
a. Write the syntax for the plot () function and brief description.
Syntax: - plot (x, t, s).
Plot is a function that is used to plot a chart of vector having amplitude ‘x’ and time ‘t’ and
character string ‘s’. ‘x’ is a combination of different trigonometric equations, while ‘t’ is
any vector that indicate time and ‘s’ is character string that tell us about the graph
characteristic that is going to be made.

b. Did the code run successfully? Not at all.


c. What error did you encounter? Please write the error displayed by
MATLAB in your command window.
Length error, as we have increase length of
amplitude by one (52).

d. How can you avoid such errors?


We can get ride of this error by making length of amplitude ‘x’ equal to length of time ’t’.

e. Define a new variable Fs = 8000; where Fs is the sampling frequency, write


the following code and listen to the sound.
See the expression for plot and the dimension
of the variables. What happens when we only
use the plot command as plot (t, , ‘b-’)? How
many cycles of the signals can you see in the
figure?

After coding we get: -

and when we plot (t, , ‘b-’) ,it gives error because


the is no amplitude and without one parameter
it cannot plot it. We see 12 cycles in graph.
Exercise Questions.
Q1. Create a 5 × 5 matrix A using the magic function and comments on the
results. (Include results as in command window in our report)
a. Add the matrix A with another 5 × 5 matrix created using the rand ()
function.

b. Multiply the matrix A by 2.

c. Multiply the matrix A by a 1 × 5

vector created using rand () function.


d. Multiply the matrix with a diagonal matrix.
(Construct the diagonal matrix manually).

e. Find out the inverse of


this matrix.

f. Multiply this matrix by


itself to get A2. The find
the maximum value of the
matrix using the max ()
function.

g. Multiply this matrix by itself using the element-


by-element operator.
h. Interchange the rows 2 and 3 of the matrix A.

i. Replace the numbers on the locations


(2:3,3) and (4,3:5) with zeros.

Q2. Evaluate the following expressions in MATLAB and give the length of time,
‘t’ and output, ‘x’ vectors. Plot the signals as well. (t=start value: 0.05: end value;).

a. x(t) = 2cos(10𝜋𝑡) ∀ 𝑡 ∈ [0,5]


b. x(t) = 2cos(10𝜋𝑡). 𝑠𝑖𝑛 (16𝜋 𝑡) ∀ 𝑡 ∈ [0,5]

c. 𝑥(𝑡) = −2𝑒^0.5𝑡 𝑠𝑖𝑛 (2𝜋 𝑡) ∀ 𝑡 ∈ [0,10]

d. 𝑥(𝑡) = log2(1.5 + 10𝑒^0.5𝑡 𝑠𝑖𝑛 (2𝜋 𝑡)) ∀ 𝑡 ∈ [0,10]

Q3. What is the purpose of the following built-in MATLAB functions and
special variables; give concise answers include format of the functions and an
example as well. (Use MathWorks website or MATLAB help): -
1. abs (): -
It is used to give absolute value of any complex number (say X).
Format: - abs (X)
Example: -

2. real (): -
It is used to give the complex part of complex number.
Format: - real ()
Example: -

3. conj (): -
It is used to give the complex number as output.
Format: - conj(X)
Example: -

4. diag(): -
It is used to make the diagonal matrix.
Format: - diag (X, Y)
Example: -

5. size (): -
It is used to get the size of matrix (means order of matrix).
Format: -size(X)
Example: -
6. inv (): -
It is used to give inverse of specified matrix(X).
Format: -inv(X)
Example: -

7. max (): -
It is used to get maximum number of matrix(X).
Format: - max ()
Example: -

8. atan(): -
It is used to give inverse of number.
Format: - atan(X)
Example: -

9. sound (): -
It is used to make signal in audible form (signal having time t).
Format: - sound (X, t)
Example: -

10. soundsc (): -


Similarly, as in sound it plays sound of signal, but it first passes it from scaling which make it
louder.
Format: - soundsc (X, t, Sf)
Example: -

11.audioread(): -
It is used to read any audio file.
Format: - audioread (filename).

12.audiowrite (): -
It is used to write a matrix of audio data.
Format: -audiowrite(filename)

13.NaN: -
It is used to return the scalar representation of ‘not a number’.
Format: - NaN (X)

14. Inf/inf: -
It is used to represent infinity.
Format: - inf (m, n)
Example: -
15. mod (): -
It is used to give mode of number(n).
Format: - mode(n)
Example: -

16.ceil (): -
It gives closet integer number from number(n).
Format: - ceil(n)
Example: -

17. rand (): -


It is used to make a matrix having random elements.
Format: - rand (n)
Example: -

18. plot (): -


It is used to plot a specific signal.
Format: - plot (X, t)
Example: -
Q4. Use MATLAB to listen to a three-second sinusoidal waveform scaled by a
decaying exponential given by ∀ t ∈ [0, 1], f (t) = 𝑒 −5𝑡 𝑠𝑖𝑛 (2𝜋 × 450t). Use a
sample rate (Fs) of 8,000 samples/second. Describe how this sound is different
from pure sinusoidal sounds that you listened to in the in-lab section.
From pure sine signal this sound is louder in start while pure sine signal remains same. Also,
frequency difference is there. Because of small frequency sine signal also has more bass than this
signal.

Where did you use the element-by-element multiplication? Why was it necessary?
Yes, we use element-by-element multiplication, without this error will occur. This is because of
incorrect dimensions in both matrixes.

You might also like