Matlab Lecture 5
Matlab Lecture 5
90
80
70
60
Score
50
40
30
20
10
0
1 2 3 4 5 6 7 8 9 10
Student
• Two and Three dimensional graphs
• If x, y, and z are vectors with the same
size (length), they can be plotted using
the command plot3.
• Plot3(x,y,z) generates a line in three
dimensions through the points whose
coordinates are the elements of x,y and
z and then produces a 2-dimensional
projection of that line on the screen.
• >> t=(0:0.02*pi:25*pi);
• >> x=sin(t); y=cos(t); z=t;
• >> plot3(x,y,z)
80
60
40
20
0
1
0.5 1
0 0.5
0
-0.5 -0.5
-1 -1
• Three-dimensional plots basically
display a surface defined by a
function in two variables,
• g = f (x, y).
• As before, to define g, we first
create a set of x, y points over the
domain of the function using the
• meshgrid command.
• Next, we assign the function
itself.
• Finally, we use the surf command
to create a surface plot.
• Let us create a 3D surface map
for the function g = xe-(x2 + y2)
• >> [x,y] = meshgrid(-2:.2:2);
• >> g = x.*exp(-x.^2 - y.^2);
• >> surf(x,y,g)
0.5
-0.5
2
1 2
0 1
0
-1 -1
-2 -2
• Remember you can also use the
mesh command to generate a
three-dimensional surface.
However, the surf command
displays both the connecting lines
and the faces of the surface in color,
whereas, the mesh command
creates a wireframe surface with
colored lines connecting the
defining points.
• Example.
• Re-plot the above using the mesh
grid.
• >> [x,y] = meshgrid(-2:.2:2);
• g = x.*exp(-x.^2 - y.^2);
• >> plot3(x,y,g).
• This gives
0.5
-0.5
2
1 2
0 1
0
-1 -1
-2 -2
• Consider a scalar function of two variables Z=
f(x,y). This function defines a surface in the
three-dimensional space. The surface can be
graphically represented using mesh, meshc,
surf. Surfc surfl and other commands and
functions.
• In general, the function Z = f(x,y) is calculated
and stored as the Z matrix, and a grid is
defined.
• To evaluate the functions, meshgrid is used.
Here, the (x,y) data is generated to evaluate Z
= f(x,y).
• We can calculate the nonlinear functions
•
• The x, y and z can be plotted using a 3-
dimensional plot applying meshgrid.
• [x,y]=meshgrid(-4:0.1:4);
• Z=x.*2*y.*exp(x.^2-y.^4);
• Plot3(x,y,z)
>> [x,y]=meshgrid(-4:0.1:4);
>> z = (x.^2.*y)*(exp(x.^2+y.^4));
>> surf(x,y,z)
>> z = (x.^2.*y).*(exp(x.^2+y.^4));
>> surf(x,y,z)
>> z = ((x.^2).*y).*(exp(x.^2+y.^4));
>> surf(x,y,z)
>> z = (x.^2).*(y).*(exp(x.^2+y.^4));
>> surf(x,y,z)
>> z = (x.^2).*(y).*(exp(x.^2)+(y.^4));
>> surf(x,y,z)
>> plot3(x,y,z)