Matlab

How to Create Surface Contour Plots in MATLAB Using surfc() Function

The surface contour, commonly referred to as surfc is a three-dimensional data visualization plot used for creating the contour plot under the surface plot of the type z= f(x,y) having a solid edge and face colors. Where x, as well as y, are independent variables and z is the dependent variable which means that each combination of x and y within a specified domain can be used to calculate the value of z.

In this guide, we will learn how to create a surfc plot in MATLAB using different examples.

What is a Surfc Plot in MATLAB?

A surfc plot is a contour plot that allows us to depict f(x, y, z) in three-dimensional spaces under the surface plot. We can create these plots in MATLAB using MATLAB’s built-in surfc() function. This function is used for creating three-dimensional plots having solid face and edge colors in MATLAB. This function accepts the values of X, Y, and Z as arguments and creates a three-dimensional surfc plot that represents the function’s behavior in the form z = f(x, y).

Syntax

The surfc() function uses the simple syntax in MATLAB:

surfc(X,Y,Z)
surfc(X,Y,Z,C)
surfc(Z)
surfc(Z,C)

Here:

The function surfc(X, Y, Z) creates a three-dimensional surface plot having the contour plot underneath with solid face and edge colors where matrix Z represents a height above the x-y plane and matrices X and Y represent the x-y plane.

The function surfc(X, Y, Z, C) creates the contour plot under the surface plot by specifying the additional colors.

The function surfc(Z) produces a surface plot with the contour using matrix Z by utilizing column as well as row indices as the respective x and y coordinates.

The function surfc(Z, C) yields to specify the additional edges’ colors.

How to Create Surface Contour Plots in MATLAB Using the surfc() Function?

Follow the given three steps to create contour under the surface plots in MATLAB using the surfc() function.

Step 1: Create a mesh grid in the xy-plane utilizing the meshgrid() function that covers the domain of the given function.

Step 2: Compute the value of the specified function for each point in the created mesh grid.

Step 3: Draw the function z = f(x, y) utilizing the surfc() function.

Examples

Consider some examples to understand the surfc() function’s working.

Example 1: How to Create Surface Contour Plot Using the surfc(X,Y,Z) Function?

The given example creates a contour under the surface plot for the function Z = exp(X).*cos(X).^2 using the surfc(X, Y, Z) function.

[X,Y] = meshgrid(-1:0.1:1,-1:10);
Z = exp(X).*cos(X).^2;
surfc(X,Y,Z)

Example 2: How to Create Surface Contour Plot Using the surfc(Z) Function?

This example describes the working of the surfc(Z) function to generate a contour under the surface plot for the given function.

[X,Y] = meshgrid(1:0.5:10,1:20);
Z = Y.*cos(X)+X.*sin(Y);
surfc(Z)

Example 3: How to Create Surface Contour Plot Using the surfc(Z,C) Function?

This MATLAB code explains how to specify the different colors in the surfc plot using the surfc(Z, C) function where C represents the colormap.

[X,Y] = meshgrid(1:0.5:10,1:20);
Z = (cos(X)+sin(Y)).^3;
C = X + Y;
surfc(Z, C)

Conclusion

MATLAB is a useful programming tool that allows us to create different plots of different types using built-in functions. It provides us with the surfc() function which is used for making contours under the surface plots that have solid face and edge colors. This function accepts one or more mandatory and optional arguments and generates a surfc plot for the given function. This guide described the functionality of the surfc() function using some examples, allowing you to create your surfc plots in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.