Sunteți pe pagina 1din 15

Exercise No.

3
2D and 3D PLOTS

NAME: DATE PERFORMED:


COURSE/ SECTION: DATE SUBMITTED:
GROUP NO.: INSTRUCTOR:

I. OBJECTIVE:
To learn how to use Matlab in creating 2D and 3D plots

II. OVERVIEW:

Getting help on plotting:


By typing help plot you can see the various capabilities of this main command for two-dimensional plotting. You can also use help
graph2d

Plotting a point:

plot ( variablename, ‘symbol’)

the function plot () creates a graphics window, called a Figure window, and named by default “Figure No. 1”

commands for axes:

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

Adding new curves to the existing graph:

Use the hold command to add lines/points to an existing plot.


hold on – retain existing axes, add new curves to current axes. Axes are
rescaled when necessary.
hold off – release the current figure window for new plots

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 1 of 15
Grids and Labels:

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.

Additional Commands for Plotting:

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)

2. You can label the axes and add a title.

Type:

xlabel('x')

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 2 of 15
ylabel('sin(x)')
title('Plot of the Sine Function')

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

For example, 'g:*' requests a dotted green line with * markers.

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.

4. To add plots to an existing figure, use hold.

Type:

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 3 of 15
hold on

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:

% Generate the polynomial:


x = linspace (0, 10, 100);
y = 2*x.^2 + 7*x + 9;

% plotting the polynomial:


figure (1);
subplot (2,2,1), plot (x,y);
title ('Polynomial, linear/linear scale');
ylabel ('y'), grid;
subplot (2,2,2), semilogx (x,y);
title ('Polynomial, log/linear scale');
ylabel ('y'), grid;
subplot (2,2,3), semilogy (x,y);
title ('Polynomial, linear/log scale');
xlabel('x'), ylabel ('y'), grid;
subplot (2,2,4), loglog (x,y);
title ('Polynomial, log/log scale');
xlabel('x'), ylabel ('y'), grid;

returns:

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 4 of 15
6. Obtain the graph of y = cos(x) from – π to π, we can first define the vector x with components equally spaced numbers between –
π and π, with increment, say 0.01.
Typing:

x=-pi:0.01:pi;

y=cos(x);
plot(x,y)

returns:

Typing:

xlabel('x')
ylabel('y=cos(x)')

title('Graph of cosine from - pi to pi')

returns:

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 5 of 15
7. Various line types, plot symbols and colors can be used. If these are not specified (as in the case
above) MATLAB will assign (and cycle through) the default ones as given in the table below.
y yellow . point
m magenta o circle
c cyan x x-mark
r red + plus
g green - solid
b blue * star
w white : dotted
k black -. dashdot
-- dashed

Type:

plot(x,y,’m’)

What happened to the plot?

Note: where the third argument indicating the color, appears within single quotes.

Typing:
plot(x,y,’ * ’)

What happened to the plot?

Typing:
plot(x,y,’b:’)

What happened to the plot?

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

Then, create a surface plot. Type:

surf(X,Y,Z)

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 6 of 15
Returns:

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:

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 7 of 15
Note: The first two inputs to the subplot function indicate the number of plots in each row and column. The third input specifies which
plot is active.

10. More example:

Create a variable in the workspace,

x = -2*pi:pi/25:2*pi;
Use the plottools command to create a figure group with the plotting tools attached.
plottools

Click 2D Axes in the New Subplot panel of the Figure Palette.

Once the axes appears, select it and the Add Data button on the Plot Browser is activated.

Click this button to display the Add Data to Axes dialog.

When the Add Data to Axes dialog is displayed, enter the following values:

 Select plot as the Plot Type.


 Set X Data Source to x.
 Set Y Data Source to sin(x).^2.
 Click OK to plot this data.

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 8 of 15
Returns:

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:

% Generate the values for x and y


[x,y] = meshgrid(-2:.2:2);
% Evaluate z as a function of x and y
z = x.*exp(-x.^2-y.^2);

Create a figure and attach to it the Figure Palette:

figure;
figurepalette

Expand the figure palette Variables panel (unless it is already open).

Select x, and then y, and then z in the Variables panel with shift+click to indicate the variables to pass the plotting function.

Open a context menu by right-clicking any of the three variables.

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 9 of 15
Since the function you want to use, surf, does not appear in the list, select Plot Catalog.

The Plot Catalog tool opens in a new window.

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.

The following graph results:

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 10 of 15
12. More examples: You can enter MATLAB expressions in the Plot Catalog tool. This example uses two variables to plot a line
graph.

Example, You want to plot a line graph of t versus this expression:


exp(-alpha*t).*sin(.5*t)

Create the following variables in the workspace.

Type:

t = 0:.01:20;
alpha =.055;

Create a figure and open the Figure Palette for it:

Type:

figure,figurepalette

Select the variable t and right-click to display the context menu. Select Plot Catalog from the menu.

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 11 of 15
When the Plot Catalog tool opens, add the expression to the Plotted Variables text field so it reads t,exp(-alpha*t).*sin(.5*t). You
can reference the variable alpha because you created it in the base workspace.

Click the thumbnail or the Plot button to create the graph.

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 12 of 15
The figure looks like the following illustration.

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)

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 13 of 15
IV. EXERCISES:

Plot the following equations. Attach print screens of the result:

1. Line 1: in red solid line, 4x + 5y = -12


Line 2: in blue solid line, 3x – 2y = 7

Matlab code:

2. Circle 1: in green solid line, x2 + (y - 5)2 = 25


Circle 2: in yellow solid line, (x - 3)2 + (y + 2)2 = 25

Matlab code:

3. The parabola: in magenta dashed line, (x + 3)2 = -12(y + 1)

Matlab code:

4. The ellipse: in blue dashed-dot line, (x - 2)2 + (y + 1)2 = 1


25 9

Matlab code:

5. The hyperbola: in red dashed line, (x - 1)2 - (y + 2)2 = 1


16 9

Matlab code:

6. Graph a solid sphere of radius = 10 units.

Matlab code:

7. Graph a cube with side = 10 units

Matlab code:

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 14 of 15
V. GENERALIZATION:
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________

Exercise No. 3 – 2D and 3D plots


Student’s Manual page 15 of 15

S-ar putea să vă placă și