0% found this document useful (0 votes)
13 views9 pages

S S.Lab.6

Uploaded by

Usama Javed
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)
13 views9 pages

S S.Lab.6

Uploaded by

Usama Javed
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/ 9

EXPERIMENT NO.

6
OBJECTIVE:
Imitate the generation of three dimension (3-D) plotting in MATLAB.
LEARNING OUTCOMES:
In this lab, students will be able to understand:
 3D plotting in MATLAB
 Types of 3D plots in MATLAB
 Generating 3-dimensional plots
THEORY
MATLAB is a powerful computational software package that allows for efficient analysis
and visualization of three-dimensional data. Three-dimensional (3D) plots can be used to
display complex data sets and provide a more comprehensive view of the data than a two-
dimensional plot. In MATLAB, 3D plotting can be achieved using several functions,
including the "plot3", "scatter3", and "surf" functions. These functions enable users to create
3D plots of surfaces, contours, and scatter plots. MATLAB also provides numerous tools for
customizing the appearance of 3D plots, such as adjusting the viewpoint, changing the color
scheme, and adding labels and annotations. Whether you are visualizing complex data sets or
creating stunning graphics for presentations and publications, 3D plotting in MATLAB can
be a valuable tool for analyzing and communicating your results.
3-D PLOTTING:
The following is a list of different 3D plots that can be constructed in MATLAB:
 Mesh Plot
 Surface Plot
 Ribbon Plot
 Contour Plot

Mesh Plot:
A mesh plot is a type of 3D plot that displays a surface as a mesh of connected lines or
polygons. In MATLAB, a mesh plot can be created using the "mesh" function, which takes as
input matrices of x, y, and z coordinates that define the surface to be plotted. The mesh plot
displays the surface as a wireframe mesh, with the color of the mesh lines or polygons
indicating the height or intensity of the z-values. The mesh plot can be rotated and viewed
from different angles using MATLAB's built-in visualization tools, allowing for a detailed
analysis of the surface's structure. Additionally, the appearance of the mesh plot can be
customized using various options in MATLAB, such as changing the color scheme, adjusting
the lighting, and adding labels and annotations. Mesh plots are commonly used to visualize
functions of two variables or to display data from simulations or experiments that produce
surfaces with irregular shapes or textures.

Syntax: Mesh function transforms the domain specified by vectors (X, Y, Z) into arrays
(x,y,z).
The syntax for the Mesh Plot is,
mesh(x , y , z )
[ X ,Y , Z ]=meshgrid (x , y , z)

Surface Plot:
A surface plot is a type of 3D plot that displays a surface as a 3D object with color coding to
represent the values of a function or data set. In MATLAB, a surface plot can be created
using the "surf" function, which takes as input matrices of x, y, and z coordinates that define
the surface to be plotted. The surface plot displays the surface as a smooth, continuous 3D
object, with the color of the surface indicating the height or intensity of the z-values. The
surface plot can be rotated and viewed from different angles using MATLAB's built-in
visualization tools, allowing for a detailed analysis of the surface's structure.

They are especially useful for visualizing scientific or engineering data, such as topographic
maps, heat maps, or fluid flow simulations. A surface plot is somewhat similar to a mesh plot.
The main difference between them is, in the surface plot, the connecting lines and the faces
both will be displayed in the dark colour.

Syntax: In the surface plot, ‘surf’ function is used. So, you can write a simple format like
‘function name(array)’.
surf ( x , y , z )
surf (z )

Ribbon Plot:
A ribbon plot is a type of 3D plot that is commonly used to visualize protein structures or
other complex molecular structures. In MATLAB, a ribbon plot can be created using the
"ribbon" function, which takes as input a matrix of values representing the structure of the
molecule. The ribbon plot displays the structure of the molecule as a ribbon-like tube, with
different colors or shading used to represent different parts of the molecule, such as helices,
sheets, or loops. The ribbon plot can be rotated and viewed from different angles using
MATLAB's built-in visualization tools, allowing for a detailed analysis of the molecular
structure. Additionally, the appearance of the ribbon plot can be customized using various
options in MATLAB, such as changing the color scheme, adjusting the thickness of the
ribbon, and adding labels and annotations. Overall, the ribbon plot is a powerful tool for
visualizing complex molecular structures and can be an important tool for researchers in
fields such as biochemistry, molecular biology, and drug design. As the name ribbon
suggests, this 3D plot graph will be having different colour ribbons.

Syntax: The general syntax for writing code:


ribbon(x , y , z)

ribbon(x , y)

ribbon(z )

Contour Plot:
A contour plot is a type of 2D plot that displays the level sets or contours of a function of two
variables. In MATLAB, a contour plot can be created using the "contour" function, which
takes as input matrices of x, y, and z coordinates that define the function to be plotted. The
contour plot displays a series of curves or contour lines that connect points with the same z-
value, creating a map of the function's level sets. The contour lines can be colored or labeled
to indicate the value of the function at each level, providing a clear visual representation of
the function's behavior. Contour plots are commonly used in scientific and engineering
applications to visualize data such as topographic maps, weather patterns, or the behavior of
fluid flow or electric fields. To create the three dimensional [3D] contour plot, we are using
the ‘contour3’ function.

Syntax: The syntax for the three-dimensional contour plot is:


contour 3(x , y , z)

contour 3( z)

Example 1:
Generating a 3-D mesh.
Code:
% Generate the peaks data
[X,Y,Z] = peaks(25);

% Create the mesh plot


figure;
mesh(X,Y,Z);
title('3D Mesh using the "peaks" command');
xlabel('X');
ylabel('Y');
zlabel('Z');

Explanation:

Z = peaks returns the z-coordinates of the peaks function evaluated over a 49-by-49 grid

Z = peaks(n) returns the peaks function evaluated over an n-by-n grid. If you
specify n as a vector of length k, MATLAB® evaluates the function over a k-by-k grid.

This code uses the built-in "peaks" command to generate sample data for the x, y, and z
coordinates of the mesh. The "peaks" function generates a 25-by-25 matrix of z-values,
along with corresponding x and y values. The code then creates the mesh plot using the
"mesh" function, which takes as input the x, y, and z data generated by the "peaks"
function. Finally, the plot is labeled with titles and axis labels using the "title", "xlabel",
"ylabel", and "zlabel" functions. The resulting plot displays a 3D mesh of the "peaks" data,
with the x, y, and z coordinates represented by the axes and the height of the mesh.
Output:

Figure 1/:3-D mesh

Example 2:
Plot a mesh 3D plot for a square root mathematical function.
z=√ x 2+ y 2

Code:
% Define the range of x and y values
x = linspace(-5, 5, 100);
y = linspace(-5, 5, 100);

% Create a grid of x and y values


[X, Y] = meshgrid(x, y);

% Calculate the square root function


Z = sqrt(X.^2 + Y.^2);

% Create the mesh plot


figure;
mesh(X, Y, Z);
title('Mesh Plot of Square Root Function');
xlabel('X');
ylabel('Y');
zlabel('Z');

Explanation:

This code defines the range of x and y values using the "linspace" function, creates a grid
of x and y values using the "meshgrid" function, calculates the square root function using
element-wise operations with the "." operator, and creates the mesh plot using the "mesh"
function. The resulting plot displays a 3D mesh of the square root function, with the x and
y values on the axes and the z-values represented by the height of the mesh. The plot is
labelled with titles and axis labels using the "title", "xlabel", "ylabel", and "zlabel"
functions.
Output:

Figure 2/:mesh plot of square root function

Example 3
Generate a Surface 3d plot.

Code:
% Generate the peaks data
[X,Y,Z] = peaks(25);

% Create the surface plot


figure;
surf(X,Y,Z);
title('Surface 3D Plot using the "peaks" command');
xlabel('X');
ylabel('Y');
zlabel('Z');

Output:

Figure 3/;surface 3D plot using the "peaks"command


Task 1
Generate a 3D surface plot for the given exponential function:
2

z=e−0.9 (x¿¿ 2+0.5 ( x− y ) )¿


Hint: use point multiplication when multiplying x and (x-y) with their powers.

Code:
clc; close all;
[x,y]=peaks(30);
z = exp(-0.9*(x.^2+0.5*(x-y).^2))
surf(x,y,z)
title ('SURFACE PLOT FOR EXPONENTIAL FUNCTION')
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
Output:

Figure 4/:surface plot for exponential function


Task 2:
Create a ribbon plot for a mathematical function given:
2 2
(x − y )
Hint: use point multiplication when multiplying x and (x-y) with their powers

Code:
[x,y] = peaks(30);
z =[(x.^2)-(y.^2)];
ribbon(z);
title ('RIBBON PLOT FOR EXPONENTIAL FUNCTION')
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
Output:

Figure 5/;RIBBON PLOT FOR EXPONENTIAL FUNCTION

Task 3:
2 2
Generate a contour plot for the mathematical exponential function: e (−x − y )
Hint: use point multiplication when multiplying x and (x-y) with their powers

Code:
[x,y] = peaks(25);
z = exp(-x.^2-y.^2);
contour3(x,y,z);
title ('CONTOUR PLOT FOR EXPONENTIAL FUNCTION')
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');

Output:

Figure 6/;CONTOUR PLOT FOR EXPONENTIAL FUNCTION


Task 4:
Plot a 3D helix shape of sine and cosine values. Define t as value between 0 and 10π.

Hint:

To plot a 3D helix shape in MATLAB, you can use the parametric equations for a helix:
x (t)=r∗cos (t)
y (t )=r∗sin(t)
z (t)=c∗t

where r is the radius of the helix and c is the distance between each turn.

Code:
t = linspace(0,10*pi)
r = 1;
c=10;
x = r*cos(t);
y = r*sin(t);
z = c*t;
plot3(x,y,z);
title ('helix PLOT FOR EXPONENTIAL FUNCTION')
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');

Output:

Figure 7/;helix PLOT FOR EXPONENTIAL FUNCTION


Task 5:
Qs. 1. How does changing the parameters r and c affect the shape of the 3D helix?
Describe the effect of increasing or decreasing each parameter on the radius and pitch
of the helix.
When ‘r’ is increase (radius of helix increases) and decrease (radius of helix decreases) by
increasing the radius it compresses. Also, when we increase the ‘c’ pitch of helix increases
and decrease.
Qs. 2. In a surface plot, how does the number of data points affect the accuracy and
smoothness of the plot? What are some techniques that can be used to improve the
smoothness of a surface plot with a limited number of data points?
By taking More data points mean more interpolation between them, resulting in smoother and
more accurate plots. We can improve our data smoothness by removing noise and increasing
data points.

DISCUSSION & CONCLUSION:


In this lab we learned about of three-dimension (3-D) plotting. Three-dimensional (3D)
charts can be used to display complex data sets and provide a more comprehensive view of
the data than two-dimensional charts. 3D plotting can be achieved with several functions,
such as the "plot3", "scatter3", and "surf" functions. These features allow the user to create
her 3D plots of surfaces, contours and scatterplots.

You might also like