You are on page 1of 9

EL220 Signals and Systems

Experiment # 02

Plotting Graphs in MATLAB

Performed on ____________________

Student Name:
Roll Number: Section:

Maximum Marks Performance = 05 Viva = 05 Total = 10


Marks Obtained
Remarks (if any)

Experiment evaluated by

Instructor Name: Rameen Anwar

Signature:

Copyright © Department of Electrical Engineering – Usman Institute of Technology


Theory

MATLAB is a powerful high level programming language used for scientific


computations. It supports a rich suite of scientific,mathematical,statistical function and
this functionality extended to the interactive graphical functionalities which will be
discussed in the following lab. In this LAB we will learn basic plotting functionality in
MATLAB. Some of the functions used in plotting along with their description are given
in the table below:

Copyright © Department of Electrical Engineering – Usman Institute of Technology


1. Plotting:
Plotting can be done for continuous time (CT) signals or discrete time (DT) signals.
Commands for them can be:

 plot (for CT signals)


 stem (for DT signals) ~ these will be implemented in next experiment.

In the command window in MATLAB window, input the time from t=0 sec to t=10 sec in
increments of 0.1 sec as follows:

>>t=[0:0.1:10];
>>y=cos(t);
>>plot(t,y)

2. Plotting Multiple Dataset In One Graph:


We can plot multiple graphs on
one plot. See the example

>>t=0:pi/100:2*pi;

Copyright © Department of Electrical Engineering – Usman Institute of Technology


>>y=sin(t);
>>y2=sin(t-0.25);
>>y3=sin(t-0.5);
>>plot(t,y,t,y2,t,y3)

Another approach is to use hold command


>>x=0:0.01:2*pi;
>>plot(x,sin(x))
>>hold on
>>plot(x,cos(x))
>>hold off

3. Sub Plots:
It can sometimes be useful to display
multiple plots on the same figure for comparison. This can be done using the subplot
function that takes arguments for number of rows of plots, number of columns of plots,
and plot number currently being plotted:

Example:
clear all
close all
clc
x=0:0.1:2*pi;
subplot(2,2,1)
plot(x,sin(x));
subplot(2,2,2)
plot(x,cos(x));
subplot(2,2,3)
plot(x,exp(-x));
subplot(2,2,4);
plot(x, x.^3);

4. Colors, Symbols and Line Types

Copyright © Department of Electrical Engineering – Usman Institute of Technology


You can change colors, symbols and line types as demonstrated

x=0:pi/16:2*pi;
y=sin(x);
plot(x,y,'r *--')

xlabel('x')
ylabel('sin(x)')

5. Multiple Plots on a Single Graph


x=0:pi/16:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,'* -',x,y2,'r s -')
xlabel('x')
ylabel('sin(x), cos(x)')
title('Trig Functions')
legend('sin','cos')

Scatter Plot:

A scatterplot is a useful summary of two or more variables, usually drawn before


working out a linear correlation coefficient or fitting a regression line. It gives a good
visual picture of the relationship between the two variables, and aids the interpretation of
the correlation coefficient or regression model.
Command:
scatter3(x,y,z)

Copyright © Department of Electrical Engineering – Usman Institute of Technology


Fig: 3D Scatter Plot Fig: 2D Plain Scatter Plot

Contour Plot:-

Contour plots (sometimes called Level Plots) are a way to show a three-dimensional
surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis
and a response variable Z as contours. These contours are sometimes called z-slices or
iso-response values. This type of graph is widely used in cartography, where contour
lines on a topological map indicate elevations that are the same. Many other disciples use
contour graphs including: astrology, meteorology, and physics. Contour lines commonly
show altitude (like height of a geographical features), but they can also be used to show
density, brightness, or electric potential.
Command:
contour(X,Y,Z)

Fig: Regular contour plot Fig: Polar contour plot

Copyright © Department of Electrical Engineering – Usman Institute of Technology


Heat Map (Color-map) :

A heat map (or heatmap) is a graphical representation of data where the individual values


contained in a matrix are represented as colors

Fig: Heatmap

Default color map option in matlab

parula

jet

hsv

hot

cool

spring

summer

autumn

winter

gray

bone

copper

Copyright © Department of Electrical Engineering – Usman Institute of Technology


pink

lines

colorcube

prism

flag

white
Fig: Default color map color range
Command:
colormap(bone)

Tasks:
Q1. Plot sine wave w.r.t time interval t = [0:0.5:20]

Q2: What is function of linspace()? Describe the function on the basis of the following
code.
x=linspace(0,2*pi,25);
y=sin(x);
y1=cos(x);
subplot(1,2,1)
plot(x,y);
subplot(1,2,2)
plot(x,y1);

Q3: In above question, change line color for sine wave to cyan and for cosine wave to
magenta as indicated in color table. Also assign legends and appropriate title to each
plot.
Q6: Using the code in Q3, generate and plot the discrete form of both y and y1 using
appropriate command.

Q4: Obtain scatter plot for the given code.

[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
scatter3(x,y,z)

Q5: Obtain contour plot for the following code


x = linspace(-2*pi,2*pi);

Copyright © Department of Electrical Engineering – Usman Institute of Technology


y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
contour(Z)

Q6: Obtain heat map for the following code


load spine
figure
image(X)

Q7. Generate multiple plots with the following data :


Suppose t=0:0.01:1.Now plot the following three equations with different color and line
styles on same graph
Y1 = cos(2πt)
Y2 = cos(2πt+π/2)
Y3 = cos(2πt-π/2)
Y4 = sin(2πt)

Open Ended Task:

Q1. Why we use stem() command. Comment on it by using any example


Q2. What would be impact on waveform if we increase range of time? Give any example

Copyright © Department of Electrical Engineering – Usman Institute of Technology

You might also like