Professional Documents
Culture Documents
Lab # 02
Signals and Systems
Name:(PC) Muhammad Shoaib Talib (281018)
Deg/Syn:DE-40 CE” A”
Submitted To: Ma’am Sitwat Khaliq
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:
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
H is created and then multiplied by the scalar 5.But the values stored will be same after
execution of the commands
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:
• 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:
• 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)
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”