You are on page 1of 12

SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

NATIONAL UNIVERSITY OF SCIENCES


AND
TECHNOLOGY

Lab # 02
Signals and Systems
Name:(PC) Muhammad Shoaib Talib (281018)
Deg/Syn:DE-40 CE” A”
Submitted To: Ma’am Sitwat Khaliq

MUHAMMAD SHOAIB TALIB 1


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

Lab 02
Lab Objectives
The objective of this lab is to create an understanding of using loops and conditional statements
in MATLAB. It also introduces the basics of plotting signals

Lab Tasks
Task#01:
Write a MATLAB code to display the following using for loop, while loop and if statements
separately:
• First 30 numbers
• First 30 even numbers
• First 30 odd numbers

Solution:

MUHAMMAD SHOAIB TALIB 2


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

MUHAMMAD SHOAIB TALIB 3


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

MUHAMMAD SHOAIB TALIB 4


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

MUHAMMAD SHOAIB TALIB 5


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

Task#02:
(a) Check whether the following set of commands :
for i = 1:20
H(i) = i * 5
End
have the same result as:
H = 1:20;
H = H*5
Solution:

Both commands will initialize the H, but the output of the first command will be different because
the values are initialized in the loop and displayed. Whereas in the second command first vector

MUHAMMAD SHOAIB TALIB 6


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

H is created and then multiplied by the scalar 5.But the values stored will be same after
execution of the commands

(b) Check whether following set of commands:


for n = 1:100
x(n) = sin(n*pi/10)
end
have the same result as:
n = 1:100;
x = sin(n*pi/10)
Solution:

MUHAMMAD SHOAIB TALIB 7


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

In the first command the values are initialized in the variable x after taking the sin of
the values in the loop therefore the initialized values in the x is displayed in the loop
whereas in the second command ,first the vector is being created and then sin of all the
elements in the vector is taken. After the execution of both commands the values stored
in the vector will be the same but the output will be difficult.

Task#03:
Run the following three MATLAB lines of code and explain why the plots are different:
• t=0:2*pi; plot(t, sin(t))
• t=0:0.2:2*pi;plot(t, sin(t))
• t=0:0.02:2*pi; plot(t, sin(t))

Solution:

MUHAMMAD SHOAIB TALIB 8


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

• These lines are plotted in the different way because the distribution of the points along the
X-Axis(time) is different. The plot t=0:2*pi; plot(t, sin(t)) contains the points from 0->2pi but
the difference between the points is of 1 unit so the plot is not smooth, Whereas the plot
t=0:0.2:2*pi;plot(t, sin(t)) contains the points having the difference of 0.2 unit which contains
the more distribution of the points as compared to the other one and having more uniform
smooth plot.

The third plot t=0:0.02:2*pi; plot(t, sin(t)) is having the difference of 0.02 between the points
and the plot is the most smoothest as compared to the others due to having more points

Task#04:
For the following, use the signal described as:

t=0:0.2:2*pi
Now perform the following operations on the signal t:

MUHAMMAD SHOAIB TALIB 9


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

• Put two plots on the same axis, i.e. sin(t) and sin(2t)
• Produce a plot without connecting the points
• Try the following command and comment:
• t=0:0.2:2*pi; plot (t, sin(t), t, sin(t),’r.’)

Solution:
Put two plots on the same axis, i.e. sin(t) and sin(2t)

MUHAMMAD SHOAIB TALIB 10


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

Produce a plot without connecting the points

Try the following command and comment:


• t=0:0.2:2*pi; plot (t, sin(t), t, sin(t),’r.’)

MUHAMMAD SHOAIB TALIB 11


SIGNALS AND SYSTEMS LAB#02 SYN-A LAB # 02

This command will print the two same plots on same figure and on same plot but it will print
the plot of the second command in “Red Color” with “dot(.) Marker”

MUHAMMAD SHOAIB TALIB 12

You might also like