Matlab

How to Create Surface Plot in MATLAB Using surf() Function

The surface plot seems like a three-dimensional data visualization graph used to produce plots of the type z= f(x,y) having solid edges as well as face colors, where x as well as y behave like the independent variables and z behaves like 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 blog, we will explore what is a surface plot in MATLAB and how it works using different examples.

What is a Surface Plot in MATLAB?

A surface plot allows us to depict f(x, y, z) in three-dimensional spaces. We can create these plots in MATLAB using MATLAB’s built-in surf() 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 surface plot that represents the behavior of the function in the form z = f(x, y).

Syntax
The surf() function uses different syntaxes for different scenarios in MATLAB, as shown below:

surf(X,Y,Z)
surf(Z)
surf(Z,C)

Here:
The function surf(X, Y, Z) generates a three-dimensional surface plot of X, Y, and Z with the solid face as well as edge colors where matrix Z indicates the height above the x-y plane and matrices X and Y indicate the x-y plane.

The function surf(Z) draws a surface plot utilizing matrix Z by utilizing column as well as row indices as the respective x and y coordinates.

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

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

Follow the given three steps to create surface plots in MATLAB using the surf() function:

1: Make a mesh grid in the xy-plane utilizing the meshgrid() function covering the given function’s domain.

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

3: Plot the function z = f(x, y) using the surf() function.

Examples

To understand the functionality of the surf() function, consider some examples.

Example 1: Create Surface Plot Using surf(X,Y,Z) Function

The given example creates a surface plot for the function Z = cos(X)+sin(Y) using the surf(X, Y, Z) function.

[X,Y] = meshgrid(-1:0.1:1,-1:10);
Z = cos(X)+sin(Y);
surf(X,Y,Z)

Example 2: Create Surface Plot Using surf(Z) Function

This example describes the working of the surf(Z) function to create a surface plot for the given function.

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

Example 3: Create Surface Plot Using surf(Z,C) Function

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

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

Conclusion

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

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.