# how to draw a plane from X Y Z points

21 views (last 30 days)
Mikel on 30 Sep 2022
Edited: William Rose on 3 Oct 2022
Hello,
I have a X, Y , Z vectors with 15k points that represents the response of a system through the time. There is posibility to draw a plane of 13x17 using this data only?
I know how to create a plane asigning the values of Z at the vertexes of the X Y plane, but my idea is now to use this 3 vectors to create somehow a plane of the same dimension as the first one.
% vertexes
d1=(-4:0.5:4);
d2=(-3:0.5:3);
% fp function for van der pol system plant (P plant)
mup=1;
fp=@(fp1,fp2)(mup.*(1-(fp1.^2)).*fp2-fp1);
[X,Y]=meshgrid(d1,d2); % creates a 13x 17 plane
%values of both functions x2prima at vertexes
PLUT= fp(X, Y)
PLUT = 13×17
49.0000 37.2500 27.0000 18.2500 11.0000 5.2500 1.0000 -1.7500 -3.0000 -2.7500 -1.0000 2.2500 7.0000 13.2500 21.0000 30.2500 41.0000 41.5000 31.6250 23.0000 15.6250 9.5000 4.6250 1.0000 -1.3750 -2.5000 -2.3750 -1.0000 1.6250 5.5000 10.6250 17.0000 24.6250 33.5000 34.0000 26.0000 19.0000 13.0000 8.0000 4.0000 1.0000 -1.0000 -2.0000 -2.0000 -1.0000 1.0000 4.0000 8.0000 13.0000 19.0000 26.0000 26.5000 20.3750 15.0000 10.3750 6.5000 3.3750 1.0000 -0.6250 -1.5000 -1.6250 -1.0000 0.3750 2.5000 5.3750 9.0000 13.3750 18.5000 19.0000 14.7500 11.0000 7.7500 5.0000 2.7500 1.0000 -0.2500 -1.0000 -1.2500 -1.0000 -0.2500 1.0000 2.7500 5.0000 7.7500 11.0000 11.5000 9.1250 7.0000 5.1250 3.5000 2.1250 1.0000 0.1250 -0.5000 -0.8750 -1.0000 -0.8750 -0.5000 0.1250 1.0000 2.1250 3.5000 4.0000 3.5000 3.0000 2.5000 2.0000 1.5000 1.0000 0.5000 0 -0.5000 -1.0000 -1.5000 -2.0000 -2.5000 -3.0000 -3.5000 -4.0000 -3.5000 -2.1250 -1.0000 -0.1250 0.5000 0.8750 1.0000 0.8750 0.5000 -0.1250 -1.0000 -2.1250 -3.5000 -5.1250 -7.0000 -9.1250 -11.5000 -11.0000 -7.7500 -5.0000 -2.7500 -1.0000 0.2500 1.0000 1.2500 1.0000 0.2500 -1.0000 -2.7500 -5.0000 -7.7500 -11.0000 -14.7500 -19.0000 -18.5000 -13.3750 -9.0000 -5.3750 -2.5000 -0.3750 1.0000 1.6250 1.5000 0.6250 -1.0000 -3.3750 -6.5000 -10.3750 -15.0000 -20.3750 -26.5000
figure
surf(X,Y,PLUT);
%%
figure
plot3(x1cp,x2cp,fintcp,'r', 'LineWidth',2)

William Rose on 30 Sep 2022
Here is a code fragment that fits a plane to the 3D data in the file, and plots the 3D data from the file as well as the points on the best-fit plane.
%skip row 1 since fintcp(1)=NaN
fintcp=fintcp(2:end); x1cp=x1cp(2:end); x2cp=x2cp(2:end);
mdl=fitlm([x1cp;x2cp]',fintcp'); %fit linear model
d1=(-4:0.5:4); %mesh x-coordiates
d2=(-3:0.5:3); %mesh y-coordinates
[X,Y]=meshgrid(d1,d2); %create X,Y meshes, 13x17
%Next lines use the linear model to predict z-values at the mesh points
Xv=reshape(X,[],1); Yv=reshape(Y,[],1); %reshape meshes into vectors
Xall=[ones(size(Xv)),Xv,Yv]; %221x3 matrix for linear prediction
zpred=Xall*mdl.Coefficients.Estimate; %vector: 221 predicted values
zpred=reshape(zpred,size(X)); %reshape vector to mesh
%plot results
figure
plot3(x1cp,x2cp,fintcp,'r', 'LineWidth',2)
xlabel('X'); ylabel('Y'); zlabel('Z'); hold on; grid on
surf(X,Y,zpred)
After you run this, you can rotate the 3D plot by clicking on the 3D rotate icon at the top, then click and drag in the plot.
Try it. Good luck.
##### 2 CommentsShowHide 1 older comment
William Rose on 3 Oct 2022
@Mikel, You are welcome!

William Rose on 30 Sep 2022
Edited: William Rose on 3 Oct 2022
[edit: corrected a + that should have been -, on right side of equation z=... below]
It seems that you are using the word "plane" for a surface that is not a plane. Therefore I am not sure if I understand your question. The first part of your code created a non-planar surface with 13x17 points. The second part of your code generates a 3D curve, using data from file points.mat. It appears that this curve also does not lie in a plane.
If you want to fit a smooth non-planar surface to the data in points.mat, you can try fitting a quadratic surface such as
or the more general
(The factors of 1/2 in the first equation are to make it consistent with the second equation. The second equation is a general quadratic surface. By defining it this way, you can compute important quantities as explained here.)
If you want the best fit plane to either data set, you can use multiple linear regression or singular value decomposition. The former will minimize the z-axis errors. The latter will minimize the sum of the perpendicular distances between the measured points and the plane.

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!