Vous êtes sur la page 1sur 5

Introduction to MATLAB

Kathmandu University
Mathematics Group

1 Plotting
The plots in MATLAB appear in the graphic/figure window.
MATLAB provides very good facilities for both 2-D and 3-D graphics.
It can produce 3-D mesh as well as 3-D faceted surface and 3-D contour
plots.
The most direct commands to produce a graph in 2-D and 3-D plot are
the plot and plot3 commands respectively. The primary command for
mesh, surface and contour plots are mesh surf, and contour respec-
tively.

2-D Plot Command


plot(xdata,ydata)
where ydata are the values of y variable corresponding to x variable which
are given as xdata. This command plots xdata along horizontal axis and
ydata along vertical axis. xdata and ydata both are vectors having of same
length.
Example 1 Plot the function y = x2 10x + 15, where x [0, 10].
Solution:
>> x = [0:10]; % Creates vector x (xdata)
>> y = x.^2 - 10.*x + 15; % Calculates y (ydata)
>> plot(x,y)

Example 2 plot3() is a three-dimensional analogue of plot(). plot3(x,y,z),


where x, y and z are three vectors of the same length, plots a curve in 3-space
through the points whose coordinates are the elements of x, y and z. Use
plot3() to plot the function f (x, y) = x3 3xy 2 over the rectangular region
5 x 5, 5 y 5.
Solution:
>> x = [-5:0.5:5]; % Creates vector x (xdata)
>> y = x; % Creates vector y (ydata)
>> z = x.^3 - 3.*x.*y.^2; % Calculates z (zdata)
>> plot3(x,y,z)

1
Helpful built-in-functions used in plotting: The figures can be given
titles, axes labeled and text placed within the figure with the following com-
mands which take a string as argument.

title() figure title


xlabel() x-axis label
ylabel() y-axis label
zlabel() z-axis label
gtext() place text on the graph using the mouse
text() position text as specified coordinates
grid grid lines on

Type

>> help legend

Usually legend command is useful in multiple plot.

Continued from above example:

>> title(plot of y = x^2 - 10x + 15);


>> xlabel(x-axis);
>> ylabel(y-axis);

By default figure color is blue. We can insert other color also by typing:

>> plot(x,y,k);

We can select color, marker type and line style according to our choice as
given below.

Color: These are the following colors in MATLAB.


Symbol y m c r g b w k
Color yellow magenta cyan red green blue white black

Marker style: The followings are the marker style used in MATLAB.

Symbol . o x + * s d
Style point circle x-mark plus star square diamond
Symbol < > p h
Style triangle triangle triangle triangle pentagram hexagram
down up left right

2
Line style: The followings are the line styles used in MATLAB.
Symbol : .
Style solid dotted dash dot dashed

Multiple Plot

The command plot creates linear x-y plots. If x and y are vectors of same
length, the command plot(x,y) opens a graphics window and draws an xy
plot of the element of x versus the ones of y, e.g.

>> x = -4:0.01:4; y = x.^2; plot(x,y);


analogously
>> s = -10:0.01:10; t = exp(-s.^2); plot(s,t);

By default, the plot command always opens figure(1) and overwrite the old
graphics. If a current graphics window (figure) should be kept, another
one can be opened with the command figure(2) or simply figure which then
becomes the current figure where graphs from subsequent plotting commands
will be placed.

>> figure(1);x = -4:0.01:4; y = x.^2; plot(x,y);


>> figure(2);s = -10:0.01:10; t = exp(-s.^2); plot(s,t);

Both plots can open in a single figure, that is, the multiple plot can made in
a single figure by using the command

>> x = -2:0.01:2; y = x.^2;


>> s = -10:0.01:10; t = exp(-s.^2);
>> plot(x,y,s,t);
or
>> plot(x,y); hold on; plot(s,t); hold off;

For plotting specified line types, marker types and colors can be chosen, for
example

>> plot(x,y,r--,s,t,b:+);

renders a red dashed line for the first graph and a blue dotted one for the
second one where additionally the nodes are marked by the symbol +.
Line width of the plotting can be chosen according to our choice, for
example

>> plot(x,y,r--,s,t,b:,LineWidth,3);

The conventional plot is a linear plot. The followings are the combi-
nation of linear axes with logarithmic axes:

The semilogx function plots x data on logarithmic axes and y


data on linear axes.

3
The semilogy function plots x data on linear axes and y data on
logarithmic axes.
The loglog function plots both x and y data on logarithmic axes.

Other speciliazed 2-D plotting functions that are worth to be explore


via help are polar, bar, pie, stem, stairs, hist etc.
polar(theta,r); stem(x,y); stairs(x,y); hist(x,y); bar(x,y) For verti-
cal bar plot; barh(x,y)For horizontal bar plot; pie(x); pie(x,explode)
The operational array explode controls whether or not individual
pie slices are seperated from the remainder of the pie.

>> x = [10 37 5 6 6];


>> explode = [0 1 0 0 0];
>> pie(x,explode);
>> title(\bfExample of a pie plot);
>> legend(One,Two,Three,Four,Five);

For visualizing 3-D graphics and generating animations: plot3(x,y,z)


mesh(x,y,z) surf(x,y,z) surfc(x,y,z) contour(x,y,z) etc. For example

>> x = linspace(-4,4,20); y = x; [X,Y] = meshgrid(x,y);


>> Z = exp(-0.5*(X.^2 + Y.^2)); mesh(X,Y,Z);

For mesh, surf, contour plotting create the meshgrid by using the
command

[X,Y] = meshgrid(xstart:xincrement:xend, ystart:yincrement:yend)

There are several other plotting functions to produce plots if you are
plotting a function: fplot, ezplot, ezpolar, ezplot3, ezcontour,
ezcontourf, ezsurf, ezsurfc etc. For these try to use online help:
> > help fplot etc.

Sublot:
The command subplot can be applied to partition the screen so that several
small plots can be placed in one figure.

Syntax: subplot(m,n,p)

This command divides the current figure into m n equal sized regions,
arranged in m rows and n columns, and creates a set if axes at position p
to recieve all plotting commands. The subplots are numbered from left to
right end from top to bottom. For example, the command subplot(2,3,4)
would divide the current figure into six regions arranged in two rows and
three columns, and create an axis in position 4 (the lower left one) to accept
new plot data. The following example will display the use of subplot.

4
>> figure(1)
>> subplot(2,3,1);
>> x = -pi:pi/20:pi; y = sin(x); plot(x,y);
>> subplot(2,3,5);
>> x = -pi:pi/20:pi; y = cos(x);plot(x,y);
>> title(Subplot 2 title);

Vous aimerez peut-être aussi