0% found this document useful (0 votes)
8 views

Lab 4

This document discusses various plotting functions in MATLAB, including: 1. Plotting functions support 2D and 3D plotting of functions with commands like plot(x,y) to plot quantities on the x and y axes. 2. Basic plotting commands include plot, stem, log, bar, and polar for different coordinate systems. 3. Additional commands like xlabel, ylabel, title, grid, gtext, and text can customize plots with labels, grids, and positioned text. 4. Subplot divides figures into grids for multiple axes. Stem plots discrete data as stems from a baseline. Bar graphs create bars from data arrays.

Uploaded by

Mashood Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 4

This document discusses various plotting functions in MATLAB, including: 1. Plotting functions support 2D and 3D plotting of functions with commands like plot(x,y) to plot quantities on the x and y axes. 2. Basic plotting commands include plot, stem, log, bar, and polar for different coordinate systems. 3. Additional commands like xlabel, ylabel, title, grid, gtext, and text can customize plots with labels, grids, and positioned text. 4. Subplot divides figures into grids for multiple axes. Stem plots discrete data as stems from a baseline. Bar graphs create bars from data arrays.

Uploaded by

Mashood Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PLOTTING FUNCTIONS IN MATLAB

 MATLAB supports the 2D and 3D plotting of functions.


 The most basic command is plot(array).
 A commonly used syntax is plot(quantity on x-axis, quantity on y-axis).
 Coding example:
t=0:0.01:2;
x = cos(2*pi*4*t);
plot(t,x);
xlabel('time');
ylabel('amplitude');
title('plots');
PLOTTING IN MATLAB

(x(8),y(8))

(x(2),y(2))
SIMPLE PLOTTING COMMANDS
Command Effect
plot Plots in linear coordinates as a continuous function
stem Plots in linear coordinates as a discrete function
log Logarithmic x & y axis
bar Bar Graph
polar Polar coordinates
PLOT VECTORS or MATRICES: plot(X,Y)
• If X and Y are vectors, vector Y versus vector X is
plotted. (X & Y should be of equal length)

• If X and Y are matrix, columns of Y are plotted


against columns of X. (X & Y should be of equal
size)

• If one of X or Y is a scalar and other is either a


scalar or a vector, then discrete points are plotted.
PLOT VECTORS or MATRICES: plot(X,Y)
• 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.
EXAMPLE
Plotting Multiple Lines in One Figure:
Specifying Line Style, Color and
For more information: SearchMarker
“Specify Line and Marker Appearance in Plots”
on MathWorks (MATLAB help website
When plotting multiple plots the 3rd argument is often used to distinguish
plots i.e. plot(x-axis,y-axis, ‘text for distinguishing’)

Text for color Text for markers Plot marker


y(yellow) . point
m(magenta) O circle
c(cyan) X x-mark
r(red) + plus
g(green) - solid plot(t,x,'r+')
b(blue) * star
w(white) : (line style) dotted
k(black) -. (line style) dashdot
-- (line style) dashed
Specifying Line Style, Color and Marker
CUSTOMIZATION OF PLOTS
After entering the ‘plot’ command, following commands can be
used to enhance the plot figure.

Command Effect
xlabel Labels x-axis
ylabel Labels y-axis
grid Adds a grid to the plot
gtext Allows positioning of text with the mouse
text Allows placing text at specified coordinates of the plot
axis Allows changing the x and y axes
figure Create a figure for plotting
figure (n) Make figure number n the current figure
hold on Allows multiple plots to be superimposed on the same axes
hold off Release hold on current plot
close(n) Close figure number n
Add Title and Axis Labels to Plot:
SUBPLOT (subplot(a,b,c))
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 1st col of the 1st
row, the second subplot is 2nd col of the 1st row and so on.

1 2

3 4
Upper and Lower Subplots:
Multiple Subplots
Multiple Subplots with Varying sizes
Upper and Lower Subplots:
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.

 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.
 To create a stem plot for random discrete values, use the following code:
Plotting a Sinusoid
Plotting Multiple Data Series
Fill in Plot Markers
Specifying Stem and Marker Options
UNDERSTANDING (To built a unitstep
function) tic;
t(index) 1
t = -10:1:10 %discrete values
2 3 4 5 6 7 8 9 10 11 12 13 14 15
toc;
16 17 18 19 20 21

values -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10

Now, built another array such that for all the negative values of t it is ‘0’
and ‘1’ for rest. We will get an array like following.
u(index) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

values 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1

Now, if we plot ‘u’ against ‘t’, we get a unit-step function. But, we need
to be flexible while coding as the user can enter array containing any values
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.


Bar Graph with Group of Bars

You might also like