How can I generate a plane surface in MATLAB?

234 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Nov 2020
Edited: MathWorks Support Team on 5 Nov 2020
The following are some examples of how the xy-plane can be plotted in MATLAB. A generalized example, showing how to plot any arbitrary plane, follows.
PLOTTING THE XY-PLANE
=====================
The following shows two methods for constructing an xy-plane, bounded at (1,1,0), (-1,1,0), (-1,-1,0), and (1,-1,0).
Method 1: The PATCH Function
You can draw a polygonal graphics object by using the PATCH function and specifying its vertices. By specifying four corners, you can construct the xy-plane as follows:
% Use fourth input for color scale.
patch([1 -1 -1 1], [1 1 -1 -1], [0 0 0 0], [1 1 -1 -1])
Method 2: The MESHGRID and SURF functions.
Using the MESHGRID function, you can generate data points for the xy-plane. Afterwards, use the SURF function to generate the surface plot.
[x y] = meshgrid(-1:0.1:1); % Generate x and y data
z = zeros(size(x, 1)); % Generate z data
surf(x, y, z) % Plot the surface
Note that by making some simple changes to the above examples, the xz- and yz-planes can be plotted. For example, to plot the xz-plane use:
patch( [1 -1 -1 1] , [0 0 0 0], [1 1 -1 -1], [1 1 -1 -1])
For more information on the PATCH function, see the following URL:
PLOTTING AN ARBITRARY PLANE
===========================
The following shows two methods for constructing, an arbitrary plane of the form:
Ax + By + Cz + D = 0,
where the coefficients "A", "B", "C", and "D" are known values.
Method 1: The PATCH Function
x = [1 -1 -1 1]; % Generate data for x vertices
y = [1 1 -1 -1]; % Generate data for y vertices
z = -1/C*(A*x + B*y + D); % Solve for z vertices data
patch(x, y, z);
Method 2: The MESHGRID and SURF Functions.
[x y] = meshgrid(-1:0.1:1); % Generate x and y data
z = -1/C*(A*x + B*y + D); % Solve for z data
surf(x,y,z) %Plot the surface
For more information on the MESHGRID and SURF functions, see the following URLs:
https://www.mathworks.com/help/matlab/ref/meshgrid.html

More Answers (1)

xingxingcui
xingxingcui on 29 Mar 2025
For R2024b or later ,please use constantplane function.
  2 Comments
John D'Errico
John D'Errico on 29 Apr 2025
I never noticed this function appear. It was probably my feature request that caused it to happen, as a few years back, I asked for exactly this capability in MATLAB. I really should check the release notes more often.
Sam Chak
Sam Chak on 29 Apr 2025
Thank you for promoting this function. It operates similarly to xline and yline, but for 3D.
[X, Y] = meshgrid(-1:0.05:1);
Z = exp(- 5*(X.^2 + Y.^2));
mesh(X,Y,Z)
xlabel('x'), ylabel('y'), zlabel('z')
zlim([0, 1.5])
cp = constantplane("x", 0, FaceAlpha=0.5);

Sign in to comment.

Products


Release

R13SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!