how to draw a plane from X Y Z points
28 次查看(过去 30 天)
显示 更早的评论
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)
figure
surf(X,Y,PLUT);
%%
%load vectors
load('points.mat','x1cp','x2cp','fintcp')
figure
plot3(x1cp,x2cp,fintcp,'r', 'LineWidth',2)
0 个评论
采纳的回答
William Rose
2022-9-30
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.
load points.mat;
%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.
更多回答(1 个)
William Rose
2022-9-30
编辑:William Rose
2022-10-3
[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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!