You are on page 1of 5

SIGNAL AND SYSTEMS LABORATORY

By: Abdulbasit Sabaawi

Experiment [5]
Overview of MATLAB Plotting

MATLAB has three different types of arithmetic operations: column oriented


operations, array oriented operations and matrix arithmetic operations.
MATLAB has an excellent set of graphic tools. Plotting a given data set or the results
of computation is possible with very few commands. You are highly encouraged to plot
mathematical functions and results of analysis as often as possible. Trying to understand
mathematical equations with graphics is an enjoyable and very efficient way of learning
mathematics. Being able to plot mathematical functions and data freely is the most
important step, and this section is written to assist you to do just that.
Creating a Graph
The type of graph you choose to create depends on the nature of your data and what
you want to reveal about the data. MATLAB predefines many graph types, such as line,
bar, histogram, and other graphs. There are also 3-D graphs, such as surfaces, slice
planes, and streamlines.
Graph Components
MATLAB displays graphs in a special window known as a figure. To create a graph,
you need to define a coordinate system. Therefore every graph is placed within axes,
which are contained in a figure. The actual visual representation of the data is achieved
with graphics objects like lines and surfaces. These objects are drawn within the
coordinate system defined by the axis, which MATLAB automatically creates
specifically to accommodate the range of the data. The actual data is stored as properties
of the graphics objects. You might find it useful to combine both approaches. For
example, you might issue a plotting command to create a graph and then modify the
graph using one of the interactive tools.

rotate 3D Insert
colorbar Insert
zoom in/zoom out data cursor 1legend Hide / display
plot tools
SIGNAL AND SYSTEMS LABORATORY

command Syntax
linspace linspace(min,max,points)
logspace logspace(min,max,points)
plot plot(x,y,'property')
stem stem(x,y,'property')
xlabel xlabel('title of x-axis')
ylabel ylabel('title of y-axis')
title title('title of graph')
text text(x,y,'text')
legend Display a legend on graph
grid on
to display and hide the grid on the figure background
grid off
hold on
Multiple data in one graph
hold off
figure Multiple data in multiple graphs
subplot Multiple data in one graph but in different figures

Simple Plots:
The basic MATLAB graphing procedure, for example in 2D, is to take a vector (x) that
contains N samples x=(x1, … ,xN) and a vector (y) of N samples, y (yl, … , yN), locate the
points (xi, yi), with i =1, 2, …, N and then join them by straight lines. x and y both have
to be row arrays or column arrays of the same length.
clc
clear all
x=0:0.01:10;
y=sin (x);
plot (x, y);
xlabel ('input');
ylabel('output');
title('wave');
grid

2
SIGNAL AND SYSTEMS LABORATORY

specify line style and colors:


It is possible to specify color, line styles, and markers when you plot your data using the
plot command.

p1ot(x,y, 'color style marker');

(color style marker) is a string containing one to four characters (enclosed in single
quotation marks) constructed from a color, a line style, and a marker type as follows:

• Color strings are 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'. These correspond to cyan, magenta,
yellow, red, green, blue, white, and black.
• Line style strings are '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot. Omit the
line style for no line.
• The marker types are '+', 'o', '*', and 'x', and the filled marker types are 's' for square, 'd'
for diamond, '^' for up triangle, 'v' for down triangle, '>' for right triangle, '<' for left
triangle, 'p' for pentagram, 'h' for hexagram, and none for no marker.

Note: If you specify a marker type but not a line style, MATLAB draws only the marker.
For example: plot(x,y,'ks');
plots black squares at each data point, but does not connect the markers with a line. The
statement plot(x,y,'r:+');
plots a red dotted line and places plus sign markers at each data point.

Multiple data sets in one Graph:


Multiple x-y pair arguments create multiple graphs with a single call to plot. MATLAB
automatically cycles through a predefined (but user settable) list of colors to allow
discrimination among sets of data. See the axes Color Order and Line Style Order
properties.
For example, these statements plot three related functions of x, with each curve in a
separate distinguishing color.
1
sin(x)
1. Using a single plot: 0.8
sin(x-.25)
sin(x-.5)
0.6

x = 0:pi/100:2*pi; 0.4

y = sin(x); 0.2

y2 = sin(x-0.25); 0

y3 = sin(x-0.5); -0.2

plot(x,y,x,y2,x,y3); -0.4

The legend command provides an -0.6

easy way to identify the individual plots: -0.8

-1
0 1 2 3 4 5 6 7

legend('sin(x)','sin(x-.25)','sin(x-.5)');
3
SIGNAL AND SYSTEMS LABORATORY

2. using hold on and off:

x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
hold on
plot(x,y);
plot (x,y2);
plot (x,y3);
hold off

3. using subplot:

The subplot command enables you to display multiple plots in the same window or
print them on the same piece of paper. Typing:
Subplot(m, n, p);
partitions the figure window into an m-by-n matrix of small subplots and selects the p th
subplot for the current plot. For example, these statements plot data in two different sub
regions of the figure window:

x= 0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
subplot(2,1,1)
plot(x,y1);
grid on
subplot(2,1,2)
plot(x,y2)
grid on

x= 0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
subplot(1,2,1)
plot(x,y1);
grid on
subplot(1,2,2)
plot(x,y2)
grid on

4
SIGNAL AND SYSTEMS LABORATORY

4. using figures

clear all
x= 0:0.l:2*pi;
yl = sin(x);
y2 =cos(x);
plot(x,y1);
grid on
figure;
plot(x,y2);
grid on

Experiment:

Plot the function:

1) y=e-2t for -3≤ t ≤ 3


2) y=tan (2πt) for -4 ≤ t≤ 4
-1

3) y= | x | for -6 ≤ x ≤ 6

a- in one figure
b- in different figures
c- in one figure but different parts

You might also like