Brandon Choque Molina's Calculus 3 Project: How to Visualize Functions of Several Variables with MATLAB
Overview
This Project has been completed as part of a standard Calculus 3 synchronous online course during Spring 2021 Semester at MassBay Community College, Wellesley Hills, MA.
1. How to visualize functions of several variables with MATLAB
CONTENT
1. How to visualize functions of several variables with MATLAB
Table of contents:
1.1. Introduction
1.2. Introduction to MATLAB
1.3. Variables in matlab
1.4. MATLAB: 3D Plot
1.4.1 Line graphs
1.4.1.1. Plot3 function
1.4.1.2. Comet3 function
1.4.2. Surface graphics
1.4.2.1. Mesh function: mesh(z)
1.4.2.2. Mesh function: mesh(x,y,z)
1.4.2.3. Meshgrid function
1.4.2.4. Surf function
1.4.2.5. Shading function
1.4.2.6. colormap function
1.4.3. Three-dimensional graphics
1.4.3.1. Mesh graph with curtain
1.4.3.2. Mesh graph with contour
1.4.3.3. Surf graph with contour
1.4.3.4. Surf graph with light
1.4.3.5. Waterfall graph
1.4.4. Sketch level curves
1.4.4.1. Contour function
1.4.4.2. Contour3 function
1.4.4.3. Clabel function
1.4.4.4. Pseudo Color Graphs
1.5. Conclusion
1.6. References
$$\color{red}{\text{ NOTE: If you want to see the pictures in detail, right click on them and open the image in a new tab.}}$$
1.1. Introduction
So far we have been solving functions of a single independent variable:
But most common calculus problems come using two or more independent variables. For example, the volume of a right circular cylinder, is a function of two variables:
Where: r=radius of the cylinder
h=height of the cylinder
Or the volume of a rectangular solid, is a function of three variables:
Where:
l=length
w= width
h= height
Looking at the examples we can say that the way to write a function of two or more variables is:
Just as it happened to the functions of a single variable, we can learn a lot about a function if we draw its graph.
The graph of a function of two variables is the set of points (x,y,z) that satisfy the function z=f(x,y), with (x,y) in the domain of f. We can interpret geometrically as a surface in the space.
Now that we know what a multivariable function is, it is time to draw the graph. For this we will use the platform MATLAB.
To introduce yourself into the world of equations check my classmate Deidra wiki page:
To understand the different types of coordiante systems needed for this wiki page, check Joshua's wiki page:
1.2. Introduction to MATLAB
What is MATLAB?
MATLAB is a programming platform designed specifically for engineers and scientists.
We can use MATLAB for a range of applications, including deep learning and machine learning, signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology.
But for this wiki page we willl use MATLAB as a graphic calculator.
Important features that we will use in MATLAB:
clc = clean the command window
clear = clean the workspace (deletes the variables)
% make a comment
; = do not show the step in the command window
1.3. Variables in MATLAB
What a variable?
It a piece of information that changes and updates as you use a function. See it as a container for storing a value.
How do we use a variable in MatLab?
We have 3 steps for making and using a variable:
1) Declare the variable
2) Assign a value to the variable
3) Declare the function
1.4. MATLAB: 3D Plot
MATLAB has the ability to perform several types of 3D graphics, many of them are simple generalizations of functions available for two-dimensional graphing.
Main functions for three-dimensional graphics
plot3 (x, y, z) = Create a three-dimensional line graph
comet3 (x, y, z) = Generate an animated version of plot3
mesh (z) or mesh (x, y, z) = Create a mesh surface graphic
surf (z) or surf (x, y, z) = Create a surface plot
shaded interp = Interpolate between colors
flat shading = Color each section a single color
colormap (map_name) = Select the color pattern
contour (z) or contour (x, y, z) = Generate a contour plot
surfc (z) or surfc (x, y, z) = Create a combined surface plot with a contour plot
pcolor (z) or pcolor (x, y, z) = Create a pseudo-color chart
1.4.1. Line graphs
1.4.1.1. Plot3 function
The plot3 function is similar to the plot function, except that accepts data in three dimensions. The user must give three vectors: x, y, and z. Then these vectors are graphed in a space of three dimensions and are connected with straight lines.
Example:
%Example PLOT3
%We clean the workspace
clear, clc
%We generate our vectors
%x is a row vector of 1000 evenly spaced points between 0 and 10*pi
x=linspace(0,10*pi,1000);
%y and z are our functions to plot
y=cos(x);
z=sin(x);
%we generate a 3D graph in red color with a line thicker than the default of Matlab
plot3(x,y,z,'r','linewidth',3);
%we add a grid
grid on;
%we label the coordinate axes
xlabel('x=angle');
ylabel('y=cos(x)');
zlabel('z=sin(x)');
%Finally, we add a title to our graph
title('Spring using plot3')
1.4.1.2. Comet3 function
The comet3 function is similar to the comet function for two-dimensional, it generates an animation from of the points to be plotted.
Example
%Example COMET3
%We clean the workspace
clear, clc
%We generate our vectors
%t is a row vector between 0 and 10*pi increasing each pi/50
t = 0:pi/50:10*pi;
z = t;
%x and y are our functions to plot
x = sin(t);
y = cos(t);
%we generate an animated 3D graph
comet3(x,y,z);
%We hold the graph, so it will not dissapear with the next plot
hold on;
%We create a new figure window and return the handle to the current
%position
figure(gcf);
%we add a grid
grid on;
%We create a new figure window and return the handle to the current position
%we label the coordinate axes
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
%Finally, we add a title to our graph
title('Spring using comet3');
1.4.2. Surface graphics
Now we will see in detail how we can draw a function of two variables (z = f (x, y)) in a rectangular domain.
These are known as surface graphs and allow you to represent data as a surface.
We will plot with two types of funtion of surface: mesh graphics and surface graphics.
1.4.2.1. Mesh function: mesh(Z)
There are many ways to use mesh graphics. It can be used to give a professional effect to a simple two-dimensional matrix m x n.
mesh(Z) draws a wireframe mesh using X=1:n and Y=1:m, [m,n]=size(Z). The height, Z, is a single-valued function defined over a rectangular grid. Color is proportional to surface height.
Example:
%Example MESH(Z)
%We clean the workspace
clear, clf;
%We generate our matriz
%The columns leads to the X-axis
%The rows to the Y-axis
%and the intersection is the value in Z
%for example: Column x=9, row y=2, it gives z=18
z = [ 1 2 3 4 5 6 7 8 9 10;
2 4 6 8 10 12 14 16 18 20;
3 4 5 6 7 8 9 10 11 12];
%We call the function mesh for z
mesh(z),
%we label the coordinate axis
xlabel ('X-axis')
ylabel ('Y-axis')
zlabel('Z-axis')
%we add a grid
grid on;
%Finally, we add a title to our graph
title('Mesh(Z) Surface');
1.4.2.2. Mesh function: mesh(x,y,z)
The mesh function can also be used with three arguments: mesh (x, y, z). The vector x whose length is the number of columns in matrix Z; the length of the vector must be the same than the number of rows in Z. The coordinates of the points that we need to make the grid are of the type (x(j),y(i),z(i,j))
Example:
%Example MESH(X,Y,Z)
%We clean the workspace
clear, clf;
%We generate our matrix
x=linspace(1,50,10);
y=linspace(500,1000,3);
%we generate the value z
z = [ 1 2 3 4 5 6 7 8 9 10;
2 4 6 8 10 12 14 16 18 20;
3 4 5 6 7 8 9 10 11 12];
%We call the function mesh for z
mesh(x,y,z)
%we label the coordinate axis
xlabel ('X-axis')
ylabel ('Y-axis')
zlabel('Z-axis')
%we add a grid
grid on;
%Finally, we add a title to our graph
title('Mesh(Z) Surface');
1.4.2.3. Meshgrid function
Let x and y be two vectors containing the coordinates in any direction on the grid (mesh or grid) on which to draw the function. With the meshgrid function two matrices are created, X whose rows are copies of x. and Y whose columns are copies of Y. These matrices represent respectively the x and y coordinates of all points on the grid. The matrix of Z values are calculated from the matrices of X and Y coordinates. Finally we have to draw this matrix Z with the function mesh, whose elements are the function element to element of the elements of X and Y.
Example:
%Example MESHGRID
%We clean the workspace
clear, clf;
%We generate our matrices
x= [-2:0.2:2] ;
y= [-2:0.2:2] ;
%We generate the meshgrid with the values of X and Y
[X,Y]=meshgrid(x,y) ;
%We calculate Z using the function with the X and Y coordinates
Z= X.*exp(-X.^2 - Y.^2);
%We plot this matrix Z with the function mesh
mesh(X,Y,Z);
%we label the coordinate axes
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
%Finally, we add a title to our graph
title('PLOT MESHGRID');
Application Example:
The initial heat distribution in a steel plate is given by the function:
u (x, y) = 80*y^2 * e^(-x^2−0.3*y^2)
With:
−2.1 ≤ x ≤ 2.1 and −6 ≤ y ≤ 6,
Graph the function with a grid growth of 0.15 in both addresses.
Solution:
%Example MESHGRID
%We clean the workspace
clear, clf;
%We generate our matrices
%−2.1 ≤ x ≤ 2.1 with a growth of 0.15
x=[-2.1:0.15:2.1];
%−6 ≤ y ≤ 6, with a growth of 0.15
y=[-6:0.15:6];
%We generate the meshgrid with the values of X and Y
[X,Y]=meshgrid(x,y) ;
%We calculate Z using the function with the X and Y coordinates
Z= 80*Y.^2.*exp(-X.^2 - 0.3*Y.^2);
%We plot this matrix Z with the function mesh
mesh(X,Y,Z);
%we label the coordinate axes
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
%Finally, we add a title to our graph
title('PLOT MESHGRID');
1.4.2.4. Surf function
Surf graphics are similar to mesh graphics, but surf creates a colorful three-dimensional surface instead of a grid.
Surface colors vary with the value of z.
The surf command takes the same input as mesh:
Surf (Z) a single entry, in which case the row and the columns are taken as x and y coordinates.
Surf (x, y, Z) a vector x of dimension n, a vector y of dimension m and a Z matrix of dimension mxn.
Surf (X, Y, Z) three matrices where X and Y were obtained from the meshgrid command.
Example:
%Example SURF
%We clean the workspace
clear, clf;
%We generate our matrices
%−2 ≤ x ≤ 2 with a growth of 0.2
x= [-2 :0.2: 2];
%−2 ≤ y ≤ 2, with a growth of 0.2
y= [-2 :0.2: 2];
%We generate the meshgrid with the values of X and Y
[X,Y]=meshgrid(x,y) ;
%We calculate Z using the function with the X and Y coordinates
Z= X.*exp(-X.^2 - Y.^2);
%Plot, by default, is a single plot. If you want to plot several results you can use "hold on"
%command and plot many results on a plot with a set x range and set y range using the plot command.
%If you want to have multiple plots of different results - say with different ranges - then you use subplot
%first graph
subplot (1,2,1);
%We plot this matrix Z with the function mesh
mesh(X,Y,Z);
%we add a title to our graph
title('MESH');
%we label the coordinate axes for the first subplot
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
%second graph
subplot(1,2,2);;
%We plot this matrix Z with the function mesh
surf(X,Y,Z);
%we add a title to our graph
title('SURF');
%we label the coordinate axes for the second subplot
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
1.4.2.5. Shading function
The shaded scheme for surface plots is controlled by the shading command.
Shading faceted: (default) the mesh is displayed in black color and the surface adopts a color for each mesh region depending on its height.
Shading flat: flattened shading that, like shading faceted, assigns a color for each region of the mesh depending on your height and remove meshes.
Shading interp: interpolated shading that eliminates the meshed and blurs color changes.
Example:
%Example SURF
%We clean the workspace
clear, clf;
%We generate our matrices
%−2 ≤ x ≤ 2 with a growth of 0.2
x= [-2 :0.2: 2];
%−2 ≤ y ≤ 2, with a growth of 0.2
y= [-2 :0.2: 2];
%We generate the meshgrid with the values of X and Y
[X,Y]=meshgrid(x,y) ;
%We calculate Z using the function with the X and Y coordinates
Z= X.*exp(-X.^2 - Y.^2);
%Plot, by default, is a single plot. If you want to plot several results you can use "hold on"
%command and plot many results on a plot with a set x range and set y range using the plot command.
%If you want to have multiple plots of different results - say with different ranges - then you use subplot
%first graph
subplot (1,2,1);
%We plot this matrix Z with the function surf
surf(X,Y,Z);
%we add the function shadin flat
shading flat;
%we add a title to our graph
title('SHADING FLAT');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
%second graph
subplot(1,2,2);;
%We plot this matrix Z with the function mesh
surf(X,Y,Z);
%we add the function shading interp
shading interp,
%we add a title to our graph
title('SHADING INTERP');
%we label the coordinate axes for the second subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.2.6. Colormap function
A colormap is defined as a matrix of three columns, each of which contains a value between 0 and 1, which represents the intensity of one of the fundamental colors: R (red), G (green) and
B (blue).
Colors:
Black 'k' [0 0 0]
White 'w' [1 1 1]
Red 'r' [1 0 0]
Green 'g' [0 1 0]
Blue 'b' [0 0 1]
Yellow 'y' [1 1 0]
Magenta 'm' [1 0 1]
Color maps can be created at will, for example M = [0 0 0; 1 0 0; 0 1 0; 0 0 1; 1 1 0] is a map of color that has black, red, green, blue and yellow.
colormap (FIG,M) selects the function FIG to color and then installs the matrix M as colormap to use for the current figure. If there is none active figure, replace the previous colormap to the following figures to be drawn.
Some color maps are predefined in MATLAB, see the next chart:
Example:
%Example COLORMAP
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 with a growth of 0.5
x = -10:0.5:10;;
%−10 ≤ y ≤ 10, with a growth of 0.5
y= -10:0.5:10;;
%We generate the meshgrid with the values of X and Y
[X,Y]=meshgrid(x,y) ;
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%Plot, by default, is a single plot. If you want to plot several results you can use "hold on"
%command and plot many results on a plot with a set x range and set y range using the plot command.
%If you want to have multiple plots of different results - say with different ranges - then you use subplot
%first graph
subplot (2,2,1);
%We plot this matrix Z with the function surf
surf (X,Y,Z);
%We use the colormap DEFAULT for the fisrt subplot
colormap(subplot (2,2,1),'default');
%we add the function shading interp
shading interp;
%we add a title to our graph
title('COLORMAP: DEFAULT');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
%second graph
subplot(2,2,2);;
%We plot this matrix Z with the function mesh
surf (X,Y,Z),
%We use the colormap SUMMER for the fisrt subplot
colormap(subplot(2,2,2),'SUMMER');
%we add the function shading interp
shading interp,
%we add a title to our graph
title('COLORMAP: SUMMER');
%we label the coordinate axes for the second subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
%third graph
subplot(2,2,3);;
%We plot this matrix Z with the function mesh
surf (X,Y,Z),
%We use the colormap COPPER for the fisrt subplot
colormap(subplot(2,2,3),'copper');
%we add the function shading interp
shading interp,
%we add a title to our graph
title('COLORMAP: COPPER');
%we label the coordinate axes for the second subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
%fourth graph
subplot(2,2,4);;
%We plot this matrix Z with the function mesh
surf (X,Y,Z),
%We use the colormap COPPER for the fisrt subplot
colormap(subplot(2,2,4),'hot');
%we add the function shading interp
shading interp,
%we add a title to our graph
title('COLORMAP: HOT');
%we label the coordinate axes for the second subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.3. Three-dimensional graphics
The following graphics are the same as the previous ones already mentioned, with the difference that they add or remove details from the final graphic.
1.4.3.1. Mesh graph with curtain
Meshz(...) is the same as MESH(...) except that a "curtain" or reference plane is drawn beneath.
Example:
%Example MESHZ
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function meshz
meshz(X,Y,Z);
%we add a title to our graph
title('FUNCTION MESHZ');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.3.2. Mesh graph with contour
Meshc(...) is the same as MESH(...) except that a contour plot is drawn beneath the mesh.
Example:
%Example MESHC
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function meshc
meshc(X,Y,Z);
%we add a title to our graph
title('FUNCTION MESHC');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.3.3. Surf graph with contour
Surfc(...) is the same as SURF(...) except that a contour plot is drawn beneath the surface.
Example:
%Example SURFC
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function surfc
surfc(X,Y,Z);
%we add a title to our graph
title('FUNCTION SURFC');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.3.4. Surf graph with light.
Surfl(...) is the same as SURF(...) except that it draws the surface with highlights from a light source.
Example:
%Example SURFL
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function surfl
surfl(X,Y,Z);
%we add a title to our graph
title('FUNCTION SURFL');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.3.5. Waterfall graph
waterfall(...) is the same as MESH(...) except that the column lines of the mesh are not drawn - thus producing a "waterfall" plot. For column-oriented data analysis, use waterfall(Z') or
waterfall(X',Y',Z').
Example:
%Example WATERFALL ROW ORIENTED
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function waterfall
waterfall(X,Y,Z);
%we add a title to our graph
title('FUNCTION WATERFALL');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
%Example WATERFALL COLUMN ORIENTED
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function waterfall
waterfall(X',Y',Z');
%we add a title to our graph
title('FUNCTION WATERFALL COLUMN ORIENTED');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.4. Sketch level curves
In the same way that a function of one variable has a graphical representation by a curve in the plane, when the function has two variables it could be represented by a surface in three-dimensional space. This surface would be formed by the points of the form (x, y, z).
1.4.4.1. Contour function.
Contour(X,Y,Z) draws a contour plot of Z using vertices from the mesh defined by X and Y. X and Y can be vectors or matrices.
Example:
%Example contour
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function contour
contour(X,Y,Z);
%we add a title to our graph
title('FUNCTION CONTOUR');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.4.2. Contour3 function.
Contour3(...) is the same as CONTOUR(...) except the contour lines are drawn in multiple planes. Each line is drawn in a horizontal plane at a height equal to the corresponding contour level.
Example:
%Example contour3
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function contour3
contour3(X,Y,Z);
%we add a title to our graph
title('FUNCTION CONTOUR3');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.4.3. Clabel function.
The clabel function applied to a graph of contour write the level values that represent the level curves.
Example:
%Example contour3
%We clean the workspace
clear, clf;
%We generate our matrices
%−10 ≤ x ≤ 10 and −10 ≤ y ≤ 10, both with a growth of 0.5
x = -10:0.5:10;
y = x;
%We generate the meshgrid with the values of X and Y
[X,Y] = meshgrid (x,y);
%We calculate Z using the function with the X and Y coordinates
Z = sin (sqrt (X .^2 + Y .^2)) ./ sqrt (X .^ 2 + Y .^ 2 + 0.1);
%We plot this matrix Z with the function contour3
graph=contour3(X,Y,Z);
%We apply the level curves
clabel(graph)
%we add a title to our graph
title('FUNCTION CONTOUR3');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
1.4.4.4. PseudoColor Graphs
Pseudo-color charts are similar to contour graphs, except instead of lines that highlight a specific contour, a map is generated in a two-dimensional shading on a grid.
MATLAB includes a sample function called peaks which generates the matrices x, y and z from a interesting surface that looks like a Mountain.
Example:
%Example contour3
%We clean the workspace
clear, clf;
%We cal;l the function PEAKS
%We generate the meshgrid with the values of X and Y
[X,Y,Z] = peaks;
%We plot peaks with the function surf
surf(X,Y,Z);
%we add a title to our graph
title('PEAKS');
%we label the coordinate axes for the first subplot
xlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');
Now let's look at the pseudo color graphs applying different types of shading.
Example:
%We call the function Peaks
[x,y,z] = peaks;
%First graph
subplot(2,2,1);
pcolor(x,y,z);
%Second graph
subplot(2,2,2);
pcolor(x,y,z);
shading interp;
%Third graph
subplot(2,2,3);
pcolor(x,y,z);
shading interp;
hold on;
contour(x,y,z,20,' k ');
%Fourth graph
subplot(2,2,4);
contour(x,y,z);
1.5 Conclusion
Over time, we have been taught to use a formula or a computational algorithm to graph functions. With just a couple of steps you already know what you have to do, you already know the way and you just have to apply it. But many times we come across problems in which it is necessary to graph carefully in order to correctly understand the behavior of a function. That is why this wiki page included several graphic forms, because we should not focus on a single path, we have to relate ideas, organize data and above all maintain a positive and creative attitude.
It has been a pleasure writing this post and I hope it has been an educational experience for you, dear reader.
Thanks you .
Brandon Choque
1.6. References
Mesh, Meshc, Meshz (MATLAB Functions), matlab.izmiran.ru/help/techdoc/ref/mesh.html
Surf, Surfc (MATLAB Functions), matlab.izmiran.ru/help/techdoc/ref/surf.html
“Creating 3-D Plots.” Creating 3-D Plots - MATLAB & Simulink Example, https://www.mathworks.com/help/matlab/visualize/creating-3-d-plots.html
“Peaks Function.” Peaks Function - MATLAB, https://www.mathworks.com/help/matlab/ref/peaks.html
Arnold, David. “Level Curves.” https://www.math.tamu.edu/~mpilant/math696/m696_240/jsamayoa/public_html/levelcurves.pdf
“3D Plots in Matlab: Learn the Types of 3D Plots in MATLAB.” EDUCBA, 4 Mar. 2021, www.educba.com/3d-plots-in-matlab