Documente Academic
Documente Profesional
Documente Cultură
3
2D and 3D PLOTS
I. OBJECTIVE:
To learn how to use Matlab in creating 2D and 3D plots
II. OVERVIEW:
Plotting a point:
the function plot () creates a graphics window, called a Figure window, and named by default “Figure No. 1”
command description
axis ([xmin xmax ymin ymax]) Define minimum and maximum values of the axes
axis square Produce a square plot
axis equal equal scaling factors for both axes
axis normal turn off axis square, equal
axis (auto) return the axis to defaults
Plotting Curves:
plot (x,y) – generates a linear plot of the values of x (horizontal axis) and y (vertical axis).
semilogx (x,y) – generate a plot of the values of x and y using a logarithmic scale for x and a linear scale for y
semilogy (x,y) – generate a plot of the values of x and y using a linear scale for x and a logarithmic scale for y.
loglog(x,y) – generate a plot of the values of x and y using logarithmic scales for both x and y
Multiple Curves:
plot (x, y, w, z) – multiple curves can be plotted on the same graph by using multiple arguments in a plot command. The variables
x, y, w, and z are vectors. Two curves will be plotted: y vs. x, and z vs. w.
legend (‘string1’, ‘string2’,…) – used to distinguish between plots on the same graph
Multiple Figures:
figure (n) – used in creation of multiple plot windows. place this command before the plot() command, and the corresponding figure
will be labeled as “Figure n”
close – closes the figure n window.
close all – closes all the figure windows.
Subplots:
subplot (m, n, p) – m by n grid of windows, with p specifying the current plot as the pth window
Command Description
grid on Adds dashed grids lines at the tick marks
grid off removes grid lines (default)
grid toggles grid status (off to on, or on to off)
title (‘text’) labels top of plot with text in quotes
xlabel (‘text’) labels horizontal (x) axis with text is quotes
ylabel (‘text’) labels vertical (y) axis with text is quotes
text (x,y,’text’) Adds text in quotes to location (x,y) on the current axes, where (x,y) is in units from the
current plot.
III. IMPLEMENTATION:
1. To create two-dimensional line plots, use the plot function.
For example, to plot the value of the sine function from 0 to 2π,
Type:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Type:
xlabel('x')
3. By adding a third input argument to the plot function, you can plot the same variables using a red dashed line.
Type:
plot(x,y,'r--')
Note: The 'r--' string is a line specification. Each specification can include characters for the line color, style, and marker. A marker is
a symbol that appears at each plotted data point, such as a +, o, or *.
Note: Notice that the titles and labels that you defined for the first plot are no longer in the current figure window.
By default, MATLAB clears the figure each time you call a plotting function, resetting the axes and other elements to prepare the
new plot.
Type:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
y2 = cos(x);
plot(x,y2,'r:')
legend('sin','cos')
Note: Until you use hold off or close the window, all plots appear in the current figure window.
5. More Example: plot the polynomial using linear/linear scale, log/linear scale, linear/log scale, & log/log scale:
y = 2x2 + 7x + 9
type:
returns:
x=-pi:0.01:pi;
y=cos(x);
plot(x,y)
returns:
Typing:
xlabel('x')
ylabel('y=cos(x)')
returns:
Type:
plot(x,y,’m’)
Note: where the third argument indicating the color, appears within single quotes.
Typing:
plot(x,y,’ * ’)
Typing:
plot(x,y,’b:’)
Type:
z = sin(x);
Type:
plot(x,y,'r--',x,z,'b:')
What happened to the plots?
8. Three-dimensional plots typically display a surface defined by a function in two variables, z = f (x,y).
To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid.
Type:
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
Note: Both the surf function and its companion mesh display surfaces in three dimensions. surf displays both the connecting lines
and the faces of the surface in color. mesh produces wireframe surfaces that color only the lines connecting the defining points.
9. You can display multiple plots in different subregions of the same window using the subplot function.
For example, create four plots in a 2-by-2 grid within a figure window.
Type:
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X); title('X');
subplot(2,2,2); mesh(Y); title('Y');
subplot(2,2,3); mesh(Z); title('Z');
subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');
returns:
x = -2*pi:pi/25:2*pi;
Use the plottools command to create a figure group with the plotting tools attached.
plottools
Once the axes appears, select it and the Add Data button on the Plot Browser is activated.
When the Add Data to Axes dialog is displayed, enter the following values:
11. More example: this procedure shows how to use the Figure Palette and the Plot Catalog to select a graph type for the data you
want to plot. A surface graph is often a useful way to visualize a function of two variables. Use the following steps represent the
example data as a surface graph (using the surf function).
Create three variables in your workspace (x, y, z) that represent a mathematical function evaluated over a specified domain (-2 to
2).:
Type:
figure;
figurepalette
Select x, and then y, and then z in the Variables panel with shift+click to indicate the variables to pass the plotting function.
In the Plot Catalog tool, select the 3D Surfaces in the first column and surf(x,y,z) from the second column, as shown here.
Create the plot by clicking either the surf icon or the Plot button.
Type:
t = 0:.01:20;
alpha =.055;
Type:
figure,figurepalette
Select the variable t and right-click to display the context menu. Select Plot Catalog from the menu.
The Plot Catalog tool issued the following commands, which appear in the Command Window:
plot(t,exp(-alpha*t).*sin(.5*t),'DisplayName',...
'exp(-alpha*t).*sin(.5*t) vs. t','XDataSource',...
't','YDataSource','exp(-alpha*t).*sin(.5*t)');
figure(gcf)
Matlab code:
Matlab code:
Matlab code:
Matlab code:
Matlab code:
Matlab code:
Matlab code: