Laboratory 9. 2D and 3D graphics Aim of the laboratory 2D and 3D

Laboratory 9. 2D and 3D graphics
Aim of the laboratory
2D and 3D graphical representations. Understanding the way some functions work.
Necessary equipment
Work stations with MATLAB installed.
Theoretical approach
Graphical representation has some basic rules that need to be obeyed:
- data gathering
- creating the interface where the plot will be displayed
- plotting
- furthermore:
o line editing
o axis editing
o naming the axes
o printing
Data gathering can be a pretty important step. Matlab can plot data in different ways – the
simplest representation is the one on 2 axes, one being the OX axis and the other the f(x) axis. For
example, the plots in the previous labs:
x = -pi:pi/100:pi;
fx= sin(x);
Out of the numerous functions in 2D plotting we have only considered some: plot, hold,
subplot, ezplot, etc.
The plot function
Connects x and y coordinates – (x, y) - where x and y are vectors. In order to plot the
function y=f(x) we have to know the relationship between the two vectors (y = f(x)), where x must
also be defined.
For example:
x=-3:0.001:3;
y=x.^3;
plot(x,y);
Plot characteristics refer to color, line type, title, name of the axes, etc; we used many of
them in the previous labs. The plotting syntax was in those cases:
plot(x,y,S);
where S is the property specifier, properties that can be accessed in the HELP menu.
S is composed of two characteristics: one regarding the color and the other the line type.
The types of lines are:
Specifier
-:
-.
Line type
Continuous line
Dashed line
Dotted line
Dash Dot
Table 1
There is also the option of representing several functions using the same plot function. The
syntax is:
plot(x1,y1,S1,x2,y2,S2);
Other plotting types:
- logarithmic representation;
- semi-logarithmic representation - one of the axis is logarithmic, the other one is
real;
- there is also the option of plotting two OY axes, one to the right and one to the
left, using the plotyy function.
For example:
x1= -pi:0.01:pi;
x2= -3:0.01:3;
y1=sin(1./x1);
y2=x2 .*sin(1./x2);
plot(x1,y1,'b',x2,y2,'r');
If we want a better understanding of the plot we can use the legend function. Below, we
have the syntax of this function, where text 1 and text 2 correspond to the lines that we want to
explain:
legend(text 1','text 2');
Matlab offers a great amount of control over the plotting shape; many shapes can be
modified by a simple click. Of course, a lot of parameters can be modified using predefined
functions. As seen before, we can change the type of the line, its color, but we can also modify the
shape of some points. The types of markers are represented in Table 2.
Figure 1
Specifier
+
o
*
.
x
s
d
^
v
>
<
P
h
Marker type
plus sign
circle
times sign
dot
cross sign
square sign
rhombus sign
equilateral triangle, base downwards
equilateral triangle, base upwards
equilateral triangle, facing left
equilateral triangle, facing right
5-sided star
6-sided star
Table 2
Specifier
w
k
y
m
c
b
g
r
Color
White
Black
Yellow
Pink
Cyan
Blue
Green
Red
Table 3
For example:
x = -pi:pi/10:pi;
sine = sin(x);
cosine = cos(x);
plot (x, sine, ':hr', x, cosine, '--pk');
The hold function
If we want to plot a function that has the same graphical properties as another function, the
“hold on” function (which can be used for both 2D and 3D plotting) allows the storage of the
current plot with all its properties. “Hold on” activates the option and “hold off” deactivates it.
For example:
x = -3:0.1:3;
y = sin(1./x);
plot(x,y)
hold on
y = cos(1./x);
plot(x,y,'c')
The subplot function
The subplot(m,n,p) function divides the plotting window in m*n windows and selects the
figure indicated by specifier p.
For example:
x= - pi:0.01:pi;
y1 = sin(x); y2 = cos(x); y3 = y1+y2; y4 = y1-y2;
subplot(2,2,1); plot(x,y1); xlabel('p=1');
subplot(2,2,2); plot(x,y2); xlabel('p=2');
subplot(2,2,3); plot(x,y3); xlabel('p=3');
subplot(2,2,4); plot(x,y4); xlabel('p=4');
The outcome of this program is shown in figure 2.
The ezplot function
Plots the graph of a function on a given period. The syntax is the one below:
ezplot(fun,[min,max])
For example:
ezplot(sin(x),[-pi,pi]);
Figure 2
The funtool function
This is a calculator function, which manipulates and plots a function of one variable. For
more details access Help.
Another type of graphical representation is in 3D. The function used is plot3, with three
parameters. They represent the coordinates on the three axes.
For example:
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t,':pb');
Figure 3
Similar to the plot function, plot3 allows us to perform some modifications regarding the
way that the plot is displayed.
Most of the times we want to plot graphic surfaces, more than simple curves in 3D graphics.
For such cases, Matlab offers two special functions: surf and mesh.
The mesh function
This function creates the inner structure of a surface given by the x, y and z coordinates.
For example:
[X, Y] = meshgrid(-pi:pi/10:pi);
Z = sin (X) .* sin (Y);
mesh (Z);
The surf function
Is used to view mathematical functions in a rectangular surface.
For example:
[X, Y] = meshgrid(-pi:pi/10:pi);
Z = sin (X) .* sin (Y);
surf (Z);
Figure 4
Figure 5
The meshgrid function
Performs a conversion of the domain given by the x and y vectors into two or more matrices,
depending on the number of the return parameters, in this case x and y. The matrices will be used to
create a 3D plotting of the two vectors. The lines of the resulting X matrix will be copies of the x
vector and the columns of the resulting Y matrix will be copies of the y vector.
For example:
[X, Y] = meshgrid(1:3),
The resulting matrices are:
X =
1
1
1
2
2
2
3
3
3
1
2
3
1
2
3
1
2
3
Y =
For example:
Consider the function: f(x, y) = 2x + 2y. In order to obtain the 3D plotting of f, with respect
to x and y, we use the following code lines:
coord = 0:0.5:8;
% x and y are integer values in the interval 0-8
[x, y] = meshgrid(coord, coord);
figure(1)
mesh(x, y, 2.^x + 2.^y)
xlabel('x')
ylabel('y')
zlabel('Functia 2^x + 2^y')
figure(2)
surf(x, y, 2.^x + 2.^y)
% plots f(x, y) as a surface
% draws f(x, y) as a surface and colors it
In the case of functions of two variables, we can obtain plots with contours corresponding to
level curves. These contours will be labeled with the value of the function on that certain level
curve.
For example:
Consider the following function and code lines:
f ( x, y ) = x 2 + y 2
x = -8:0.5:8;
y = x;
[X Y] = meshgrid(x, y);
r = sqrt(X.^2 + Y.^2) + eps;
Z = sin(r)./r;
surfc(X, Y, Z);
title('sin(r)/r');
mesh(X, Y, Z);
view(10,10);
% c = contour(Z);
% clabel(c);
By erasing the % sign from the beginning of the two code lines we obtain the contours of the
surfaces.
Homework
1. Analyze all the functions presented in this lab.
2. Plot the following function:
f (x ) =
x−3
,
x2 − 5
for x [-5,5]
3. Plot the following function:
( )
f ( x ) = 2e x − sin x 2
Determine the minimum of the function.
2