3D plane with more than 3 points

13 views (last 30 days)
Katie
Katie on 10 Apr 2014
Edited: Matt J on 10 Apr 2014
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
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
Katie on 10 Apr 2014
OK thanks, I'll give it a go! Probably have some more questions down the line.
Katie
Katie on 10 Apr 2014
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
Katie
Katie on 10 Apr 2014
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
Matt J
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!