3D plane with more than 3 points
Show older comments
Hi all,
I am fairly new to matlab-so English please! I am trying to create a 3D plane with approximately 30 x,y,z co-ordinates. Can anyone tell me how I would go about doing this. I can do it with 3 points fine, but anything more and i'm not sure. Appreciate any help. Katie
Answers (3)
Sean de Wolski
on 10 Apr 2014
Perhaps ndgrid or meshgrid will help get you started:
[xx,yy,zz] = meshgrid(1:3,4:6,7:9); % grid of x,y,z
V = sin(xx)+cos(yy).^2.*zz; % some function of x,y,z
isosurface(xx,yy,zz,V,2) % show it
You'll have to provide us with more detail about what you want if you would like more specific info.
6 Comments
Katie
on 10 Apr 2014
Edited: Sean de Wolski
on 10 Apr 2014
Sean de Wolski
on 10 Apr 2014
What do you want to do with them?
Katie
on 10 Apr 2014
Sean de Wolski
on 10 Apr 2014
Do you have the Curve Fitting Toolox?
If so, run:
>>cftool
This will allow you to interactively fit all sorts of planes through the points.
Katie
on 10 Apr 2014
Katie
on 10 Apr 2014
The code below will generate a plane fit whose equation is dot(N,r)=d where N is the plane's normal.
P=[x(:),y(:),z(:)];
c=mean(P);
P=bsxfun(@minus,P,c);
[U,S,V]=svd(P);
N=V(:,end);
d=dot(N,c);
5 Comments
Katie
on 10 Apr 2014
I'm sorry but I'm not very matlab literate. I'm guessing that for the first row I would insert all my values of x,y and z?
You told Sean that your x-coordinate data was in a vector called x and similarly for y and z. I assumed that to be the case here as well.
The rest of the code should run as posted.
Katie
on 10 Apr 2014
Katie
on 10 Apr 2014
Joseph Cheng
on 10 Apr 2014
Edited: Joseph Cheng
on 10 Apr 2014
you can always try the Ordinary least squares method to get the coefficients for the plane by the equation Ax=b; A and b are defined below and x will contain the coefficients for you plane.
With your n data points (x[i], y[i], z[i]), the 3x3 symmetric matrix A can be computed by
sum_i x[i]*x[i], sum_i x[i]*y[i], sum_i x[i]
sum_i x[i]*y[i], sum_i y[i]*y[i], sum_i y[i]
sum_i x[i], sum_i y[i], n
Then compute the 3 element vector b:
{sum_i x[i]*z[i], sum_i y[i]*z[i], sum_i z[i]}
Then solve Ax = b for the given A and b by x = A\b;
reading that it's a bit confusing so i've included a quick implementation to show the above math.
x=[1:10];
y=[1:10];
[X Y]=meshgrid(x,y);
Z = [2*X+3*Y+5+rand(size(X))]; %create a dummy set of x y and z points
%calculate the matrix A
A = [sum(X(:).^2) sum(X(:).*Y(:)) sum(X(:)); sum(X(:).*Y(:)) sum(Y(:).^2) sum(Y(:)); sum(X(:)) sum(Y(:)) length(X(:))]
%calculate b
b=[sum(X(:).*Z(:)) sum(Y(:).*Z(:)) sum(Z(:))]
%solve for the coefficients
x = A\b'; %x=[a b c]'. the equation of the plane will be z = a*x+b*y+c.
%the coefficients should match closely to the coefficients I used for Z above.
planefit = [x(1)*X+x(2)*Y+x(3)]; %
figure,plot3(X(:),Y(:),Z(:),'r.','markersize',20); hold on, surf(X,Y,planefit);
1 Comment
Matt J
on 10 Apr 2014
It's not really a good idea to use Ordinary Least Squares for plane fitting. Your x,y,z data, and hence your A matrix, will have errors in them, too, making the estimator biased. Total Least Squares is better.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!