0% found this document useful (0 votes)
189 views5 pages

Activity #5

This document describes how to create 3D plots in MATLAB using commands like plot3, mesh, and surf. It provides examples of generating a 3D line plot, surface mesh plot, surface surf plot, contour plot, and a combined mesh and contour plot for the function z = x*exp(-((x-y^2)^2 + y^2)) over the ranges -2 ≤ x ≤ 2 and -2 ≤ y ≤ 2. Key commands demonstrated include plot3, mesh, surf, contour, and meshc.

Uploaded by

Crishen Vinzon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views5 pages

Activity #5

This document describes how to create 3D plots in MATLAB using commands like plot3, mesh, and surf. It provides examples of generating a 3D line plot, surface mesh plot, surface surf plot, contour plot, and a combined mesh and contour plot for the function z = x*exp(-((x-y^2)^2 + y^2)) over the ranges -2 ≤ x ≤ 2 and -2 ≤ y ≤ 2. Key commands demonstrated include plot3, mesh, surf, contour, and meshc.

Uploaded by

Crishen Vinzon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ECE120L – INTRODUCTION TO MATLAB

LABORATORY ACTIVITY #5
3 – Dimensional Plots

I. Learning Objectives:
At the end of the laboratory activity, the students should be able to:
1. To implement three – dimensional plots in MATLAB
2. Use 3 – dimensional plot, mesh, and surf plotting commands available in MATLAB

II. Introduction:
Three – Dimensional Plots
MATLAB is capable of generating three – dimensional plots. These three – dimensional plots may be
plotted with the use of the command plot3, mesh, and/or surf, among many other plotting commands available
in MATLAB.

1. Three – Dimensional Line Plots


Lines in three – dimensional space can be plotted with the syntax plot3(x, y, z). Consider the following
equations: 𝑥 = 𝑒 −0.05𝑡 sin 𝑡, 𝑦 = 𝑒 −0.05𝑡 cos 𝑡 and 𝑧 = 𝑡
To generate a three – dimensional curve from these three equations from 𝑡 = 0 to 𝑡 = 10𝜋 with 𝜋⁄50
resolution, type 3 - D Line Plot

%Three dimensional line plot 40


clear
t = [0:pi/50:10*pi]; 30

x = exp(-0.05*t).*sin(t);
z-axis

y = exp(-0.05*t).*cos(t);
20

z = t; 10

plot3(x, y, z,'r','linewidth',2), grid on


title('3 - D Line Plot'),xlabel('x-axis') 0
1
ylabel('y-axis'), zlabel('z-axis') 0.5 1

0 0.5
0
-0.5
-0.5
-1 -1
y-axis x-axis

2. Surface Mesh Plot


To create a surface plot with mesh function, type the syntax mesh(x,y,z). Consider the function
2
−[(𝑥−𝑦2 ) +𝑦2 ]
𝑧 = 𝑥𝑒 , generate the surface plot for −2 ≤ 𝑥 ≤ 2 and −2 ≤ 𝑦 ≤ 2, with a spacing of 0.1.
Surface Mesh Plot

%Surface Surf Plot


[x,y] = meshgrid(-2:0.1:2); 0.5
z = x.*exp(-((x-y.^2).^2 + y.^2));
mesh(x,y,z),xlabel('x'),ylabel('y')
zlabel('z'), title('Surface Mesh Plot')
0
z

The function [X,Y] = meshgrid(min:spacing:max)


specifies the minimum (min) and maximum (max) -0.5

values of both x and y, in that order. Spacing is the 2

1 2

desired spacing of the x and y values. 0


0
1

-1
-1
-2 -2
y x

PREPARED BY: RONEL V. VIDAL, PECE 1


3. Surface Surf Plot
To create a surface plot with surf function, type the syntax surf(x,y,z). Consider the function
2
−[(𝑥−𝑦2 ) +𝑦2 ]
𝑧 = 𝑥𝑒 , generate the surface plot for −2 ≤ 𝑥 ≤ 2 and −2 ≤ 𝑦 ≤ 2, with a spacing of 0.1.
Surface Surf Plot

%Surface Surf Plot


[x,y] = meshgrid(-2:0.1:2); 0.5
z = x.*exp(-((x-y.^2).^2 + y.^2));
surf(x,y,z),xlabel('x'),ylabel('y')
zlabel('z'), title('Surface Mesh Plot')
0

z
-0.5
2
1 2
0 1
0
-1
4. Contour Plot y -2 -2
-1
x

Contour plot or topographic plot is a plot of contour lines that shows the contour of the land by means of
constant elevation lines. Along a contour the elevation is the same. Contour plot can help visualize the shape of
2
−[(𝑥−𝑦2 ) +𝑦2 ]
a function. It can be created with the syntax contour(x, y, z). Consider the function 𝑧 = 𝑥 𝑒 , generate
the surface plot for −2 ≤ 𝑥 ≤ 2 and −2 ≤ 𝑦 ≤ 2, with a spacing of 0.1.
Contour Plot

%Contour Plot 2

clear 1.5

[x,y] = meshgrid(-2:0.1:2);
z = x.*exp(-((x-y.^2).^2 + y.^2)); 1

contour(x,y,z),xlabel('x'),ylabel('y') 0.5

zlabel('z'),title('Contour Plot')
0
y

-0.5

-1

-1.5

5. Mesh and Contour Plots in One Plot -2


-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x

Mesh and Contour Plots

%Mesh and Contour Plots


clear 0.5
[x,y] = meshgrid(-2:0.1:2);
z = x.*exp(-((x-y.^2).^2 + y.^2));
meshc(x,y,z),xlabel('x'),ylabel('y')
zlabel('z'),title('Mesh and Contour Plots') 0
z

-0.5
2

1 2

0 1
0
-1
-1
-2 -2
y x

PREPARED BY: RONEL V. VIDAL, PECE 2


6. Emphasizing Surface Shape
MATLAB provides a number of techniques that can enhance the information content of graphs. For
2
−[(𝑥−𝑦2 ) +𝑦2 ]
example, this graph of the 𝑧 = 𝑥𝑒 function uses the same data as the previous graph, but
employs lighting and view adjustment to emphasize the shape of the graphed function.

%Emphasizing Surface Shape


[x,y] = meshgrid(-2:0.1:2);
z = x.*exp(-((x-y.^2).^2 + y.^2));
surf(x,y,z,'FaceColor','interp',...
'EdgeColor','none',...
'FaceLighting','phong')
daspect([5 5 1])
axis tight
view(-50,30)
camlight left
zlabel('z'), title('Surface Plot')
xlabel('x'),ylabel('y')

7. Three – Dimensional Mesh Surface Plot of a Sphere Mesh Plot of a Sphere

SPHERE Generate sphere.


[X,Y,Z] = SPHERE(N) generates three (N+1)-by-(N+1)
matrices so that SURF(X,Y,Z) produces a unit sphere. 1

0.5

[X,Y,Z] = SPHERE uses N = 20.


z-axis

%Three dimensional surface mesh plot of a sphere


clear -0.5

n = 20;
[x,y,z] = sphere(n); -1
1
mesh(x,y,z), title('Mesh Plot of a Sphere') 0.5 1
xlabel('x-axis'), ylabel('y-axis') 0 0.5

zlabel('z-axis'), axis('equal') -0.5


-0.5
0

y-axis -1 -1
x-axis

Surf Plot of a Sphere


8. Three – Dimensional Surf Surface Plot of a Sphere

%Three dimensional surface surf plot of a sphere 1


clear
n = 30; 0.5
[x,y,z] = sphere(n);
z-axis

surf(x,y,z), title('Surf Plot of a Sphere') 0

xlabel('x-axis'), ylabel('y-axis')
zlabel('z-axis'), axis('equal') -0.5

-1

0.5 1
0
0
-0.5
y-axis -1
x-axis

PREPARED BY: RONEL V. VIDAL, PECE 3


9. Three – Dimensional Mesh Surface Plot of a Cylinder

Cylinder generates cylinder.


[x,y,x] = cylinder(r,n) forms the unit cylinder based on the generator curve in the vector r. Vector r contains
the radius at equally spaced points along the unit height of the cylinder. The cylinder has n points around the
circumference. SURF(x,y,z) displays the cylinder.
[x,y,z] = cylinder(r), and [x,y,z] = cylinder default to n = 20 and R = [1 1].

% Surface Plot of a cylinder


clear
r =3;
n = 30;
[x,y,z] = cylinder(r,n);
surf(x,y,z,'FaceColor','interp',...
'EdgeColor','none',...
'FaceLighting','phong')
daspect([5 5 1])
axis tight
view(-50,30)
camlight left
zlabel('z'), title('Surface Plot of Cylinder')
xlabel('x'),ylabel('y')

III. Laboratory Exercises


1. 3 Dimensional Line Plot
Given the following equations 𝑥 = (1 − 𝑒 −0.05𝑡 ) cos(2𝑡), 𝑥 = (1 − 𝑒 −0.05𝑡 ) sin(2𝑡), and 𝑧 = 𝑡, generate a
three – dimensional curve from these three equations from t = 0 to t = 10 with /50 resolution. Include
appropriate labels and title.

2. Surface Mesh Plot, Surface Surf Plot, Mesh and Contour in one plot, and emphasizing surface shape plot
Create a 3 dimensional plot of the function sinc(√𝑥 2 + 𝑦 2 ) using the following 3 – dimensional plots.
Generate the surface plot for −8 < 𝑥 < 8, −8 < 𝑦 < 8, with a spacing of 0.5. Include label for each axis and a
title.
a) Surface mesh plot
b) Surface surf plot
c) Mesh and contour plot
d) Emphasizing surface shape plot

3. Three – Dimensional Surf Surface Plot and Emphasizing Surface Shape Plot of a Sphere
Generate the 3 dimensional surf surface plot and with emphasis on the surface shape plot of a sphere
with n = 60.

PREPARED BY: RONEL V. VIDAL, PECE 4


4. Use the following three – dimensional plot commands of a cylinder to create
a) Surf surface plot
b) Emphasizing surface shape plot
*Note: set the axis to square

t = [0:pi/10:3*pi];
n = 0.5 + exp(-0.5*t).*sin(2*t);
[x,y,z] = cylinder(n);

t = [0:pi/10:2*pi];
n = 2 + cos(t);
[x,y,z] = cylinder(n);

t = [0:pi/10:2*pi];
n = 2 + sin(t);
[x,y,z] = cylinder(n);

t = [0:pi/20:2*pi];
n = 20 + exp(0.5*t).*sin(2*t);
[x,y,z] = cylinder(n);

PREPARED BY: RONEL V. VIDAL, PECE 5

You might also like