plotting in matlab

5
8/15/2013 1 PLOTTING IN MATLAB LINE PLOTS 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 t = 0:pi/100:2*pi; y = sin(t); plot(t,y) grid on % Turn on grid lines for this plot GRAPH 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. SAMPLE SYNTAX y = sin(t); y2 = sin(t-0.25); y3 = sin(t-0.5); plot(t,y,t,y2,t,y3)

Upload: zesi-villamor-delos-santos

Post on 14-Dec-2015

3 views

Category:

Documents


0 download

DESCRIPTION

Simple Guide in using MATLAB for plotting waves

TRANSCRIPT

Page 1: Plotting in Matlab

8/15/2013

1

PLOTTING IN MATLAB

LINE PLOTS

• 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

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

y = sin(t);

plot(t,y)

grid on % Turn on grid lines for this plot

GRAPH

• 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.

SAMPLE SYNTAX

y = sin(t);

y2 = sin(t-0.25);

y3 = sin(t-0.5);

plot(t,y,t,y2,t,y3)

Page 2: Plotting in Matlab

8/15/2013

2

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

Page 3: Plotting in Matlab

8/15/2013

3

SAMPLE SYNTAX

x = -pi:pi/10:pi;

y = tan(sin(x)) - sin(tan(x));

plot(x,y,'--rs','LineWidth',2,...

'MarkerEdgeColor','k',...

'MarkerFaceColor','g',...

'MarkerSize',10)

GRAPH

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.

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

GRAPH PLOTTING ONLY THE DATA POINTS

x = 0:pi/15:4*pi;

y = -exp(2*cos(x));

plot(x,y,'r+')

Page 4: Plotting in Matlab

8/15/2013

4

PLOTTING MARKERS AND LINES

x = 0:pi/15:4*pi;

y = -exp(2*cos(x));

plot(x,y,'-r',x,y,'ok')

EXAMPLE #1

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

SYNTAX

>> 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) = xˆ2 - 2x - 3’)

>> grid on

GRAPH

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)’)

Page 5: Plotting in Matlab

8/15/2013

5

GRAPH

LABORATORY EXERCISE #3

PLOTTING IN MATLAB

• Use MATLAB’s 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 MATLAB’s 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]