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

Experiment 7 Plotting Function

The document discusses various MATLAB plotting functions and techniques: 1) The plot function can produce graphs of vectors versus their indices or of one vector versus another. Line styles, symbols, and colors can be specified. 2) The hold command allows adding plots to an existing graph without replacing it. 3) The subplot command enables displaying multiple plots in the same figure window arranged in a grid. 4) The axis command customizes the axis limits, and xlabel, ylabel, title add labels and titles to the plot.

Uploaded by

kurddoski28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

Experiment 7 Plotting Function

The document discusses various MATLAB plotting functions and techniques: 1) The plot function can produce graphs of vectors versus their indices or of one vector versus another. Line styles, symbols, and colors can be specified. 2) The hold command allows adding plots to an existing graph without replacing it. 3) The subplot command enables displaying multiple plots in the same figure window arranged in a grid. 4) The axis command customizes the axis limits, and xlabel, ylabel, title add labels and titles to the plot.

Uploaded by

kurddoski28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Zakho Petroleum Engineering Department MATLAB Laboratory

______________________________________________________________________________________________

Experiment No. 7
Plotting Function
MATLAB has extensive facilities for displaying vectors and matrices as graphs, as
well as annotating and printing these graphs.

Creating a Plot using plot function


The plot function has different forms, depending on the input arguments. If y is a
vector, plot(y) produces a piecewise linear graph of the elements of y versus the
index of the elements of y. If you specify two vectors as arguments, plot(x,y)
produces a graph of y versus x. For example, these statements use the colon
operator to create a vector of x values ranging from zero to 2×pi, compute the sine
of these values, and plot the result.
x = 0:pi/100:2*pi;
y = sin (x);
plot (y)

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 50 100 150 200 250

(7 - 1)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________

Now plot y variable by using:


plot (x,y)
can you see the difference at x-axis
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

Specifying Line Styles and Colors


Various line types, plot symbols and colors may be obtained with plot (x,y,s) where
s is a character string made from one of the element the following 3 Columns:

(7 - 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Color Marker Line style
b Blue . Point - Solid
g Green o Circle : Dotted
r Red x x-mark -. Dashdot
c Cyan + Plus -- Dashed
m Magenta * Star (none) No line
y Yellow s Square
k Black d Diamond
v Triangle (down)
> Triangle (left)
p Pentagram
h hexagram

Example:
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot (x1, sin (x1) , 'r:', x2, cos (x2), 'r+');
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

Adding Plots to an Existing Graph


The hold command enables you to add plots to an existing graph. When you type

(7 - 3)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
hold on MATLAB does not replace the existing graph when you issue another
plotting command; it adds the new data to the current graph, rescaling the axes if
necessary.
Example:
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot (x1, sin(x1), 'r:')
hold on
plot (x2, cos(x2), 'r+')
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

Multiple Plots in One Figure


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
pth subplot for the current plot. The plots are numbered along first the top row of

(7 - 4)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
the figure window, then the second row, and so on. For example, these statements
plot data in four different sub regions of the figure window.
t = 0:pi/10:2*pi;
x=sin(t); y=cos(t);
z= 2*y-3*x; v=5-z;
subplot (2,2,1); plot(x)
subplot (2,2,2); plot(y)
subplot (2,2,3); plot(z)
subplot (2,2,4); plot(v)

Setting Axis Limits


By default, MATLAB finds the maxima and minima of the data to choose the axis
limits to span this range. The axis command enables you to specify your own limits
axis([xmin xmax ymin ymax])
Axis Labels and Titles
The xlabel, ylabel, and zlabel commands add x-, y-, and z-axis labels. The title
command adds a title at the top of the figure and the text function inserts text
anywhere in the figure.
(7 - 5)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Example:
t = -pi:pi/100:pi; y = sin(t);
plot (t,y)
axis([-pi pi -1 1])
xlabel('x-axis')
ylabel('sin(t)')
title('Graph of the sine function')
text(1,-1/3,'Note the odd symmetry')
Graph of the sine function
1

0.8

0.6

0.4

0.2
sin(t)

-0.2
Note the odd symmetry
-0.4

-0.6

-0.8

-1
-3 -2 -1 0 1 2 3
x-axis

Exercises:

1- Plot Sinc function, where sinc (x) = (sin(x) )/ x, and -2π < x <2π
2- Plot sin(x) and cos(x) on the same figure, then on the same axis using
different colors. Also, plot both of them on two figures separately, where
0 < x <2π.

(7 - 6)

You might also like