SNS Lab #04 Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

MUHAMMAD AHMED

190319

BEEE-4B

EXPERIMENT 04

Plotting functions in MATLAB

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).

Simple Plotting Commands:


The simple 2D plotting commands include:

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.

Some Important Points:


 If X and Y are both vectors, then they must have equal length.
 If X and Y are both matrices, then they must have equal size. The plot function plots columns of
Y versus columns of X.
 If one of X or Y is a vector and the other is a matrix, then the matrix must have dimensions such
that one of its dimensions equals the vector length. If the number of matrix rows equals the vector
length, then the plot function plots each matrix column versus the vector. If the number of matrix
columns equals the vector length, then the function plots each matrix row versus the vector. If the
matrix is square, then the function plots each column versus the vector.
 If one of X or Y is a scalar and the other is either a scalar or a vector, then the plot function plots
discrete points.

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.

Plotting Multiple Lines in One Figure:


To plot multiple lines in one figure, use figure command as used in the given code.
Specifying Line Style, Color and Marker:
In order to differentiate between different plots in a figure, you might need to use different line styles for
these plots. For example:
Adding Title and Axis Labels to Plot:
The plot axes can be labelled by using xlabel command for x-axis and ylabel command to label y-axis.
The legend command is used to assign a label to the respective plot line. It is important to assign the
legend in the order of plotting. For example:
Subplots:
subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by
p. The first subplot is the first column of the first row, the second subplot is the second column of the first
row, and so on. If axes exist in the specified position, then this command makes the axes the current axes.

Upper and Lower Subplots:


To create a figure with 2 stacked subplots, type the following commands:
Multiple Subplots:
To create a figure with four subplots, type the following commands:

Multiple Subplots with Varying Sizes:


To create a figure with multiple subplots with different sizes, type the following commands:
Stem Plots:
The stem command plots discrete sequence data. stem(Y) plots the data sequence, Y, as stems that extend
from a baseline along the x-axis. The data values are indicated by circles terminating each stem.

 If Y is a vector, then the x-axis scale ranges from 1 to length(Y).


 If Y is a matrix, then stem plots all elements in a row against the same x value, and the x-axis
scale ranges from 1 to the number of rows in Y.

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.

 If X and Y are both vectors, then stem plots entries in Y


against corresponding entries in X.
 If X is a vector and Y is a matrix, then stem plots each
column of Y against the set of values specified by X, such
that all elements in a row of Y are plotted against the same
value.
 If X and Y are both matrices, then stem plots columns of Y
against corresponding columns of X.

Plotting Discrete Values:


To create a stem plot for random discrete values, use the following code:
Plotting a Sinusoid:
Create a stem plot for cosine(t) using the following code:
Plotting Multiple Data Series:
To create a plot with two data series using stem command, type the following code:

Fill in Plot Markers:


Using ‘filled’ keyword in the stem command, fills the plot markers as shown in the following example:

Specifying Stem and Marker Options:


To specify marker options for stem command, use the following code:

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:

Bar Graph with Group of Bars:


To create groups of bars where each bar might represent varying characteristic, use the following code:
Lab Tasks:
1. Plot the following function in MATLAB.
x ( t )= A∗cos ⁡¿
where, frequency f = 100 Hz, t is from 0 to 5 seconds and angle = 10 and A is the amplitude
with the value 3.

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.

You might also like