You are on page 1of 5

8/15/2013

LINE PLOTS
PLOTTING IN
MATLAB

The plot function has different forms


depending on the input arguments. For
example, if y is a vector, plot(y) produces a
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.

SAMPLE SYNTAX

GRAPH

t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
grid on % Turn on grid lines for this plot

SAMPLE SYNTAX
You can plot multiple graphs in one call to plot
using x-y pairs. MATLAB automatically cycles
through a predefined list of colors (determined
by the axes ColorOrder property) to allow
discrimination between sets of data.

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

8/15/2013

GRAPH

SPECIFYING LINE STYLE


You can assign different line styles to each data
set by passing line style identifier strings to plot
t = 0:pi/100:2*pi;
y = sin(t);
y2 = sin(t-0.25);
y3 = sin(t-0.5);
plot(t,y,'-',t,y2,'--',t,y3,':')

GRAPH

COLORS, LINE STYLES and


MARKERS
The basic plotting functions accepts
character-string arguments that specify
various line styles, marker symbols, and
colors for each vector plotted. In the general
form,
plot(x,y,'linestyle_marker_color')
linestyle_marker_color is a character string
(delineated by single quotation marks)
constructed from
A line style (e.g., dashed, dotted, etc.)
A marker type (e.g., x, *, o, etc.)
A predefined color specifier (c, m, y, k, r, g, b, w)

plot(x,y,':squarey')
plots a yellow dotted line and places square
markers at each data point. If you specify a
marker type, but not a line style, only the marker
is plotted.

SPECIFYING THE COLOR AND SIZE


OF LINES
LineWidth Width of the line in units of
points
MarkerEdgeColor Color of the marker or
the edge color for filled markers (circle,
square, diamond, pentagram, hexagram,
and the four triangles)
MarkerFaceColor Color of the face of
filled markers
MarkerSize Size of the marker in units of
points

8/15/2013

SAMPLE SYNTAX

GRAPH

x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)

ADDING PLOTS TO AN EXISTING


GRAPH: HOLD COMMAND
You can add plots to an existing graph using the
hold command. When you set hold to on,
MATLAB does not remove the existing graph; it
adds the new data to the current graph, rescaling
if the new data falls outside the range of the
previous axis limits.

GRAPH

SAMPLE SYNTAX
semilogx(1:100,'+')
hold all % hold plot and cycle line colors
plot(1:3:300,1:100,'--')
hold off
grid on % Turn on grid lines for this plot

PLOTTING ONLY THE DATA


POINTS

x = 0:pi/15:4*pi;
y = -exp(2*cos(x));
plot(x,y,'r+')

8/15/2013

PLOTTING MARKERS AND LINES

EXAMPLE #1
Use Matlab to draw the graph of f (x) = x2 2x
3 on the interval *1, 3+.

x = 0:pi/15:4*pi;
y = -exp(2*cos(x));
plot(x,y,'-r',x,y,'ok')

SYNTAX

GRAPH

>> x=-1:.1:3;y=x.2-2*x-3;
>> plot(x,y)
>> xlabel(x-axis)
>> ylabel(y-axis)
>> title(The graph of f(x) = x2 - 2x - 3)
>> grid on

EXAMPLE #2
Sketch the graphs of f (x) = 1/x and g(x) = log(x
1) on the interval [2, 5].

SYNTAX
>> x=2:.1:5;
>> f=1./x;
>> g=log(x-1);
>> plot(x,f,-,x,g,--)
>> legend(y = f(x),y = g(x))

8/15/2013

GRAPH

PLOTTING IN MATLAB

LABORATORY EXERCISE #3

Use MATLABs plot command to draw the graph of


f(x) = 5 4x x2 with an interval [-6,2]. Label each
axis and provide a title for your plot.
Use MATLABs plot command to draw the graph of
both given functions on the same axes, drawn on
the given interval. Label each axis. Provide a title
for your plot.
s(x) = cos 2x + sin 3x, v(x) = 2 sin 2x + 3 cos 3x, [0, 4]

You might also like