3D plane with more than 3 points

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)

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

Thanks for answering, OK so this is a selection of the points that I have:
x y z
690089 7542396 1308
693029 7543757 1508
694311 7544425 1508
695194 7543782 1508
694752 7541297 1508
694319 7536947 1408
694545 7533813 1308
687581 7530576 1308
685263 7528122 1308
Is this what you need?
What do you want to do with them?
I need to create a 3D plane that passes through the points.
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.
OK thanks, I'll give it a go! Probably have some more questions down the line.
The curve fitting tool says that my 'data sizes are incompatible'? Not quite sure why. Part of my data is in seans answer at the top. Thanks

Sign in to comment.

Matt J
Matt J on 10 Apr 2014
Edited: Matt J 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

Hi Matt, 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? When [U,S,V]=svd(p) do I need to change anything here. I do not really understand what this is saying?
Matt J
Matt J on 10 Apr 2014
Edited: Matt J 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.
Hi Matt, when I enter my values it says that 'index exceeds matrix dimensions'. Any ideas?
Scrap that, so I have done the code above, but nothing happens at the end? How do I plot the plane?
Matt J
Matt J on 10 Apr 2014
Edited: Matt J on 10 Apr 2014
You can plot using the PLOT3 command, the same as in Joseph Cheng's code. However, I don't recommend that you generate the fit as he has done, for reasons I give in the comment to his post.

Sign in to comment.

Joseph Cheng
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

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.

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Asked:

on 10 Apr 2014

Edited:

on 10 Apr 2014

Community Treasure Hunt

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

Start Hunting!