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

Respuestas (3)

Sean de Wolski
Sean de Wolski el 10 de Abr. de 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 comentarios

Katie
Katie el 10 de Abr. de 2014
Editada: Sean de Wolski el 10 de Abr. de 2014
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?
Sean de Wolski
Sean de Wolski el 10 de Abr. de 2014
What do you want to do with them?
Katie
Katie el 10 de Abr. de 2014
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.
Katie
Katie el 10 de Abr. de 2014
OK thanks, I'll give it a go! Probably have some more questions down the line.
Katie
Katie el 10 de Abr. de 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

Iniciar sesión para comentar.

Matt J
Matt J el 10 de Abr. de 2014
Editada: Matt J el 10 de Abr. de 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 comentarios

Katie
Katie el 10 de Abr. de 2014
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 el 10 de Abr. de 2014
Editada: Matt J el 10 de Abr. de 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
Katie el 10 de Abr. de 2014
Hi Matt, when I enter my values it says that 'index exceeds matrix dimensions'. Any ideas?
Katie
Katie el 10 de Abr. de 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 el 10 de Abr. de 2014
Editada: Matt J el 10 de Abr. de 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.

Iniciar sesión para comentar.

Joseph Cheng
Joseph Cheng el 10 de Abr. de 2014
Editada: Joseph Cheng el 10 de Abr. de 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 comentario

Matt J
Matt J el 10 de Abr. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Abr. de 2014

Editada:

el 10 de Abr. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by