3D plane with more than 3 points
13 views (last 30 days)
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
0 Comments
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!