SNS Lab #04 Report
SNS Lab #04 Report
SNS Lab #04 Report
190319
BEEE-4B
EXPERIMENT 04
Objectives:
Familiarizing students with MATLAB plots
Equipment required:
MATLAB installed on PCs
Background Knowledge:
Plotting:
MATLAB can be used to create both two- and three-dimensional plots. The basic command is “plot( )”
which can have multiple optional arguments. To plot a function, following steps need to be followed:
Define x, by specifying the range of values for the variable x, for which the function is to be
plotted.
Define the function, y = f(x).
Call the plot command, as plot(x, y).
Command Plot
plot Plot in linear coordinates as a continuous function
stem Plot in linear coordinates as a discrete function
log Logarithmic x and y axis
bar Plots a bar graph
polar Polar coordinates
The figure below shows different types of plot in MATLAB using the above-mentioned commands.
Plot Vectors or Matrices:
plot(x,y) plots vector x versus vector y. If x or y is a matrix, then the vector is plotted versus the rows or
columns of the matrix. plot(y) plots the columns of y versus its index. If y is complex, plot(y) is
equivalent to plot(real(y),imag(y)). In all other uses of plot, the imaginary part is ignored.
Customization of Plots:
Line style, marker, and color, can be specified to customize the plots. The symbols can appear in any
order. You do not need to specify all three characteristics (line style, marker, and color). For example, if
you omit the line style and specify the marker, then the plot shows only the marker and no line.
There are many commands used to customize plots by annotations, titles, axes labels, etc. A few of the
most frequently used commands are:
Example:
Create x as a vector of linearly spaced values between 0 and 2π. Use an increment of π/10 between the
values. Create y as cosine values of x. Create a line plot of the data.
stem(X,Y) plots the data sequence, Y, at values specified by X. The X and Y inputs must be vectors or
matrices of the same size. Additionally, X can be a row or column vector and Y must be a matrix with
length(X) rows.
Bar Graph:
bar(y) creates a bar graph with one bar for each element in y. If y is an m-by-n matrix, then bar creates m
groups of n bars. bar(x,y) draws the bars at the locations specified by x.
To create a bar graph for a vector y, use the following code:
Code:
Output/ Graph:
2. Plot the given function:
x ( t )=sin ¿
where:
i. frequency f = 10 Hz , t is from 0 to 3 seconds and angle is 0. Apply time shift to this
signal toward right by time t o . Take time shift t o=0.25∧0.5 seconds.
ii. frequency f = 10 Hz , t is from 0 to 3 seconds and angle is π/2. Apply time shift to this
signal toward right by time t o . Take time shift t o=0.25∧0.5 seconds.
Plot all the versions of signal in a single window with the help of ‘subplot ()’ command.
(Hint: You will have four plots in total)
Code:
Output:
3. Make a function that plots continuous time unit step and unit impulse signals. Call this function in
the script file by passing the time vector ‘t’ in its arguments.
Impulse Signal:
Code:
Output:
Code:
Output:
4. Plot the following function in MATLAB.
x ( n )=u ( n ) −u ( n−10 )
where, n is from 0 to 100. Use stem command to plot x(n).
Code:
Output:
5. Generate bar graph for the following data in MATLAB.
Year Price
2012 51,677
2013 52,929
2014 38,484
2015 38,850
2016 34,565
2017 36,378
2018 40,326
(Hint: Year should be on the x-axis and each bar should have a categorical tick label of the
respective year)
Code:
Output:
6. Let y 1=cos ( x 1) and y 2=2∗sin ( x 2), where x1 and x2 are different sets of 50 data values. x1
ranges from 0 to 2π while x2 ranges from 0 to 3π. Use stem command to plot these two data
series in the same figure.
Code:
Output:
Conclusion:
In this lab I learnt about different command to graph the discrete time (stem
command) and continuous time signals (plot command). I also learnt about labelling ,subplot
command and how to make different figures and I also learnt about how to make a bar graph.