
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Plot Function of Two Variables in MATLAB
MATLAB provides various built-in functions to plot a function of two variables with the help of different types of plots like mesh, surface, scatter plots, etc. In this tutorial, I am going to explain the process of plotting a function of two variables in MATLAB.
Plot a Function of Two Variables in MATLAB
The step-by-step process to plot a function of two variables in MATLAB is explained below.
Step (1) Define the range of x and y points.
Step (2) Create a grid of x and y points. For this, use the "meshgrid" function in MATLAB.
Syntax
[X, Y] = meshgrid(x, y);
Step (3) Define a function in two variables that you need to plot.
Step (4) Create a plot of the function.
Hence, plotting a function of two variables in MATLAB is a simple four step process.
Let us understand the above given algorithm with the help of MATLAB examples.
Plot a Function of Two Variables Using Mesh Plot
In MATLAB, we have a function "mesh" that is used to plot a 3D surface plot of a function. We can use it to plot a function of two-variables.
Example
The following example demonstrates how we can use the "mesh" function to plot a function of two-variables.
% MATLAB code to plot function of two-variable using mesh surface plot % Define the range of x and y points x = linspace(-5, 5, 50); y = linspace(-5, 5, 50); % Create a grid of x and y points [X, Y] = meshgrid(x, y); % Define a function of two variables Z = X.^2 + Y.^2; % Plot the function using the mesh surface plot mesh(X, Y, Z); title('Two Variable Function Mesh Plot'); xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis');
Output

Plot a Function of Two Variables Using Surface Plot
In MATLAB, we can also plot a function of two-variables using the surface plot. For this, there is a built-in function in MATLAB library named, "surf" that plots a 3D surface plot for the defined function.
Example
Let us see an example to understand how to use the "surf" function to plot a function of two variables in MATLAB.
% MATLAB code to plot a function of two-variable using surface plot % Define the range of x and y points x = linspace(-5, 5, 50); y = linspace(-5, 5, 50); % Create a grid of x and y points [X, Y] = meshgrid(x, y); % Define a function of two variables Z = X.^2 + Y.^2; % Plot the function using the 3D surface plot surf(X, Y, Z); title('Two Variable Function Surface Plot'); xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis');
Output

Plot a Function of Two Variables Using Scatter Plot
We can also plot a function of two variable using the scatter plot. For this, we have a built-in function "scatter" in MATLAB.
Example
Here is an example demonstrating how to plot a function of two variables using the scatter plot.
% MATLAB code to plot a function of two-variable using scatter plot % Define the range of x and y points x = linspace(-5, 5, 50); y = linspace(-5, 5, 50); % Create a grid of x and y points [X, Y] = meshgrid(x, y); % Define a function of two variables Z = X.^2 + Y.^2; % Plot the function using the scatter plot scatter(X(:), Y(:), Z(:), 'green'); title('Two Variable Function Scatter Plot'); xlabel('X-axis'); ylabel('Y-axis'); colorbar; % Z values
Output

Plot a Function of Two Variables Using Contour Plot
In MATLAB, we can plot a function of two variables using the contour plot. For this, we use the "contour" function in MATLAB.
Example
The following example shows the MATLAB code implementation to plot a function of two variables using the contour plot.
% MATLAB code to plot a function of two-variable using contour plot % Define the range of x and y points x = linspace(-5, 5, 50); y = linspace(-5, 5, 50); % Create a grid of x and y points [X, Y] = meshgrid(x, y); % Define a function of two variables Z = X.^2 + Y.^2; % Plot the function using the contour plot contour(X, Y, Z); title('Two Variable Function Contour Plot'); xlabel('X-axis'); ylabel('Y-axis'); colorbar; % Z values
Output

Conclusion
In MATLAB, we can plot a function of two variables using different types of plots. In this tutorial, I explained the step-by-step process and examples in MATLAB to plot a function of two variables. You can try all these codes with different functions as well.